Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved continueOnCapturedContext test #34

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions AsyncKeyedLock.Tests/AsyncKeyedLocker/OriginalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
{
var locks = 5000;
var concurrency = 50;
var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker();

Check warning on line 61 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<(bool entered, int key)>();

var tasks = Enumerable.Range(1, locks * concurrency)
Expand Down Expand Up @@ -462,7 +462,7 @@
public async Task Test1AtATime()
{
var range = 25;
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>();

var tasks = Enumerable.Range(1, range * 2)
Expand Down Expand Up @@ -497,7 +497,7 @@
{
var range = 4;
var asyncKeyedLocker = new AsyncKeyedLock.AsyncKeyedLocker(o => o.MaxCount = 2);
var concurrentQueue = new ConcurrentQueue<int>();

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

View workflow job for this annotation

GitHub Actions / build

'AsyncKeyedLocker.AsyncKeyedLocker(Action<AsyncKeyedLockOptions>)' is obsolete: 'Unless you're mixing different types of objects, it is recommended to use the generic version AsyncKeyedLocker<T>.'

var tasks = Enumerable.Range(1, range * 4)
.Select(async i =>
Expand Down Expand Up @@ -595,26 +595,47 @@
}

[Fact]
public async Task TestContinueOnCapturedContext()
public Task TestContinueOnCapturedContextTrue()
=> TestContinueOnCapturedContext(true);

[Fact]
public Task TestContinueOnCapturedContextFalse()
=> TestContinueOnCapturedContext(false);

private async Task TestContinueOnCapturedContext(bool continueOnCapturedContext)
{
const string Key = "test";

var asyncKeyedLocker = new AsyncKeyedLocker<string>();
var testContext = new TestSynchronizationContext();
var currentThreadId = Environment.CurrentManagedThreadId;

async Task Callback()
void Callback()
{
await Task.Yield();

SynchronizationContext.Current.Should().Be(testContext);
Environment.CurrentManagedThreadId.Should().Be(currentThreadId);
if(continueOnCapturedContext)
{
Environment.CurrentManagedThreadId.Should().Be(testContext.LastPostThreadId);
}
else
{
testContext.LastPostThreadId.Should().Be(default);
}
}

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

try
{
await asyncKeyedLocker.TryLockAsync("test", Callback, 0, true);
// 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, continueOnCapturedContext);
}
finally
{
Expand Down
5 changes: 5 additions & 0 deletions AsyncKeyedLock.Tests/Helpers/TestSynchronizatonContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

using System.Collections.Concurrent;

namespace AsyncKeyedLock.Tests.Helpers
{
public class TestSynchronizationContext : SynchronizationContext
{
public int LastPostThreadId { get; private set; }

public override void Post(SendOrPostCallback d, object? state)
{
LastPostThreadId = Environment.CurrentManagedThreadId;
d(state);
}
}
Expand Down
39 changes: 30 additions & 9 deletions AsyncKeyedLock.Tests/StripedAsyncKeyedLocker/OriginalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,26 +391,47 @@ public async Task Test2AtATimeGenerics()
}

[Fact]
public async Task TestContinueOnCapturedContext()
public Task TestContinueOnCapturedContextTrue()
=> TestContinueOnCapturedContext(true);

[Fact]
public Task TestContinueOnCapturedContextFalse()
=> TestContinueOnCapturedContext(false);

private async Task TestContinueOnCapturedContext(bool continueOnCapturedContext)
{
var stripedAsyncKeyedLocker = new StripedAsyncKeyedLocker<string>();
const string Key = "test";

var asyncKeyedLocker = new AsyncKeyedLocker<string>();
var testContext = new TestSynchronizationContext();
var currentThreadId = Environment.CurrentManagedThreadId;

async Task Callback()
void Callback()
{
await Task.Yield();

SynchronizationContext.Current.Should().Be(testContext);
Environment.CurrentManagedThreadId.Should().Be(currentThreadId);
if (continueOnCapturedContext)
{
Environment.CurrentManagedThreadId.Should().Be(testContext.LastPostThreadId);
}
else
{
testContext.LastPostThreadId.Should().Be(default);
}
}

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

try
{
await stripedAsyncKeyedLocker.TryLockAsync("test", Callback, 0, true);
// 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, continueOnCapturedContext);
}
finally
{
Expand Down
Loading