Skip to content

Commit

Permalink
Allowed optional ConfigureAwaitOptions on .NET 8.0 or greater, improv…
Browse files Browse the repository at this point in the history
…ed tests.
  • Loading branch information
MarkCiliaVincenti committed Dec 6, 2023
1 parent 4ab7514 commit 0788c05
Show file tree
Hide file tree
Showing 17 changed files with 3,405 additions and 2,462 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

55 changes: 52 additions & 3 deletions AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public async Task BasicTest()
{
var locks = 5000;
var concurrency = 50;
var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker();
var asyncKeyedLocker = new AsyncKeyedLocker<object>();
var concurrentQueue = new ConcurrentQueue<(bool entered, int key)>();

var tasks = Enumerable.Range(1, locks * concurrency)
Expand Down Expand Up @@ -462,7 +462,7 @@ public async Task BasicTestGenericsString()
public async Task Test1AtATime()
{
var range = 25000;
var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker();
var asyncKeyedLocker = new AsyncKeyedLocker<object>();
var concurrentQueue = new ConcurrentQueue<int>();

int threadNum = 0;
Expand Down Expand Up @@ -497,7 +497,7 @@ public async Task Test1AtATime()
public async Task Test2AtATime()
{
var range = 4;
var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker(o => o.MaxCount = 2);
var asyncKeyedLocker = new AsyncKeyedLocker<object>(o => o.MaxCount = 2);
var concurrentQueue = new ConcurrentQueue<int>();

var tasks = Enumerable.Range(1, range * 4)
Expand Down Expand Up @@ -644,5 +644,54 @@ void Callback()
SynchronizationContext.SetSynchronizationContext(previousContext);
}
}

[Fact]
public Task TestOptionContinueOnCapturedContext()
=> TestConfigureAwaitOptions(ConfigureAwaitOptions.ContinueOnCapturedContext);

[Fact]
public Task TestOptionForceYielding()
=> TestConfigureAwaitOptions(ConfigureAwaitOptions.ForceYielding);

private async Task TestConfigureAwaitOptions(ConfigureAwaitOptions configureAwaitOptions)
{
const string Key = "test";

var asyncKeyedLocker = new AsyncKeyedLocker<string>();
var testContext = new TestSynchronizationContext();

void Callback()
{
if (configureAwaitOptions == ConfigureAwaitOptions.ContinueOnCapturedContext)
{
Environment.CurrentManagedThreadId.Should().Be(testContext.LastPostThreadId);
}
else
{
testContext.LastPostThreadId.Should().Be(default);
}
}

var previousContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(testContext);

try
{
// This is just to make WaitAsync in TryLockAsync not finish synchronously
var obj = asyncKeyedLocker.Lock(Key);

_ = Task.Run(async () =>
{
await Task.Delay(1000);
obj.Dispose();
});

await asyncKeyedLocker.TryLockAsync(Key, Callback, 5000, configureAwaitOptions);
}
finally
{
SynchronizationContext.SetSynchronizationContext(previousContext);
}
}
}
}
241 changes: 0 additions & 241 deletions AsyncKeyedLock.Tests/AsyncKeyedLocker/PooledTestsForAsyncKeyedLock.cs

This file was deleted.

Loading

0 comments on commit 0788c05

Please sign in to comment.