コード例 #1
0
        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();
        }
コード例 #2
0
        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();
        }
コード例 #3
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");
        }
コード例 #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");
        }
コード例 #5
0
        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();
        }