public async Task SuccessTest()
        {
            var tcg   = new TaskContinuationGenerator <TaskContinuationGeneratorTests, TaskContinuationGeneratorTests, Task>();
            var cTask = tcg.SetContinuation(this, GetPreviousTask(), null, CallTargetState.GetDefault());

            await cTask;

            async Task GetPreviousTask()
            {
                await Task.Delay(1000).ConfigureAwait(false);
            }
        }
示例#2
0
        public async Task CancelledTest()
        {
            // Normal
            var task = GetPreviousTask();
            await Assert.ThrowsAsync <CustomCancellationException>(() => task);

            Assert.Equal(TaskStatus.Canceled, task.Status);

            // Using the continuation
            var tcg = new TaskContinuationGenerator <TaskContinuationGeneratorTests, TaskContinuationGeneratorTests, Task>();

            task = tcg.SetContinuation(this, GetPreviousTask(), null, CallTargetState.GetDefault());
            await Assert.ThrowsAsync <CustomCancellationException>(() => task);

            Assert.Equal(TaskStatus.Canceled, task.Status);
        public async Task ExceptionGenericTest()
        {
            Exception ex = null;

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

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

            // Using the continuation
            var tcg = new TaskContinuationGenerator <TaskContinuationGeneratorTests, TaskContinuationGeneratorTests, Task <bool>, bool>();

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

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

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

                throw new CustomException("Internal Test Exception");
            }
        }
示例#4
0
        /// <summary>
        /// Adds a continuation based on the current returnValue
        /// </summary>
        /// <typeparam name="TState">Type of the state</typeparam>
        /// <param name="returnValue">Return value</param>
        /// <param name="ex">Exception</param>
        /// <param name="state">State value</param>
        /// <param name="continuation">Continuation delegate</param>
        /// <param name="generator">Continuation generator</param>
        /// <returns>Return value after the continuation</returns>
        public static object AddContinuation <TState>(object returnValue, Exception ex, TState state, Func <object, Exception, TState, Task <object> > continuation, TaskContinuationGenerator generator = null)
        {
            if (returnValue is Task returnTask && ex is null)
            {
                generator ??= GetContinuationFrom(returnTask.GetType());
                return(generator.SetTaskContinuationAsync(returnTask, state, continuation));
            }

            SynchronizationContext currentContext = SynchronizationContext.Current;

            try
            {
                SynchronizationContext.SetSynchronizationContext(null);
                return(continuation(returnValue, ex, state).GetAwaiter().GetResult());
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(currentContext);
            }
        }
示例#5
0
        /// <summary>
        /// Adds a continuation based on the current returnValue
        /// </summary>
        /// <typeparam name="TState">Type of the state</typeparam>
        /// <param name="returnValue">Return value</param>
        /// <param name="ex">Exception</param>
        /// <param name="state">State value</param>
        /// <param name="continuation">Continuation delegate</param>
        /// <param name="generator">Continuation generator</param>
        /// <returns>Return value after the continuation</returns>
        public static object AddContinuation <TState>(object returnValue, Exception ex, TState state, Func <object, Exception, TState, object> continuation, TaskContinuationGenerator generator = null)
        {
            if (returnValue is Task returnTask && ex is null)
            {
                generator ??= GetContinuationFrom(returnTask.GetType());
                return(generator.SetTaskContinuation(returnTask, state, continuation));
            }

            return(continuation(returnValue, ex, state));
        }