From c009b6490bd7fe66e25e2e938dfa0f836129cfa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Sat, 27 Apr 2024 01:35:03 +0200 Subject: [PATCH 1/3] chore: Cleanup resources in ReusableResourceTest Fixes #1157 --- Directory.Packages.props | 1 + .../ReusableResourceTest.cs | 3 +++ .../Testcontainers.Platform.Linux.Tests.csproj | 1 + tests/Testcontainers.Platform.Linux.Tests/Usings.cs | 1 + 4 files changed, 6 insertions(+) diff --git a/Directory.Packages.props b/Directory.Packages.props index 039256594..a00645801 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -10,6 +10,7 @@ + diff --git a/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs b/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs index 6a6f1a228..bfb4429ee 100644 --- a/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs +++ b/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs @@ -51,6 +51,9 @@ await Task.WhenAll(container.StartAsync(), network.CreateAsync(), volume.CreateA public Task DisposeAsync() { + // We don't want to leak resources, but .WithReuse(true).WithCleanUp(true) is not supported. + // So we cheat by setting a non-empty SessionId in order DisposeAsyncCore to delete the container, network and volume. + new List { 0, 1, 2 }.ForEach(i => _disposables[i].AsDynamic()._configuration.SessionId = ResourceReaper.DefaultSessionId); return Task.WhenAll(_disposables.Select(disposable => disposable.DisposeAsync().AsTask())); } diff --git a/tests/Testcontainers.Platform.Linux.Tests/Testcontainers.Platform.Linux.Tests.csproj b/tests/Testcontainers.Platform.Linux.Tests/Testcontainers.Platform.Linux.Tests.csproj index 5cde0f59f..d7c3cd709 100644 --- a/tests/Testcontainers.Platform.Linux.Tests/Testcontainers.Platform.Linux.Tests.csproj +++ b/tests/Testcontainers.Platform.Linux.Tests/Testcontainers.Platform.Linux.Tests.csproj @@ -8,6 +8,7 @@ + diff --git a/tests/Testcontainers.Platform.Linux.Tests/Usings.cs b/tests/Testcontainers.Platform.Linux.Tests/Usings.cs index 7674d6b7e..ea641a068 100644 --- a/tests/Testcontainers.Platform.Linux.Tests/Usings.cs +++ b/tests/Testcontainers.Platform.Linux.Tests/Usings.cs @@ -20,4 +20,5 @@ global using JetBrains.Annotations; global using Microsoft.Extensions.Logging.Abstractions; global using Microsoft.Extensions.Logging.Testing; +global using ReflectionMagic; global using Xunit; \ No newline at end of file From f0117d57e4682840dcf1655e0ac5c53c57f75eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Sat, 27 Apr 2024 14:48:29 +0200 Subject: [PATCH 2/3] Improve setting the session id for all resources Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> --- .../ReusableResourceTest.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs b/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs index bfb4429ee..be80eb2b9 100644 --- a/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs +++ b/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs @@ -51,10 +51,14 @@ await Task.WhenAll(container.StartAsync(), network.CreateAsync(), volume.CreateA public Task DisposeAsync() { - // We don't want to leak resources, but .WithReuse(true).WithCleanUp(true) is not supported. - // So we cheat by setting a non-empty SessionId in order DisposeAsyncCore to delete the container, network and volume. - new List { 0, 1, 2 }.ForEach(i => _disposables[i].AsDynamic()._configuration.SessionId = ResourceReaper.DefaultSessionId); - return Task.WhenAll(_disposables.Select(disposable => disposable.DisposeAsync().AsTask())); + return Task.WhenAll(_disposables.Select(disposable => + { + // We do not want to leak resources, but `WithCleanUp(true)` cannot be used + // alongside `WithReuse(true)`. As a workaround, we set the `SessionId` using + // reflection afterward to delete the container, network, and volume. + disposable.AsDynamic()._configuration.SessionId = ResourceReaper.DefaultSessionId; + return disposable.DisposeAsync().AsTask(); + })); } public void Dispose() From 2945036b157ec1a34deab33729291562b30f9044 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Sat, 27 Apr 2024 15:30:59 +0200 Subject: [PATCH 3/3] fix: Move Containers...DifferentHashes to its own test collection --- .../ReusableResourceTest.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs b/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs index be80eb2b9..0c209feb6 100644 --- a/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs +++ b/tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs @@ -83,13 +83,18 @@ public async Task ShouldReuseExistingResource() Assert.Single(response.Volumes); } - [Fact] - public void ContainersWithDifferentNamesShouldHaveDifferentHashes() + public static class ReuseHash { - var hash1 = new ReuseHashContainerBuilder().WithName("Name1").GetReuseHash(); - var hash2 = new ReuseHashContainerBuilder().WithName("Name2").GetReuseHash(); - - Assert.NotEqual(hash1, hash2); + public sealed class NotEqual + { + [Fact] + public void ForDifferentNames() + { + var hash1 = new ReuseHashContainerBuilder().WithName("Name1").GetReuseHash(); + var hash2 = new ReuseHashContainerBuilder().WithName("Name2").GetReuseHash(); + Assert.NotEqual(hash1, hash2); + } + } } public static class UnsupportedBuilderConfigurationTest