private async Task EnsureTaskFail <TE>(Task task) where TE : Exception { Exception caught = null; try { await task; } catch (Exception e) { caught = e; } if (caught == null) { Assert.Fail("should have caught {0} but there was no exception.", typeof(TE).Name); } if (caught is AggregateException) { caught = TaskEx.UnwrapWithPriority((AggregateException)caught); } if (!(caught is TE)) { Assert.Fail("should have caught {0} but {1} occurred", typeof(TE).Name, caught.GetType().Name); } }
/// <summary> /// The Wrapping does 2 things for children: /// (1) propagate error to other children of the host /// (2) call completion back of the child /// </summary> protected Task GetWrappedCompletion(Task rawCompletion) { if (Kind == DependencyKind.External) { return(rawCompletion); } //for internal dependencies, we have something to do var tcs = new TaskCompletionSource <object>(); rawCompletion.ContinueWith( task => { if (task.Status == TaskStatus.Faulted) { var exception = TaskEx.UnwrapWithPriority(task.Exception); tcs.SetException(exception); if (!(exception is PropagatedException)) { m_host.Fault(exception); //fault other blocks if this is an original exception //todo: log this original exception } { //todo: extension point } } else if (task.Status == TaskStatus.Canceled) { tcs.SetCanceled(); m_host.Fault(new TaskCanceledException()); } else //success { try { //call callback if (m_completionCallback != null) { m_completionCallback(task); } tcs.SetResult(string.Empty); } catch (Exception e) { LogHelper.Logger.Error( h => h( "{0} Error when callback {1} on its completion", m_host.FullName, this.DisplayName), e); tcs.SetException(e); m_host.Fault(e); } } }); return(tcs.Task); }
public async Task AssertFlowError <T>(Dataflow f) where T : Exception { try { await f.CompletionTask; } catch (Exception e) { Assert.IsTrue(TaskEx.UnwrapWithPriority(e as AggregateException) is T); } }
/// <summary> /// The Wrapping does 2 things: /// (1) propagate error to other children of the host /// (2) call completion back of the child /// </summary> protected Task GetWrappedCompletion(Task rawCompletion) { var tcs = new TaskCompletionSource <object>(); rawCompletion.ContinueWith(task => { if (task.Status == TaskStatus.Faulted) { var exception = TaskEx.UnwrapWithPriority(task.Exception); tcs.SetException(exception); if (!(exception is PropagatedException)) { m_host.Fault(exception); //fault other blocks if this is an original exception } } else if (task.Status == TaskStatus.Canceled) { tcs.SetCanceled(); m_host.Fault(new TaskCanceledException()); } else //success { try { //call callback if (m_completionCallback != null) { m_completionCallback(task); } tcs.SetResult(string.Empty); } catch (Exception e) { LogHelper.Logger.Error(h => h("[{0}] Error when callback {1} on its completion", m_host.Name, this.DisplayName), e); tcs.SetException(e); m_host.Fault(e); } } }); return(tcs.Task); }