Esempio n. 1
0
        public async Task 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.");

            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(userMailbox.HasMessages, "Mailbox should have processed msg2 because processing of msg1 is completed.");
        }
Esempio n. 2
0
        public async Task 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 TestMessageWithTaskCompletionSource();
            var msg2 = new TestMessageWithTaskCompletionSource();

            mailbox.PostUserMessage(msg1);
            mailbox.PostUserMessage(msg2);

            await Task.Delay(1000);

            Assert.True(userMailbox.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(userMailbox.HasMessages,
                         "Mailbox should have processed msg2 because processing of msg1 is completed."
                         );
        }
        public async Task GivenUserMessage_ShouldInvokeMessagePosted()
        {
            var msg1 = new TestMessageWithTaskCompletionSource();

            _mailbox.PostUserMessage(msg1);
            await Task.Delay(1000);

            Assert.Contains(msg1, _mailboxStatistics.Posted);
        }
Esempio n. 4
0
        public void GivenCompletedUserMessage_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.PostUserMessage(msg1);
            mailbox.PostUserMessage(msg2);
            Assert.False(userMailbox.HasMessages, "Mailbox should have processed both messages because they were already completed.");
        }
        public void GivenUserMessage_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.PostUserMessage(msg1);
            Assert.Contains(msg1, mailboxStatistics.Posted);
        }
        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);
        }
Esempio n. 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.");
        }
        public void GivenCompletedUserMessageThrewException_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.PostUserMessage(msg1);

            Assert.DoesNotContain(msg1, mailboxStatistics.Received);
        }
Esempio n. 9
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.");
        }
        public async Task GivenNonCompletedUserMessageThrewException_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.PostUserMessage(msg1);

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

            Assert.DoesNotContain(msg1, mailboxStatistics.Received);
        }
        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);
        }
Esempio n. 12
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."
                        );
        }