Skip to content

Commit

Permalink
fix: Prevent race conditions in tar output stream test teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Jun 15, 2024
1 parent b37eb52 commit 08075b6
Showing 1 changed file with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -33,13 +59,13 @@ public void TestFileExistsInTarFile()
}

[UsedImplicitly]
public sealed class FromResourceMapping : TarOutputMemoryStreamTest, IResourceMapping, IClassFixture<FromResourceMapping.HttpFixture>, IAsyncLifetime, IDisposable
public sealed class FromResourceMapping : TarOutputMemoryStreamTest, IResourceMapping, IClassFixture<FromResourceMapping.HttpFixture>, 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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -174,7 +194,7 @@ public Task DisposeAsync()
}

[UsedImplicitly]
public sealed class FromFile : TarOutputMemoryStreamTest, IAsyncLifetime, IDisposable
public sealed class FromFile : TarOutputMemoryStreamTest, IAsyncLifetime
{
public Task InitializeAsync()
{
Expand All @@ -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()
{
Expand All @@ -205,12 +219,6 @@ public Task DisposeAsync()
{
return Task.CompletedTask;
}

public void Dispose()
{
_tarOutputMemoryStream.Dispose();
_testFile.Delete();
}
}

public sealed class UnixFileModeTest
Expand Down

0 comments on commit 08075b6

Please sign in to comment.