Exemplo n.º 1
0
 public void WaitAsync_Cancelled_DoesNotTakeSlot()
 {
     Test.Async(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);
     });
 }
Exemplo n.º 2
0
 public void Release_ZeroSlots_HasNoEffect()
 {
     var semaphore = new AsyncSemaphore(1);
     Assert.AreEqual(1, semaphore.CurrentCount);
     semaphore.Release(0);
     Assert.AreEqual(1, semaphore.CurrentCount);
 }
Exemplo n.º 3
0
 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;
     });
 }
Exemplo n.º 4
0
 public void Release_Overflow_ThrowsException()
 {
     var semaphore = new AsyncSemaphore(int.MaxValue);
     Assert.AreEqual(int.MaxValue, semaphore.CurrentCount);
     AssertEx.ThrowsException<InvalidOperationException>(() => semaphore.Release());
 }
Exemplo n.º 5
0
 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);
 }