Exemplo n.º 1
0
        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);
            });
        }
Exemplo n.º 2
0
 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);
     });
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
 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);
     });
 }
Exemplo n.º 5
0
 public DebugView(AsyncSemaphore semaphore)
 {
     _semaphore = semaphore;
 }
Exemplo n.º 6
0
 public DebugView(AsyncSemaphore semaphore)
 {
     _semaphore = semaphore;
 }
Exemplo n.º 7
0
 public void Id_IsNotZero()
 {
     var semaphore = new AsyncSemaphore(0);
     Assert.AreNotEqual(0, semaphore.Id);
 }
Exemplo n.º 8
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.º 9
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.º 10
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.º 11
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);
 }