示例#1
0
        public void PointToPoint()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var latch      = new CountdownEvent(1);
            var processor1 = new LatchedProcessor(latch);
            var processor2 = new LatchedProcessor(latch);

            dispatcher.AddHandler(CreateConsumer(processor1));
            dispatcher.AddHandler(CreateConsumer(processor2));
            dispatcher.Dispatch(Message.Create("test"));
            Assert.True(latch.Wait(3000));
            Assert.Equal(1, processor1.Counter + processor2.Counter);
        }
示例#2
0
        public void MiddleHandlerReturnsTrue()
        {
            var dispatcher = new UnicastingDispatcher(provider);
            var target1    = new CountingTestEndpoint(false);
            var target2    = new CountingTestEndpoint(true);
            var target3    = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target1);
            dispatcher.AddHandler(target2);
            dispatcher.AddHandler(target3);
            Assert.True(dispatcher.Dispatch(new GenericMessage("test")));
            Assert.Equal(2, target1.Counter + target2.Counter + target3.Counter);
        }
示例#3
0
        public void MiddleHandlerReturnsTrue()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var target1    = new CountingTestEndpoint(false);
            var target2    = new CountingTestEndpoint(true);
            var target3    = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target1);
            dispatcher.AddHandler(target2);
            dispatcher.AddHandler(target3);
            Assert.True(dispatcher.Dispatch(Message.Create("test")));
            Assert.Equal(2, target1.Counter + target2.Counter + target3.Counter);
        }
示例#4
0
        public void NoDuplicateSubscriptions()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var target     = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target);
            dispatcher.AddHandler(target);
            try
            {
                dispatcher.Dispatch(Message.Create("test"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(1, target.Counter);
        }
示例#5
0
        public void NoDuplicateSubscriptions()
        {
            var dispatcher = new UnicastingDispatcher(provider);
            var target     = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target);
            dispatcher.AddHandler(target);
            try
            {
                dispatcher.Dispatch(new GenericMessage("test"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(1, target.Counter);
        }
示例#6
0
        public void SingleMessage()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var latch      = new CountdownEvent(1);
            var processor  = new LatchedProcessor(latch);

            dispatcher.AddHandler(CreateConsumer(processor));
            dispatcher.Dispatch(Message.Create("test"));
            Assert.True(latch.Wait(500));
        }
示例#7
0
        public void RemoveConsumerBetweenSends()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var target1    = new CountingTestEndpoint(false);
            var target2    = new CountingTestEndpoint(false);
            var target3    = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target1);
            dispatcher.AddHandler(target2);
            dispatcher.AddHandler(target3);
            try
            {
                dispatcher.Dispatch(Message.Create("test1"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(3, target1.Counter + target2.Counter + target3.Counter);
            dispatcher.RemoveHandler(target2);
            try
            {
                dispatcher.Dispatch(Message.Create("test2"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(5, target1.Counter + target2.Counter + target3.Counter);
            dispatcher.RemoveHandler(target1);
            try
            {
                dispatcher.Dispatch(Message.Create("test3"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(6, target1.Counter + target2.Counter + target3.Counter);
        }
示例#8
0
        public void SingleMessage()
        {
            var dispatcher = new UnicastingDispatcher(provider);
            var latch      = new CountdownEvent(1);
            var processor  = new LatchedProcessor(latch);

            dispatcher.AddHandler(CreateConsumer(processor));
            dispatcher.Dispatch(new GenericMessage("test"));
            Assert.True(latch.Wait(500));
        }
示例#9
0
        public void AllHandlersReturnFalse()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var target1    = new CountingTestEndpoint(false);
            var target2    = new CountingTestEndpoint(false);
            var target3    = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target1);
            dispatcher.AddHandler(target2);
            dispatcher.AddHandler(target3);
            try
            {
                Assert.False(dispatcher.Dispatch(Message.Create("test")));
            }
            catch (Exception)
            {
            }

            Assert.Equal(3, target1.Counter + target2.Counter + target3.Counter);
        }
示例#10
0
        public void RemoveConsumerBeforeSend()
        {
            var dispatcher = new UnicastingDispatcher(provider);
            var target1    = new CountingTestEndpoint(false);
            var target2    = new CountingTestEndpoint(false);
            var target3    = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target1);
            dispatcher.AddHandler(target2);
            dispatcher.AddHandler(target3);
            dispatcher.RemoveHandler(target2);
            try
            {
                dispatcher.Dispatch(new GenericMessage("test"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(2, target1.Counter + target2.Counter + target3.Counter);
        }
示例#11
0
        public void RemoveConsumerLastTargetCausesDeliveryException()
        {
            var dispatcher = new UnicastingDispatcher(provider.GetService <IApplicationContext>());
            var target1    = new CountingTestEndpoint(false);

            dispatcher.AddHandler(target1);
            try
            {
                dispatcher.Dispatch(Message.Create("test1"));
            }
            catch (Exception)
            {
                // ignore
            }

            Assert.Equal(1, target1.Counter);
            dispatcher.RemoveHandler(target1);
            Assert.Throws <MessageDispatchingException>(() => dispatcher.Dispatch(Message.Create("test2")));
        }
示例#12
0
        public void NoHandlerExhaustion()
        {
            dispatcher.AddHandler(handlerMock1.Object);
            dispatcher.AddHandler(handlerMock2.Object);
            dispatcher.AddHandler(handlerMock3.Object);
            dispatcher.AddHandler(handlerMock4.Object);

            var start   = new CountdownEvent(1);
            var allDone = new CountdownEvent(TOTAL_EXECUTIONS);
            var message = messageMock.Object;
            var failed  = false;

            void MessageSenderTask()
            {
                try
                {
                    start.Wait();
                }
                catch (Exception)
                {
                    throw;
                }

                if (!dispatcher.Dispatch(message))
                {
                    failed = true;
                }

                allDone.Signal();
            }

            for (var i = 0; i < TOTAL_EXECUTIONS; i++)
            {
                Task.Run(MessageSenderTask);
            }

            start.Signal();
            Assert.True(allDone.Wait(10000));
            Assert.False(failed);
            handlerMock1.Verify((h) => h.HandleMessage(messageMock.Object), Times.Exactly(TOTAL_EXECUTIONS / 4));
            handlerMock2.Verify((h) => h.HandleMessage(messageMock.Object), Times.Exactly(TOTAL_EXECUTIONS / 4));
            handlerMock3.Verify((h) => h.HandleMessage(messageMock.Object), Times.Exactly(TOTAL_EXECUTIONS / 4));
            handlerMock4.Verify((h) => h.HandleMessage(messageMock.Object), Times.Exactly(TOTAL_EXECUTIONS / 4));
        }
示例#13
0
 public void DispatchMessageWithSingleHandler()
 {
     dispatcher.AddHandler(handlerMock.Object);
     dispatcher.Dispatch(messageMock.Object);
     handlerMock.Verify((h) => h.HandleMessage(messageMock.Object));
 }