Skip to content

Commit

Permalink
chore: Prevent ReusableResourceTest from leaking resources (#1164)
Browse files Browse the repository at this point in the history
Co-authored-by: Andre Hofmeister <[email protected]>
  • Loading branch information
0xced and HofmeisterAn authored Apr 27, 2024
1 parent 619233a commit d5552d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0"/>
<PackageVersion Include="Microsoft.Bcl.HashCode" Version="1.1.1"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4"/>
<PackageVersion Include="ReflectionMagic" Version="5.0.1"/>
<PackageVersion Include="SharpZipLib" Version="1.4.2"/>
<PackageVersion Include="SSH.NET" Version="2023.0.0"/>
<PackageVersion Include="System.Text.Json" Version="6.0.9"/>
Expand Down
26 changes: 19 additions & 7 deletions tests/Testcontainers.Platform.Linux.Tests/ReusableResourceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ await Task.WhenAll(container.StartAsync(), network.CreateAsync(), volume.CreateA

public Task DisposeAsync()
{
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()
Expand All @@ -76,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing"/>
<PackageReference Include="coverlet.collector"/>
<PackageReference Include="ReflectionMagic"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="xunit"/>
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tests/Testcontainers.Platform.Linux.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit d5552d0

Please sign in to comment.