test: guarantee driver fixture cleanup without masking failures
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Tests;
|
||||
|
||||
internal static class FixtureCleanup
|
||||
{
|
||||
internal const string SecondaryExceptionKey = "FixtureCleanup.SecondaryException";
|
||||
|
||||
internal static async Task RunAsync(Func<Task> body, Func<Task> cleanup, Action<Exception> report)
|
||||
{
|
||||
Exception? primary = null;
|
||||
try
|
||||
{
|
||||
await body();
|
||||
}
|
||||
catch (Exception failure)
|
||||
{
|
||||
primary = failure;
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
await cleanup();
|
||||
}
|
||||
catch (Exception secondary) when (primary is not null)
|
||||
{
|
||||
try { primary.Data[SecondaryExceptionKey] = secondary; } catch { }
|
||||
try { report(secondary); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user