diff --git a/src/Testcontainers.Xunit/ContainerLifetime.cs b/src/Testcontainers.Xunit/ContainerLifetime.cs index a88ea88b0..a96caa133 100644 --- a/src/Testcontainers.Xunit/ContainerLifetime.cs +++ b/src/Testcontainers.Xunit/ContainerLifetime.cs @@ -36,10 +36,16 @@ public TContainerEntity Container #if !XUNIT_V3 /// - LifetimeTask IAsyncLifetime.DisposeAsync() => DisposeAsync(); + LifetimeTask IAsyncLifetime.DisposeAsync() => DisposeAsyncCore(); #else /// - LifetimeTask IAsyncDisposable.DisposeAsync() => DisposeAsync(); + async LifetimeTask IAsyncDisposable.DisposeAsync() + { + await DisposeAsyncCore() + .ConfigureAwait(false); + + GC.SuppressFinalize(this); + } #endif /// @@ -80,7 +86,7 @@ await Container.StartAsync() } /// - protected virtual async LifetimeTask DisposeAsync() + protected virtual async LifetimeTask DisposeAsyncCore() { if (_exception == null) { diff --git a/src/Testcontainers.Xunit/DbContainerFixture.cs b/src/Testcontainers.Xunit/DbContainerFixture.cs index 32c60fb15..c8e6ffbda 100644 --- a/src/Testcontainers.Xunit/DbContainerFixture.cs +++ b/src/Testcontainers.Xunit/DbContainerFixture.cs @@ -25,7 +25,7 @@ await base.InitializeAsync() } /// - protected override async LifetimeTask DisposeAsync() + protected override async LifetimeTask DisposeAsyncCore() { if (_testMethods != null) { @@ -33,7 +33,7 @@ await _testMethods.DisposeAsync() .ConfigureAwait(true); } - await base.DisposeAsync() + await base.DisposeAsyncCore() .ConfigureAwait(true); } diff --git a/src/Testcontainers.Xunit/DbContainerTest.cs b/src/Testcontainers.Xunit/DbContainerTest.cs index 907e29efc..aed622ebe 100644 --- a/src/Testcontainers.Xunit/DbContainerTest.cs +++ b/src/Testcontainers.Xunit/DbContainerTest.cs @@ -12,7 +12,6 @@ public abstract class DbContainerTest(ITestOut where TBuilderEntity : IContainerBuilder, new() where TContainerEntity : IContainer, IDatabaseContainer { - private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private DbContainerTestMethods _testMethods; /// @@ -25,7 +24,7 @@ await base.InitializeAsync() } /// - protected override async LifetimeTask DisposeAsync() + protected override async LifetimeTask DisposeAsyncCore() { if (_testMethods != null) { @@ -33,7 +32,7 @@ await _testMethods.DisposeAsync() .ConfigureAwait(true); } - await base.DisposeAsync() + await base.DisposeAsyncCore() .ConfigureAwait(true); }