Skip to content

Commit 14bc48f

Browse files
committed
Speed up tests and fix flaky CancellingWaitSync() test
1 parent c0da963 commit 14bc48f

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

UnitTests/AsyncSpawn.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public async Task AsyncExecution(bool locked)
2929
var count = 0;
3030
var tasks = new List<Task>(70);
3131
var asyncLock = new AsyncLock();
32+
var rng = new Random();
3233

3334
{
3435
using var l = locked ? await asyncLock.LockAsync() : new NullDisposable();
@@ -42,11 +43,10 @@ public async Task AsyncExecution(bool locked)
4243
Assert.AreEqual(Interlocked.Increment(ref count), 1);
4344
await Task.Yield();
4445
Assert.AreEqual(count, 1);
45-
await Task.Delay(100);
46+
await Task.Delay(rng.Next(1, 10) * 10);
4647
using (await asyncLock.LockAsync())
4748
{
48-
await Task.Delay(100);
49-
await Task.Yield();
49+
await Task.Delay(rng.Next(1, 10) * 10);
5050
Assert.AreEqual(Interlocked.Decrement(ref count), 0);
5151
}
5252

@@ -58,10 +58,7 @@ public async Task AsyncExecution(bool locked)
5858
}
5959
}
6060

61-
foreach (var task in tasks)
62-
{
63-
await task;
64-
}
61+
await Task.WhenAll(tasks);
6562

6663
Assert.AreEqual(count, 0);
6764
}

UnitTests/CancellationTests.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,37 @@ public void CancellingWait()
2828
}).Wait();
2929

3030
}
31+
3132
[TestMethod]
3233
public void CancellingWaitSync()
3334
{
3435
var asyncLock = new AsyncLock();
35-
var cts = new CancellationTokenSource(500);
36-
Task.Run(async () =>
36+
var cts = new CancellationTokenSource(250);
37+
var delayStarted = new ManualResetEventSlim(false);
38+
var waiter1Finished = new SemaphoreSlim(0, 1);
39+
40+
new Thread(() =>
3741
{
3842
using (asyncLock.Lock(cts.Token))
3943
{
4044
// hold the lock until our later attempt is called
41-
await Task.Delay(1000);
45+
delayStarted.Set();
46+
waiter1Finished.Wait();
4247
}
43-
});
48+
}).Start();
49+
4450
Assert.ThrowsException<OperationCanceledException>(() =>
4551
{
52+
delayStarted.Wait();
4653
using (asyncLock.Lock(cts.Token))
4754
{
4855
Assert.Fail("should never reach here if cancellation works properly.");
4956
}
5057
});
51-
// we should still be able to obtain a lock afterward to make sure resources were reobtained
52-
var newCts= new CancellationTokenSource(1000);
58+
waiter1Finished.Release(1);
59+
60+
// We should still be able to obtain a lock afterward to make sure resources were reobtained
61+
var newCts = new CancellationTokenSource(2000);
5362
using (asyncLock.Lock(newCts.Token))
5463
{
5564
// reaching this line means the test passed

UnitTests/MixedSyncAsync.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task MixedSyncAsyncExecution()
2626
var threads = new List<Thread>(10);
2727
var tasks = new List<Task>(10);
2828
var asyncLock = new AsyncLock();
29+
var rng = new Random();
2930

3031
{
3132
using var l = asyncLock.Lock();
@@ -36,7 +37,7 @@ public async Task MixedSyncAsyncExecution()
3637
using (asyncLock.Lock())
3738
{
3839
Assert.AreEqual(Interlocked.Increment(ref count), 1);
39-
Thread.Sleep(100);
40+
Thread.Sleep(rng.Next(1, 10) * 10);
4041
using (asyncLock.Lock())
4142
{
4243
Thread.Sleep(10);
@@ -59,7 +60,7 @@ public async Task MixedSyncAsyncExecution()
5960
{
6061
Assert.AreEqual(Interlocked.Increment(ref count), 1);
6162
Assert.AreEqual(count, 1);
62-
await Task.Delay(100);
63+
await Task.Delay(rng.Next(1, 10) * 10);
6364
using (await asyncLock.LockAsync())
6465
{
6566
await Task.Delay(10);
@@ -74,10 +75,7 @@ public async Task MixedSyncAsyncExecution()
7475
}
7576
}
7677

77-
foreach (var task in tasks)
78-
{
79-
await task;
80-
}
78+
await Task.WhenAll(tasks);
8179
foreach (var thread in threads)
8280
{
8381
thread.Join();

UnitTests/MixedSyncAsyncTimed.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public async Task MixedSyncAsyncExecution()
2626
var threads = new List<Thread>(10);
2727
var tasks = new List<Task>(10);
2828
var asyncLock = new AsyncLock();
29+
var rng = new Random();
2930

3031
{
3132
using var l = asyncLock.Lock();
@@ -36,7 +37,7 @@ public async Task MixedSyncAsyncExecution()
3637
using (asyncLock.Lock())
3738
{
3839
Assert.AreEqual(Interlocked.Increment(ref count), 1);
39-
Thread.Sleep(100);
40+
Thread.Sleep(rng.Next(1, 10) * 10);
4041
using (asyncLock.Lock())
4142
{
4243
Thread.Sleep(10);
@@ -60,12 +61,12 @@ public async Task MixedSyncAsyncExecution()
6061
{
6162
Assert.AreEqual(Interlocked.Increment(ref count), 1);
6263
Assert.AreEqual(count, 1);
63-
await Task.Delay(100);
64+
await Task.Delay(rng.Next(1, 10) * 10);
6465
if (captured % 2 == 0)
6566
{
6667
using (await asyncLock.LockAsync())
6768
{
68-
await Task.Delay(10);
69+
await Task.Yield();
6970
Assert.AreEqual(Interlocked.Decrement(ref count), 0);
7071
}
7172
}
@@ -81,9 +82,9 @@ public async Task MixedSyncAsyncExecution()
8182
}, TimeSpan.FromMilliseconds(1 /* guarantees no zero-ms optimizations */));
8283
Assert.IsTrue(nestedExecuted);
8384
Interlocked.Decrement(ref count);
84-
await Task.Delay(10);
85+
await Task.Yield();
8586
Assert.AreEqual(Interlocked.Decrement(ref count), 0);
86-
}, TimeSpan.FromMilliseconds(100));
87+
}, TimeSpan.FromMilliseconds(rng.Next(1, 10) * 10));
8788
Assert.IsTrue(executed, "TryLockAsync() did not end up executing!");
8889
}
8990

@@ -95,10 +96,7 @@ public async Task MixedSyncAsyncExecution()
9596
}
9697
}
9798

98-
foreach (var task in tasks)
99-
{
100-
await task;
101-
}
99+
await Task.WhenAll(tasks);
102100
foreach (var thread in threads)
103101
{
104102
thread.Join();

UnitTests/ReentranceLockoutTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ReentranceLockoutTests
1515
private LimitedResource _resource;
1616
private CountdownEvent _countdown;
1717
private Random _random = new Random((int)DateTime.UtcNow.Ticks);
18-
private int DelayInterval => _random.Next(5, 10) * 10;
18+
private int DelayInterval => _random.Next(1, 5) * 10;
1919

2020
private void ResourceSimulation(Action action)
2121
{

0 commit comments

Comments
 (0)