Skip to content

Commit be37ce4

Browse files
committed
Simplify TestContainerExtensions.WrapException into StartAsync(failureMessage)
1 parent 5a8419d commit be37ce4

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

src/Dibix.Testing/TestContainers/TestContainerExtensions.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,16 @@ public static async Task ThrowOnNonZeroExitCode(this IContainer container)
8080
throw await WrapException($"Container exited with exit code {exitCode}", container).ConfigureAwait(false);
8181
}
8282

83-
public static async Task<Exception> WrapException(TimeoutException exception, IContainer container, string serviceName, int port, TimeSpan timeout)
83+
public static async Task StartAsync(this IContainer container, string failureMessage)
8484
{
85-
string message = $"{serviceName} did not respond on port {port} within the given timeout: {timeout}";
86-
return await WrapException(message, container, exception).ConfigureAwait(false);
87-
}
88-
public static async Task<Exception> WrapException(string message, IContainer container, Exception innerException = null)
89-
{
90-
return new InvalidOperationException($"""
91-
{message}
92-
-
93-
{await GetErrors(container).ConfigureAwait(false)}
94-
-
95-
Image: {container.Image.FullName}
96-
Name: {(Exists(container) ? container.Name : null)}
97-
Health: {container.Health}
98-
State: {container.State}
99-
""", innerException);
85+
try
86+
{
87+
await container.StartAsync().ConfigureAwait(false);
88+
}
89+
catch (TimeoutException timeoutException)
90+
{
91+
throw await WrapException(failureMessage, container, timeoutException).ConfigureAwait(false);
92+
}
10093
}
10194

10295
public static string GenerateContainerName(this IImage image)
@@ -123,6 +116,21 @@ await logger.WriteLineAsync($"""
123116
""").ConfigureAwait(false);
124117
}
125118

119+
private static async Task<Exception> WrapException(string message, IContainer container, Exception innerException = null)
120+
{
121+
return new InvalidOperationException($"""
122+
{message}
123+
-
124+
{await GetErrors(container).ConfigureAwait(false)}
125+
-
126+
Name: {(Exists(container) ? container.Name : null)}
127+
Image: {container.Image.FullName}
128+
State: {container.State}
129+
Health: {container.Health}
130+
HealthCheckFailingStreak: {container.HealthCheckFailingStreak}
131+
""", innerException);
132+
}
133+
126134
private static async Task<string> GetErrors(IContainer container)
127135
{
128136
(_, string stdErr) = await container.GetLogsAsync().ConfigureAwait(false);

tests/Dibix.Dapper.Tests/Environment/ContainerServices.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ private static async Task<MsSqlServerContainerInstance> CreateMsSqlServer(TextWr
6161
MsSqlContainer container = builder.Build();
6262

6363
await builder.LogDockerRunDebugStatement(logger).ConfigureAwait(false);
64-
try
65-
{
66-
await container.StartAsync().ConfigureAwait(false);
67-
}
68-
catch (TimeoutException exception)
69-
{
70-
throw await TestContainerExtensions.WrapException("Container did not start in time", container, exception).ConfigureAwait(false);
71-
}
64+
await container.StartAsync(failureMessage: "Container did not start in time").ConfigureAwait(false);
7265
await container.ExecScriptAsync(initializeDatabaseScript).ConfigureAwait(false);
7366
await logger.WriteLineAsync("Container is ready").ConfigureAwait(false);
7467

0 commit comments

Comments
 (0)