-
-
Notifications
You must be signed in to change notification settings - Fork 806
Expand file tree
/
Copy pathPostgresMessageOutboxTests.cs
More file actions
153 lines (125 loc) · 4.91 KB
/
Copy pathPostgresMessageOutboxTests.cs
File metadata and controls
153 lines (125 loc) · 4.91 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
using Microsoft.EntityFrameworkCore;
using Mocha.EntityFrameworkCore.Postgres.Tests.Helpers;
using Mocha.Middlewares;
using Mocha.Outbox;
using Npgsql;
namespace Mocha.EntityFrameworkCore.Postgres.Tests;
public sealed class PostgresMessageOutboxTests : IClassFixture<PostgresFixture>
{
private readonly PostgresFixture _fixture;
public PostgresMessageOutboxTests(PostgresFixture fixture)
{
_fixture = fixture;
}
[Fact]
public async Task PersistAsync_Should_InsertRow_When_Called()
{
// Arrange
var (context, outbox, _) = await CreateOutboxAsync();
await using var _ = context;
using var __ = outbox;
var envelope = CreateTestEnvelope();
// Act
await outbox.PersistAsync(envelope, CancellationToken.None);
// Assert
var connection = (NpgsqlConnection)context.Database.GetDbConnection();
if (connection.State != System.Data.ConnectionState.Open)
{
await connection.OpenAsync(CancellationToken.None);
}
await using var cmd = connection.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM \"outbox_messages\"";
var count = (long)(await cmd.ExecuteScalarAsync(CancellationToken.None))!;
Assert.Equal(1, count);
}
[Fact]
public async Task PersistAsync_Should_SignalOutbox_When_NoTransaction()
{
// Arrange
var (context, outbox, signal) = await CreateOutboxAsync();
await using var _ = context;
using var __ = outbox;
var envelope = CreateTestEnvelope();
// Act
await outbox.PersistAsync(envelope, CancellationToken.None);
// Assert
Assert.True(signal.WasSet);
Assert.Equal(1, signal.SetCallCount);
}
[Fact]
public async Task PersistAsync_Should_NotSignal_When_TransactionActive()
{
// Arrange
var (context, outbox, signal) = await CreateOutboxAsync();
await using var _ = context;
using var __ = outbox;
var envelope = CreateTestEnvelope();
await context.Database.BeginTransactionAsync(CancellationToken.None);
// Act
await outbox.PersistAsync(envelope, CancellationToken.None);
// Assert
Assert.False(signal.WasSet);
Assert.Equal(0, signal.SetCallCount);
}
[Fact]
public async Task PersistAsync_Should_SerializeEnvelopeAsJson_When_Called()
{
// Arrange
var (context, outbox, _) = await CreateOutboxAsync();
await using var _ = context;
using var __ = outbox;
var envelope = CreateTestEnvelope();
// Act
await outbox.PersistAsync(envelope, CancellationToken.None);
// Assert
var connection = (NpgsqlConnection)context.Database.GetDbConnection();
if (connection.State != System.Data.ConnectionState.Open)
{
await connection.OpenAsync(CancellationToken.None);
}
await using var cmd = connection.CreateCommand();
cmd.CommandText = "SELECT \"envelope\"::text FROM \"outbox_messages\" LIMIT 1";
var json = (string)(await cmd.ExecuteScalarAsync(CancellationToken.None))!;
Assert.Contains("\"messageId\"", json);
Assert.Contains("test-event", json);
Assert.Contains("memory://test/queue", json);
}
[Fact]
public void Dispose_Should_NotThrow_When_Called()
{
// Arrange
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql("Host=localhost;Database=test").Options;
using var context = new TestDbContext(options);
var queries = PostgresMessageOutboxQueries.From(new OutboxTableInfo());
var signal = new StubOutboxSignal();
var outbox = new PostgresMessageOutbox(context, signal, queries.InsertEnvelope);
// Act & Assert
var ex = Record.Exception(outbox.Dispose);
Assert.Null(ex);
}
private async Task<(
TestDbContext Context,
PostgresMessageOutbox Outbox,
StubOutboxSignal Signal)> CreateOutboxAsync()
{
var connectionString = await _fixture.CreateDatabaseAsync();
var options = new DbContextOptionsBuilder<TestDbContext>().UseTestNpgsql(connectionString).Options;
var context = new TestDbContext(options);
await context.Database.EnsureCreatedAsync();
var queries = PostgresMessageOutboxQueries.From(new OutboxTableInfo());
var signal = new StubOutboxSignal();
var outbox = new PostgresMessageOutbox(context, signal, queries.InsertEnvelope);
return (context, outbox, signal);
}
private static MessageEnvelope CreateTestEnvelope()
{
return new MessageEnvelope
{
MessageId = Guid.NewGuid().ToString(),
MessageType = "urn:message:test-event",
DestinationAddress = "memory://test/queue",
SentAt = DateTimeOffset.UtcNow,
Body = "{\"value\":42}"u8.ToArray()
};
}
}