From 08075b6a8a106add67d917a1eb4cf24fc4894122 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Sat, 15 Jun 2024 17:18:20 +0200 Subject: [PATCH] fix: Prevent race conditions in tar output stream test teardown --- .../TarOutputMemoryStreamTest.cs | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs b/tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs index 98ae58dd5..8c2bf7cb0 100644 --- a/tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs +++ b/tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs @@ -1,19 +1,45 @@ namespace Testcontainers.Tests; -public abstract class TarOutputMemoryStreamTest +public abstract class TarOutputMemoryStreamTest : IDisposable { private const string TargetDirectoryPath = "/tmp"; private readonly TarOutputMemoryStream _tarOutputMemoryStream = new TarOutputMemoryStream(TargetDirectoryPath, NullLogger.Instance); - private readonly FileInfo _testFile = new FileInfo(Path.Combine(TestSession.TempDirectoryPath, Path.GetRandomFileName())); + private readonly FileInfo _testFile = new FileInfo(Path.Combine(TestSession.TempDirectoryPath, Guid.NewGuid().ToString("D"), Path.GetRandomFileName())); + + private bool _disposed; protected TarOutputMemoryStreamTest() { + _ = Directory.CreateDirectory(_testFile.Directory!.FullName); + using var fileStream = _testFile.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite); fileStream.WriteByte(13); } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + if (disposing) + { + _tarOutputMemoryStream.Dispose(); + _testFile.Directory!.Delete(true); + } + + _disposed = true; + } + [Fact] public void TestFileExistsInTarFile() { @@ -33,13 +59,13 @@ public void TestFileExistsInTarFile() } [UsedImplicitly] - public sealed class FromResourceMapping : TarOutputMemoryStreamTest, IResourceMapping, IClassFixture, IAsyncLifetime, IDisposable + public sealed class FromResourceMapping : TarOutputMemoryStreamTest, IResourceMapping, IClassFixture, IAsyncLifetime { private readonly string _testHttpUri; private readonly string _testFileUri; - public FromResourceMapping(FromResourceMapping.HttpFixture httpFixture) + public FromResourceMapping(HttpFixture httpFixture) { _testHttpUri = httpFixture.BaseAddress; _testFileUri = new Uri(_testFile.FullName).ToString(); @@ -70,12 +96,6 @@ public Task DisposeAsync() return Task.CompletedTask; } - public void Dispose() - { - _tarOutputMemoryStream.Dispose(); - _testFile.Delete(); - } - Task IFutureResource.CreateAsync(CancellationToken ct) { return Task.CompletedTask; @@ -119,7 +139,7 @@ public async Task TestFileExistsInContainer() .WithEntrypoint(CommonCommands.SleepInfinity) .WithResourceMapping(_testFile, new FileInfo(targetFilePath1)) .WithResourceMapping(_testFile.FullName, targetDirectoryPath1) - .WithResourceMapping(_testFile.Directory.FullName, targetDirectoryPath2) + .WithResourceMapping(_testFile.Directory!.FullName, targetDirectoryPath2) .WithResourceMapping(_testHttpUri, targetFilePath2) .WithResourceMapping(_testFileUri, targetDirectoryPath3) .Build(); @@ -137,7 +157,7 @@ await container.CopyAsync(fileContent, targetFilePath3) await container.CopyAsync(_testFile.FullName, targetDirectoryPath4) .ConfigureAwait(true); - await container.CopyAsync(_testFile.Directory.FullName, targetDirectoryPath5) + await container.CopyAsync(_testFile.Directory!.FullName, targetDirectoryPath5) .ConfigureAwait(true); // Then @@ -174,7 +194,7 @@ public Task DisposeAsync() } [UsedImplicitly] - public sealed class FromFile : TarOutputMemoryStreamTest, IAsyncLifetime, IDisposable + public sealed class FromFile : TarOutputMemoryStreamTest, IAsyncLifetime { public Task InitializeAsync() { @@ -185,16 +205,10 @@ public Task DisposeAsync() { return Task.CompletedTask; } - - public void Dispose() - { - _tarOutputMemoryStream.Dispose(); - _testFile.Delete(); - } } [UsedImplicitly] - public sealed class FromDirectory : TarOutputMemoryStreamTest, IAsyncLifetime, IDisposable + public sealed class FromDirectory : TarOutputMemoryStreamTest, IAsyncLifetime { public Task InitializeAsync() { @@ -205,12 +219,6 @@ public Task DisposeAsync() { return Task.CompletedTask; } - - public void Dispose() - { - _tarOutputMemoryStream.Dispose(); - _testFile.Delete(); - } } public sealed class UnixFileModeTest