示例#1
0
 public void Failed0()
 {
     AsyncContext.Run(async() =>
     {
         await AssertEx.ThrowsExceptionAsync <InvalidOperationException>(AsyncFactory.FromApm(BeginFail0, End));
     });
 }
        public void UpgradeableReadAndReadLocked_UpgradeAborted_CompletesAsCanceled()
        {
            AsyncContext.Run(async() =>
            {
                var rwl           = new AsyncReaderWriterLock();
                var readLockTaken = new TaskCompletionSource();
                var task          = Task.Run(async() =>
                {
                    using (await rwl.ReaderLockAsync())
                    {
                        readLockTaken.SetResult();
                        await TaskConstants.Never;
                    }
                });
                await readLockTaken.Task;
                Task upgradeTask;
                using (var key = await rwl.UpgradeableReaderLockAsync())
                {
                    upgradeTask = key.UpgradeAsync();
                    Assert.IsFalse(key.Upgraded);
                }

                await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(upgradeTask);
            });
        }
示例#3
0
        public void AsyncLock_CancelledLock_LeavesLockUnlocked()
        {
            AsyncContext.Run(async() =>
            {
                var mutex     = new AsyncLock();
                var cts       = new CancellationTokenSource();
                var taskReady = new TaskCompletionSource();
                Task <IDisposable> initialLockTask = null, lockTask = null;

                initialLockTask = mutex.LockAsync();
                var unlock      = await initialLockTask;
                var task        = Task.Run(async() =>
                {
                    lockTask = mutex.LockAsync(cts.Token);
                    taskReady.SetResult();
                    await lockTask;
                });
                await taskReady.Task;
                cts.Cancel();
                await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(task);
                Assert.IsTrue(task.IsCanceled);
                unlock.Dispose();

                var finalLockTask = mutex.LockAsync();
                await finalLockTask;
            });
        }
示例#4
0
 public void PostPhaseAction_PropagatesExceptions()
 {
     AsyncContext.Run(async() =>
     {
         var barrier = new AsyncBarrier(1, _ => { throw new NotImplementedException(); });
         await AssertEx.ThrowsExceptionAsync <NotImplementedException>(barrier.SignalAndWaitAsync(), allowDerivedTypes: false);
     });
 }
示例#5
0
 public void TryCompleteFromEventArgs_PropagatesCancellation()
 {
     AsyncContext.Run(async() =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromEventArgs(new AsyncCompletedEventArgs(null, true, null));
         await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(() => tcs.Task);
     });
 }
示例#6
0
 public void TryCompleteFromEventArgs_PropagatesException()
 {
     AsyncContext.Run(async() =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromEventArgs(new AsyncCompletedEventArgs(new NotImplementedException(), false, null));
         await AssertEx.ThrowsExceptionAsync <NotImplementedException>(() => tcs.Task);
     });
 }
示例#7
0
 public void TryCompleteFromCompletedTask_PropagatesCancellation()
 {
     AsyncContext.Run(async() =>
     {
         var tcs = new TaskCompletionSource();
         tcs.TryCompleteFromCompletedTask(TaskConstants.Canceled);
         await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(() => tcs.Task);
     });
 }
        public void AsTask_ConstructedCanceledToken_CancelsTask()
        {
            AsyncContext.Run(async() =>
            {
                var token = new CancellationToken(true);
                var task  = token.AsTask();

                await AssertEx.ThrowsExceptionAsync <TaskCanceledException>(task);
            });
        }
        public void DequeueAsync_EmptyAndComplete_ThrowsException()
        {
            AsyncContext.Run(async() =>
            {
                var queue = new AsyncProducerConsumerQueue <int>();
                await queue.CompleteAddingAsync();

                await AssertEx.ThrowsExceptionAsync <InvalidOperationException>(() => queue.DequeueAsync());
            });
        }
 public void UpgradeableKey_MultiUpgrade_ThrowsException()
 {
     AsyncContext.Run(async() =>
     {
         var rwl = new AsyncReaderWriterLock();
         var key = await rwl.UpgradeableReaderLockAsync();
         await key.UpgradeAsync();
         await AssertEx.ThrowsExceptionAsync <InvalidOperationException>(async() => { await key.UpgradeAsync(); });
     });
 }
        public void AsTask_CanceledToken_CancelsTask()
        {
            AsyncContext.Run(async() =>
            {
                var cts = new CancellationTokenSource();
                cts.Cancel();
                var task = cts.Token.AsTask();

                await AssertEx.ThrowsExceptionAsync <TaskCanceledException>(task);
            });
        }
 public void WaitAsync_Cancelled_ThrowsException()
 {
     AsyncContext.Run(async() =>
     {
         var are = new AsyncAutoResetEvent();
         var cts = new CancellationTokenSource();
         cts.Cancel();
         var task = are.WaitAsync(cts.Token);
         await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(task);
     });
 }
示例#13
0
 public void Cancelled_WhenInQueue_CancelsTask()
 {
     AsyncContext.Run(async() =>
     {
         var queue = new DefaultAsyncWaitQueue <object>() as IAsyncWaitQueue <object>;
         var cts   = new CancellationTokenSource();
         var task  = queue.Enqueue(cts.Token);
         cts.Cancel();
         await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(task);
     });
 }
示例#14
0
        public void TryCompleteFromCompletedTask_PropagatesException()
        {
            AsyncContext.Run(async() =>
            {
                var source = new TaskCompletionSource();
                source.TrySetException(new NotImplementedException());

                var tcs = new TaskCompletionSource();
                tcs.TryCompleteFromCompletedTask(source.Task);
                await AssertEx.ThrowsExceptionAsync <NotImplementedException>(() => tcs.Task);
            });
        }
示例#15
0
        public void FromEvent1_AsyncEventFailed_Fails()
        {
            AsyncContext.Run(() => Task.Run(async() =>
            {
                var bgw     = new BackgroundWorker();
                bgw.DoWork += (_, args) => { throw new NotImplementedException(); };
                var task    = AsyncFactory <RunWorkerCompletedEventArgs> .FromEvent(bgw, "RunWorkerCompleted");
                bgw.RunWorkerAsync();

                await AssertEx.ThrowsExceptionAsync <NotImplementedException>(task);
            }));
        }
示例#16
0
 public void WaitWithoutExceptionWithCancellationToken_TokenCanceled_ThrowsException()
 {
     AsyncContext.Run(async() =>
     {
         var cts    = new CancellationTokenSource();
         var task   = Task.Run(() => TaskConstants.Never.WaitWithoutException(cts.Token));
         var result = task.Wait(500);
         Assert.IsFalse(result);
         cts.Cancel();
         await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(() => task);
     });
 }
        public void DequeueFromAnyAsync_EmptyAndComplete_ThrowsException()
        {
            AsyncContext.Run(async() =>
            {
                var queue1 = new AsyncProducerConsumerQueue <int>();
                await queue1.CompleteAddingAsync();
                var queue2 = new AsyncProducerConsumerQueue <int>();
                await queue2.CompleteAddingAsync();
                var queues = new[] { queue1, queue2 };

                await AssertEx.ThrowsExceptionAsync <InvalidOperationException>(() => queues.DequeueFromAnyAsync());
            });
        }
        public void EnqueueAsync_Cancelled_Throws()
        {
            AsyncContext.Run(async() =>
            {
                var queue = new AsyncProducerConsumerQueue <int>(new[] { 13 }, 1);
                var cts   = new CancellationTokenSource();
                var task  = queue.EnqueueAsync(7, cts.Token);

                cts.Cancel();

                await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(() => task);
            });
        }
        public void DequeueFromAnyAsync_Cancelled_Throws()
        {
            AsyncContext.Run(async() =>
            {
                var queue1 = new AsyncProducerConsumerQueue <int>();
                var queue2 = new AsyncProducerConsumerQueue <int>();
                var queues = new[] { queue1, queue2 };
                var cts    = new CancellationTokenSource();
                var task   = queues.DequeueFromAnyAsync(cts.Token);

                cts.Cancel();

                await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(() => task);
            });
        }
示例#20
0
        public void AsyncLock_CanceledLock_ThrowsException()
        {
            AsyncContext.Run(async() =>
            {
                var mutex = new AsyncLock();
                var cts   = new CancellationTokenSource();

                var initialLockTask = mutex.LockAsync();
                await initialLockTask;
                var canceledLockTask = mutex.LockAsync(cts.Token);
                cts.Cancel();

                await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(canceledLockTask);
            });
        }
        public void WriteLocked_UpgradeableReaderLockCancelled_DoesNotTakeLockWhenUnlocked()
        {
            AsyncContext.Run(async() =>
            {
                var rwl = new AsyncReaderWriterLock();
                using (await rwl.WriterLockAsync())
                {
                    var cts  = new CancellationTokenSource();
                    var task = rwl.UpgradeableReaderLockAsync(cts.Token);
                    cts.Cancel();
                    await AssertEx.ThrowsExceptionAsync <OperationCanceledException>(() => task);
                }

                await rwl.UpgradeableReaderLockAsync();
            });
        }