public void Publish_MultipleSubscribedMessageTypes_AllMessagesReceived()
        {
            // Arrange
            TestMessageType1  msg1     = new TestMessageType1("Msg1", 1, 1.2);
            TestMessageType2  msg2     = new TestMessageType2("Msg2", 2);
            TestMessageType3  msg3     = new TestMessageType3("Msg3", 3);
            TestMessageType4  msg4     = new TestMessageType4("Msg4", 4);
            TestEventConsumer consumer = GetTestConsumer();

            // Act
            GetTestPublisher().Publish(AggregateType12, AggregateType12, new List <IDomainEvent> {
                msg1
            });
            GetTestPublisher().Publish(AggregateType12, AggregateType12, new List <IDomainEvent> {
                msg2
            });
            GetTestPublisher().Publish(AggregateType34, AggregateType34, new List <IDomainEvent> {
                msg3
            });
            GetTestPublisher().Publish(AggregateType34, AggregateType34, new List <IDomainEvent> {
                msg4
            });

            // Allow time for messages to process
            int count = 10;

            while (consumer.TotalMessageCount() < 4 && count > 0)
            {
                Thread.Sleep(1000);
                count--;
            }

            ShowTestResults();

            // Assert
            Assert.That(GetDbContext().Messages.Count(),
                        Is.EqualTo(4), "Number of messages produced");
            Assert.That(GetDbContext().Messages.Count(msg => msg.Published == 0),
                        Is.EqualTo(0), "Number of unpublished messages");
            foreach (Type eventType in consumer.GetEventTypes())
            {
                TestEventConsumer.EventStatistics eventStatistics = consumer.GetEventStatistics(eventType);
                Assert.That(eventStatistics.MessageCount,
                            Is.EqualTo(1), $"Number of {eventType.Name} messages received by consumer");
                Assert.That(eventStatistics.ReceivedMessages.Count,
                            Is.EqualTo(1), $"Number of received {eventType.Name} messages");
            }

            Assert.That(consumer.TotalMessageCount(),
                        Is.EqualTo(4), "Total number of messages received by consumer");
            Assert.That(GetDbContext().ReceivedMessages.Count(msg => msg.MessageId != null),
                        Is.EqualTo(4), "Number of received messages");
            msg1.AssertGoodMessageReceived(consumer.GetEventStatistics(typeof(TestMessageType1)).ReceivedMessages[0]);
            msg2.AssertGoodMessageReceived(consumer.GetEventStatistics(typeof(TestMessageType2)).ReceivedMessages[0]);
            msg3.AssertGoodMessageReceived(consumer.GetEventStatistics(typeof(TestMessageType3)).ReceivedMessages[0]);
            msg4.AssertGoodMessageReceived(consumer.GetEventStatistics(typeof(TestMessageType4)).ReceivedMessages[0]);

            GetTestMessageInterceptor()?.AssertCounts(4, 4, 4, 4, 4, 4);
        }
        public void Publish_SingleSubscribedMessageType1_MessageReceived()
        {
            // Arrange
            TestMessageType1 msg1 = new TestMessageType1("Msg1", 1, 1.2);

            TestEventConsumer.EventStatistics eventStatistics = GetTestConsumer().GetEventStatistics(
                typeof(TestMessageType1));

            // Act
            GetTestPublisher().Publish(AggregateType12, AggregateType12, new List <IDomainEvent> {
                msg1
            });

            // Allow time for messages to process
            AssertMessagesArePublishedAndConsumed(eventStatistics);

            msg1.AssertGoodMessageReceived(eventStatistics.ReceivedMessages[0]);

            GetTestMessageInterceptor()?.AssertCounts(1, 1, 1, 1, 1, 1);
        }
        public void Publish_DefaultEventTypeName_CorrectEventTypeHeader()
        {
            // Arrange
            TestMessageType1  msg1     = new TestMessageType1("Msg1", 2, 3.3);
            TestEventConsumer consumer = GetTestConsumer();

            // Act
            GetTestPublisher().Publish(AggregateType12, AggregateType12, new List <IDomainEvent> {
                msg1
            });

            // Allow time for messages to process
            int count = 10;

            TestEventConsumer.EventStatistics type1Statistics = consumer.GetEventStatistics(typeof(TestMessageType1));
            while (type1Statistics.MessageCount < 1 && count > 0)
            {
                Thread.Sleep(1000);
                count--;
            }

            ShowTestResults();

            // Assert
            Assert.That(GetDbContext().Messages.Count(),
                        Is.EqualTo(1), "Number of messages produced");
            Assert.That(GetDbContext().Messages.Count(msg => msg.Published == 0),
                        Is.EqualTo(0), "Number of unpublished messages");
            Assert.That(GetDbContext().ReceivedMessages.Count(msg => msg.MessageId != null),
                        Is.EqualTo(1), "Number of received messages");
            Assert.That(type1Statistics.MessageCount,
                        Is.EqualTo(1), "Number of Type 1 messages received by consumer");
            Assert.That(type1Statistics.ReceivedMessages.Count,
                        Is.EqualTo(1), "Number of received type 1 messages");

            msg1.AssertGoodMessageReceived(type1Statistics.ReceivedMessages[0]);
            Assert.That(type1Statistics.ReceivedMessages[0].Message.GetHeader(EventMessageHeaders.EventType),
                        Is.EqualTo(typeof(TestMessageType1).FullName), "Event type header");

            GetTestMessageInterceptor()?.AssertCounts(1, 1, 1, 1, 1, 1);
        }