public void WaitAsync_SlotAvailable_IsCompleted()
 {
     Test.Async(async () =>
     {
         var semaphore = new AsyncSemaphore(1);
         Assert.AreEqual(1, semaphore.CurrentCount);
         var task1 = semaphore.WaitAsync();
         Assert.AreEqual(0, semaphore.CurrentCount);
         Assert.IsTrue(task1.IsCompleted);
         var task2 = semaphore.WaitAsync();
         Assert.AreEqual(0, semaphore.CurrentCount);
         await AssertEx.NeverCompletesAsync(task2);
     });
 }
        public void WaitAsync_Cancelled_DoesNotTakeSlot()
        {
            AsyncContext.Run(async () =>
            {
                var semaphore = new AsyncSemaphore(0);
                Assert.AreEqual(0, semaphore.CurrentCount);
                var cts = new CancellationTokenSource();
                var task = semaphore.WaitAsync(cts.Token);
                Assert.AreEqual(0, semaphore.CurrentCount);
                Assert.IsFalse(task.IsCompleted);

                cts.Cancel();

                try
                {
                    await task;
                }
                catch (OperationCanceledException)
                {
                }
                semaphore.Release();
                Assert.AreEqual(1, semaphore.CurrentCount);
                Assert.IsTrue(task.IsCanceled);
            });
        }
 public void WaitAsync_NoSlotsAvailable_IsNotCompleted()
 {
     Test.Async(async () =>
     {
         var semaphore = new AsyncSemaphore(0);
         Assert.AreEqual(0, semaphore.CurrentCount);
         var task = semaphore.WaitAsync();
         Assert.AreEqual(0, semaphore.CurrentCount);
         await AssertEx.NeverCompletesAsync(task);
     });
 }
        public void WaitAsync_PreCancelled_NoSlotAvailable_CancelsSynchronously()
        {
            var semaphore = new AsyncSemaphore(0);
            Assert.AreEqual(0, semaphore.CurrentCount);
            var token = new CancellationToken(true);

            var task = semaphore.WaitAsync(token);

            Assert.AreEqual(0, semaphore.CurrentCount);
            Assert.IsTrue(task.IsCompleted);
            Assert.IsTrue(task.IsCanceled);
            Assert.IsFalse(task.IsFaulted);
        }
 public void Release_WithWaiters_ReleasesWaiters()
 {
     Test.Async(async () =>
     {
         var semaphore = new AsyncSemaphore(0);
         Assert.AreEqual(0, semaphore.CurrentCount);
         var task = semaphore.WaitAsync();
         Assert.AreEqual(0, semaphore.CurrentCount);
         Assert.IsFalse(task.IsCompleted);
         semaphore.Release();
         Assert.AreEqual(0, semaphore.CurrentCount);
         await task;
     });
 }
 public void Release_WithoutWaiters_IncrementsCount()
 {
     var semaphore = new AsyncSemaphore(0);
     Assert.AreEqual(0, semaphore.CurrentCount);
     semaphore.Release();
     Assert.AreEqual(1, semaphore.CurrentCount);
     var task = semaphore.WaitAsync();
     Assert.AreEqual(0, semaphore.CurrentCount);
     Assert.IsTrue(task.IsCompleted);
 }