-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRabbitMqMessageTransportIntegrationTests.cs
More file actions
256 lines (214 loc) · 9.23 KB
/
RabbitMqMessageTransportIntegrationTests.cs
File metadata and controls
256 lines (214 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
namespace NetEvolve.Pulse.Tests.Integration.RabbitMQ;
using System.Text;
using global::RabbitMQ.Client;
using global::RabbitMQ.Client.Events;
using Microsoft.Extensions.Options;
using NetEvolve.Extensions.TUnit;
using NetEvolve.Pulse.Extensibility.Outbox;
using NetEvolve.Pulse.Internals;
using NetEvolve.Pulse.Outbox;
using TUnit.Assertions.Extensions;
using TUnit.Core;
/// <summary>
/// Integration tests for <see cref="RabbitMqMessageTransport"/> against a real RabbitMQ broker.
/// </summary>
[ClassDataSource<RabbitMqContainerFixture>(Shared = SharedType.PerTestSession)]
[TestGroup("RabbitMQ")]
[Timeout(120_000)]
public sealed class RabbitMqMessageTransportIntegrationTests(RabbitMqContainerFixture containerFixture)
: IAsyncDisposable
{
private const string ExchangeName = "pulse.integration.test";
private IConnection? _connection;
private IChannel? _adminChannel;
private async Task<(IConnection Connection, IChannel AdminChannel)> GetConnectionAndChannelAsync(
CancellationToken cancellationToken
)
{
if (_connection is not null && _adminChannel is not null)
{
return (_connection, _adminChannel);
}
var factory = new ConnectionFactory { Uri = new Uri(containerFixture.ConnectionString) };
_connection = await factory.CreateConnectionAsync(cancellationToken).ConfigureAwait(false);
_adminChannel = await _connection
.CreateChannelAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
await _adminChannel
.ExchangeDeclareAsync(
ExchangeName,
ExchangeType.Fanout,
durable: false,
autoDelete: true,
cancellationToken: cancellationToken
)
.ConfigureAwait(false);
return (_connection, _adminChannel);
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (_adminChannel is not null)
{
await _adminChannel.CloseAsync().ConfigureAwait(false);
_adminChannel.Dispose();
}
if (_connection is not null)
{
await _connection.CloseAsync().ConfigureAwait(false);
await _connection.DisposeAsync().ConfigureAwait(false);
}
}
[Test]
public async Task SendAsync_Publishes_message_to_exchange(CancellationToken cancellationToken)
{
var (connection, adminChannel) = await GetConnectionAndChannelAsync(cancellationToken).ConfigureAwait(false);
var queueName = await adminChannel
.QueueDeclareAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
await adminChannel
.QueueBindAsync(
queueName.QueueName,
ExchangeName,
routingKey: string.Empty,
cancellationToken: cancellationToken
)
.ConfigureAwait(false);
var adapter = new RabbitMqConnectionAdapter(connection);
using var transport = CreateTransport(adapter);
var outboxMessage = CreateOutboxMessage();
await transport.SendAsync(outboxMessage, cancellationToken).ConfigureAwait(false);
var received = await ConsumeOneMessageAsync(adminChannel, queueName.QueueName, cancellationToken)
.ConfigureAwait(false);
using (Assert.Multiple())
{
_ = await Assert.That(received).IsNotNull();
var body = Encoding.UTF8.GetString(received!.Body.ToArray());
_ = await Assert.That(body).IsEqualTo(outboxMessage.Payload);
_ = await Assert.That(received.BasicProperties.MessageId).IsEqualTo(outboxMessage.Id.ToString());
_ = await Assert.That(received.BasicProperties.ContentType).IsEqualTo("application/json");
}
}
[Test]
public async Task SendBatchAsync_Publishes_all_messages_to_exchange(CancellationToken cancellationToken)
{
const int messageCount = 5;
var (connection, adminChannel) = await GetConnectionAndChannelAsync(cancellationToken).ConfigureAwait(false);
var queueName = await adminChannel
.QueueDeclareAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
await adminChannel
.QueueBindAsync(
queueName.QueueName,
ExchangeName,
routingKey: string.Empty,
cancellationToken: cancellationToken
)
.ConfigureAwait(false);
var adapter = new RabbitMqConnectionAdapter(connection);
using var transport = CreateTransport(adapter);
var messages = Enumerable.Range(0, messageCount).Select(_ => CreateOutboxMessage()).ToList();
await transport.SendBatchAsync(messages, cancellationToken).ConfigureAwait(false);
var receivedMessages = await ConsumeManyMessagesAsync(
adminChannel,
queueName.QueueName,
messageCount,
cancellationToken
)
.ConfigureAwait(false);
_ = await Assert.That(receivedMessages.Count).IsEqualTo(messageCount);
}
[Test]
public async Task IsHealthyAsync_When_connection_open_returns_true(CancellationToken cancellationToken)
{
var (connection, _) = await GetConnectionAndChannelAsync(cancellationToken).ConfigureAwait(false);
var adapter = new RabbitMqConnectionAdapter(connection);
using var transport = CreateTransport(adapter);
// Trigger channel creation by sending a message
await transport.SendAsync(CreateOutboxMessage(), cancellationToken).ConfigureAwait(false);
var healthy = await transport.IsHealthyAsync(cancellationToken).ConfigureAwait(false);
_ = await Assert.That(healthy).IsTrue();
}
[Test]
public async Task IsHealthyAsync_Before_first_send_returns_false(CancellationToken cancellationToken)
{
var (connection, _) = await GetConnectionAndChannelAsync(cancellationToken).ConfigureAwait(false);
var adapter = new RabbitMqConnectionAdapter(connection);
using var transport = CreateTransport(adapter);
// No sends yet — channel has not been created
var healthy = await transport.IsHealthyAsync(cancellationToken).ConfigureAwait(false);
_ = await Assert.That(healthy).IsFalse();
}
private static RabbitMqMessageTransport CreateTransport(IRabbitMqConnectionAdapter adapter) =>
new(
adapter,
new SimpleTopicNameResolver(),
Options.Create(new RabbitMqTransportOptions { ExchangeName = ExchangeName })
);
private static OutboxMessage CreateOutboxMessage() =>
new()
{
Id = Guid.NewGuid(),
EventType = typeof(IntegrationTestEvent),
Payload = """{"id":"test"}""",
CorrelationId = Guid.NewGuid().ToString(),
CreatedAt = DateTimeOffset.UtcNow,
UpdatedAt = DateTimeOffset.UtcNow,
RetryCount = 0,
ProcessedAt = null,
};
private static async Task<BasicDeliverEventArgs?> ConsumeOneMessageAsync(
IChannel channel,
string queueName,
CancellationToken cancellationToken
)
{
var tcs = new TaskCompletionSource<BasicDeliverEventArgs?>(TaskCreationOptions.RunContinuationsAsynchronously);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(15));
cts.Token.Register(() => tcs.TrySetResult(null));
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += (_, ea) =>
{
tcs.TrySetResult(ea);
return Task.CompletedTask;
};
await channel
.BasicConsumeAsync(queueName, autoAck: true, consumer: consumer, cancellationToken: cancellationToken)
.ConfigureAwait(false);
return await tcs.Task.ConfigureAwait(false);
}
private static async Task<List<BasicDeliverEventArgs>> ConsumeManyMessagesAsync(
IChannel channel,
string queueName,
int expectedCount,
CancellationToken cancellationToken
)
{
var received = new List<BasicDeliverEventArgs>();
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(15));
cts.Token.Register(() => tcs.TrySetResult(false));
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += (_, ea) =>
{
received.Add(ea);
if (received.Count >= expectedCount)
{
tcs.TrySetResult(true);
}
return Task.CompletedTask;
};
await channel
.BasicConsumeAsync(queueName, autoAck: true, consumer: consumer, cancellationToken: cancellationToken)
.ConfigureAwait(false);
await tcs.Task.ConfigureAwait(false);
return received;
}
private sealed class SimpleTopicNameResolver : ITopicNameResolver
{
public string Resolve(OutboxMessage message) => message.EventType.Name;
}
private sealed record IntegrationTestEvent;
}