|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 2.0. |
| 3 | +// Copyright (c) 2007-2023 VMware, Inc. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using RabbitMQ.Stream.Client; |
| 8 | +using RabbitMQ.Stream.Client.Reliable; |
| 9 | +using Xunit; |
| 10 | +using Xunit.Abstractions; |
| 11 | + |
| 12 | +namespace Tests; |
| 13 | + |
| 14 | +public class DeduplicationProducerTests |
| 15 | +{ |
| 16 | + private readonly ITestOutputHelper _testOutputHelper; |
| 17 | + |
| 18 | + public DeduplicationProducerTests(ITestOutputHelper testOutputHelper) |
| 19 | + { |
| 20 | + _testOutputHelper = testOutputHelper; |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task ValidateDeduplicationProducer() |
| 25 | + { |
| 26 | + SystemUtils.InitStreamSystemWithRandomStream(out var system, out var stream); |
| 27 | + Assert.Throws<ArgumentException>(() => new DeduplicatingProducerConfig(system, stream, null)); |
| 28 | + await Assert.ThrowsAsync<ArgumentException>(async () => |
| 29 | + // reference is white space, not valid |
| 30 | + await DeduplicatingProducer.Create(new DeduplicatingProducerConfig(system, stream, " "))); |
| 31 | + await SystemUtils.CleanUpStreamSystem(system, stream); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task GetLastIdShouldBeEqualtoTheMessagesSent() |
| 36 | + { |
| 37 | + // here we create a producer with a reference |
| 38 | + // the reference is used to enable the deduplication |
| 39 | + // then we query the sequence externally form the producer to be sure that |
| 40 | + // the values are the same |
| 41 | + SystemUtils.InitStreamSystemWithRandomStream(out var system, out var stream); |
| 42 | + var testPassed = new TaskCompletionSource<ulong>(); |
| 43 | + const ulong TotalMessages = 1000UL; |
| 44 | + var p = await DeduplicatingProducer.Create( |
| 45 | + new DeduplicatingProducerConfig(system, stream, "my_producer_reference") |
| 46 | + { |
| 47 | + ConfirmationHandler = async confirmation => |
| 48 | + { |
| 49 | + if (confirmation.PublishingId == TotalMessages) |
| 50 | + testPassed.SetResult(TotalMessages); |
| 51 | + await Task.CompletedTask; |
| 52 | + }, |
| 53 | + }); |
| 54 | + for (ulong i = 1; i <= TotalMessages; i++) |
| 55 | + { |
| 56 | + await p.Send(i, new Message(new byte[10])); |
| 57 | + } |
| 58 | + |
| 59 | + new Utils<ulong>(_testOutputHelper).WaitUntilTaskCompletes(testPassed); |
| 60 | + SystemUtils.Wait(); |
| 61 | + Assert.Equal(TotalMessages, await p.GetLastPublishedId()); |
| 62 | + await p.Close(); |
| 63 | + Assert.False(p.IsOpen()); |
| 64 | + |
| 65 | + // here we query the sequence externally form the producer to be sure that |
| 66 | + // the values are the same |
| 67 | + Assert.Equal(TotalMessages, await system.QuerySequence("my_producer_reference", stream)); |
| 68 | + await SystemUtils.CleanUpStreamSystem(system, stream); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task DeduplicationInActionSendingTheSameIdMessagesWontStore() |
| 73 | + { |
| 74 | + // in this test we send the same messages again with the same publishing id |
| 75 | + // to see the deduplication in action |
| 76 | + |
| 77 | + SystemUtils.InitStreamSystemWithRandomStream(out var system, out var stream); |
| 78 | + var testPassed = new TaskCompletionSource<ulong>(); |
| 79 | + const ulong TotalMessages = 1000UL; |
| 80 | + var p = await DeduplicatingProducer.Create( |
| 81 | + new DeduplicatingProducerConfig(system, stream, "my_producer_reference") |
| 82 | + { |
| 83 | + ConfirmationHandler = async confirmation => |
| 84 | + { |
| 85 | + if (confirmation.PublishingId == TotalMessages) |
| 86 | + testPassed.SetResult(TotalMessages); |
| 87 | + await Task.CompletedTask; |
| 88 | + }, |
| 89 | + }); |
| 90 | + // first send and the messages are stored |
| 91 | + for (ulong i = 1; i <= TotalMessages; i++) |
| 92 | + { |
| 93 | + await p.Send(i, new Message(new byte[10])); |
| 94 | + } |
| 95 | + |
| 96 | + new Utils<ulong>(_testOutputHelper).WaitUntilTaskCompletes(testPassed); |
| 97 | + SystemUtils.Wait(); |
| 98 | + Assert.Equal(TotalMessages, await p.GetLastPublishedId()); |
| 99 | + |
| 100 | + // we send the same messages again with the same publishing id |
| 101 | + // so the messages won't be stored due of the deduplication |
| 102 | + for (ulong i = 1; i <= TotalMessages; i++) |
| 103 | + { |
| 104 | + await p.Send(i, new Message(new byte[10])); |
| 105 | + } |
| 106 | + |
| 107 | + SystemUtils.WaitUntil(() => SystemUtils.HttpGetQMsgCount(stream) == (int)TotalMessages); |
| 108 | + |
| 109 | + // we are out of the deduplication window so the messages will be stored |
| 110 | + // we start from the last published id + 1 |
| 111 | + await p.Send(await p.GetLastPublishedId() + 1, new Message(new byte[10])); |
| 112 | + await p.Send(await p.GetLastPublishedId() + 2, new Message(new byte[10])); |
| 113 | + |
| 114 | + // the total messages should be the TotalMessages + 2 new messages |
| 115 | + SystemUtils.WaitUntil(() => SystemUtils.HttpGetQMsgCount(stream) == (int)TotalMessages + 2); |
| 116 | + await p.Close(); |
| 117 | + Assert.False(p.IsOpen()); |
| 118 | + |
| 119 | + await SystemUtils.CleanUpStreamSystem(system, stream); |
| 120 | + } |
| 121 | +} |
0 commit comments