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

            var handler1 = new SyncCommandHandlerWithQueueName1
            {
                WaitForSignal = true
            };

            var handler2 = new SyncCommandHandlerWithQueueName2
            {
                WaitForSignal = false
            };

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler1);
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName2))).Returns(handler2);

            Dispatch(new DispatchCommand());

            Wait.Until(() => handler1.HandleStarted, 5.Seconds());
            var stopTask = Task.Run(() => _messageDispatcher.Stop()).WaitForActivation();

            Wait.Until(() => _messageDispatcher.Status == MessageDispatcherStatus.Stopping, 10.Seconds());
            Wait.Until(() => handler2.HandleStopped, 5.Seconds());

            handler1.WaitForSignal = false;
            handler2.HandleStopped = false;

            Assert.Throws <InvalidOperationException>(() => Dispatch(new DispatchCommand()));

            handler1.CalledSignal.Set();
            stopTask.Wait(5.Seconds()).ShouldBeTrue();
        }
        public void should_run_invoker_synchronously_if_dispatch_queue_name_equals_current_queue_name()
        {
            LoadAndStartDispatcher();

            var handler1 = new SyncCommandHandlerWithQueueName1
            {
                Callback = () =>
                {
                    var handler2 = new SyncCommandHandlerWithQueueName1();
                    _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler2);

                    // should be run synchronously
                    Dispatch(new DispatchCommand());

                    Wait.Until(() => handler2.HandleStopped, 5.Seconds());
                }
            };

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler1);

            // should be enqueued
            Dispatch(new DispatchCommand());

            Wait.Until(() => handler1.HandleStopped, 5.Seconds());
        }
        public void should_handle_local_dispatch_when_stopping()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler1 = new SyncCommandHandlerWithQueueName1
            {
                WaitForSignal = true
            };

            var handler2 = new SyncCommandHandlerWithQueueName2
            {
                WaitForSignal = false
            };

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler1);
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName2))).Returns(handler2);

            Dispatch(new DispatchCommand());

            Wait.Until(() => handler1.HandleStarted, 150.Milliseconds());
            var stopTask = Task.Run(() => _messageDispatcher.Stop());

            Wait.Until(() => _messageDispatcher.Status == MessageDispatcherStatus.Stopping, 150.Milliseconds());
            Wait.Until(() => handler2.HandleStopped, 150.Milliseconds());

            handler1.WaitForSignal = false;
            handler2.HandleStopped = false;

            Dispatch(new DispatchCommand(), true);

            Wait.Until(() => handler2.HandleStopped, 150.Milliseconds());
            handler1.CalledSignal.Set();

            stopTask.Wait(150.Milliseconds()).ShouldBeTrue();
        }
Ejemplo n.º 4
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_set_queue_name_in_message_context()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler1 = new SyncCommandHandlerWithQueueName1();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler1);

            Dispatch(new DispatchCommand());

            Wait.Until(() => handler1.HandleStopped, 150.Milliseconds());

            handler1.DispatchQueueName.ShouldEqual("DispatchQueue1");
        }
        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");
        }
Ejemplo n.º 7
0
        public void should_set_queue_name_in_message_context()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler1 = new SyncCommandHandlerWithQueueName1();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler1);

            Dispatch(new DispatchCommand());

            Wait.Until(() => handler1.HandleStopped, 150.Milliseconds());

            handler1.DispatchQueueName.ShouldEqual("DispatchQueue1");
        }
        public void should_clone_message_when_dispatching_locally_to_a_different_queue()
        {
            LoadAndStartDispatcher();

            var handler = new SyncCommandHandlerWithQueueName1();

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

            var command = new DispatchCommand();

            Dispatch(command, true);

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

            handler.ReceivedMessage.ShouldNotBeNull();
            handler.ReceivedMessage.ShouldNotBeTheSameAs(command);
            handler.ReceivedMessage.Guid.ShouldEqual(command.Guid);
        }
Ejemplo n.º 9
0
        public void should_not_hang_when_running_invoker_synchronously_in_same_dispatch_queue()
        {
            _messageDispatcher.ConfigureHandlerFilter(x => x == typeof(ForwardCommandHandler) || x == typeof(SyncCommandHandlerWithQueueName1));
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler1 = new ForwardCommandHandler {
                Action = x => Dispatch(new DispatchCommand(), x)
            };

            _containerMock.Setup(x => x.GetInstance(typeof(ForwardCommandHandler))).Returns(handler1);

            var handler2 = new SyncCommandHandlerWithQueueName1();

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler2);

            Dispatch(new ForwardCommand());

            Wait.Until(() => handler2.HandleStopped, 1000.Milliseconds());
        }
        public void should_handle_local_dispatch_when_stopping()
        {
            LoadAndStartDispatcher();

            var handler1 = new SyncCommandHandlerWithQueueName1
            {
                WaitForSignal = true
            };

            var handler2 = new SyncCommandHandlerWithQueueName2
            {
                WaitForSignal = false
            };

            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler1);
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName2))).Returns(handler2);

            Dispatch(new DispatchCommand());

            Wait.Until(() => handler1.HandleStarted, 10.Seconds());

            var stoppingSignal = new ManualResetEventSlim();

            _messageDispatcher.Stopping += () => stoppingSignal.Set();

            var stopTask = Task.Run(() => _messageDispatcher.Stop()).WaitForActivation();

            stoppingSignal.Wait(10.Seconds()).ShouldBeTrue();

            Wait.Until(() => handler2.HandleStopped, 10.Seconds());

            handler1.WaitForSignal = false;
            handler2.HandleStopped = false;

            Dispatch(new DispatchCommand(), true);

            Wait.Until(() => handler2.HandleStopped, 10.Seconds());
            handler1.CalledSignal.Set();

            stopTask.Wait(10.Seconds()).ShouldBeTrue();
        }
Ejemplo n.º 11
0
        public void should_run_invoker_synchronously_if_dispatch_queue_name_equals_current_queue_name()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler = new SyncCommandHandlerWithQueueName1()
            {
                WaitForSignal = true
            };

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

            var task = Task.Run(() =>
            {
                var context = MessageContext.CreateTest("u.name").WithDispatchQueueName("DispatchQueue1");
                Dispatch(new DispatchCommand(), context);
            });

            Thread.Sleep(150);
            task.IsCompleted.ShouldBeFalse("Dispatch should run synchronously");

            handler.CalledSignal.Set();
            Wait.Until(() => task.IsCompleted, 150.Milliseconds());
        }
        public void should_run_invoker_synchronously_if_dispatch_queue_name_equals_current_queue_name()
        {
            _messageDispatcher.LoadMessageHandlerInvokers();

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

            var task = Task.Run(() =>
            {
                var context = MessageContext.CreateTest("u.name").WithDispatchQueueName("DispatchQueue1");
                Dispatch(new DispatchCommand(), context);
            });

            Thread.Sleep(150);
            task.IsCompleted.ShouldBeFalse("Dispatch should run synchronously");

            handler.CalledSignal.Set();
            Wait.Until(() => task.IsCompleted, 150.Milliseconds());
        }
        public void should_not_hang_when_running_invoker_synchronously_in_same_dispatch_queue()
        {
            _messageDispatcher.ConfigureHandlerFilter(x => x == typeof(ForwardCommandHandler) || x == typeof(SyncCommandHandlerWithQueueName1));
            _messageDispatcher.LoadMessageHandlerInvokers();

            var handler1 = new ForwardCommandHandler { Action = x => Dispatch(new DispatchCommand(), x) };
            _containerMock.Setup(x => x.GetInstance(typeof(ForwardCommandHandler))).Returns(handler1);

            var handler2 = new SyncCommandHandlerWithQueueName1();
            _containerMock.Setup(x => x.GetInstance(typeof(SyncCommandHandlerWithQueueName1))).Returns(handler2);

            Dispatch(new ForwardCommand());

            Wait.Until(() => handler2.HandleStopped, 1000.Milliseconds());
        }