Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public async Task Cancellation_during_blocked_insert_rolls_back_business_changes
var commit = transaction.CommitAsync(cancellation.Token);
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(15));
// Cancel only after PostgreSQL confirms the insert is waiting on our held lock.
while (await database.Scalar("SELECT count(*) FROM pg_stat_activity WHERE wait_event='advisory'") == 0)
while (await database.Scalar("SELECT count(*) FROM pg_stat_activity WHERE wait_event='advisory' AND datname = current_database()") == 0)
await Task.Delay(10, timeout.Token);
cancellation.Cancel();
await Assert.That(async () => await commit).Throws<OperationCanceledException>();
Expand Down
33 changes: 18 additions & 15 deletions tests/Relayer.Outbox.PostgreSql.Tests/ChangeCaptureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task Registration_validates_protocol_limits_and_duplicate_names()
public async Task Startup_requires_persistent_slot_and_insert_only_unfiltered_publication()
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
using var worker = Worker(source, fixture.ConnectionString, new());
Expand All @@ -62,7 +62,7 @@ public async Task Startup_requires_persistent_slot_and_insert_only_unfiltered_pu
public async Task Captures_commits_drains_pre_slot_backlog_and_preserves_ordered_and_future_rows()
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
for (var i = 1; i <= 5; i++) await Insert(source, Id(i));
Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task Captures_commits_drains_pre_slot_backlog_and_preserves_ordered
public async Task Shutdown_after_native_acceptance_replays_same_identity_and_body_on_restart()
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
await Provision(source, fixture.ConnectionString);
Expand Down Expand Up @@ -137,7 +137,7 @@ public async Task Shutdown_after_native_acceptance_replays_same_identity_and_bod
public async Task Failed_send_stays_durable_after_checkpoint_and_restart_recovers_without_new_insert()
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
await Provision(source, fixture.ConnectionString);
Expand Down Expand Up @@ -167,9 +167,12 @@ public async Task Failed_send_stays_durable_after_checkpoint_and_restart_recover
finally { await second.StopAsync(timeout.Token); }
}

// Replication slot names are unique per server, which every test shares.
private static string Slot => TestContext.Current!.Isolation.GetIsolatedName("orders");

private static PostgreSqlChangeCaptureOptions Options(int batchSize = 3, bool retainSentMessages = false) => new()
{
Slot = new("orders"), Replication = new("orders", PgOutputProtocolVersion.V1), BatchSize = batchSize, RetainSentMessages = retainSentMessages
Slot = new(Slot), Replication = new("orders", PgOutputProtocolVersion.V1), BatchSize = batchSize, RetainSentMessages = retainSentMessages
};

[Test]
Expand All @@ -179,7 +182,7 @@ public async Task Failed_send_stays_durable_after_checkpoint_and_restart_recover
public async Task Database_failure_propagates_without_checkpoint_and_restart_recovers(string phase)
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
await Provision(source, fixture.ConnectionString);
Expand Down Expand Up @@ -211,7 +214,7 @@ await Sql(source, $"""
catch (OperationCanceledException) when (observed.IsCancellationRequested) { }
await Assert.That(first.ExecuteTask!.IsFaulted).IsTrue();
await Assert.That(async () => await first.ExecuteTask!).Throws<PostgresException>();
await using var checkpoint = source.CreateCommand("SELECT confirmed_flush_lsn < @position FROM pg_replication_slots WHERE slot_name = 'orders'");
await using var checkpoint = source.CreateCommand($"SELECT confirmed_flush_lsn < @position FROM pg_replication_slots WHERE slot_name = '{Slot}'");
checkpoint.Parameters.AddWithValue("position", NpgsqlDbType.PgLsn, position);
await Assert.That(await checkpoint.ExecuteScalarAsync(timeout.Token) is true).IsTrue();
await Assert.That(await Count(source)).IsEqualTo(1L);
Expand All @@ -235,7 +238,7 @@ await Sql(source, $"""
public async Task Non_transient_replication_failure_stops_host()
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
await Provision(source, fixture.ConnectionString);
Expand All @@ -261,14 +264,14 @@ public async Task Non_transient_replication_failure_stops_host()
public async Task Transient_native_disconnect_reconnects_to_persistent_slot_and_captures_new_commits()
{
await using var fixture = new PostgreSqlFixture();
await fixture.StartAsync(logicalReplication: true);
await fixture.StartAsync();
await using var source = NpgsqlDataSource.Create(fixture.ConnectionString);
await PostgreSqlOutboxStoreTests.CreateSchema(source);
await Provision(source, fixture.ConnectionString);
var transport = new RecordingTransport();
using var worker = new PostgreSqlChangeCaptureWorker(source,
() => new(fixture.ConnectionString) { WalReceiverStatusInterval = TimeSpan.FromMilliseconds(25) },
new() { Slot = new("orders"), Replication = new("orders", PgOutputProtocolVersion.V1), ReconnectDelay = TimeSpan.FromMilliseconds(20) },
new() { Slot = new(Slot), Replication = new("orders", PgOutputProtocolVersion.V1), ReconnectDelay = TimeSpan.FromMilliseconds(20) },
[transport], NullLogger<PostgreSqlChangeCaptureWorker>.Instance, NullLogger<PostgreSqlMaintenanceWorker>.Instance,
NullLogger<PostgreSqlDirectDispatcher>.Instance);
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30));
Expand All @@ -277,7 +280,7 @@ public async Task Transient_native_disconnect_reconnects_to_persistent_slot_and_
{
await Streaming(source, timeout.Token);
int previous;
await using (var command = source.CreateCommand("SELECT active_pid FROM pg_replication_slots WHERE slot_name = 'orders'"))
await using (var command = source.CreateCommand($"SELECT active_pid FROM pg_replication_slots WHERE slot_name = '{Slot}'"))
previous = (int)(await command.ExecuteScalarAsync(timeout.Token))!;
await using (var kill = source.CreateCommand("SELECT pg_terminate_backend(@pid)"))
{
Expand All @@ -287,7 +290,7 @@ public async Task Transient_native_disconnect_reconnects_to_persistent_slot_and_
var position = await Insert(source, Id(1));
await Acknowledged(source, position, timeout.Token);
await Assert.That(transport.Ids.SequenceEqual([Id(1)])).IsTrue();
await using var current = source.CreateCommand("SELECT active_pid FROM pg_replication_slots WHERE slot_name = 'orders'");
await using var current = source.CreateCommand($"SELECT active_pid FROM pg_replication_slots WHERE slot_name = '{Slot}'");
await Assert.That((int)(await current.ExecuteScalarAsync(timeout.Token))!).IsNotEqualTo(previous);
}
finally { await worker.StopAsync(timeout.Token); }
Expand All @@ -303,7 +306,7 @@ private static async Task Provision(NpgsqlDataSource source, string connectionSt
await Sql(source, $"CREATE PUBLICATION orders FOR TABLE relayer_outbox WITH (publish = '{publish}')");
await using var connection = new LogicalReplicationConnection(connectionString);
await connection.Open();
await connection.CreatePgOutputReplicationSlot("orders");
await connection.CreatePgOutputReplicationSlot(Slot);
}

private static async Task<NpgsqlLogSequenceNumber> Insert(NpgsqlDataSource source, Guid id,
Expand All @@ -326,13 +329,13 @@ private static async Task<NpgsqlLogSequenceNumber> Insert(NpgsqlDataSource sourc

private static Task Streaming(NpgsqlDataSource source, CancellationToken token) => Until(async () =>
{
await using var command = source.CreateCommand("SELECT EXISTS (SELECT 1 FROM pg_replication_slots s JOIN pg_stat_replication r ON r.pid = s.active_pid WHERE s.slot_name = 'orders' AND r.state = 'streaming')");
await using var command = source.CreateCommand($"SELECT EXISTS (SELECT 1 FROM pg_replication_slots s JOIN pg_stat_replication r ON r.pid = s.active_pid WHERE s.slot_name = '{Slot}' AND r.state = 'streaming')");
return (bool)(await command.ExecuteScalarAsync(token))!;
}, token);

private static Task Acknowledged(NpgsqlDataSource source, NpgsqlLogSequenceNumber position, CancellationToken token) => Until(async () =>
{
await using var command = source.CreateCommand("SELECT confirmed_flush_lsn >= @position FROM pg_replication_slots WHERE slot_name = 'orders'");
await using var command = source.CreateCommand($"SELECT confirmed_flush_lsn >= @position FROM pg_replication_slots WHERE slot_name = '{Slot}'");
command.Parameters.AddWithValue("position", NpgsqlDbType.PgLsn, position);
return await command.ExecuteScalarAsync(token) is true;
}, token);
Expand Down
2 changes: 1 addition & 1 deletion tests/Relayer.Outbox.PostgreSql.Tests/InboxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ internal async Task WaitForInboxLock()
await connection.OpenAsync(timeout.Token);
while (true)
{
await using var command = new NpgsqlCommand("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE 'INSERT INTO %test_inbox%'", connection);
await using var command = new NpgsqlCommand("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE 'INSERT INTO %test_inbox%' AND datname = current_database()", connection);
if ((long)(await command.ExecuteScalarAsync(timeout.Token))! > 0) return;
await Task.Delay(10, timeout.Token);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Relayer.Outbox.PostgreSql.Tests/MaintenanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task Connected_sweeper_holds_leadership_through_native_send_then_ri
await using var observer = NpgsqlDataSource.Create(fixture.ConnectionString);
while (true)
{
await using var waiting = observer.CreateCommand("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE 'INSERT INTO %leases%'");
await using var waiting = observer.CreateCommand("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE 'INSERT INTO %leases%' AND datname = current_database()");
if ((long)(await waiting.ExecuteScalarAsync(timeout.Token))! > 0) break;
await Task.Yield();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Relayer.Outbox.PostgreSql.Tests/OrderedDispatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public async Task Database_connection_crash_replays_head_before_later_rows_durin
await entered.Task.WaitAsync(cancellation.Token);
try
{
await using var terminate = source.CreateCommand("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND query LIKE 'UPDATE %SET claim_token%'");
await using var terminate = source.CreateCommand("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND query LIKE 'UPDATE %SET claim_token%' AND datname = current_database()");
await terminate.ExecuteNonQueryAsync(cancellation.Token);
}
finally { release.TrySetResult(); }
Expand Down Expand Up @@ -633,7 +633,7 @@ private static async Task WaitForLock(NpgsqlDataSource source, CancellationToken
{
while (true)
{
await using var command = source.CreateCommand("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE 'INSERT INTO %leases%'");
await using var command = source.CreateCommand("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock' AND query LIKE 'INSERT INTO %leases%' AND datname = current_database()");
if ((long)(await command.ExecuteScalarAsync(cancellationToken))! > 0) return;
await Task.Yield();
}
Expand Down
13 changes: 0 additions & 13 deletions tests/Relayer.Outbox.PostgreSql.Tests/Parallelism.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public async Task Fixture_uses_plaintext_password_authentication_without_encrypt
await Assert.That(settings.SslMode).IsEqualTo(SslMode.Disable);
await Assert.That(settings.GssEncryptionMode).IsEqualTo(GssEncryptionMode.Disable);
await Assert.That(settings.Pooling).IsFalse();
await Assert.That(settings.Timeout).IsEqualTo(2);
}

[Test]
Expand Down
Loading
Loading