Beispiel #1
0
        public async Task TryCompleteFromCompletedTask_PropagatesCancellation()
        {
            TaskCompletionSource <int> tcs = new TaskCompletionSource <int>();

            tcs.TryCompleteFromCompletedTask(TaskConstants.Canceled, () => - 1);
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(() => tcs.Task).ConfigureAwait(false);
        }
Beispiel #2
0
 public async Task ThrowsAsync_SynchronousTaskDoesNotThrow_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync(Task.FromResult(0));
     });
 }
Beispiel #3
0
        public async Task TryCompleteFromCompletedTask_PropagatesException()
        {
            TaskCompletionSource <int> tcs = new TaskCompletionSource <int>();

            tcs.TryCompleteFromCompletedTask(Task.FromException(new NotImplementedException()), () => - 1);
            await AsyncAssert.ThrowsAsync <NotImplementedException>(() => tcs.Task).ConfigureAwait(false);
        }
Beispiel #4
0
        public async Task ThrowsAsync_SynchronousTaskThrowsExpectedException_ReturnsException()
        {
            var expectedException = new InvalidOperationException();
            var result            = await AsyncAssert.ThrowsAsync <InvalidOperationException>(Task.FromException(expectedException));

            Assert.Same(expectedException, result);
        }
Beispiel #5
0
 public async Task ThrowsAsync_SynchronousTaskThrowsWrongException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync <NotImplementedException>(Task.FromException(new InvalidOperationException()));
     });
 }
Beispiel #6
0
 public async Task ThrowsAsync_SynchronousTaskThrowsBaseException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync <TaskCanceledException>(Task.FromException(new OperationCanceledException()));
     });
 }
Beispiel #7
0
        public async Task ThrowsAsync_SynchronousTaskThrowsDerivedException_ReturnsException()
        {
            var expectedException = new TaskCanceledException();
            var result            = await AsyncAssert.ThrowsAsync <OperationCanceledException>(Task.FromException(expectedException));

            Assert.Same(expectedException, result);
        }
Beispiel #8
0
 public async Task ThrowsAsync_ExpectingSpecificException_SynchronousTaskThrowsDerivedException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync <OperationCanceledException>(Task.FromException(new TaskCanceledException()), false);
     });
 }
Beispiel #9
0
        public async Task AsyncLock_CancelledLock_LeavesLockUnlocked()
        {
            AsyncLock mutex = new AsyncLock();
            CancellationTokenSource       cts       = new CancellationTokenSource();
            TaskCompletionSource <object> taskReady = TaskCompletionSourceExtensions.CreateAsyncTaskSource <object>();

            IDisposable unlock = await mutex.LockAsync();

            Task task = Task.Run(async() =>
            {
                AwaitableDisposable <IDisposable> lockTask = mutex.LockAsync(cts.Token);
                taskReady.SetResult(null);
                await lockTask;
            });
            await taskReady.Task;

            cts.Cancel();
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(task).ConfigureAwait(false);

            Assert.True(task.IsCanceled);
            unlock.Dispose();

            AwaitableDisposable <IDisposable> finalLockTask = mutex.LockAsync();
            await finalLockTask;
        }
        public async Task ThrowsCorrectExceptionOnCustomerCancellationTokens(Type initial, Type translated)
        {
            // not supported on sync
            if (!IsAsync)
            {
                Assert.Ignore();
            }

            var stream1 = new MockReadStream(100, throwAfter: 25, canSeek: true, exceptionType: initial);

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            });
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                ResponseClassifier.Shared,
                maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            var exception = await AsyncAssert.ThrowsAsync <Exception>(
                async() => await ReadAsync(reliableStream, _buffer, 25, 25, new CancellationToken(true)));

            Assert.IsInstanceOf(translated, exception);

            AssertReads(_buffer, 25);
        }
Beispiel #11
0
        public async Task AsyncLock_CancelledLock_LeavesLockUnlocked()
        {
            var mutex     = new AsyncLock();
            var cts       = new CancellationTokenSource();
            var taskReady = new TaskCompletionSource <object>();

            var unlock = await mutex.LockAsync();

            var task = Task.Run(async() =>
            {
                var lockTask = mutex.LockAsync(cts.Token);
                taskReady.SetResult(null);
                await lockTask;
            });
            await taskReady.Task;

            cts.Cancel();
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(task);

            Assert.True(task.IsCanceled);
            unlock.Dispose();

            var   finalLockTask = mutex.LockAsync();
            await finalLockTask;
        }
Beispiel #12
0
        public async Task TryCompleteFromCompletedTaskTResult_PropagatesCancellation()
        {
            var tcs = new TaskCompletionSource <int>();

            tcs.TryCompleteFromCompletedTask(TaskConstants <int> .Canceled);
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(() => tcs.Task);
        }
        public async Task DequeueAsync_EmptyAndComplete_ThrowsException()
        {
            var queue = new AsyncProducerConsumerQueue <int>();

            queue.CompleteAdding();

            await AsyncAssert.ThrowsAsync <InvalidOperationException>(() => queue.DequeueAsync());
        }
Beispiel #14
0
        public async Task EnqueueAsync_CompleteAdding_ThrowsException()
        {
            AsyncProducerConsumerQueue <int> queue = new AsyncProducerConsumerQueue <int>();

            queue.CompleteAdding();

            await AsyncAssert.ThrowsAsync <InvalidOperationException>(() => queue.EnqueueAsync(3)).ConfigureAwait(false);
        }
Beispiel #15
0
        public async Task WaitAsync_Cancelled_ThrowsException()
        {
            var are = new AsyncAutoResetEvent();
            var cts = new CancellationTokenSource();

            cts.Cancel();
            var task = are.WaitAsync(cts.Token);
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(task);
        }
Beispiel #16
0
        public async Task WaitAsync_Cancelled_ThrowsException()
        {
            AsyncAutoResetEvent     are = new AsyncAutoResetEvent();
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.Cancel();
            Task task = are.WaitAsync(cts.Token);
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(task).ConfigureAwait(false);
        }
Beispiel #17
0
        public async Task Cancelled_WhenInQueue_CancelsTask()
        {
            var queue = new DefaultAsyncWaitQueue <object>() as IAsyncWaitQueue <object>;
            var cts   = new CancellationTokenSource();
            var task  = queue.Enqueue(new object(), cts.Token);

            cts.Cancel();
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(task);
        }
        public async Task Cancelled_WhenInQueue_CancelsTask()
        {
            IAsyncWaitQueue <object> queue = new DefaultAsyncWaitQueue <object>() as IAsyncWaitQueue <object>;
            CancellationTokenSource  cts   = new CancellationTokenSource();
            Task <object>            task  = queue.Enqueue(new object(), cts.Token);

            cts.Cancel();
            await AsyncAssert.ThrowsAsync <OperationCanceledException>(task).ConfigureAwait(false);
        }
Beispiel #19
0
 public async Task ThrowsAsync_SynchronousDelegateThrowsBaseException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync <TaskCanceledException>(() =>
         {
             throw new OperationCanceledException();
         });
     });
 }
        public async Task EnqueueAsync_Cancelled_Throws()
        {
            var queue = new AsyncProducerConsumerQueue <int>(new[] { 13 }, 1);
            var cts   = new CancellationTokenSource();
            var task  = queue.EnqueueAsync(7, cts.Token);

            cts.Cancel();

            await AsyncAssert.ThrowsAsync <OperationCanceledException>(() => task);
        }
 public async Task ThrowsAsync_DelegateDoesNotThrow_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync(async() =>
         {
             await Task.Yield();
         });
     });
 }
 public async Task ThrowsAsync_TaskDoesNotThrow_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         var tcs      = new TaskCompletionSource <object>();
         var testTask = AsyncAssert.ThrowsAsync(tcs.Task);
         tcs.SetResult(null);
         await testTask;
     });
 }
 public async Task ThrowsAsync_TaskThrowsWrongException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         var tcs      = new TaskCompletionSource <object>();
         var testTask = AsyncAssert.ThrowsAsync <NotImplementedException>(tcs.Task);
         tcs.SetException(new InvalidOperationException());
         await testTask;
     });
 }
 public async Task ThrowsAsync_TaskThrowsBaseException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         var tcs      = new TaskCompletionSource <object>();
         var testTask = AsyncAssert.ThrowsAsync <TaskCanceledException>(tcs.Task);
         tcs.SetException(new OperationCanceledException());
         await testTask;
     });
 }
 public async Task ThrowsAsync_ExpectingSpecificException_TaskThrowsDerivedException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         var tcs      = new TaskCompletionSource <object>();
         var testTask = AsyncAssert.ThrowsAsync <OperationCanceledException>(tcs.Task, false);
         tcs.SetException(new TaskCanceledException());
         await testTask;
     });
 }
Beispiel #26
0
 public async Task ThrowsAsync_ExpectingSpecificException_SynchronousDelegateThrowsDerivedException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync <OperationCanceledException>(() =>
         {
             throw new TaskCanceledException();
         }, false);
     });
 }
Beispiel #27
0
        public async Task ThrowsAsync_SynchronousDelegateThrowsDerivedException_ReturnsException()
        {
            var expectedException = new TaskCanceledException();
            var result            = await AsyncAssert.ThrowsAsync <OperationCanceledException>(() =>
            {
                throw expectedException;
            });

            Assert.Same(expectedException, result);
        }
Beispiel #28
0
        public async Task DequeueAsync_Cancelled_Throws()
        {
            AsyncProducerConsumerQueue <int> queue = new AsyncProducerConsumerQueue <int>();
            CancellationTokenSource          cts   = new CancellationTokenSource();
            Task <int> task = queue.DequeueAsync(cts.Token);

            cts.Cancel();

            await AsyncAssert.ThrowsAsync <OperationCanceledException>(() => task).ConfigureAwait(false);
        }
Beispiel #29
0
        public async Task ThrowsAsync_SynchronousDelegateThrows_ReturnsException()
        {
            var expectedException = new InvalidOperationException();
            var result            = await AsyncAssert.ThrowsAsync(() =>
            {
                throw expectedException;
            });

            Assert.Same(expectedException, result);
        }
Beispiel #30
0
 public async Task ThrowsAsync_SynchronousDelegateThrowsWrongException_Fails()
 {
     await Assert.ThrowsAsync <Exception>(async() =>
     {
         await AsyncAssert.ThrowsAsync <NotImplementedException>(() =>
         {
             throw new InvalidOperationException();
         });
     });
 }