예제 #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.");
        }
        public async Task GivenSystemMessage_ShouldInvokeMessagePosted()
        {
            var msg1 = new TestMessageWithTaskCompletionSource();

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

            Assert.Contains(msg1, _mailboxStatistics.Posted);
        }
예제 #4
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.");
        }
        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);
        }
        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);
        }
        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);
        }
        public void GivenCompletedSystemMessageTaskThrewException_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.PostSystemMessage(msg1);

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

            Assert.Equal(taskException, e.InnerException);
        }