public async Task explicitly_release_global_session_locks() { var settings = new PostgresqlSettings(); using (var conn1 = new NpgsqlConnection(Servers.PostgresConnectionString)) using (var conn2 = new NpgsqlConnection(Servers.PostgresConnectionString)) using (var conn3 = new NpgsqlConnection(Servers.PostgresConnectionString)) { await conn1.OpenAsync(); await conn2.OpenAsync(); await conn3.OpenAsync(); await settings.GetGlobalLock(conn1, 1); // Cannot get the lock here (await settings.TryGetGlobalLock(conn2, 1)).ShouldBeFalse(); await settings.ReleaseGlobalLock(conn1, 1); for (var j = 0; j < 5; j++) { if (await settings.TryGetGlobalLock(conn2, 1)) { return; } await Task.Delay(250); } throw new Exception("Advisory lock was not released"); } }
[Fact] // - too slow public async Task global_session_locks() { var settings = new PostgresqlSettings(); using (var conn1 = new NpgsqlConnection(Servers.PostgresConnectionString)) using (var conn2 = new NpgsqlConnection(Servers.PostgresConnectionString)) using (var conn3 = new NpgsqlConnection(Servers.PostgresConnectionString)) { await conn1.OpenAsync(); await conn2.OpenAsync(); await conn3.OpenAsync(); await settings.GetGlobalLock(conn1, 24); try { // Cannot get the lock here (await settings.TryGetGlobalLock(conn2, 24)).ShouldBeFalse(); // Can get the new lock (await settings.TryGetGlobalLock(conn3, 25)).ShouldBeTrue(); // Cannot get the lock here (await settings.TryGetGlobalLock(conn2, 25)).ShouldBeFalse(); } finally { await settings.ReleaseGlobalLock(conn1, 24); await settings.ReleaseGlobalLock(conn3, 25); } } }