Skip to content

Commit

Permalink
Always specify context parameter to AsyncToSyncAdapter.Await
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Dec 2, 2024
1 parent 5a4f35c commit 18f9331
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/NUnitFramework/framework/Assert.Exceptions.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public partial class Assert
Exception? caughtException = null;
try
{
AsyncToSyncAdapter.Await(code.Invoke);
AsyncToSyncAdapter.Await(TestExecutionContext.CurrentContext, code.Invoke);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitFramework/framework/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public static void Multiple(AsyncTestDelegate testDelegate)
{
using (EnterMultipleScope())
{
AsyncToSyncAdapter.Await(testDelegate.Invoke);
AsyncToSyncAdapter.Await(TestExecutionContext.CurrentContext, testDelegate.Invoke);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NUnitFramework/framework/Constraints/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected Constraint(params object?[] args)
public virtual ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> del)
{
if (AsyncToSyncAdapter.IsAsyncOperation(del))
return ApplyTo(AsyncToSyncAdapter.Await(() => del.Invoke()));
return ApplyTo(AsyncToSyncAdapter.Await(TestExecutionContext.CurrentContext, () => del.Invoke()));

return ApplyTo(GetTestObject(del));
}
Expand Down
8 changes: 0 additions & 8 deletions src/NUnitFramework/framework/Constraints/DelayedConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,6 @@ public override async Task<ConstraintResult> ApplyToAsync<TActual>(Func<Task<TAc
return new DelegatingConstraintResult(this, await BaseConstraint.ApplyToAsync(taskDel));
}

private static object? InvokeDelegate<T>(ActualValueDelegate<T> del)
{
if (AsyncToSyncAdapter.IsAsyncOperation(del))
return AsyncToSyncAdapter.Await(() => del.Invoke());

return del();
}

/// <summary>
/// Returns the string representation of the constraint.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public AsyncEnumeratorWrapper(IAsyncEnumerator<object?> asyncEnumerator)
public object? Current => _asyncEnumerator.Current;

public void Dispose()
=> AsyncToSyncAdapter.Await(() => _asyncEnumerator.DisposeAsync());
=> AsyncToSyncAdapter.Await(context: null, () => _asyncEnumerator.DisposeAsync());

public bool MoveNext()
=> AsyncToSyncAdapter.Await<bool>(() => _asyncEnumerator.MoveNextAsync());
=> AsyncToSyncAdapter.Await<bool>(context: null, () => _asyncEnumerator.MoveNextAsync());

public void Reset()
=> throw new InvalidOperationException("Can not reset an async enumerable.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public AsyncEnumeratorWrapper(AsyncEnumerableShapeInfo shape, object asyncEnumer
public object? Current => _getCurrentMethod.Invoke();

public void Dispose()
=> AsyncToSyncAdapter.Await(() => _disposeAsyncMethod.Invoke(_asyncEnumerator, null));
=> AsyncToSyncAdapter.Await(context: null, () => _disposeAsyncMethod.Invoke(_asyncEnumerator, null));

public bool MoveNext()
=> AsyncToSyncAdapter.Await<bool>(() => _moveNextAsyncMethod.Invoke());
=> AsyncToSyncAdapter.Await<bool>(context: null, () => _moveNextAsyncMethod.Invoke());

public void Reset()
=> throw new InvalidOperationException("Can not reset an async enumerable.");
Expand Down
6 changes: 0 additions & 6 deletions src/NUnitFramework/framework/Internal/AsyncToSyncAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public static bool IsAsyncOperation(Delegate @delegate)
return IsAsyncOperation(@delegate.GetMethodInfo());
}

public static object? Await(Func<object?> invoke)
=> Await<object?>(default(TestExecutionContext), invoke);

public static TResult? Await<TResult>(Func<object?> invoke)
=> Await<TResult>(default(TestExecutionContext), invoke);

public static object? Await(TestExecutionContext? context, Func<object?> invoke)
=> Await<object?>(context, invoke);

Expand Down
4 changes: 2 additions & 2 deletions src/NUnitFramework/framework/Internal/DisposeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public static void EnsureDisposed(object? value)
#if NETFRAMEWORK
if (TryGetAsyncDispose(value.GetType(), out var method))
{
AsyncToSyncAdapter.Await(() => method.Invoke(value, null));
AsyncToSyncAdapter.Await(context: null, () => method.Invoke(value, null));
}
#else
if (value is IAsyncDisposable asyncDisposable)
{
AsyncToSyncAdapter.Await(() => asyncDisposable.DisposeAsync());
AsyncToSyncAdapter.Await(context: null, () => asyncDisposable.DisposeAsync());
}
#endif
else if (value is IDisposable disposable)
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitFramework/framework/Internal/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private static List<Exception> FlattenExceptionHierarchy(Exception exception)
{
try
{
AsyncToSyncAdapter.Await(parameterlessDelegate.DynamicInvokeWithTransparentExceptions);
AsyncToSyncAdapter.Await(TestExecutionContext.CurrentContext, parameterlessDelegate.DynamicInvokeWithTransparentExceptions);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static class MethodInfoExtensions
public static TReturn? InvokeMaybeAwait<TReturn>(this MethodInfo m, object?[]? methodArgs)
{
if (AsyncToSyncAdapter.IsAsyncOperation(m))
return (TReturn?)AsyncToSyncAdapter.Await(() => m.Invoke(null, methodArgs));
return (TReturn?)AsyncToSyncAdapter.Await(context: null, () => m.Invoke(null, methodArgs));
return (TReturn?)m.Invoke(null, methodArgs);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NUnitFramework/tests/HelperConstraints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public override ConstraintResult ApplyTo<TActual>(TActual actual)
if (AsyncToSyncAdapter.IsAsyncOperation(@delegate))
{
stopwatch.Start();
AsyncToSyncAdapter.Await(() => @delegate.DynamicInvoke());
AsyncToSyncAdapter.Await(TestExecutionContext.CurrentContext, () => @delegate.DynamicInvoke());
stopwatch.Stop();
}
else
Expand Down

0 comments on commit 18f9331

Please sign in to comment.