Skip to content

Commit

Permalink
Tests improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCiliaVincenti committed Dec 3, 2023
1 parent 80a8ae4 commit e4c4cdb
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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++)
{
Expand All @@ -200,7 +200,7 @@ public async Task BenchmarkSimulationTest()
await Task.Yield();
}).AsParallel();

await Task.WhenAll(AsyncKeyedLockerTasks).ConfigureAwait(false);
await Task.WhenAll(AsyncKeyedLockerTasks);
}
}

Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -461,28 +461,30 @@ public async Task BasicTestGenericsString()
[Fact]
public async Task Test1AtATime()
{
var range = 25;
var range = 25000;
var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker();

Check warning on line 465 in AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs

View workflow job for this annotation

GitHub Actions / build

'AsyncKeyedLocker.AsyncKeyedLocker()' is obsolete: 'Unless you're mixing different types of objects, it is recommended to use the generic version AsyncKeyedLocker<T>.'
var concurrentQueue = new ConcurrentQueue<int>();

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;
Expand All @@ -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();
Expand All @@ -529,28 +531,30 @@ public async Task Test2AtATime()
[Fact]
public async Task Test1AtATimeGenerics()
{
var range = 25;
var range = 25000;
var asyncKeyedLocker = new AsyncKeyedLocker<int>();
var concurrentQueue = new ConcurrentQueue<int>();

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;
Expand Down

0 comments on commit e4c4cdb

Please sign in to comment.