Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src-ThirdParty/NUnitLite/Internal/AsyncInvocationRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace NUnit.Framework.Internal
{
Expand Down Expand Up @@ -99,17 +100,14 @@ public override object WaitForPendingOperationsToComplete(object invocationResul

private class AsyncTaskInvocationRegion : AsyncInvocationRegion
{
private const string TaskWaitMethod = "Wait";
private const string TaskResultProperty = "Result";
private const string SystemAggregateException = "System.AggregateException";
private const string InnerExceptionsProperty = "InnerExceptions";
private const BindingFlags TaskResultPropertyBindingFlags = BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public;

public override object WaitForPendingOperationsToComplete(object invocationResult)
{
try
{
invocationResult.GetType().GetMethod(TaskWaitMethod, new Type[0]).Invoke(invocationResult, null);
if (invocationResult is Task task)
{
task.Wait();
}
}
catch (TargetInvocationException e)
{
Expand All @@ -118,18 +116,14 @@ public override object WaitForPendingOperationsToComplete(object invocationResul
PreserveStackTrace(innerExceptions[0]);
throw innerExceptions[0];
}

PropertyInfo taskResultProperty = invocationResult.GetType().GetProperty(TaskResultProperty, TaskResultPropertyBindingFlags);

return taskResultProperty != null ? taskResultProperty.GetValue(invocationResult, null) : invocationResult;
return invocationResult;
}

private static IList<Exception> GetAllExceptions(Exception exception)
{
if (SystemAggregateException.Equals(exception.GetType().FullName))
return (IList<Exception>)exception.GetType().GetProperty(InnerExceptionsProperty).GetValue(exception, null);

return new Exception[] { exception };
if (exception is AggregateException ae)
return ae.InnerExceptions;
return [ exception ];
}
}
}
Expand Down
Loading