|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using RabbitMQ.AMQP.Client; |
| 5 | +using RabbitMQ.AMQP.Client.Impl; |
| 6 | +using Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace Tests.Consumer; |
| 10 | + |
| 11 | +public class ConsumerOutcomeTests(ITestOutputHelper testOutputHelper) : IntegrationTest(testOutputHelper) |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public void ValidateAnnotations() |
| 15 | + { |
| 16 | + const string wrongAnnotationKey = "missing-the-start-x-annotation-key"; |
| 17 | + const string annotationValue = "annotation-value"; |
| 18 | + // This should throw an exception because the annotation key does not start with "x-" |
| 19 | + Assert.Throws<ArgumentException>(() => |
| 20 | + Utils.ValidateMessageAnnotations(new Dictionary<string, object> |
| 21 | + { |
| 22 | + { wrongAnnotationKey, annotationValue } |
| 23 | + })); |
| 24 | + |
| 25 | + const string correctAnnotationKey = "x-otp-annotation-key"; |
| 26 | + // This should not throw an exception because the annotation key starts with "x-" |
| 27 | + Utils.ValidateMessageAnnotations(new Dictionary<string, object> |
| 28 | + { |
| 29 | + { correctAnnotationKey, annotationValue } |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task DiscardedMessageWithAnnotationsShouldBeDeadLeadLetteredAndContainAnnotationsWhenConfigured() |
| 35 | + { |
| 36 | + string dlqQueueName = $"dlq_{_queueName}"; |
| 37 | + await DeclareDeadLetterTopology(_queueName, dlqQueueName); |
| 38 | + |
| 39 | + |
| 40 | + Assert.NotNull(_connection); |
| 41 | + Assert.NotNull(_management); |
| 42 | + |
| 43 | + const string annotationKey = "x-opt-annotation-key"; |
| 44 | + const string annotationValue = "annotation-value"; |
| 45 | + TaskCompletionSource<bool> tcs = |
| 46 | + new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 47 | + IPublisher publisher = await _connection.PublisherBuilder().Queue(_queueName).BuildAsync(); |
| 48 | + IConsumer consumer = await _connection.ConsumerBuilder().MessageHandler( |
| 49 | + async (context, _) => |
| 50 | + { |
| 51 | + await context.DiscardAsync(new Dictionary<string, object> { { annotationKey, annotationValue } }); |
| 52 | + tcs.SetResult(true); |
| 53 | + } |
| 54 | + ).Queue(_queueName).BuildAndStartAsync(); |
| 55 | + |
| 56 | + IMessage message = new AmqpMessage($"message"); |
| 57 | + PublishResult pr = await publisher.PublishAsync(message); |
| 58 | + Assert.Equal(OutcomeState.Accepted, pr.Outcome.State); |
| 59 | + await tcs.Task.WaitAsync(TimeSpan.FromSeconds(5)); |
| 60 | + await consumer.CloseAsync(); |
| 61 | + TaskCompletionSource<IMessage> tcsDl = |
| 62 | + new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 63 | + IConsumer dlConsumer = await _connection.ConsumerBuilder().MessageHandler(async (context, message1) => |
| 64 | + { |
| 65 | + await context.AcceptAsync(); |
| 66 | + tcsDl.SetResult(message1); |
| 67 | + }).Queue(dlqQueueName).BuildAndStartAsync(); |
| 68 | + |
| 69 | + IMessage mResult = await tcsDl.Task.WaitAsync(TimeSpan.FromSeconds(5)); |
| 70 | + |
| 71 | + Assert.NotNull(mResult); |
| 72 | + Assert.Equal(mResult.Annotation(annotationKey), annotationValue); |
| 73 | + await dlConsumer.CloseAsync(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private async Task DeclareDeadLetterTopology(string queueName, string dlxQueueName) |
| 78 | + { |
| 79 | + string dlx = $"{queueName}.dlx"; |
| 80 | + Assert.NotNull(_management); |
| 81 | + await _management.Queue().Name(queueName).Type(QueueType.QUORUM).DeadLetterExchange(dlx).DeclareAsync(); |
| 82 | + await _management.Exchange(dlx).Type(ExchangeType.FANOUT).AutoDelete(true).DeclareAsync(); |
| 83 | + await _management.Queue(dlxQueueName).Exclusive(true).DeclareAsync(); |
| 84 | + await _management.Binding().SourceExchange(dlx).DestinationQueue(dlxQueueName).BindAsync(); |
| 85 | + } |
| 86 | +} |
0 commit comments