forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test restarting multiple times on all containers
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
tests/Testcontainers.Databases.Tests/ContainerRestartTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters