Exemple #1
0
        public static void CallDisposeTwice()
        {
            var @lock = new AsyncSharedLock(3);

            @lock.Dispose();
            True(@lock.DisposeAsync().IsCompletedSuccessfully);
        }
Exemple #2
0
 public static async Task StrongLocks()
 {
     using var sharedLock = new AsyncSharedLock(3);
     True(await sharedLock.TryAcquireAsync(true, TimeSpan.Zero));
     False(await sharedLock.TryAcquireAsync(false, TimeSpan.Zero));
     False(await sharedLock.TryAcquireAsync(true, TimeSpan.Zero));
 }
 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);
 }
        private static async void AcquireWeakLockAndRelease(AsyncSharedLock sharedLock, AsyncCountdownEvent acquireEvent)
        {
            await Task.Delay(100);

            await sharedLock.Acquire(false, TimeSpan.Zero);

            acquireEvent.Signal();
            await Task.Delay(100);

            sharedLock.Release();
        }
Exemple #5
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));
        }
 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);
 }
Exemple #7
0
        public static async Task StrongToWeakLockTransition()
        {
            using var acquireEvent = new AsyncCountdownEvent(2L);
            using var sharedLock   = new AsyncSharedLock(3);
            await sharedLock.AcquireAsync(true, TimeSpan.Zero);

            AcquireWeakLock(sharedLock, acquireEvent);
            AcquireWeakLock(sharedLock, acquireEvent);
            sharedLock.Release();
            True(await acquireEvent.WaitAsync(DefaultTimeout));
            Equal(1, sharedLock.RemainingCount);
        }
Exemple #8
0
        public static async Task WeakToStrongLockTransition()
        {
            using var acquireEvent = new AsyncCountdownEvent(3L);
            using var sharedLock   = new AsyncSharedLock(3);
            AcquireWeakLockAndRelease(sharedLock, acquireEvent);
            AcquireWeakLockAndRelease(sharedLock, acquireEvent);
            AcquireWeakLockAndRelease(sharedLock, acquireEvent);
            True(await acquireEvent.WaitAsync(DefaultTimeout));
            await sharedLock.AcquireAsync(true, DefaultTimeout);

            Equal(0, sharedLock.RemainingCount);
        }
        public static async Task StrongToWeakLockTransition()
        {
            using (var acquireEvent = new AsyncCountdownEvent(2L))
                using (var sharedLock = new AsyncSharedLock(3))
                {
                    await sharedLock.Acquire(true, TimeSpan.Zero);

                    AcquireWeakLock(sharedLock, acquireEvent);
                    AcquireWeakLock(sharedLock, acquireEvent);
                    sharedLock.Release();
                    True(await acquireEvent.Wait(TimeSpan.FromMinutes(1)));
                    Equal(1, sharedLock.RemainingCount);
                }
        }
Exemple #10
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));
 }
        public static async Task WeakToStrongLockTransition()
        {
            using (var acquireEvent = new AsyncCountdownEvent(3L))
                using (var sharedLock = new AsyncSharedLock(3))
                {
                    AcquireWeakLockAndRelease(sharedLock, acquireEvent);
                    AcquireWeakLockAndRelease(sharedLock, acquireEvent);
                    AcquireWeakLockAndRelease(sharedLock, acquireEvent);
                    await acquireEvent.Wait();

                    await sharedLock.Acquire(true, TimeSpan.FromMinutes(1));

                    Equal(0, sharedLock.RemainingCount);
                }
        }
Exemple #12
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);
        }
Exemple #13
0
 public static async Task WeakLocks()
 {
     using var sharedLock = new AsyncSharedLock(3);
     Equal(3, sharedLock.ConcurrencyLevel);
     True(await sharedLock.TryAcquireAsync(false, TimeSpan.Zero));
     True(await sharedLock.TryAcquireAsync(false, TimeSpan.Zero));
     Equal(1, sharedLock.RemainingCount);
     True(await sharedLock.TryAcquireAsync(false, TimeSpan.Zero));
     Equal(0, sharedLock.RemainingCount);
     False(await sharedLock.TryAcquireAsync(false, TimeSpan.Zero));
     False(await sharedLock.TryAcquireAsync(true, TimeSpan.Zero));
     sharedLock.Release();
     Equal(1, sharedLock.RemainingCount);
     False(await sharedLock.TryAcquireAsync(true, TimeSpan.Zero));
     True(await sharedLock.TryAcquireAsync(false, TimeSpan.Zero));
 }
Exemple #14
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));
     }
 }
        private static async void AcquireWeakLock(AsyncSharedLock sharedLock, AsyncCountdownEvent acquireEvent)
        {
            await sharedLock.Acquire(false, CancellationToken.None);

            acquireEvent.Signal();
        }
Exemple #16
0
 public static void DisposeAsyncCompletedAsynchronously()
 {
     using var @lock = new AsyncSharedLock(3);
     True(@lock.DisposeAsync().IsCompletedSuccessfully);
 }