コード例 #1
0
        public async Task GivenNonCompletedSystemMessage_ShouldHaltProcessingUntilCompletion()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessageWithTaskCompletionSource();
            var msg2 = new TestMessageWithTaskCompletionSource();

            mailbox.PostSystemMessage(msg1);
            mailbox.PostSystemMessage(msg2);

            Assert.True(systemMessages.HasMessages,
                        "Mailbox should not have processed msg2 because processing of msg1 is not completed."
                        );
            msg2.TaskCompletionSource.SetResult(0);
            msg1.TaskCompletionSource.SetResult(0);
            await Task.Delay(1000);

            Assert.False(systemMessages.HasMessages,
                         "Mailbox should have processed msg2 because processing of msg1 is completed."
                         );
        }
コード例 #2
0
        public async Task GivenNonCompletedSystemMessage_ShouldHaltProcessingUntilCompletion()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();
            var msg2 = new TestMessage();

            mailbox.PostSystemMessage(msg1);
            mailbox.PostSystemMessage(msg2);
            Assert.True(systemMessages.HasMessages, "Mailbox should not have processed msg2 because processing of msg1 is not completed.");

            Action resumeMailboxTrigger = () =>
            {
                // mailbox is waiting on msg1 to be completed before continuing
                // setting msg2 first guarantees that both messages will be processed
                msg2.TaskCompletionSource.SetResult(0);
                msg1.TaskCompletionSource.SetResult(0);
            };

            await mailboxHandler.ResumeMailboxProcessingAndWaitAsync(resumeMailboxTrigger)
            .ConfigureAwait(false);

            Assert.False(systemMessages.HasMessages, "Mailbox should have processed msg2 because processing of msg1 is completed.");
        }
コード例 #3
0
        public MailboxStatisticsTests()
        {
            _mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();

            _mailboxStatistics = new TestMailboxStatistics();
            _mailbox           = new DefaultMailbox(systemMessages, userMailbox, _mailboxStatistics);
            _mailbox.RegisterHandlers(_mailboxHandler, _mailboxHandler);
        }
コード例 #4
0
        public void GivenMailboxStarted_ShouldInvokeMailboxStarted()
        {
            var mailboxHandler    = new TestMailboxHandler();
            var userMailbox       = new UnboundedMailboxQueue();
            var systemMessages    = new UnboundedMailboxQueue();
            var mailboxStatistics = new TestMailboxStatistics();
            var mailbox           = new DefaultMailbox(systemMessages, userMailbox, mailboxStatistics);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            mailbox.Start();

            Assert.Contains("Started", mailboxStatistics.Stats);
        }
コード例 #5
0
        public void GivenSystemMessage_ShouldInvokeMessagePosted()
        {
            var mailboxHandler    = new TestMailboxHandler();
            var userMailbox       = new UnboundedMailboxQueue();
            var systemMessages    = new UnboundedMailboxQueue();
            var mailboxStatistics = new TestMailboxStatistics();
            var mailbox           = new DefaultMailbox(systemMessages, userMailbox, mailboxStatistics);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();

            mailbox.PostSystemMessage(msg1);
            Assert.Contains(msg1, mailboxStatistics.Posted);
        }
コード例 #6
0
        public void GivenCompletedUserMessage_ShouldInvokeMessageReceivedImmediately()
        {
            var mailboxHandler    = new TestMailboxHandler();
            var userMailbox       = new UnboundedMailboxQueue();
            var systemMessages    = new UnboundedMailboxQueue();
            var mailboxStatistics = new TestMailboxStatistics();
            var mailbox           = new DefaultMailbox(systemMessages, userMailbox, mailboxStatistics);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();

            msg1.TaskCompletionSource.SetResult(0);

            mailbox.PostUserMessage(msg1);
            Assert.Contains(msg1, mailboxStatistics.Posted);
        }
コード例 #7
0
        public void GivenNonCompletedUserMessage_ShouldSetMailboxToIdleAfterCompletion()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();

            mailbox.PostUserMessage(msg1);
            msg1.TaskCompletionSource.SetResult(0);

            Thread.Sleep(500); // wait for mailbox to finish

            Assert.True(mailbox.Status == MailboxStatus.Idle, "Mailbox should be set back to Idle after completion of message.");
        }
コード例 #8
0
        public void GivenCompletedSystemMessageThrewException_ShouldNotInvokeMessageReceived()
        {
            var mailboxHandler    = new TestMailboxHandler();
            var userMailbox       = new UnboundedMailboxQueue();
            var systemMessages    = new UnboundedMailboxQueue();
            var mailboxStatistics = new TestMailboxStatistics();
            var mailbox           = new DefaultMailbox(systemMessages, userMailbox, mailboxStatistics);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();

            msg1.TaskCompletionSource.SetException(new Exception());

            mailbox.PostSystemMessage(msg1);

            Assert.DoesNotContain(msg1, mailboxStatistics.Received);
        }
コード例 #9
0
        public void GivenCompletedSystemMessage_ShouldContinueProcessing()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();
            var msg2 = new TestMessage();

            msg1.TaskCompletionSource.SetResult(0);
            msg2.TaskCompletionSource.SetResult(0);

            mailbox.PostSystemMessage(msg1);
            mailbox.PostSystemMessage(msg2);
            Assert.False(systemMessages.HasMessages, "Mailbox should have processed both messages because they were already completed.");
        }
コード例 #10
0
        public async Task GivenNonCompletedUserMessage_ShouldSetMailboxToIdleAfterCompletion()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();

            mailbox.PostUserMessage(msg1);

            Action resumeMailboxTrigger = () => msg1.TaskCompletionSource.SetResult(0);
            await mailboxHandler.ResumeMailboxProcessingAndWaitAsync(resumeMailboxTrigger)
            .ConfigureAwait(false);

            Assert.True(mailbox.Status == MailboxStatus.Idle, "Mailbox should be set back to Idle after completion of message.");
        }
コード例 #11
0
        public async Task GivenNonCompletedSystemMessageThrewException_ShouldNotInvokeMessageReceived()
        {
            var mailboxHandler    = new TestMailboxHandler();
            var userMailbox       = new UnboundedMailboxQueue();
            var systemMessages    = new UnboundedMailboxQueue();
            var mailboxStatistics = new TestMailboxStatistics();
            var mailbox           = new DefaultMailbox(systemMessages, userMailbox, mailboxStatistics);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();

            mailbox.PostSystemMessage(msg1);

            Action resumeMailboxTrigger = () => msg1.TaskCompletionSource.SetException(new Exception());
            await mailboxHandler.ResumeMailboxProcessingAndWaitAsync(resumeMailboxTrigger)
            .ConfigureAwait(false);

            Assert.DoesNotContain(msg1, mailboxStatistics.Received);
        }
コード例 #12
0
        public void GivenNonCompletedUserMessage_ShouldHaltProcessingUntilCompletion()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessage();
            var msg2 = new TestMessage();

            mailbox.PostUserMessage(msg1);
            mailbox.PostUserMessage(msg2);
            Assert.True(userMailbox.HasMessages, "Mailbox should not have processed msg2 because processing of msg1 is not completed.");

            msg1.TaskCompletionSource.SetResult(0);
            Thread.Sleep(10);
            Assert.False(userMailbox.HasMessages, "Mailbox should have processed msg2 because processing of msg1 is completed.");
        }
コード例 #13
0
        public void GivenCompletedUserMessageTaskThrewException_ShouldEscalateFailure()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1          = new TestMessage();
            var taskException = new Exception();

            msg1.TaskCompletionSource.SetException(taskException);

            mailbox.PostUserMessage(msg1);

            Assert.Equal(1, mailboxHandler.EscalatedFailures.Count);
            var e = Assert.IsType <AggregateException>(mailboxHandler.EscalatedFailures[0]);

            Assert.Equal(taskException, e.InnerException);
        }
コード例 #14
0
        public async Task GivenNonCompletedUserMessage_ShouldSetMailboxToIdleAfterCompletion()
        {
            var mailboxHandler = new TestMailboxHandler();
            var userMailbox    = new UnboundedMailboxQueue();
            var systemMessages = new UnboundedMailboxQueue();
            var mailbox        = new DefaultMailbox(systemMessages, userMailbox);

            mailbox.RegisterHandlers(mailboxHandler, mailboxHandler);

            var msg1 = new TestMessageWithTaskCompletionSource();

            mailbox.PostUserMessage(msg1);

            await Task.Delay(1000);

            msg1.TaskCompletionSource.SetResult(0);
            await Task.Delay(1000);

            Assert.True(mailbox.Status == MailboxStatus.Idle,
                        "Mailbox should be set back to Idle after completion of message."
                        );
        }