-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathExternalConnectionStrategyTestCases.cs
More file actions
68 lines (51 loc) · 2.77 KB
/
ExternalConnectionStrategyTestCases.cs
File metadata and controls
68 lines (51 loc) · 2.77 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
using NUnit.Framework;
namespace Medallion.Threading.Tests.Data;
public abstract class ExternalConnectionStrategyTestCases<TLockProvider, TDb>
where TLockProvider : TestingLockProvider<TestingExternalConnectionSynchronizationStrategy<TDb>>, new()
where TDb : TestingDb, new()
{
private TLockProvider _lockProvider = default!;
[SetUp]
public async Task SetUp()
{
this._lockProvider = new TLockProvider();
await this._lockProvider.SetupAsync();
}
[TearDown]
public async Task TearDown() => await this._lockProvider.DisposeAsync();
[Test]
public void TestCloseLockOnClosedConnection()
{
var nonAmbientConnectionLock = this._lockProvider.CreateLock(nameof(TestCloseLockOnClosedConnection));
// Disable pooling for the ambient connection. This is important because we want to show that the lock
// will get released; in reality for a pooled connection in this scenario the lock-holding connection will
// return to the pool and would get released the next time that connection was fetched from the pool
this._lockProvider.Strategy.Db.ConnectionStringBuilder["Pooling"] = false;
this._lockProvider.Strategy.StartAmbient();
var ambientConnectionLock = this._lockProvider.CreateLock(nameof(TestCloseLockOnClosedConnection));
this._lockProvider.Strategy.AmbientConnection!.Close();
Assert.Catch<InvalidOperationException>(() => ambientConnectionLock.Acquire());
this._lockProvider.Strategy.AmbientConnection!.Open();
var handle = ambientConnectionLock.Acquire();
nonAmbientConnectionLock.IsHeld().ShouldEqual(true, this.GetType().Name);
this._lockProvider.Strategy.AmbientConnection!.Close();
// Note: in version 1.0 we'd avoid throwing in this scenario. However, that approach could hide bugs because
// merely closing the connection doesn't release the lock: it just returns the connection to the pool where
// it will continue to hold the lock until it is used again.
Assert.Throws<InvalidOperationException>(() => handle.Dispose());
// lock can be re-acquired
nonAmbientConnectionLock.IsHeld().ShouldEqual(false);
}
[Test]
public void TestIsNotScopedToTransaction()
{
var nonAmbientConnectionLock = this._lockProvider.CreateLock(nameof(TestIsNotScopedToTransaction));
this._lockProvider.Strategy.StartAmbient();
using var handle = this._lockProvider.CreateLock(nameof(TestIsNotScopedToTransaction)).Acquire();
using (var transaction = this._lockProvider.Strategy.AmbientConnection!.BeginTransaction())
{
transaction.Rollback();
}
nonAmbientConnectionLock.IsHeld().ShouldEqual(true, this.GetType().Name);
}
}