public void should_invoke_both_sync_and_async_handlers()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var asyncHandler = new AsyncCommandHandler {
                WaitForSignal = true
            };

            _containerMock.Setup(x => x.GetInstance(typeof(AsyncCommandHandler))).Returns(asyncHandler);

            var syncHandler = new SyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var command = new DispatchCommand();

            DispatchFromDefaultDispatchQueue(command);

            syncHandler.Called.ShouldBeTrue();
            asyncHandler.CalledSignal.Wait(50.Milliseconds()).ShouldBeFalse();

            command.Signal.Set();

            asyncHandler.CalledSignal.Wait(1000.Milliseconds()).ShouldBeTrue();
        }
Example #2
0
        public void should_dispatch_message_to_queue_name()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var syncHandler = new SyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var handler1List = new List <SyncCommandHandlerWithQueueName1>();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(() =>
            {
                var handler1 = new SyncCommandHandlerWithQueueName1 {
                    WaitForSignal = true
                };
                handler1List.Add(handler1);
                return(handler1);
            });

            var handler2List = new List <SyncCommandHandlerWithQueueName2>();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName2))).Returns(() =>
            {
                var handler2 = new SyncCommandHandlerWithQueueName2 {
                    WaitForSignal = true
                };
                handler2List.Add(handler2);
                return(handler2);
            });

            Dispatch(new DispatchCommand());

            syncHandler.Called.ShouldBeTrue("Sync handler should be run synchronously");

            Wait.Until(() => handler1List.Count == 1 && handler1List[0].HandleStarted, 150.Milliseconds(), "First handler should be started");
            Wait.Until(() => handler2List.Count == 1 && handler2List[0].HandleStarted, 150.Milliseconds(), "second handler should be started");

            syncHandler.Called = false;
            Dispatch(new DispatchCommand());

            syncHandler.Called.ShouldBeTrue("Sync handler should be run synchronously");
            handler1List.Count.ShouldEqual(1, "Next handler should not be created yet");
            handler2List.Count.ShouldEqual(1, "Next handler should not be created yet");

            handler1List[0].CalledSignal.Set();
            Wait.Until(() => handler1List[0].HandleStopped, 150.Milliseconds(), "First handler should be stopped");
            Wait.Until(() => handler1List.Count == 2, 150.Milliseconds(), "Next handler should be created");
            Wait.Until(() => handler1List[1].HandleStarted, 150.Milliseconds(), "Next handler should be started");

            handler1List[1].CalledSignal.Set();
            Wait.Until(() => handler1List[1].HandleStopped, 150.Milliseconds(), "Next handler should be stopped");

            handler2List[0].CalledSignal.Set();
            Wait.Until(() => handler2List[0].HandleStopped, 150.Milliseconds(), "First handler should be stopped");
            Wait.Until(() => handler2List.Count == 2, 150.Milliseconds(), "Next handler should be created");
            handler2List[1].CalledSignal.Set();
            Wait.Until(() => handler2List[0].HandleStopped && handler2List[1].HandleStopped, 150.Milliseconds(), "Both handlers should be run");
        }
        public void should_dispatch_message_to_queue_name()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var syncHandler = new SyncCommandHandler();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var handler1List = new List<SyncCommandHandlerWithQueueName1>();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(() =>
            {
                var handler1 = new SyncCommandHandlerWithQueueName1 { WaitForSignal = true };
                handler1List.Add(handler1);
                return handler1;
            });

            var handler2List = new List<SyncCommandHandlerWithQueueName2>();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName2))).Returns(() =>
            {
                var handler2 = new SyncCommandHandlerWithQueueName2 { WaitForSignal = true };
                handler2List.Add(handler2);
                return handler2;
            });

            Dispatch(new DispatchCommand());

            syncHandler.Called.ShouldBeTrue("Sync handler should be run synchronously");

            Wait.Until(() => handler1List.Count == 1 && handler1List[0].HandleStarted, 150.Milliseconds(), "First handler should be started");
            Wait.Until(() => handler2List.Count == 1 && handler2List[0].HandleStarted, 150.Milliseconds(), "second handler should be started");

            syncHandler.Called = false;
            Dispatch(new DispatchCommand());

            syncHandler.Called.ShouldBeTrue("Sync handler should be run synchronously");
            handler1List.Count.ShouldEqual(1, "Next handler should not be created yet");
            handler2List.Count.ShouldEqual(1, "Next handler should not be created yet");

            handler1List[0].CalledSignal.Set();
            Wait.Until(() => handler1List[0].HandleStopped, 150.Milliseconds(), "First handler should be stopped");
            Wait.Until(() => handler1List.Count == 2, 150.Milliseconds(), "Next handler should be created");
            Wait.Until(() => handler1List[1].HandleStarted, 150.Milliseconds(), "Next handler should be started");

            handler1List[1].CalledSignal.Set();
            Wait.Until(() => handler1List[1].HandleStopped, 150.Milliseconds(), "Next handler should be stopped");

            handler2List[0].CalledSignal.Set();
            Wait.Until(() => handler2List[0].HandleStopped, 150.Milliseconds(), "First handler should be stopped");
            Wait.Until(() => handler2List.Count == 2, 150.Milliseconds(), "Next handler should be created");
            handler2List[1].CalledSignal.Set();
            Wait.Until(() => handler2List[0].HandleStopped && handler2List[1].HandleStopped, 150.Milliseconds(), "Both handlers should be run");
        }
        public void should_not_clone_message_when_dispatching_locally_to_the_current_queue()
        {
            LoadAndStartDispatcher();

            var handler = new SyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(handler);

            var command = new DispatchCommand();

            DispatchFromDefaultDispatchQueue(command);

            Wait.Until(() => handler.Called, 5.Seconds());

            handler.ReceivedMessage.ShouldNotBeNull();
            handler.ReceivedMessage.ShouldBeTheSameAs(command);
        }
        public void should_not_clone_message_when_dispatching_a_remote_message()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler = new SyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(handler);

            var command = new DispatchCommand();

            Dispatch(command);

            Wait.Until(() => handler.Called, 5.Seconds());

            handler.ReceivedMessage.ShouldNotBeNull();
            handler.ReceivedMessage.ShouldBeTheSameAs(command);
        }
        public void should_filter_invoker()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var asyncHandler = new AsyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(AsyncCommandHandler))).Returns(asyncHandler);

            var syncHandler = new SyncCommandHandler();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var context    = MessageContext.CreateTest("u.name");
            var command    = new DispatchCommand();
            var dispatched = new ManualResetEvent(false);
            var dispatch   = new MessageDispatch(context, command, (x, r) => dispatched.Set());

            _messageDispatcher.Dispatch(dispatch, x => x == typeof(AsyncCommandHandler));

            dispatched.WaitOne(500.Milliseconds()).ShouldBeTrue();

            syncHandler.Called.ShouldBeFalse();
            asyncHandler.CalledSignal.IsSet.ShouldBeTrue();
        }
Example #7
0
        public void should_filter_invoker()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var asyncHandler = new AsyncCommandHandler();
            _containerMock.Setup(x => x.GetInstance(typeof(AsyncCommandHandler))).Returns(asyncHandler);

            var syncHandler = new SyncCommandHandler();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var context = MessageContext.CreateTest("u.name");
            var command = new DispatchCommand();
            var dispatched = new ManualResetEvent(false);
            var dispatch = new MessageDispatch(context, command, (x, r) => dispatched.Set());
            _messageDispatcher.Dispatch(dispatch, x => x == typeof(AsyncCommandHandler));

            dispatched.WaitOne(500.Milliseconds()).ShouldBeTrue();

            syncHandler.Called.ShouldBeFalse();
            asyncHandler.CalledSignal.IsSet.ShouldBeTrue();
        }
Example #8
0
        public void should_invoke_both_sync_and_async_handlers()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var asyncHandler = new AsyncCommandHandler { WaitForSignal = true };
            _containerMock.Setup(x => x.GetInstance(typeof(AsyncCommandHandler))).Returns(asyncHandler);

            var syncHandler = new SyncCommandHandler();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandler))).Returns(syncHandler);

            var command = new DispatchCommand();
            Dispatch(command);

            syncHandler.Called.ShouldBeTrue();
            asyncHandler.CalledSignal.Wait(50.Milliseconds()).ShouldBeFalse();

            command.Signal.Set();

            asyncHandler.CalledSignal.Wait(1000.Milliseconds()).ShouldBeTrue();
        }