Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4530 from stevenaw/3043-async-dispose
Async disposal of test fixtures
- Loading branch information
Showing
10 changed files
with
214 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt | ||
|
||
using System; | ||
#if NETFRAMEWORK | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
#endif | ||
|
||
namespace NUnit.Framework.Internal | ||
{ | ||
internal static class DisposeHelper | ||
{ | ||
public static bool IsDisposable(Type type) | ||
{ | ||
if (typeof(IDisposable).IsAssignableFrom(type)) | ||
{ | ||
return true; | ||
} | ||
#if NETFRAMEWORK | ||
return TryGetAsyncDispose(type, out _); | ||
#else | ||
return typeof(IAsyncDisposable).IsAssignableFrom(type); | ||
#endif | ||
} | ||
|
||
public static void EnsureDisposed(object? value) | ||
{ | ||
if (value is not null) | ||
{ | ||
#if NETFRAMEWORK | ||
if (TryGetAsyncDispose(value.GetType(), out var method)) | ||
{ | ||
AsyncToSyncAdapter.Await(() => method.Invoke(value, null)); | ||
} | ||
#else | ||
if (value is IAsyncDisposable asyncDisposable) | ||
{ | ||
AsyncToSyncAdapter.Await(() => asyncDisposable.DisposeAsync()); | ||
} | ||
#endif | ||
else if (value is IDisposable disposable) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
#if NETFRAMEWORK | ||
private static bool TryGetAsyncDispose(Type type, [NotNullWhen(true)] out MethodInfo? method) | ||
{ | ||
method = null; | ||
|
||
var asyncDisposable = type.GetInterface("System.IAsyncDisposable"); | ||
if (asyncDisposable is null) | ||
return false; | ||
|
||
method = asyncDisposable.GetMethod("DisposeAsync", Type.EmptyTypes); | ||
return method is not null; | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters