forked from madelson/DistributedLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestingPostgresProviders.cs
More file actions
51 lines (44 loc) · 2.75 KB
/
TestingPostgresProviders.cs
File metadata and controls
51 lines (44 loc) · 2.75 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
using Medallion.Threading.Internal;
using Medallion.Threading.Postgres;
using Medallion.Threading.Tests.Data;
namespace Medallion.Threading.Tests.Postgres;
public sealed class TestingPostgresDistributedLockProvider<TStrategy> : TestingLockProvider<TStrategy>
where TStrategy : TestingDbSynchronizationStrategy<TestingPostgresDb>, new()
{
public override IDistributedLock CreateLockWithExactName(string name) =>
this.Strategy.GetConnectionOptions()
.Create(
(connectionString, options) => new PostgresDistributedLock(
new PostgresAdvisoryLockKey(name, allowHashing: false),
connectionString,
ToPostgresOptions(options)
),
connection => new PostgresDistributedLock(new PostgresAdvisoryLockKey(name, allowHashing: false), connection),
transaction => new PostgresDistributedLock(new PostgresAdvisoryLockKey(name, allowHashing: false), transaction.Connection!)
);
public override string GetSafeName(string name) => new PostgresAdvisoryLockKey(name, allowHashing: true).ToString();
public override string GetConnectionStringForCrossProcessTest() => TestingPostgresDb.DefaultConnectionString;
internal static Action<PostgresConnectionOptionsBuilder> ToPostgresOptions((bool useMultiplexing, bool useTransaction, TimeSpan? keepaliveCadence) options) => o =>
{
o.UseMultiplexing(options.useMultiplexing);
o.UseTransaction(options.useTransaction);
if (options.keepaliveCadence is { } keepaliveCadence) { o.KeepaliveCadence(keepaliveCadence); }
};
}
public sealed class TestingPostgresDistributedReaderWriterLockProvider<TStrategy> : TestingReaderWriterLockProvider<TStrategy>
where TStrategy : TestingDbSynchronizationStrategy<TestingPostgresDb>, new()
{
public override IDistributedReaderWriterLock CreateReaderWriterLockWithExactName(string name) =>
this.Strategy.GetConnectionOptions()
.Create(
(connectionString, options) =>
new PostgresDistributedReaderWriterLock(
new PostgresAdvisoryLockKey(name, allowHashing: false),
connectionString,
TestingPostgresDistributedLockProvider<TStrategy>.ToPostgresOptions(options)
),
connection => new PostgresDistributedReaderWriterLock(new PostgresAdvisoryLockKey(name, allowHashing: false), connection),
transaction => new PostgresDistributedReaderWriterLock(new PostgresAdvisoryLockKey(name, allowHashing: false), transaction.Connection!)
);
public override string GetSafeName(string name) => new PostgresAdvisoryLockKey(name, allowHashing: true).ToString();
}