From e4c4cdb68f098b0390343edf60473b6e665f4fd4 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Sun, 3 Dec 2023 18:32:30 +0100 Subject: [PATCH] Tests improvements --- .../AsyncKeyedLocker/OriginalTests.cs | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs b/AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs index d5b34d7..cfd146f 100644 --- a/AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs +++ b/AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs @@ -73,7 +73,7 @@ public async Task BasicTest() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -123,7 +123,7 @@ public async Task BasicTestGenerics() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -181,7 +181,7 @@ public async Task BenchmarkSimulationTest() { var key = i % NumberOfLocks; - using (var myLock = await AsyncKeyedLocker.LockAsync(key.ToString()).ConfigureAwait(false)) + using (var myLock = await AsyncKeyedLocker.LockAsync(key.ToString())) { for (int j = 0; j < GuidReversals; j++) { @@ -200,7 +200,7 @@ public async Task BenchmarkSimulationTest() await Task.Yield(); }).AsParallel(); - await Task.WhenAll(AsyncKeyedLockerTasks).ConfigureAwait(false); + await Task.WhenAll(AsyncKeyedLockerTasks); } } @@ -222,7 +222,7 @@ public async Task BasicTestGenericsPooling50k() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -276,7 +276,7 @@ public async Task BasicTestGenericsPooling50kUnfilled() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -326,7 +326,7 @@ public async Task BasicTestGenericsPoolingProcessorCount() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -376,7 +376,7 @@ public async Task BasicTestGenericsPooling10k() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -426,7 +426,7 @@ public async Task BasicTestGenericsString() concurrentQueue.Enqueue((false, key)); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = concurrentQueue.Count == locks * concurrency * 2; @@ -461,28 +461,30 @@ public async Task BasicTestGenericsString() [Fact] public async Task Test1AtATime() { - var range = 25; + var range = 25000; var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker(); var concurrentQueue = new ConcurrentQueue(); + int threadNum = 0; + var tasks = Enumerable.Range(1, range * 2) .Select(async i => { - var key = Convert.ToInt32(Math.Ceiling((double)i / 2)); + var key = Convert.ToInt32(Math.Ceiling((double)Interlocked.Increment(ref threadNum) / 2)); using (await asyncKeyedLocker.LockAsync(key)) { concurrentQueue.Enqueue(key); - await Task.Delay(100 * key); + await Task.Delay(1); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = true; var list = concurrentQueue.ToList(); for (int i = 0; i < range; i++) { - if (list[i] != list[i + range]) + if (list[i] == list[i + range]) { valid = false; break; @@ -509,7 +511,7 @@ public async Task Test2AtATime() await Task.Delay((100 * key) + 1000); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = true; var list = concurrentQueue.ToList(); @@ -529,28 +531,30 @@ public async Task Test2AtATime() [Fact] public async Task Test1AtATimeGenerics() { - var range = 25; + var range = 25000; var asyncKeyedLocker = new AsyncKeyedLocker(); var concurrentQueue = new ConcurrentQueue(); + int threadNum = 0; + var tasks = Enumerable.Range(1, range * 2) .Select(async i => { - var key = Convert.ToInt32(Math.Ceiling((double)i / 2)); + var key = Convert.ToInt32(Math.Ceiling((double)Interlocked.Increment(ref threadNum) / 2)); using (await asyncKeyedLocker.LockAsync(key)) { concurrentQueue.Enqueue(key); - await Task.Delay(100 * key); + await Task.Delay(1); } }); - await Task.WhenAll(tasks.AsParallel()).ConfigureAwait(false); + await Task.WhenAll(tasks.AsParallel()); bool valid = true; var list = concurrentQueue.ToList(); for (int i = 0; i < range; i++) { - if (list[i] != list[i + range]) + if (list[i] == list[i + range]) { valid = false; break;