-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityFrameworkOutboxStoreTests.cs
More file actions
293 lines (252 loc) · 11.4 KB
/
Copy pathEntityFrameworkOutboxStoreTests.cs
File metadata and controls
293 lines (252 loc) · 11.4 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.Extensions.Options;
using Vulthil.xUnit;
namespace Vulthil.SharedKernel.Outbox.EntityFrameworkCore.Tests;
public sealed class EntityFrameworkOutboxStoreTests : BaseUnitTestCase
{
private readonly SqliteConnection _connection = new("DataSource=:memory:");
protected override async ValueTask Initialize()
{
await _connection.OpenAsync(CancellationToken);
await using var context = NewContext();
await context.Database.EnsureCreatedAsync(CancellationToken);
}
protected override async ValueTask Dispose() => await _connection.DisposeAsync();
[Fact]
public async Task DeadLettersAMessageAfterExhaustingRetries()
{
// Arrange
await using var seed = NewContext();
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), DateTimeOffset.UtcNow));
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 1);
// Act
await store.ProcessBatchAsync((_, _) => Task.FromResult<string?>("boom"), CancellationToken);
// Assert
await using var verify = NewContext();
var message = await verify.OutboxMessages.SingleAsync(CancellationToken);
message.FailedOnUtc.ShouldNotBeNull();
message.ProcessedOnUtc.ShouldBeNull();
message.RetryCount.ShouldBe(1);
message.Error.ShouldBe("boom");
}
[Fact]
public async Task DoesNotFetchADeadLetteredMessage()
{
// Arrange
await using var seed = NewContext();
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), DateTimeOffset.UtcNow, failedOnUtc: DateTimeOffset.UtcNow));
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 3);
var dispatched = new List<Guid>();
// Act
var processed = await store.ProcessBatchAsync((data, _) =>
{
dispatched.Add(data.Id);
return Task.FromResult<string?>(null);
}, CancellationToken);
// Assert
processed.ShouldBe(0);
dispatched.ShouldBeEmpty();
}
[Fact]
public async Task ReturnsOnlyTheSuccessfullyDispatchedCountWhenTheBatchHasFailures()
{
// Arrange
await using var seed = NewContext();
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), DateTimeOffset.UtcNow));
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), DateTimeOffset.UtcNow));
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 3);
var dispatchCount = 0;
// Act
var processed = await store.ProcessBatchAsync((_, _) =>
{
dispatchCount++;
return Task.FromResult<string?>(dispatchCount == 1 ? "boom" : null);
}, CancellationToken);
// Assert
processed.ShouldBe(1);
}
[Fact]
public async Task FetchesMessagesOrderedByOccurredOnThenId()
{
// Arrange
var occurredOn = DateTimeOffset.UtcNow;
var first = new Guid("00000000-0000-0000-0000-000000000001");
var second = new Guid("00000000-0000-0000-0000-000000000002");
await using var seed = NewContext();
seed.OutboxMessages.Add(NewMessage(second, occurredOn));
seed.OutboxMessages.Add(NewMessage(first, occurredOn));
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 3);
var dispatched = new List<Guid>();
// Act
await store.ProcessBatchAsync((data, _) =>
{
dispatched.Add(data.Id);
return Task.FromResult<string?>(null);
}, CancellationToken);
// Assert
dispatched.ShouldBe([first, second]);
}
[Fact]
public async Task ThrottlesParallelDispatchToTheConfiguredMaxDegreeOfParallelism()
{
// Arrange
await using var seed = NewContext();
for (var i = 0; i < 6; i++)
{
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), DateTimeOffset.UtcNow));
}
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 3, enableParallelPublishing: true, maxDegreeOfParallelism: 2);
var dispatcher = new ConcurrencyTrackingDispatcher(saturationCount: 2);
// Act
var processTask = store.ProcessBatchAsync((_, token) => dispatcher.DispatchAsync(token), CancellationToken);
await dispatcher.SaturationReached.WaitAsync(TimeSpan.FromSeconds(30), CancellationToken);
dispatcher.Release();
var processed = await processTask;
// Assert
processed.ShouldBe(6);
dispatcher.PeakConcurrency.ShouldBe(2);
}
[Fact]
public async Task RecordsEachMessageOutcomeIndividuallyWhenAParallelBatchHasAFailure()
{
// Arrange
var failing = new Guid("00000000-0000-0000-0000-000000000001");
await using var seed = NewContext();
seed.OutboxMessages.Add(NewMessage(failing, DateTimeOffset.UtcNow));
seed.OutboxMessages.Add(NewMessage(new Guid("00000000-0000-0000-0000-000000000002"), DateTimeOffset.UtcNow));
seed.OutboxMessages.Add(NewMessage(new Guid("00000000-0000-0000-0000-000000000003"), DateTimeOffset.UtcNow));
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 3, enableParallelPublishing: true);
// Act
var processed = await store.ProcessBatchAsync(
(data, _) => Task.FromResult<string?>(data.Id == failing ? "boom" : null),
CancellationToken);
// Assert
processed.ShouldBe(2);
await using var verify = NewContext();
var failed = await verify.OutboxMessages.SingleAsync(message => message.Id == failing, CancellationToken);
failed.ProcessedOnUtc.ShouldBeNull();
failed.FailedOnUtc.ShouldBeNull();
failed.RetryCount.ShouldBe(1);
failed.Error.ShouldBe("boom");
var succeeded = await verify.OutboxMessages.Where(message => message.Id != failing).ToListAsync(CancellationToken);
succeeded.Count.ShouldBe(2);
foreach (var message in succeeded)
{
message.ProcessedOnUtc.ShouldNotBeNull();
message.FailedOnUtc.ShouldBeNull();
message.RetryCount.ShouldBe(0);
message.Error.ShouldBeNull();
}
}
[Fact]
public async Task DeleteProcessedRemovesOldTerminalRowsButKeepsRecentAndPending()
{
// Arrange
var now = DateTimeOffset.UtcNow;
await using var seed = NewContext();
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), now.AddDays(-10), processedOnUtc: now.AddDays(-10)));
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), now.AddDays(-10), failedOnUtc: now.AddDays(-10)));
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), now.AddDays(-1), processedOnUtc: now.AddDays(-1)));
seed.OutboxMessages.Add(NewMessage(Guid.CreateVersion7(), now));
await seed.SaveChangesAsync(CancellationToken);
await using var context = NewContext();
var store = NewStore(context, maxRetries: 3);
// Act
var deleted = await store.DeleteProcessedAsync(now.AddDays(-7), batchSize: 100, CancellationToken);
// Assert
deleted.ShouldBe(2);
await using var verify = NewContext();
var remaining = await verify.OutboxMessages.ToListAsync(CancellationToken);
remaining.Count.ShouldBe(2);
remaining.ShouldContain(message => message.ProcessedOnUtc == null);
remaining.ShouldContain(message => message.ProcessedOnUtc >= now.AddDays(-7));
}
private static EntityFrameworkOutboxStore<TestDbContext> NewStore(TestDbContext context, int maxRetries, bool enableParallelPublishing = false, int maxDegreeOfParallelism = 4) =>
new(context, TimeProvider.System, Options.Create(new OutboxProcessingOptions
{
MaxRetries = maxRetries,
EnableParallelPublishing = enableParallelPublishing,
MaxDegreeOfParallelism = maxDegreeOfParallelism,
}));
private static OutboxMessage NewMessage(Guid id, DateTimeOffset occurredOn, DateTimeOffset? failedOnUtc = null, DateTimeOffset? processedOnUtc = null) => new()
{
Id = id,
Type = "Test",
Content = "{}",
OccurredOnUtc = occurredOn,
Destination = OutboxDestination.DomainEvent,
FailedOnUtc = failedOnUtc,
ProcessedOnUtc = processedOnUtc,
};
private TestDbContext NewContext() => new(new DbContextOptionsBuilder<TestDbContext>().UseSqlite(_connection).Options);
private sealed class ConcurrencyTrackingDispatcher(int saturationCount)
{
private readonly TaskCompletionSource _saturated = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly TaskCompletionSource _gate = new(TaskCreationOptions.RunContinuationsAsynchronously);
private int _inFlight;
private int _peak;
private int _arrivals;
public Task SaturationReached => _saturated.Task;
public int PeakConcurrency => Volatile.Read(ref _peak);
public async Task<string?> DispatchAsync(CancellationToken cancellationToken)
{
RecordArrival();
await _gate.Task.WaitAsync(cancellationToken);
Interlocked.Decrement(ref _inFlight);
return null;
}
public void Release() => _gate.TrySetResult();
private void RecordArrival()
{
UpdatePeak(Interlocked.Increment(ref _inFlight));
if (Interlocked.Increment(ref _arrivals) >= saturationCount)
{
_saturated.TrySetResult();
}
}
private void UpdatePeak(int current)
{
var seen = Volatile.Read(ref _peak);
while (current > seen)
{
var previous = Interlocked.CompareExchange(ref _peak, current, seen);
if (previous == seen)
{
return;
}
seen = previous;
}
}
}
public sealed class TestDbContext(DbContextOptions<TestDbContext> options) : DbContext(options), ISaveOutboxMessages
{
public DbSet<OutboxMessage> OutboxMessages => Set<OutboxMessage>();
public bool IsInTransaction => Database.CurrentTransaction is not null;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
ArgumentNullException.ThrowIfNull(modelBuilder);
modelBuilder.ApplyOutbox();
var utcConverter = new ValueConverter<DateTimeOffset, DateTime>(
value => value.UtcDateTime,
value => new DateTimeOffset(value, TimeSpan.Zero));
var entity = modelBuilder.Entity<OutboxMessage>();
entity.Property(message => message.OccurredOnUtc).HasConversion(utcConverter);
entity.Property(message => message.ProcessedOnUtc).HasConversion(utcConverter);
entity.Property(message => message.FailedOnUtc).HasConversion(utcConverter);
}
}
}