Skip to content

Commit 5cf055d

Browse files
authored
fix: Add HTTP wait strategy to prevent race-condition in WaitUntilHttpRequestIsSucceededTest (#1299)
1 parent 42df420 commit 5cf055d

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

.github/settings.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ branches:
175175
# Required. Require branches to be up to date before merging.
176176
strict: true
177177
# Required. The list of status checks to require in order to merge into this branch
178-
contexts: ["analyze (csharp)", "build (ubuntu-22.04)", "build (windows-2022)", "netlify/testcontainers-dotnet/deploy-preview"]
178+
contexts: ["Test Report", "analyze (csharp)"]
179179
# Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
180180
enforce_admins: false
181181
# Prevent merge commits from being pushed to matching branches
@@ -207,7 +207,7 @@ branches:
207207
# Required. Require branches to be up to date before merging.
208208
strict: true
209209
# Required. The list of status checks to require in order to merge into this branch
210-
contexts: ["analyze (csharp)", "build (ubuntu-22.04)", "build (windows-2022)", "netlify/testcontainers-dotnet/deploy-preview"]
210+
contexts: ["Test Report", "analyze (csharp)"]
211211
# Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
212212
enforce_admins: false
213213
# Prevent merge commits from being pushed to matching branches

tests/Testcontainers.Commons/CommonImages.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public static class CommonImages
77

88
public static readonly IImage Alpine = new DockerImage("alpine:3.17");
99

10+
public static readonly IImage Socat = new DockerImage("alpine/socat:1.8.0.0");
11+
1012
public static readonly IImage Curl = new DockerImage("curlimages/curl:8.00.1");
1113

1214
public static readonly IImage Nginx = new DockerImage("nginx:1.22");

tests/Testcontainers.Platform.Linux.Tests/TarOutputMemoryStreamTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ public sealed class HttpFixture : IAsyncLifetime
172172
private const ushort HttpPort = 80;
173173

174174
private readonly IContainer _container = new ContainerBuilder()
175-
.WithImage(CommonImages.Alpine)
176-
.WithEntrypoint("/bin/sh", "-c")
177-
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\r\n\" | nc -l -p {HttpPort}; done")
175+
.WithImage(CommonImages.Socat)
176+
.WithCommand("-v")
177+
.WithCommand($"TCP-LISTEN:{HttpPort},crlf,reuseaddr,fork")
178+
.WithCommand("EXEC:\"echo -e 'HTTP/1.1 200 OK'\n\n\"")
178179
.WithPortBinding(HttpPort, true)
179-
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
180-
request.ForPath("/")))
180+
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request))
181181
.Build();
182182

183183
public string BaseAddress

tests/Testcontainers.Tests/Unit/Configurations/WaitUntilHttpRequestIsSucceededTest.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public sealed class WaitUntilHttpRequestIsSucceededTest : IAsyncLifetime
1717
private const ushort HttpPort = 80;
1818

1919
private readonly IContainer _container = new ContainerBuilder()
20-
.WithImage(CommonImages.Alpine)
21-
.WithEntrypoint("/bin/sh", "-c")
22-
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\r\n\" | nc -l -p {HttpPort}; done")
20+
.WithImage(CommonImages.Socat)
21+
.WithCommand("-v")
22+
.WithCommand($"TCP-LISTEN:{HttpPort},crlf,reuseaddr,fork")
23+
.WithCommand("EXEC:\"echo -e 'HTTP/1.1 200 OK'\n\n\"")
2324
.WithPortBinding(HttpPort, true)
25+
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request))
2426
.Build();
2527

2628
public static TheoryData<HttpWaitStrategy> GetHttpWaitStrategies()
@@ -74,15 +76,15 @@ public async Task HttpWaitStrategySendsHeaders()
7476
await Task.Delay(TimeSpan.FromSeconds(1))
7577
.ConfigureAwait(true);
7678

77-
var (stdout, _) = await _container.GetLogsAsync()
79+
var (_, stderr) = await _container.GetLogsAsync()
7880
.ConfigureAwait(true);
7981

8082
// Then
8183
Assert.True(succeeded);
82-
Assert.Contains("Authorization", stdout);
83-
Assert.Contains("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", stdout);
84-
Assert.Contains(httpHeaders.First().Key, stdout);
85-
Assert.Contains(httpHeaders.First().Value, stdout);
84+
Assert.Contains("Authorization", stderr);
85+
Assert.Contains("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", stderr);
86+
Assert.Contains(httpHeaders.First().Key, stderr);
87+
Assert.Contains(httpHeaders.First().Value, stderr);
8688
}
8789

8890
[Fact]
@@ -104,13 +106,13 @@ public async Task HttpWaitStrategyUsesCustomHttpClientHandler()
104106
await Task.Delay(TimeSpan.FromSeconds(1))
105107
.ConfigureAwait(true);
106108

107-
var (stdout, _) = await _container.GetLogsAsync()
109+
var (_, stderr) = await _container.GetLogsAsync()
108110
.ConfigureAwait(true);
109111

110112
// Then
111113
Assert.True(succeeded);
112-
Assert.Contains("Cookie", stdout);
113-
Assert.Contains("Key1=Value1", stdout);
114+
Assert.Contains("Cookie", stderr);
115+
Assert.Contains("Key1=Value1", stderr);
114116
}
115117

116118
[Fact]

0 commit comments

Comments
 (0)