Skip to content

Commit

Permalink
Test restarting multiple times on all containers
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Jan 23, 2024
1 parent 1736001 commit 81dd2ff
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/Testcontainers.Databases.Tests/ContainerRestartTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
namespace Testcontainers;

public class ContainerRestartTest
{
[Theory]
[ClassData(typeof(ContainerBuilderTheoryData))]
public async Task RestartMultipleTimes(Type containerBuilderType)
{
var builder = Activator.CreateInstance(containerBuilderType);
var container = ((IContainer)builder!.GetType().GetMethod("Build")!.Invoke(builder, parameters: null))!;
try
{
// Given
var timeoutSource = new CancellationTokenSource(TimeSpan.FromSeconds(60));

// When
var exception = await RestartAsync(container, timeoutSource.Token);

// Then
Assert.Null(exception);
}
finally
{
await container.DisposeAsync();
}
}

private static async Task<Exception> RestartAsync(IContainer container, CancellationToken cancellationToken)
{
for (var i = 0; i < 3; i++)
{
var exception = await Record.ExceptionAsync(async () =>
{
await container.StartAsync(cancellationToken);
await container.StopAsync(cancellationToken);
});

if (exception != null)
{
return exception;
}
}

return null;
}

private class ContainerBuilderTheoryData : TheoryData<Type>
{
public ContainerBuilderTheoryData()
{
var containerBuilderTypes = Directory.GetFiles(".", "Testcontainers.*.Tests.dll", SearchOption.TopDirectoryOnly)
.Select(Path.GetFullPath)
.Select(Assembly.LoadFrom)
.SelectMany(assembly => assembly.GetReferencedAssemblies().Where(a => a.Name != null && a.Name.StartsWith("Testcontainers.") && !a.Name.StartsWith("Testcontainers.Commons")))
.Select(Assembly.Load)
.SelectMany(referencedAssembly => referencedAssembly.ExportedTypes)
.Where(type => type.BaseType?.IsGenericType == true && type.BaseType.GetGenericTypeDefinition() == typeof(ContainerBuilder<,,>))
.OrderBy(type => type.Name)
.ToArray();
AddRange(containerBuilderTypes);
}
}
}
3 changes: 3 additions & 0 deletions tests/Testcontainers.Databases.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
global using System.IO;
global using System.Linq;
global using System.Reflection;
global using System.Threading;
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Containers;
global using Xunit;

0 comments on commit 81dd2ff

Please sign in to comment.