public static async Task StrongLocks() { using (var sharedLock = new AsyncSharedLock(3)) { True(await sharedLock.TryAcquire(true, TimeSpan.Zero)); False(await sharedLock.TryAcquire(false, TimeSpan.Zero)); False(await sharedLock.TryAcquire(true, TimeSpan.Zero)); } }
public static void GracefulShutdown() { using var @lock = new AsyncSharedLock(3); True(@lock.TryAcquire(false)); var task = @lock.DisposeAsync(); False(task.IsCompleted); @lock.Release(); True(task.IsCompletedSuccessfully); Throws <ObjectDisposedException>(() => @lock.TryAcquire(true)); }
public static void DowngradeFromStrongToWeakLock() { using var sharedLock = new AsyncSharedLock(3); True(sharedLock.TryAcquire(true)); Equal(0, sharedLock.RemainingCount); False(sharedLock.TryAcquire(false)); sharedLock.Downgrade(); Equal(2, sharedLock.RemainingCount); sharedLock.Release(); Equal(3, sharedLock.RemainingCount); }
public static void DowgradeWeakLock() { using var sharedLock = new AsyncSharedLock(3); True(sharedLock.TryAcquire(false)); Equal(2, sharedLock.RemainingCount); sharedLock.Downgrade(); False(sharedLock.IsLockHeld); Throws <SynchronizationLockException>(sharedLock.Downgrade); }
public static void GracefulShutdown3() { using var @lock = new AsyncSharedLock(3); True(@lock.TryAcquire(false)); var task = @lock.DisposeAsync(); False(task.IsCompleted); var acquisition = @lock.AcquireAsync(true, CancellationToken.None); False(acquisition.IsCompleted); @lock.Downgrade(); True(task.IsCompletedSuccessfully); True(acquisition.IsFaulted); Throws <ObjectDisposedException>(acquisition.GetAwaiter().GetResult); }
public static void FailFastLock() { using var sharedLock = new AsyncSharedLock(3); True(sharedLock.TryAcquire(false)); True(sharedLock.TryAcquire(false)); True(sharedLock.TryAcquire(false)); False(sharedLock.TryAcquire(true)); False(sharedLock.TryAcquire(false)); sharedLock.Release(); sharedLock.Release(); sharedLock.Release(); True(sharedLock.TryAcquire(true)); False(sharedLock.TryAcquire(false)); }
public static async Task WeakLocks() { using (var sharedLock = new AsyncSharedLock(3)) { True(await sharedLock.TryAcquire(false, TimeSpan.Zero)); True(await sharedLock.TryAcquire(false, TimeSpan.Zero)); Equal(1, sharedLock.RemainingCount); True(await sharedLock.TryAcquire(false, TimeSpan.Zero)); Equal(0, sharedLock.RemainingCount); False(await sharedLock.TryAcquire(false, TimeSpan.Zero)); False(await sharedLock.TryAcquire(true, TimeSpan.Zero)); sharedLock.Release(); Equal(1, sharedLock.RemainingCount); False(await sharedLock.TryAcquire(true, TimeSpan.Zero)); True(await sharedLock.TryAcquire(false, TimeSpan.Zero)); } }