public static async Task Invoke(this Func <Task> action, ExceptionAggregator aggregator, ExecutionTimer timer) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await aggregator?.RunAsync( () => timer.AggregateAsync( async() => { await action(); var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { aggregator.Add(ex); } })); } finally { SetSynchronizationContext(oldSyncContext); } }
private async Task <IDisposable[]> InvokeBodyAsync() { var stepContext = new StepContext(this.step); if (this.body != null) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await this.aggregator.RunAsync( () => this.timer.AggregateAsync( async() => { await this.body(stepContext); var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { this.aggregator.Add(ex); } })); } finally { SetSynchronizationContext(oldSyncContext); } } return(stepContext.Disposables.ToArray()); }
/// <inheritdoc/> protected override async Task <decimal> InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); await this.Aggregator.RunAsync( () => this.Timer.AggregateAsync( async() => { var parameterCount = this.TestMethod.GetParameters().Length; var valueCount = this.TestMethodArguments == null ? 0 : this.TestMethodArguments.Length; if (parameterCount != valueCount) { this.Aggregator.Add( new InvalidOperationException( $"The test method expected {parameterCount} parameter value{(parameterCount == 1 ? "" : "s")}," + $"but {valueCount} parameter value{(valueCount == 1 ? "" : "s")} {(valueCount == 1 ? "was" : "were")} provided.") ); } else { var result = this.TestMethod.Invoke(testClassInstance, this.TestMethodArguments); var task = result as Task; if (task != null) { await task; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { this.Aggregator.Add(ex); } } } } ) ); //if (Aggregator.HasExceptions) //{ // var handleTestFailure = testClassInstance as INeedToKnowTestFailure; // if (handleTestFailure != null) // { // await // Aggregator.RunAsync( // () => Timer.AggregateAsync( // () => handleTestFailure.HandleFailureAsync(Test, Aggregator.ToException()))); // } //} } finally { SetSynchronizationContext(oldSyncContext); } return(this.Timer.Total); }