Пример #1
0
        public async Task SetUp()
        {
            await Task.WhenAll(Task.Run(async() =>
            {
                while (await TestObjectCreated1stConsumer.WaitAsync(500))
                {
                    ;
                }
                await TestObjectCreated1stConsumer.ResetAsync();
            }), Task.Run(async() =>
            {
                while (await TestObjectCreated2ndConsumer.WaitAsync(500))
                {
                    ;
                }
                await TestObjectCreated2ndConsumer.ResetAsync();
            }), Task.Run(async() =>
            {
                while (await TestObjectUpdatedConsumer.WaitAsync(500))
                {
                    ;
                }
                await TestObjectUpdatedConsumer.ResetAsync();
            }), Task.Run(async() =>
            {
                while (await TestObjectSharedConsumer.WaitAsync(500))
                {
                    ;
                }
                await TestObjectSharedConsumer.ResetAsync();
            }));

            _watch.Restart();
        }
Пример #2
0
        public async Task Publish_Consume_Redelivery()
        {
            var bus = Host.Services.GetRequiredService <IServiceBus>();
            var msg = new TestObjectCreated
            {
                Id = Guid.NewGuid(),
                ExpectedRedeliveries = 1,
                ChainedMessage       = new TestObjectUpdated
                {
                    Id = Guid.NewGuid(),
                    ExpectedRedeliveries = 1
                }
            };
            await bus.Publish(msg);

            //test that the 1st consumer has received the message 3 times, redelivered once, and completed once
            await TestObjectCreated1stConsumer.WaitAsync(5000);

            Assert.AreEqual(3, TestObjectCreated1stConsumer.Consumed);
            Assert.AreEqual(1, TestObjectCreated1stConsumer.Completed);
            Assert.AreEqual(0, TestObjectCreated1stConsumer.Remaining);
            Assert.NotNull(TestObjectCreated1stConsumer.LastContext);
            Assert.AreEqual(0, TestObjectCreated1stConsumer.LastContext.Retries);
            Assert.AreEqual(1, TestObjectCreated1stConsumer.LastContext.Redeliveries);

            //test that the 2nd consumer has received the message 3 times, redelivered once, and completed once
            await TestObjectCreated2ndConsumer.WaitAsync(5000);

            Assert.AreEqual(3, TestObjectCreated2ndConsumer.Consumed);
            Assert.AreEqual(1, TestObjectCreated2ndConsumer.Completed);
            Assert.AreEqual(0, TestObjectCreated2ndConsumer.Remaining);
            Assert.NotNull(TestObjectCreated2ndConsumer.LastContext);
            Assert.AreEqual(0, TestObjectCreated2ndConsumer.LastContext.Retries);
            Assert.AreEqual(1, TestObjectCreated2ndConsumer.LastContext.Redeliveries);

            //test that the consumer has recieved the message 6 times and completed twice
            await TestObjectUpdatedConsumer.WaitAsync(5000); //wait for 1st message

            await TestObjectUpdatedConsumer.WaitAsync(5000); //wait for 2nd message

            Assert.AreEqual(6, TestObjectUpdatedConsumer.Consumed);
            Assert.AreEqual(2, TestObjectUpdatedConsumer.Completed);
            Assert.AreEqual(0, TestObjectUpdatedConsumer.Remaining);
            Assert.NotNull(TestObjectUpdatedConsumer.LastContext);
            Assert.AreEqual(0, TestObjectUpdatedConsumer.LastContext.Retries);
            Assert.AreEqual(1, TestObjectUpdatedConsumer.LastContext.Redeliveries);

            //test that message id has not changed
            Assert.NotNull(TestObjectUpdatedConsumer.LastMessage);
            Assert.AreEqual(msg.ChainedMessage.Id, TestObjectUpdatedConsumer.LastMessage.Id);
        }
Пример #3
0
        public async Task Publish_Consume_Performance()
        {
            var watch = Stopwatch.StartNew();

            watch.Restart();
            var bus = Host.Services.GetRequiredService <IServiceBus>();
            await Task.WhenAll(Enumerable.Range(0, PublishCount).Select(async i =>
            {
                var msg = new TestObjectUpdated {
                    Id = Guid.NewGuid()
                };
                await bus.Publish(msg);
            }));

            watch.Stop();
            var rate          = Math.Round(PublishCount / watch.Elapsed.TotalMinutes, 2);
            var avg           = Math.Round(watch.Elapsed.TotalMilliseconds / PublishCount, 2);
            var publishResult = new
            {
                Published   = PublishCount,
                PublishRate = $"{rate:n} msgs per min",
                PublishAvg  = $"{avg:n}ms per msg"
            };

            watch.Restart();
            while (TestObjectUpdatedConsumer.Completed < PublishCount)
            {
                if (!await TestObjectUpdatedConsumer.WaitAsync(5000))
                {
                    break;
                }
            }
            watch.Stop();
            rate = Math.Round(TestObjectUpdatedConsumer.Completed / watch.Elapsed.TotalMinutes, 2);
            avg  = Math.Round(watch.Elapsed.TotalMilliseconds / TestObjectUpdatedConsumer.Completed, 2);
            var consumeResult = new
            {
                TestObjectUpdatedConsumer.Completed,
                ConsumeRate = $"{rate:n} msgs per min",
                ConsumeAvg  = $"{avg:n}ms per msg"
            };

            Console.WriteLine(publishResult);
            Console.WriteLine(consumeResult);
            Assert.AreEqual(PublishCount, TestObjectUpdatedConsumer.Completed);
        }
Пример #4
0
        public async Task Publish_Consume()
        {
            var bus = Host.Services.GetRequiredService <IServiceBus>();
            var msg = new TestObjectUpdated {
                Id = Guid.NewGuid()
            };
            await bus.Publish(msg);

            //test that the update consumer has received the message only once
            await TestObjectUpdatedConsumer.WaitAsync(5000);

            Assert.AreEqual(1, TestObjectUpdatedConsumer.Consumed);
            Assert.AreEqual(1, TestObjectUpdatedConsumer.Completed);
            Assert.AreEqual(0, TestObjectUpdatedConsumer.Remaining);
            Assert.NotNull(TestObjectUpdatedConsumer.LastContext);
            Assert.AreEqual(0, TestObjectUpdatedConsumer.LastContext.Retries);
            Assert.AreEqual(0, TestObjectUpdatedConsumer.LastContext.Redeliveries);
        }