From def62b9b5de180c785510c52b75ca3f0dbf89f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Sun, 11 Feb 2024 11:02:04 +0100 Subject: [PATCH] Test restarting multiple times on all containers --- .../ContainerRestartTest.cs | 63 +++++++++++++++++++ .../Testcontainers.Databases.Tests/Usings.cs | 3 + 2 files changed, 66 insertions(+) create mode 100644 tests/Testcontainers.Databases.Tests/ContainerRestartTest.cs diff --git a/tests/Testcontainers.Databases.Tests/ContainerRestartTest.cs b/tests/Testcontainers.Databases.Tests/ContainerRestartTest.cs new file mode 100644 index 000000000..dec00de6b --- /dev/null +++ b/tests/Testcontainers.Databases.Tests/ContainerRestartTest.cs @@ -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.FromMinutes(5)); + + // When + var exception = await RestartAsync(container, timeoutSource.Token); + + // Then + Assert.Null(exception); + } + finally + { + await container.DisposeAsync(); + } + } + + private static async Task 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 + { + 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); + } + } +} \ No newline at end of file diff --git a/tests/Testcontainers.Databases.Tests/Usings.cs b/tests/Testcontainers.Databases.Tests/Usings.cs index 2ea483b34..edd45bc4a 100644 --- a/tests/Testcontainers.Databases.Tests/Usings.cs +++ b/tests/Testcontainers.Databases.Tests/Usings.cs @@ -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; \ No newline at end of file