public static async Task ConcurrentLock() { using var are = new AutoResetEvent(false); using var @lock = new AsyncExclusiveLock(); await @lock.AcquireAsync(TimeSpan.Zero); var task = new TaskCompletionSource <bool>(); ThreadPool.QueueUserWorkItem(async state => { False(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(10))); True(ThreadPool.QueueUserWorkItem(static ev => ev.Set(), are, false)); await @lock.AcquireAsync(DefaultTimeout); @lock.Release(); task.SetResult(true); });
public static async Task TrivialLock() { using var @lock = new AsyncExclusiveLock(); True(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(10))); False(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(100))); await ThrowsAsync <TimeoutException>(() => @lock.AcquireAsync(TimeSpan.FromMilliseconds(100))); @lock.Release(); True(await @lock.TryAcquireAsync(TimeSpan.FromMilliseconds(100))); }
public static void CancelSuspendedCallers() { using var @lock = new AsyncExclusiveLock(); True(@lock.TryAcquire()); var waitNode = @lock.AcquireAsync(CancellationToken.None); False(waitNode.IsCompleted); Throws <ArgumentOutOfRangeException>(() => @lock.CancelSuspendedCallers(new CancellationToken(false))); @lock.CancelSuspendedCallers(new CancellationToken(true)); True(waitNode.IsCompleted); False(waitNode.IsCompletedSuccessfully); True(waitNode.IsCanceled); }
public static void GracefulShutdown2() { using var @lock = new AsyncExclusiveLock(); True(@lock.TryAcquire()); var task = @lock.DisposeAsync(); False(task.IsCompleted); var acquisition = @lock.AcquireAsync(CancellationToken.None); False(acquisition.IsCompleted); @lock.Release(); True(task.IsCompletedSuccessfully); True(acquisition.IsFaulted); Throws <ObjectDisposedException>(acquisition.GetAwaiter().GetResult); }