public async Task CancelledTest()
        {
            // Normal
            var task = GetPreviousTask();
            await Assert.ThrowsAsync <CustomCancellationException>(() => task.AsTask());

            Assert.True(task.IsCanceled);

            // Using the continuation
            task = GetPreviousTask();
            var tcg = new ValueTaskContinuationGenerator <ValueTaskContinuationGeneratorTests, ValueTaskContinuationGeneratorTests, ValueTask>();
            await Assert.ThrowsAsync <CustomCancellationException>(() => tcg.SetContinuation(this, task, null, CallTargetState.GetDefault()).AsTask());

            Assert.True(task.IsCanceled);

            ValueTask GetPreviousTask()
            {
                var cts = new CancellationTokenSource();

                var task = Task.FromResult(true).ContinueWith(
                    _ =>
                {
                    cts.Cancel();
                    throw new CustomCancellationException(cts.Token);
                },
                    cts.Token);

                return(new ValueTask(task));
            }
        }
Пример #2
0
        public async ValueTask SuccessTest()
        {
            var tcg   = new ValueTaskContinuationGenerator <ValueTaskContinuationGeneratorTests, ValueTaskContinuationGeneratorTests, ValueTask>();
            var cTask = tcg.SetContinuation(this, GetPreviousTask(), null, CallTargetState.GetDefault());

            await cTask;

            async ValueTask GetPreviousTask()
            {
                await Task.Delay(1000).ConfigureAwait(false);
            }
        }
Пример #3
0
        public async Task ExceptionGenericTest()
        {
            Exception ex = null;

            // Normal
            ex = await Assert.ThrowsAsync <CustomException>(() => GetPreviousTask().AsTask());

            Assert.Equal("Internal Test Exception", ex.Message);

            // Using the continuation
            var tcg = new ValueTaskContinuationGenerator <ValueTaskContinuationGeneratorTests, ValueTaskContinuationGeneratorTests, ValueTask <bool>, bool>();

            ex = await Assert.ThrowsAsync <CustomException>(() => tcg.SetContinuation(this, GetPreviousTask(), null, CallTargetState.GetDefault()).AsTask());

            Assert.Equal("Internal Test Exception", ex.Message);

            async ValueTask <bool> GetPreviousTask()
            {
                await Task.Delay(1000).ConfigureAwait(false);

                throw new CustomException("Internal Test Exception");
            }
        }