protected override void ConfigureInMemoryReceiveEndpoint(IInMemoryReceiveEndpointConfigurator configurator)
        {
            _consumer = new PingConsumer(TimeSpan.FromSeconds(5), InMemoryTestHarness.InactivityToken);

            configurator.UseRetry(r => r.Immediate(5));
            _consumer.Configure(configurator);
        }
        private async Task BasicPubSub(int concurrency, int subscribers)
        {
            // arrange
            var pingConsumer = new PingConsumer();

            MessageBusBuilder
            .WithDependencyResolver(new LookupDependencyResolver(f =>
            {
                if (f == typeof(PingConsumer))
                {
                    return(pingConsumer);
                }
                throw new InvalidOperationException();
            }));

            var messageBus = MessageBus.Value;

            // act

            // publish
            var stopwatch = Stopwatch.StartNew();

            var producedMessages = Enumerable
                                   .Range(0, NumberOfMessages)
                                   .Select(i => new PingMessage {
                Counter = i, Value = Guid.NewGuid()
            })
                                   .ToList();

            var messageTasks = producedMessages.Select(m => messageBus.Publish(m));
            // wait until all messages are sent
            await Task.WhenAll(messageTasks).ConfigureAwait(false);

            stopwatch.Stop();
            Log.InfoFormat(CultureInfo.InvariantCulture, "Published {0} messages in {1}", producedMessages.Count, stopwatch.Elapsed);

            // consume
            stopwatch.Restart();
            var consumersReceivedMessages = await ConsumeAll(pingConsumer, subscribers *producedMessages.Count);

            stopwatch.Stop();

            Log.InfoFormat(CultureInfo.InvariantCulture, "Consumed {0} messages in {1}", consumersReceivedMessages.Count, stopwatch.Elapsed);

            // assert

            // ensure number of instances of consumers created matches
            consumersReceivedMessages.Count.Should().Be(subscribers * producedMessages.Count);

            // ensure all messages arrived
            // ... the count should match
            consumersReceivedMessages.Count.Should().Be(subscribers * producedMessages.Count);
            // ... the content should match
            foreach (var producedMessage in producedMessages)
            {
                var messageCopies = consumersReceivedMessages.Count(x => x.Counter == producedMessage.Counter && x.Value == producedMessage.Value);
                messageCopies.Should().Be(subscribers);
            }
        }
Beispiel #3
0
        public void BasicPubSub()
        {
            // arrange

            var topic = $"test-ping";

            var pingConsumer = new PingConsumer();

            MessageBusBuilder
            .Publish <PingMessage>(x =>
            {
                x.DefaultTopic(topic);
            })
            .SubscribeTo <PingMessage>(x => x.Topic(topic)
                                       .Group("subscriber")          // ensure consumer group exists on the event hub
                                       .WithSubscriber <PingConsumer>()
                                       .Instances(2))
            .WithDependencyResolver(new LookupDependencyResolver(f =>
            {
                if (f == typeof(PingConsumer))
                {
                    return(pingConsumer);
                }
                throw new InvalidOperationException();
            }));

            var kafkaMessageBus = MessageBus.Value;

            // act

            // publish
            var stopwatch = Stopwatch.StartNew();

            var messages = Enumerable
                           .Range(0, NumberOfMessages)
                           .Select(i => new PingMessage {
                Counter = i, Timestamp = DateTime.UtcNow
            })
                           .ToList();

            messages
            .AsParallel()
            .ForAll(m => kafkaMessageBus.Publish(m).Wait());

            stopwatch.Stop();
            Log.InfoFormat("Published {0} messages in {1}", messages.Count, stopwatch.Elapsed);

            // consume
            stopwatch.Restart();
            var messagesReceived = ConsumeFromTopic(pingConsumer);

            stopwatch.Stop();
            Log.InfoFormat("Consumed {0} messages in {1}", messagesReceived.Count, stopwatch.Elapsed);

            // assert

            // all messages got back
            messagesReceived.Count.Should().Be(messages.Count);
        }
Beispiel #4
0
        private async Task BasicPubSub(int concurrency, int subscribers, int expectedMessageCopies)
        {
            // arrange
            var consumersCreated = new ConcurrentBag <PingConsumer>();
            var consumedMessages = new List <(PingMessage Message, string MessageId)>();

            MessageBusBuilder
            .WithDependencyResolver(new LookupDependencyResolver(f =>
            {
                if (f != typeof(PingConsumer))
                {
                    throw new InvalidOperationException();
                }
                var pingConsumer = new PingConsumer(_loggerFactory.CreateLogger <PingConsumer>(), consumedMessages);
                consumersCreated.Add(pingConsumer);
                return(pingConsumer);
            }));

            var messageBus = MessageBus.Value;

            // act

            // publish
            var stopwatch = Stopwatch.StartNew();

            var producedMessages = Enumerable
                                   .Range(0, NumberOfMessages)
                                   .Select(i => new PingMessage {
                Counter = i, Value = Guid.NewGuid()
            })
                                   .ToList();

            var messageTasks = producedMessages.Select(m => messageBus.Publish(m));
            // wait until all messages are sent
            await Task.WhenAll(messageTasks).ConfigureAwait(false);

            stopwatch.Stop();
            _logger.LogInformation("Published {0} messages in {1}", producedMessages.Count, stopwatch.Elapsed);

            // consume
            stopwatch.Restart();

            await WaitUntilArriving(consumedMessages);

            stopwatch.Stop();

            // assert

            // ensure number of instances of consumers created matches
            consumersCreated.Count.Should().Be(producedMessages.Count * expectedMessageCopies);
            consumedMessages.Count.Should().Be(producedMessages.Count * expectedMessageCopies);

            // ... the content should match
            foreach (var producedMessage in producedMessages)
            {
                var messageCopies = consumedMessages.Count(x => x.Message.Counter == producedMessage.Counter && x.Message.Value == producedMessage.Value && x.MessageId == GetMessageId(x.Message));
                messageCopies.Should().Be(expectedMessageCopies);
            }
        }
Beispiel #5
0
            protected override void ConfigureInputQueueEndpoint(IInMemoryReceiveEndpointConfigurator configurator)
            {
                _consumer = new PingConsumer(TestTimeout);

                _consumer.Configure(configurator);

                configurator.Consumer <MessageConsumer>();
            }
Beispiel #6
0
        protected override void PreCreateBus(IInMemoryBusFactoryConfigurator configurator)
        {
            var _pingConsumer = new PingConsumer();

            _pongConsumer = new PongConsumer();
            configurator.ReceiveEndpoint("test_ping_queue", e =>
            {
                e.Consumer(() => _pingConsumer);
            });

            configurator.ReceiveEndpoint("test_pong_queue", e =>
            {
                e.Consumer(() => _pongConsumer);
            });
        }
        private static IList <PingMessage> ConsumeFromTopic(PingConsumer pingConsumer)
        {
            var lastMessageCount     = 0;
            var lastMessageStopwatch = Stopwatch.StartNew();

            const int newMessagesAwaitingTimeout = 5;

            while (lastMessageStopwatch.Elapsed.TotalSeconds < newMessagesAwaitingTimeout)
            {
                Thread.Sleep(100);

                if (pingConsumer.Messages.Count != lastMessageCount)
                {
                    lastMessageCount = pingConsumer.Messages.Count;
                    lastMessageStopwatch.Restart();
                }
            }
            lastMessageStopwatch.Stop();
            return(pingConsumer.Messages);
        }
        private static async Task <IList <PingMessage> > ConsumeAll(PingConsumer consumer, int expectedCount)
        {
            var lastMessageCount     = 0;
            var lastMessageStopwatch = Stopwatch.StartNew();

            const int newMessagesAwaitingTimeout = 5;

            while (lastMessageStopwatch.Elapsed.TotalSeconds < newMessagesAwaitingTimeout && expectedCount != consumer.Messages.Count)
            {
                await Task.Delay(100).ConfigureAwait(false);

                if (consumer.Messages.Count != lastMessageCount)
                {
                    lastMessageCount = consumer.Messages.Count;
                    lastMessageStopwatch.Restart();
                }
            }
            lastMessageStopwatch.Stop();
            return(consumer.Messages);
        }