-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed serious issue with concurrent threads having been limited to 1 …
…irrespective of setting in the constructor. Also added a few features and tests.
- Loading branch information
1 parent
8d0e7de
commit bb1d3a2
Showing
7 changed files
with
185 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\AsyncKeyedLock\AsyncKeyedLock.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections.Concurrent; | ||
using Xunit; | ||
|
||
namespace AsyncKeyedLock.Tests | ||
{ | ||
public class Tests | ||
{ | ||
[Fact] | ||
public async Task Test1AtATime() | ||
{ | ||
var range = 25; | ||
var asyncKeyedLocker = new AsyncKeyedLocker(); | ||
var concurrentQueue = new ConcurrentQueue<int>(); | ||
|
||
var tasks = Enumerable.Range(1, range * 2) | ||
.Select(async i => | ||
{ | ||
var key = Convert.ToInt32(Math.Ceiling((double)i / 2)); | ||
using (await asyncKeyedLocker.LockAsync(key)) | ||
{ | ||
concurrentQueue.Enqueue(key); | ||
await Task.Delay(100 * key); | ||
} | ||
}); | ||
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]) | ||
{ | ||
valid = false; | ||
break; | ||
} | ||
} | ||
|
||
Assert.True(valid); | ||
} | ||
|
||
[Fact] | ||
public async Task Test2AtATime() | ||
{ | ||
var range = 4; | ||
var asyncKeyedLocker = new AsyncKeyedLocker(2); | ||
var concurrentQueue = new ConcurrentQueue<int>(); | ||
|
||
var tasks = Enumerable.Range(1, range * 4) | ||
.Select(async i => | ||
{ | ||
var key = Convert.ToInt32(Math.Ceiling((double)i / 4)); | ||
using (await asyncKeyedLocker.LockAsync(key)) | ||
{ | ||
concurrentQueue.Enqueue(key); | ||
await Task.Delay((100 * key) + 1000); | ||
} | ||
}); | ||
await Task.WhenAll(tasks.AsParallel()); | ||
|
||
bool valid = true; | ||
var list = concurrentQueue.ToList(); | ||
|
||
for (int i = 0; i < range * 2; i++) | ||
{ | ||
if (list[i] != list[i + (range * 2)]) | ||
{ | ||
valid = false; | ||
break; | ||
} | ||
} | ||
|
||
Assert.True(valid); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters