Repository navigation
Expand file tree
/
Copy pathMethodThreadLocksTests.cs
More file actions
168 lines (131 loc) · 5.1 KB
/
Copy pathMethodThreadLocksTests.cs
File metadata and controls
168 lines (131 loc) · 5.1 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Threading;
using System.Threading.Tasks;
using ThunderDesign.Net.Threading.Locks;
using Xunit;
namespace ThunderDesign.Net_PCL.Threading.UnitTests.Locks
{
public class MethodThreadLocksTests
{
private async Task MethodA(MethodThreadLocks methodLocks, int delayMs)
{
await methodLocks.LockAsync();
try
{
await Task.Delay(delayMs);
}
finally
{
methodLocks.Unlock();
}
}
private async Task MethodB(MethodThreadLocks methodLocks, int delayMs)
{
await methodLocks.LockAsync();
try
{
await Task.Delay(delayMs);
}
finally
{
methodLocks.Unlock();
}
}
[Fact]
public async Task LockAsync_ShouldUseCallerMemberName_ToScopeLock()
{
using var methodLocks = new MethodThreadLocks();
var firstCall = MethodA(methodLocks, 200);
await Task.Delay(20); // allow first call to acquire lock
var secondCall = MethodA(methodLocks, 20);
var completedEarly = await Task.WhenAny(secondCall, Task.Delay(50)) == secondCall;
Assert.False(completedEarly);
await Task.WhenAll(firstCall, secondCall);
}
[Fact]
public async Task LockAsync_ShouldNotBlock_DifferentMethods()
{
using var methodLocks = new MethodThreadLocks();
var callA = MethodA(methodLocks, 200);
var callB = MethodB(methodLocks, 200);
var completed = await Task.WhenAny(Task.WhenAll(callA, callB), Task.Delay(500));
Assert.Equal(Task.WhenAll(callA, callB).IsCompletedSuccessfully, completed.IsCompletedSuccessfully);
}
[Fact]
public async Task LockAsync_WithExplicitMethodName_ShouldScopeIndependently()
{
using var methodLocks = new MethodThreadLocks();
await methodLocks.LockAsync("CustomKey1");
var otherKeyTask = methodLocks.LockAsync("CustomKey2");
var completed = await Task.WhenAny(otherKeyTask, Task.Delay(500)) == otherKeyTask;
Assert.True(completed);
methodLocks.Unlock("CustomKey1");
methodLocks.Unlock("CustomKey2");
}
[Fact]
public async Task LockAsync_ShouldThrowArgumentNullException_WhenMethodNameIsNullOrWhiteSpace()
{
using var methodLocks = new MethodThreadLocks();
await Assert.ThrowsAsync<ArgumentNullException>(() => methodLocks.LockAsync(" "));
}
[Fact]
public void Unlock_ShouldThrowArgumentNullException_WhenMethodNameIsNullOrWhiteSpace()
{
using var methodLocks = new MethodThreadLocks();
Assert.Throws<ArgumentNullException>(() => methodLocks.Unlock(" "));
}
[Fact]
public async Task LockAsync_ShouldThrowOperationCanceledException_WhenCancelled()
{
using var methodLocks = new MethodThreadLocks();
await methodLocks.LockAsync(nameof(LockAsync_ShouldThrowOperationCanceledException_WhenCancelled));
using var cts = new CancellationTokenSource();
var waitingTask = methodLocks.LockAsync(nameof(LockAsync_ShouldThrowOperationCanceledException_WhenCancelled), cts.Token);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(() => waitingTask);
}
[Fact]
public async Task LockAsync_ShouldThrowObjectDisposedException_WhenDisposedWhileWaiting()
{
var methodLocks = new MethodThreadLocks();
await methodLocks.LockAsync("SharedMethod");
var waitingTask = methodLocks.LockAsync("SharedMethod");
methodLocks.Dispose();
await Assert.ThrowsAsync<ObjectDisposedException>(() => waitingTask);
}
[Fact]
public async Task LockAsync_ShouldOnlyAllowOneCaller_PerMethodName_UnderConcurrentAccess()
{
using var methodLocks = new MethodThreadLocks();
var counter = 0;
var maxObservedConcurrency = 0;
var gate = new object();
async Task Worker()
{
await methodLocks.LockAsync(nameof(Worker));
try
{
lock (gate)
{
counter++;
maxObservedConcurrency = Math.Max(maxObservedConcurrency, counter);
}
await Task.Delay(20);
lock (gate)
{
counter--;
}
}
finally
{
methodLocks.Unlock(nameof(Worker));
}
}
var tasks = new Task[10];
for (int i = 0; i < tasks.Length; i++)
tasks[i] = Worker();
await Task.WhenAll(tasks);
Assert.Equal(1, maxObservedConcurrency);
}
}
}