public void Second_lock_completes_when_first_lock_is_released()
 {
     var dict = new AsyncLockDictionary<string>();
     var task0 = dict.LockAsync("A").AsTask().AssertSuccess();
     var task1 = dict.LockAsync("A").AsTask().AssertNotCompleted();
     task0.Result.Dispose();
     task1.AssertSuccess();
 }
 public async Task Cannot_lock_twice_in_parallel()
 {
     var dict = new AsyncLockDictionary<string>();
     var touchable = new Touchable(1);
     await SpamAsync(10000, async () =>
     {
         using (await dict.LockAsync("A"))
         {
             await touchable.TouchAsync();
         }
     });
     Assert.AreEqual(10000, touchable.TouchCount);
 }
 public async Task Can_lock_different_keys_in_parallel()
 {
     var dict = new AsyncLockDictionary<int>();
     var touchable = new Touchable(10);
     await Task.WhenAll(Enumerable.Range(0, 10)
         .Select(i => SpamAsync(1000, async () =>
         {
             using (await dict.LockAsync(i))
             {
                 await touchable.TouchAsync();
             }
         })));
     Assert.AreEqual(10000, touchable.TouchCount);
 }
        public async Task Dispose_ReleasesLock()
        {
            var dictionary = new AsyncLockDictionary();
            // first take a lock and wait
            IDisposable releaser1 = await dictionary.Acquire("key", CancellationToken.None);

            // then take the second lock before releasing first so that this one blocks
            Task <IDisposable> releaser2 = dictionary.Acquire("key", CancellationToken.None);

            Assert.AreEqual(TaskStatus.WaitingForActivation, releaser2.Status);
            // release first lock
            releaser1.Dispose();
            // second lock should now be aquired
            await releaser2;

            // finally release the second lock
            releaser2.Dispose();
        }
 public void Can_lock_different_keys_concurrently()
 {
     var dict = new AsyncLockDictionary<string>();
     dict.LockAsync("A").AsTask().AssertSuccess();
     dict.LockAsync("B").AsTask().AssertSuccess();
 }
 public void Cannot_lock_twice_concurrently()
 {
     var dict = new AsyncLockDictionary<string>();
     dict.LockAsync("A").AsTask().AssertSuccess();
     dict.LockAsync("A").AsTask().AssertNotCompleted();
 }
 public void Can_lock_once()
 {
     var dict = new AsyncLockDictionary<string>();
     dict.LockAsync("A").AsTask().AssertSuccess();
 }