Browse Source

Merge pull request #16645 from abpframework/SemaphoreSlimExtensions

Throw `TimeoutException` if wait failed.
pull/16653/head
liangshiwei 3 years ago
committed by GitHub
parent
commit
6f00dfcc8b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 76
      framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs
  2. 28
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/Threading/SemaphoreSlimExtensions_Tests.cs

76
framework/src/Volo.Abp.Core/Volo/Abp/Threading/SemaphoreSlimExtensions.cs

@ -6,40 +6,56 @@ namespace Volo.Abp.Threading;
public static class SemaphoreSlimExtensions
{
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim)
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim)
{
await semaphoreSlim.WaitAsync();
return GetDispose(semaphoreSlim);
}
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(cancellationToken);
return GetDispose(semaphoreSlim);
}
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
{
await semaphoreSlim.WaitAsync(millisecondsTimeout);
return GetDispose(semaphoreSlim);
if (await semaphoreSlim.WaitAsync(millisecondsTimeout))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(millisecondsTimeout, cancellationToken);
return GetDispose(semaphoreSlim);
if (await semaphoreSlim.WaitAsync(millisecondsTimeout, cancellationToken))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
{
await semaphoreSlim.WaitAsync(timeout);
return GetDispose(semaphoreSlim);
if (await semaphoreSlim.WaitAsync(timeout))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
public async static Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(timeout, cancellationToken);
return GetDispose(semaphoreSlim);
if (await semaphoreSlim.WaitAsync(timeout, cancellationToken))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim)
@ -56,26 +72,42 @@ public static class SemaphoreSlimExtensions
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
{
semaphoreSlim.Wait(millisecondsTimeout);
return GetDispose(semaphoreSlim);
if (semaphoreSlim.Wait(millisecondsTimeout))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
{
semaphoreSlim.Wait(millisecondsTimeout, cancellationToken);
return GetDispose(semaphoreSlim);
if (semaphoreSlim.Wait(millisecondsTimeout, cancellationToken))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
{
semaphoreSlim.Wait(timeout);
return GetDispose(semaphoreSlim);
if (semaphoreSlim.Wait(timeout))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
{
semaphoreSlim.Wait(timeout, cancellationToken);
return GetDispose(semaphoreSlim);
if (semaphoreSlim.Wait(timeout, cancellationToken))
{
return GetDispose(semaphoreSlim);
}
throw new TimeoutException();
}
private static IDisposable GetDispose(this SemaphoreSlim semaphoreSlim)

28
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Threading/SemaphoreSlimExtensions_Tests.cs

@ -0,0 +1,28 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.Threading;
public class SemaphoreSlimExtensions_Tests
{
[Fact]
public async Task LockAsync_Test()
{
var semaphore = new SemaphoreSlim(0, 1);
await Assert.ThrowsAsync<TimeoutException>(async () =>
{
await semaphore.LockAsync(10);
});
semaphore = new SemaphoreSlim(1, 1);
using (await semaphore.LockAsync())
{
semaphore.CurrentCount.ShouldBe(0);
}
semaphore.CurrentCount.ShouldBe(1);
}
}
Loading…
Cancel
Save