Example #1
0
        public async Task ReceiveExactCountRequeueTest(int requeueCount)
        {
            var taskQueue             = Guid.NewGuid().ToString();
            var processingQueuePrefix = Guid.NewGuid().ToString();

            var requeueLeft    = requeueCount;
            var testDtoHandler = new TestDtoHandler(async ack =>
            {
                if (Interlocked.Decrement(ref requeueLeft) > 0)
                {
                    await ack.RequeueAsync();
                }
                else
                {
                    await ack.AckAsync();
                }
            });

            using (var manager = CreaterConsumerManager(taskQueue, processingQueuePrefix, new[] { testDtoHandler }))
            {
                manager.Start();

                var messageId   = Guid.NewGuid().ToString();
                var testMessage = new TestMessage();

                await SendMessage(taskQueue, messageId, testMessage);

                await WaitingAssert.CheckBecomeTrueAsync(
                    assertion : () => Assert.Equal(requeueCount, testDtoHandler.Received.Count),
                    waitIntervalMs : 50,
                    waitRetries : 20);

                Assert.All(testDtoHandler.Received, (tuple) => Assert.Equal(messageId, tuple.MessageId));
            }
        }
Example #2
0
        public async Task NackedToDeadLetterQueueTest()
        {
            var taskQueue             = Guid.NewGuid().ToString();
            var processingQueuePrefix = Guid.NewGuid().ToString();
            var deadLetterQueue       = Guid.NewGuid().ToString();

            var testDtoHandler = new TestDtoHandler(async ack => await ack.NackAsync());

            using (var manager = CreaterConsumerManager(
                       taskQueue,
                       processingQueuePrefix,
                       new[] { testDtoHandler },
                       deadLetterQueue))
            {
                manager.Start();

                var messageId   = Guid.NewGuid().ToString();
                var testMessage = new TestMessage();

                await SendMessage(taskQueue, messageId, testMessage);

                var redisConnection = await ConnectionMultiplexer.ConnectAsync(ConnectionString);

                var db = redisConnection.GetDatabase();

                await WaitingAssert.CheckBecomeTrueAsync(
                    assertion : async() =>
                {
                    var fetched = await db.ListLeftPopAsync(deadLetterQueue);

                    Assert.True(fetched.HasValue);

                    var keybuilder = new DefaultRedisMessageKeyBuilder();
                    var properties = keybuilder.GetMessageProperties(fetched);
                    Assert.Equal(messageId, properties.MessageId);
                },
                    waitIntervalMs : 50,
                    waitRetries : 20);
            }
        }
Example #3
0
        public async Task ReceiveExactlyOnceNackedTest()
        {
            var taskQueue             = Guid.NewGuid().ToString();
            var processingQueuePrefix = Guid.NewGuid().ToString();
            var testDtoHandler        = new TestDtoHandler(async ack => await ack.NackAsync());

            using (var manager = CreaterConsumerManager(taskQueue, processingQueuePrefix, new[] { testDtoHandler }))
            {
                manager.Start();

                var messageId   = Guid.NewGuid().ToString();
                var testMessage = new TestMessage();

                await SendMessage(taskQueue, messageId, testMessage);

                await WaitingAssert.CheckBecomeTrueAsync(
                    assertion : () => Assert.Equal(1, testDtoHandler.Received.Count),
                    waitIntervalMs : 50,
                    waitRetries : 20);

                Assert.Collection(testDtoHandler.Received, (tuple) => Assert.Equal(messageId, tuple.MessageId));
            }
        }
Example #4
0
        public async Task StartingMoreThanInstanceCountConsumersTest()
        {
            var taskQueue             = Guid.NewGuid().ToString();
            var processingQueuePrefix = Guid.NewGuid().ToString();

            var messagesCount  = 500;
            var workDurationMs = 10;

            for (var i = 0; i < messagesCount; i++)
            {
                var messageId   = Guid.NewGuid().ToString();
                var testMessage = new TestMessage();

                await SendMessage(taskQueue, messageId, testMessage);
            }

            var testDtoHandler1 = new TestDtoHandler(AcknowledgementTask);
            var testDtoHandler2 = new TestDtoHandler(AcknowledgementTask);
            var testDtoHandler3 = new TestDtoHandler(AcknowledgementTask);

            using (var manager1 = CreaterConsumerManager(
                       taskQueue,
                       processingQueuePrefix,
                       new[] { testDtoHandler1 },
                       instanceCount: 2,
                       lookupDelayMilliseconds: 50))
                using (var manager2 = CreaterConsumerManager(
                           taskQueue,
                           processingQueuePrefix,
                           new[] { testDtoHandler2 },
                           instanceCount: 2,
                           lookupDelayMilliseconds: 50))
                    using (var manager3 = CreaterConsumerManager(
                               taskQueue,
                               processingQueuePrefix,
                               new[] { testDtoHandler3 },
                               instanceCount: 2,
                               lookupDelayMilliseconds: 50))
                    {
                        manager1.Start();
                        manager2.Start();

                        await Task.Delay(workDurationMs * 2);

                        manager3.Start();

                        await WaitingAssert.CheckBecomeTrueAsync(
                            assertion : () =>
                            Assert.Equal(
                                expected: messagesCount,
                                actual: testDtoHandler1.Received.Count + testDtoHandler2.Received.Count),
                            waitIntervalMs : 50,
                            waitRetries : 200);

                        Assert.Equal(0, testDtoHandler3.Received.Count);
                    }

            async Task AcknowledgementTask(IAcknowledgement ack)
            {
                await Task.Delay(workDurationMs);

                await ack.AckAsync();
            }
        }
Example #5
0
        public async Task SecondShouldStartProcessingTest()
        {
            var messagesCount = 500;
            var stopConsumerAfterNumberOfMessages = 50;
            var workDurationMs = 5;

            var taskQueue             = Guid.NewGuid().ToString();
            var processingQueuePrefix = Guid.NewGuid().ToString();

            var sendMessagesIds = new string[messagesCount];

            for (var i = 0; i < messagesCount; i++)
            {
                var messageId   = Guid.NewGuid().ToString();
                var testMessage = new TestMessage();

                await SendMessage(taskQueue, messageId, testMessage);

                sendMessagesIds[i] = messageId;
            }

            var testDtoHandler1 = new TestDtoHandler(DelayedAcknowledgementTask);
            var testDtoHandler2 = new TestDtoHandler(AcknowledgementTask);

            using (var manager1 = CreaterConsumerManager(
                       taskQueue,
                       processingQueuePrefix,
                       new[] { testDtoHandler1 },
                       instanceCount: 1,
                       lookupDelayMilliseconds: 100,
                       output: _output))
                using (var manager2 = CreaterConsumerManager(
                           taskQueue,
                           processingQueuePrefix,
                           new[] { testDtoHandler2 },
                           instanceCount: 1,
                           lookupDelayMilliseconds: 100,
                           output: _output))
                {
                    manager1.Start();
                    await Task.Delay(10);

                    manager2.Start();

                    await Task.Delay(workDurationMs *stopConsumerAfterNumberOfMessages);

                    Assert.Equal(0, testDtoHandler2.Received.Count);
                    manager1.Dispose();

                    await WaitingAssert.CheckBecomeTrueAsync(
                        assertion : () =>
                        Assert.Equal(
                            expected: messagesCount,
                            actual: testDtoHandler1.Received.Count + testDtoHandler2.Received.Count),
                        waitIntervalMs : 100,
                        waitRetries : 100);

                    // check both handlers received messages
                    Assert.NotInRange(testDtoHandler1.Received.Count, 0, 1);
                    Assert.NotInRange(testDtoHandler2.Received.Count, 0, 1);

                    Assert.All(testDtoHandler1.Received,
                               (tuple) => Assert.Contains(sendMessagesIds, id => id == tuple.MessageId));
                }

            async Task DelayedAcknowledgementTask(IAcknowledgement ack)
            {
                await Task.Delay(workDurationMs);

                await ack.AckAsync();
            }

            async Task AcknowledgementTask(IAcknowledgement ack)
            {
                await ack.AckAsync();
            }
        }