示例#1
0
 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));
     }
 }
示例#2
0
        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));
        }
示例#3
0
 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);
 }
示例#4
0
 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);
 }
示例#5
0
        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);
        }
示例#6
0
 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));
 }
示例#7
0
 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));
     }
 }