Skip to content

Commit

Permalink
New methods for conditional locking enabled also for StripedAsyncKeye…
Browse files Browse the repository at this point in the history
…dLocker and AsyncNonKeyedLocker.
  • Loading branch information
MarkCiliaVincenti committed Apr 21, 2024
1 parent addd9fe commit a2a06e1
Show file tree
Hide file tree
Showing 7 changed files with 1,066 additions and 6 deletions.
216 changes: 216 additions & 0 deletions AsyncKeyedLock.Tests/AsyncNonKeyedLockerTests/OriginalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,222 @@ namespace AsyncKeyedLock.Tests.AsyncNonKeyedLockerTests
{
public class OriginalTests
{
[Fact]
public void TestRecursion()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

double Factorial(int number, bool isFirst = true)
{
using (asyncNonKeyedLocker.ConditionalLock(isFirst))
{
if (number == 0)
return 1;
return number * Factorial(number - 1, false);
}
}

Assert.Equal(120, Factorial(5));
}

[Fact]
public async Task TestRecursionAsync()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

async Task<double> Factorial(int number, bool isFirst = true)
{
using (await asyncNonKeyedLocker.ConditionalLockAsync(isFirst))
{
if (number == 0)
return 1;
return number * await Factorial(number - 1, false);
}
}

Assert.Equal(120, await Factorial(5));
}

[Fact]
public void TestRecursionWithCancellationToken()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

double Factorial(int number, bool isFirst = true)
{
using (asyncNonKeyedLocker.ConditionalLock(isFirst, new CancellationToken(false)))
{
if (number == 0)
return 1;
return number * Factorial(number - 1, false);
}
}

Assert.Equal(120, Factorial(5));
}

[Fact]
public async Task TestRecursionWithCancellationTokenAsync()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

async Task<double> Factorial(int number, bool isFirst = true)
{
using (await asyncNonKeyedLocker.ConditionalLockAsync(isFirst, new CancellationToken(false)))
{
if (number == 0)
return 1;
return number * await Factorial(number - 1, false);
}
}

Assert.Equal(120, await Factorial(5));
}

[Fact]
public void TestRecursionWithTimeout()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

double Factorial(int number, bool isFirst = true)
{
using (asyncNonKeyedLocker.ConditionalLock(isFirst, Timeout.Infinite, out _))
{
if (number == 0)
return 1;
return number * Factorial(number - 1, false);
}
}

Assert.Equal(120, Factorial(5));
}

[Fact]
public async Task TestRecursionWithTimeoutAsync()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

async Task<double> Factorial(int number, bool isFirst = true)
{
using (await asyncNonKeyedLocker.ConditionalLockAsync(isFirst, Timeout.Infinite))
{
if (number == 0)
return 1;
return number * await Factorial(number - 1, false);
}
}

Assert.Equal(120, await Factorial(5));
}

[Fact]
public void TestRecursionWithTimeSpan()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

double Factorial(int number, bool isFirst = true)
{
using (asyncNonKeyedLocker.ConditionalLock(isFirst, TimeSpan.Zero, out _))
{
if (number == 0)
return 1;
return number * Factorial(number - 1, false);
}
}

Assert.Equal(120, Factorial(5));
}

[Fact]
public async Task TestRecursionWithTimeSpanAsync()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

async Task<double> Factorial(int number, bool isFirst = true)
{
using (await asyncNonKeyedLocker.ConditionalLockAsync(isFirst, TimeSpan.Zero))
{
if (number == 0)
return 1;
return number * await Factorial(number - 1, false);
}
}

Assert.Equal(120, await Factorial(5));
}

[Fact]
public void TestRecursionWithTimeoutAndCancellationToken()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

double Factorial(int number, bool isFirst = true)
{
using (asyncNonKeyedLocker.ConditionalLock(isFirst, Timeout.Infinite, new CancellationToken(false), out _))
{
if (number == 0)
return 1;
return number * Factorial(number - 1, false);
}
}

Assert.Equal(120, Factorial(5));
}

[Fact]
public async Task TestRecursionWithTimeoutAndCancellationTokenAsync()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

async Task<double> Factorial(int number, bool isFirst = true)
{
using (await asyncNonKeyedLocker.ConditionalLockAsync(isFirst, Timeout.Infinite, new CancellationToken(false)))
{
if (number == 0)
return 1;
return number * await Factorial(number - 1, false);
}
}

Assert.Equal(120, await Factorial(5));
}

[Fact]
public void TestRecursionWithTimeSpanAndCancellationToken()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

double Factorial(int number, bool isFirst = true)
{
using (asyncNonKeyedLocker.ConditionalLock(isFirst, TimeSpan.Zero, new CancellationToken(false), out _))
{
if (number == 0)
return 1;
return number * Factorial(number - 1, false);
}
}

Assert.Equal(120, Factorial(5));
}

[Fact]
public async Task TestRecursionWithTimeSpanAndCancellationTokenAsync()
{
var asyncNonKeyedLocker = new AsyncNonKeyedLocker();

async Task<double> Factorial(int number, bool isFirst = true)
{
using (await asyncNonKeyedLocker.ConditionalLockAsync(isFirst, TimeSpan.Zero, new CancellationToken(false)))
{
if (number == 0)
return 1;
return number * await Factorial(number - 1, false);
}
}

Assert.Equal(120, await Factorial(5));
}

[Fact]
public void TestMaxCount()
{
Expand Down
Loading

0 comments on commit a2a06e1

Please sign in to comment.