#213 Postgres: Add support for transaction-scoped advisory locks with external transactions - Continuation by Tzachi009 · Pull Request #235 · madelson/DistributedLock · GitHub
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
108 changes: 99 additions & 9 deletions src/DistributedLock.Postgres/PostgresAdvisoryLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,98 @@ public async Task TestWorksWithExternalTransaction()
transaction.Commit();
}
}

[Test]
public async Task TestTimeoutSettingsRestoredWithExternalTransaction()
{
bool isLockAcquired;

var key = new PostgresAdvisoryLockKey(0);

using var connection = new NpgsqlConnection(TestingPostgresDb.DefaultConnectionString);
await connection.OpenAsync();

using (var transaction = connection.BeginTransaction())
{
using var transactionCommand = connection.CreateCommand();
transactionCommand.Transaction = transaction;

transactionCommand.CommandText = "SET LOCAL statement_timeout = 1010;SET LOCAL lock_timeout = 510;";
await transactionCommand.ExecuteNonQueryAsync();

isLockAcquired = await PostgresDistributedLock.TryAcquireWithTransactionAsync(key, transaction).ConfigureAwait(false);
Assert.That(isLockAcquired, Is.True);

(await GetTimeoutAsync("statement_timeout", transactionCommand)).ShouldEqual("1010ms");
(await GetTimeoutAsync("lock_timeout", transactionCommand)).ShouldEqual("510ms");

isLockAcquired = PostgresDistributedLock.TryAcquireWithTransaction(key, transaction, TimeSpan.FromMilliseconds(10));
Assert.That(isLockAcquired, Is.False);

(await GetTimeoutAsync("statement_timeout", transactionCommand)).ShouldEqual("1010ms");
(await GetTimeoutAsync("lock_timeout", transactionCommand)).ShouldEqual("510ms");

transaction.Rollback();

(await GetTimeoutAsync("statement_timeout", transactionCommand)).ShouldEqual("0");
(await GetTimeoutAsync("lock_timeout", transactionCommand)).ShouldEqual("0");
}

using (var transaction = connection.BeginTransaction())
{
using var transactionCommand = connection.CreateCommand();
transactionCommand.Transaction = transaction;

transactionCommand.CommandText = "SET LOCAL statement_timeout = 1010;SET LOCAL lock_timeout = 510;";
await transactionCommand.ExecuteNonQueryAsync();

await PostgresDistributedLock.AcquireWithTransactionAsync(key, transaction).ConfigureAwait(false);

(await GetTimeoutAsync("statement_timeout", transactionCommand)).ShouldEqual("1010ms");
(await GetTimeoutAsync("lock_timeout", transactionCommand)).ShouldEqual("510ms");

Assert.Throws<TimeoutException>(() => PostgresDistributedLock.AcquireWithTransaction(key, transaction, TimeSpan.FromMilliseconds(10)));

(await GetTimeoutAsync("statement_timeout", transactionCommand)).ShouldEqual("1010ms");
(await GetTimeoutAsync("lock_timeout", transactionCommand)).ShouldEqual("510ms");

transaction.Commit();

(await GetTimeoutAsync("statement_timeout", transactionCommand)).ShouldEqual("0");
(await GetTimeoutAsync("lock_timeout", transactionCommand)).ShouldEqual("0");
}
}


[Test]
// Each lock acquisition creates the same named savepoint; this seems like it would create a conflict
// but it actually works fine in Postgres (see https://www.postgresql.org/docs/current/sql-savepoint.html)
public async Task TestWorksForMultipleLocksUnderTheSameConnectionWithExternalTransaction()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave a comment here along the lines of "Each acquisition creates the same named savepoint; this seems like it would create a conflict but it actually works fine in Postgres ".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

{
var key1 = new PostgresAdvisoryLockKey(1);
var key2 = new PostgresAdvisoryLockKey(2);

using var connection = new NpgsqlConnection(TestingPostgresDb.DefaultConnectionString);
await connection.OpenAsync();

using (var transaction = connection.BeginTransaction())
{
var isFirstLockAcquired = await PostgresDistributedLock.TryAcquireWithTransactionAsync(key1, transaction).ConfigureAwait(false);
Assert.That(isFirstLockAcquired, Is.True);

var isSecondLockAcquired = await PostgresDistributedLock.TryAcquireWithTransactionAsync(key2, transaction).ConfigureAwait(false);
Assert.That(isSecondLockAcquired, Is.True);

isSecondLockAcquired = await PostgresDistributedLock.TryAcquireWithTransactionAsync(key2, transaction).ConfigureAwait(false);
Assert.That(isSecondLockAcquired, Is.False);

transaction.Rollback();
}
}

private static Task<object> GetTimeoutAsync(string timeoutName, NpgsqlCommand command)
{
command.CommandText = $"SHOW {timeoutName}";
return command.ExecuteScalarAsync()!;
}
}