Exemple #1
0
        public void TestBinding()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();

            binder.Bind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));

            var e1 = new SampleEvent1();
            var e2 = new SampleEvent1 { Foo = 1 };
            var e3 = new SampleEvent1 { Foo = 1, Bar = "bar" };

            List<Handler> handlerChain = new List<Handler>();

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e3, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);

            binder.Unbind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e3, equivalent, handlerChain));
        }
Exemple #2
0
        public void TestDuplicateUnbinding()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();
            List<Handler> handlerChain = new List<Handler>();

            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Bind(new SampleEvent1 { Foo = 2 }, new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1));

            binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Unbind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, equivalent, handlerChain));
            Assert.AreEqual(1, binder.BuildHandlerChain(new SampleEvent1 { Foo = 2 }, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);

            // with EventSink

            var sink = new SampleEventSink();
            sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);

            sink.Unbind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);
            sink.Bind(new SampleEvent1 { Foo = 1 }, sink.OnSampleEvent1);

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(new SampleEvent1 { Foo = 1 }, equivalent, handlerChain));
            Assert.AreEqual(1, binder.BuildHandlerChain(new SampleEvent1 { Foo = 2 }, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);
        }
Exemple #3
0
        public void TestBasicPerformance()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();
            List<Handler> handlerChain = new List<Handler>();

            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSampleEvent1));

            // const int testCount = 1000000;
            const int testCount = 1;

            for (var i = 0; i < testCount; ++i)
            {
                binder.BuildHandlerChain(new SampleEvent1 {Foo = 1}, equivalent, handlerChain);
            }
            // 1,000,000 counts in 342 ms in release mode
        }
Exemple #4
0
        public void TestHandlerChainBuilding()
        {
            Binder binder = new Binder();
            var equivalent = new EventEquivalent();

            binder.Bind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));
            binder.Bind(new SampleEvent1 { Foo = 1 }, new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1));
            binder.Bind(new Event(), new MethodHandler<Event>(OnEvent));

            var e1 = new SampleEvent1();
            var e2 = new SampleEvent1 { Foo = 1 };
            var e3 = new SampleEvent1 { Foo = 1, Bar = "bar" };
            var e4 = new SampleEvent1 { Foo = 2 };
            var e5 = new SampleEvent2 { Foo = 1, Bar = "bar" };
            var e6 = new SampleEvent2 { Foo = 2, Bar = "bar" };

            List<Handler> handlerChain = new List<Handler>();

            handlerChain.Clear();
            Assert.AreEqual(2, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[1]);

            handlerChain.Clear();
            Assert.AreEqual(3, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[1]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[2]);

            handlerChain.Clear();
            Assert.AreEqual(3, binder.BuildHandlerChain(e3, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[1]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[2]);

            handlerChain.Clear();
            Assert.AreEqual(2, binder.BuildHandlerChain(e4, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[1]);

            handlerChain.Clear();
            Assert.AreEqual(3, binder.BuildHandlerChain(e5, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[1]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[2]);

            handlerChain.Clear();
            Assert.AreEqual(2, binder.BuildHandlerChain(e6, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSampleEvent1), handlerChain[0]);
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[1]);

            binder.Unbind(new SampleEvent1(), new MethodHandler<SampleEvent1>(OnSampleEvent1));

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e1, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<Event>(OnEvent), handlerChain[0]);

            binder.Unbind(new Event(), new MethodHandler<Event>(OnEvent));

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e2, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);

            handlerChain.Clear();
            Assert.AreEqual(1, binder.BuildHandlerChain(e3, equivalent, handlerChain));
            Assert.AreEqual(new MethodHandler<SampleEvent1>(OnSpecificSampleEvent1), handlerChain[0]);

            handlerChain.Clear();
            Assert.AreEqual(0, binder.BuildHandlerChain(e4, equivalent, handlerChain));
        }
        private void Run()
        {
            currentFlow = this;
            if (queue != null)
            {
                equivalent = new EventEquivalent();
                events = new List<Event>();
                handlerChain = new List<Handler>();
            }

            StartInternal();

            while (!shouldStop)
            {
                UpdateInternal();

                if (queue != null)
                {
                    while ((DateTime.UtcNow.Ticks - Time.CurrentTicks) < Resolution)
                    {
                        if (queue.TryDequeue(events))
                        {
                            for (int i = 0, count = events.Count; i < count; ++i)
                            {
                                Event e = events[i];

                                Dispatch(e);

                                if (e.GetTypeId() == (int)BuiltinEventType.FlowStop)
                                {
                                    shouldStop = true;
                                    break;
                                }
                            }
                            events.Clear();
                            if (shouldStop)
                            {
                                break;
                            }
                        }
                        else
                        {
                            Thread.Sleep(1);
                            break;
                        }
                    }
                }
                else
                {
                    var tickDelta = DateTime.UtcNow.Ticks - Time.CurrentTicks;
                    var delay = (tickDelta < Resolution ?
                        (int)((Resolution - tickDelta) / Time.TicksInMillisecond) : 0);
                    Thread.Sleep(delay);
                }
            }

            Stop();

            if (queue != null)
            {
                handlerChain = null;
                events = null;
                equivalent = null;
            }
            currentFlow = null;
        }
        private void Run()
        {
            currentFlow = this;
            equivalent = new EventEquivalent();
            handlerChain = new List<Handler>();

            while (true)
            {
                Event e = queue.Dequeue();
                if (Object.ReferenceEquals(e, null))
                {
                    break;
                }
                Dispatch(e);
            }

            handlerChain = null;
            equivalent = null;
            currentFlow = null;
        }
        private void Run()
        {
            currentFlow = this;
            equivalent = new EventEquivalent();
            events = new List<Event>();
            handlerChain = new List<Handler>();

            while (true)
            {
                queue.Dequeue(events);
                if (events.Count == 0)
                {
                    break;
                }
                for (int i = 0, count = events.Count; i < count; ++i)
                {
                    Dispatch(events[i]);
                }
                events.Clear();
            }

            handlerChain = null;
            events = null;
            equivalent = null;
            currentFlow = null;
        }