-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathDistributedUpgradeableReaderWriterLockCoreTestCases.cs
152 lines (120 loc) · 7.02 KB
/
DistributedUpgradeableReaderWriterLockCoreTestCases.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using NUnit.Framework;
namespace Medallion.Threading.Tests;
public abstract class DistributedUpgradeableReaderWriterLockCoreTestCases<TLockProvider, TStrategy>
where TLockProvider : TestingUpgradeableReaderWriterLockProvider<TStrategy>, new()
where TStrategy : TestingSynchronizationStrategy, 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 TestMultipleReadersSingleWriter()
{
IDistributedUpgradeableReaderWriterLock Lock() =>
this._lockProvider.CreateUpgradeableReaderWriterLock(nameof(TestMultipleReadersSingleWriter));
using var readHandle1 = Lock().TryAcquireReadLockAsync().AsTask().Result;
Assert.That(readHandle1, Is.Not.Null, this.GetType().ToString());
using var readHandle2 = Lock().TryAcquireReadLock();
Assert.That(readHandle2, Is.Not.Null, this.GetType().ToString());
using (var handle = Lock().TryAcquireUpgradeableReadLock())
{
Assert.That(handle, Is.Not.Null);
using var readHandle3 = Lock().TryAcquireReadLock();
Assert.That(readHandle3, Is.Not.Null);
Lock().TryAcquireUpgradeableReadLock().ShouldEqual(null);
Lock().TryAcquireWriteLock().ShouldEqual(null);
readHandle3!.Dispose();
}
readHandle1!.Dispose();
readHandle2!.Dispose();
using var upgradeHandle = Lock().TryAcquireUpgradeableReadLock();
Assert.That(upgradeHandle, Is.Not.Null);
}
[Test]
public void TestUpgradeToWriteLock()
{
IDistributedUpgradeableReaderWriterLock Lock() =>
this._lockProvider.CreateUpgradeableReaderWriterLock(nameof(TestUpgradeToWriteLock));
var readHandle = Lock().AcquireReadLock();
Task<IDistributedSynchronizationHandle> readTask;
using (var upgradeableHandle = Lock().AcquireUpgradeableReadLockAsync().AsTask().Result)
{
upgradeableHandle.TryUpgradeToWriteLock().ShouldEqual(false); // read lock still held
readHandle.Dispose();
upgradeableHandle.TryUpgradeToWriteLock().ShouldEqual(true);
readTask = Task.Run(() => Lock().AcquireReadLockAsync().AsTask());
readTask.Wait(TimeSpan.FromSeconds(.1)).ShouldEqual(false, "write lock held");
}
readTask.Wait(TimeSpan.FromSeconds(10)).ShouldEqual(true, "write lock released");
readTask.Result.Dispose();
}
[Test]
public void TestReaderWriterLockBadArguments()
{
var @lock = this._lockProvider.CreateUpgradeableReaderWriterLock(nameof(TestReaderWriterLockBadArguments));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.AcquireUpgradeableReadLock(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.AcquireUpgradeableReadLockAsync(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.TryAcquireUpgradeableReadLock(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.TryAcquireUpgradeableReadLockAsync(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.AcquireUpgradeableReadLock(TimeSpan.FromSeconds(int.MaxValue)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.AcquireUpgradeableReadLockAsync(TimeSpan.FromSeconds(int.MaxValue)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.TryAcquireUpgradeableReadLock(TimeSpan.FromSeconds(int.MaxValue)));
Assert.Catch<ArgumentOutOfRangeException>(() => @lock.TryAcquireUpgradeableReadLockAsync(TimeSpan.FromSeconds(int.MaxValue)));
using var upgradeableHandle = @lock.AcquireUpgradeableReadLock();
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.UpgradeToWriteLock(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.UpgradeToWriteLockAsync(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.TryUpgradeToWriteLock(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.TryUpgradeToWriteLockAsync(TimeSpan.FromSeconds(-2)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.UpgradeToWriteLock(TimeSpan.FromSeconds(int.MaxValue)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.UpgradeToWriteLockAsync(TimeSpan.FromSeconds(int.MaxValue)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.TryUpgradeToWriteLock(TimeSpan.FromSeconds(int.MaxValue)));
Assert.Catch<ArgumentOutOfRangeException>(() => upgradeableHandle.TryUpgradeToWriteLockAsync(TimeSpan.FromSeconds(int.MaxValue)));
}
[Test]
public void TestUpgradeableHandleDisposal()
{
var @lock = this._lockProvider.CreateUpgradeableReaderWriterLock(nameof(TestUpgradeableHandleDisposal));
var handle = @lock.AcquireUpgradeableReadLock();
handle.Dispose();
Assert.DoesNotThrow(() => handle.Dispose());
Assert.Catch<ObjectDisposedException>(() => handle.TryUpgradeToWriteLock());
Assert.Catch<ObjectDisposedException>(() => handle.TryUpgradeToWriteLockAsync());
Assert.Catch<ObjectDisposedException>(() => handle.UpgradeToWriteLock());
Assert.Catch<ObjectDisposedException>(() => handle.UpgradeToWriteLockAsync());
}
[Test]
public void TestUpgradeableHandleMultipleUpgrades()
{
var @lock = this._lockProvider.CreateUpgradeableReaderWriterLock(nameof(TestUpgradeableHandleMultipleUpgrades));
using var upgradeHandle = @lock.AcquireUpgradeableReadLock();
upgradeHandle.UpgradeToWriteLock();
Assert.Catch<InvalidOperationException>(() => upgradeHandle.TryUpgradeToWriteLock());
}
[Test]
public async Task TestCanUpgradeHandleWhileMonitoring()
{
var handleLostHelper = this._lockProvider.Strategy.PrepareForHandleLost();
var @lock = this._lockProvider.CreateUpgradeableReaderWriterLock(nameof(TestCanUpgradeHandleWhileMonitoring));
using var handle = await @lock.AcquireUpgradeableReadLockAsync();
// start monitoring
using var canceledEvent = new ManualResetEventSlim(initialState: false);
using var registration = handle.HandleLostToken.Register(canceledEvent.Set);
Assert.That(canceledEvent.Wait(TimeSpan.FromSeconds(.05)), Is.False);
Assert.DoesNotThrowAsync(() => handle.UpgradeToWriteLockAsync().AsTask());
Assert.That(canceledEvent.Wait(TimeSpan.FromSeconds(.05)), Is.False);
if (handleLostHelper != null)
{
handleLostHelper.Dispose();
Assert.That(canceledEvent.Wait(TimeSpan.FromSeconds(10)), Is.True);
}
// when the handle is lost, Dispose() may throw
try { await handle.DisposeAsync(); }
catch { }
}
}