Skip to content

Commit 615f5cc

Browse files
feat(EventHubs, ServiceBus): Replace wait strategy with HTTP health check (#1398)
1 parent 8056b24 commit 615f5cc

File tree

5 files changed

+23
-54
lines changed

5 files changed

+23
-54
lines changed

src/Testcontainers.Db2/Db2Container.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task<ExecResult> ExecScriptAsync(string scriptContent, Cancellation
4242
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
4343
.ConfigureAwait(false);
4444

45-
return await ExecAsync(new [] { "/bin/sh", "-c", db2ShellCommand}, ct)
45+
return await ExecAsync(new[] { "/bin/sh", "-c", db2ShellCommand}, ct)
4646
.ConfigureAwait(false);
4747
}
4848
}

src/Testcontainers.EventHubs/EventHubsBuilder.cs

+5-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public sealed class EventHubsBuilder : ContainerBuilder<EventHubsBuilder, EventH
1212

1313
public const ushort EventHubsPort = 5672;
1414

15+
public const ushort EventHubsHttpPort = 5300;
16+
1517
/// <summary>
1618
/// Initializes a new instance of the <see cref="EventHubsBuilder" /> class.
1719
/// </summary>
@@ -135,9 +137,9 @@ protected override EventHubsBuilder Init()
135137
return base.Init()
136138
.WithImage(EventHubsImage)
137139
.WithPortBinding(EventHubsPort, true)
138-
.WithWaitStrategy(Wait.ForUnixContainer()
139-
.UntilMessageIsLogged("Emulator Service is Successfully Up!")
140-
.AddCustomWaitStrategy(new WaitTwoSeconds()));
140+
.WithPortBinding(EventHubsHttpPort, true)
141+
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
142+
request.ForPort(EventHubsHttpPort).ForPath("/health")));
141143
}
142144

143145
/// <inheritdoc />
@@ -157,22 +159,4 @@ protected override EventHubsBuilder Merge(EventHubsConfiguration oldValue, Event
157159
{
158160
return new EventHubsBuilder(new EventHubsConfiguration(oldValue, newValue));
159161
}
160-
161-
/// <inheritdoc cref="IWaitUntil" />
162-
/// <remarks>
163-
/// This is a workaround to ensure that the wait strategy does not indicate
164-
/// readiness too early:
165-
/// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533.
166-
/// </remarks>
167-
private sealed class WaitTwoSeconds : IWaitUntil
168-
{
169-
/// <inheritdoc />
170-
public async Task<bool> UntilAsync(IContainer container)
171-
{
172-
await Task.Delay(TimeSpan.FromSeconds(2))
173-
.ConfigureAwait(false);
174-
175-
return true;
176-
}
177-
}
178162
}

src/Testcontainers.ServiceBus/ServiceBusBuilder.cs

+6-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public sealed class ServiceBusBuilder : ContainerBuilder<ServiceBusBuilder, Serv
1212

1313
public const ushort ServiceBusPort = 5672;
1414

15+
public const ushort ServiceBusHttpPort = 5300;
16+
1517
/// <summary>
1618
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
1719
/// </summary>
@@ -115,9 +117,10 @@ protected override ServiceBusBuilder Init()
115117
return base.Init()
116118
.WithImage(ServiceBusImage)
117119
.WithPortBinding(ServiceBusPort, true)
118-
.WithWaitStrategy(Wait.ForUnixContainer()
119-
.UntilMessageIsLogged("Emulator Service is Successfully Up!")
120-
.AddCustomWaitStrategy(new WaitTwoSeconds()));
120+
.WithPortBinding(ServiceBusHttpPort, true)
121+
.WithEnvironment("SQL_WAIT_INTERVAL", "0")
122+
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
123+
request.ForPort(ServiceBusHttpPort).ForPath("/health")));
121124
}
122125

123126
/// <inheritdoc />
@@ -137,22 +140,4 @@ protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, Ser
137140
{
138141
return new ServiceBusBuilder(new ServiceBusConfiguration(oldValue, newValue));
139142
}
140-
141-
/// <inheritdoc cref="IWaitUntil" />
142-
/// <remarks>
143-
/// This is a workaround to ensure that the wait strategy does not indicate
144-
/// readiness too early:
145-
/// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533.
146-
/// </remarks>
147-
private sealed class WaitTwoSeconds : IWaitUntil
148-
{
149-
/// <inheritdoc />
150-
public async Task<bool> UntilAsync(IContainer container)
151-
{
152-
await Task.Delay(TimeSpan.FromSeconds(2))
153-
.ConfigureAwait(false);
154-
155-
return true;
156-
}
157-
}
158143
}

src/Testcontainers/Containers/DockerContainer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ await _client.AttachAsync(_container.ID, _configuration.OutputConsumer, ct)
505505
await _client.StartAsync(_container.ID, ct)
506506
.ConfigureAwait(false);
507507

508-
_ = await CheckReadinessAsync(new [] { portBindingsMapped }, ct)
508+
_ = await CheckReadinessAsync(new[] { portBindingsMapped }, ct)
509509
.ConfigureAwait(false);
510510

511511
Starting?.Invoke(this, EventArgs.Empty);

src/Testcontainers/Images/DockerfileArchive.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ namespace DotNet.Testcontainers.Images
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7-
using System.Runtime.InteropServices;
87
using System.Text;
98
using System.Text.RegularExpressions;
109
using System.Threading;
@@ -150,7 +149,7 @@ public async Task<string> Tar(CancellationToken ct = default)
150149
{
151150
var entry = TarEntry.CreateTarEntry(relativeFilePath);
152151
entry.TarHeader.Size = inputStream.Length;
153-
entry.TarHeader.Mode = GetFileMode(absoluteFilePath);
152+
entry.TarHeader.Mode = GetUnixFileMode(absoluteFilePath);
154153

155154
await tarOutputStream.PutNextEntryAsync(entry, ct)
156155
.ConfigureAwait(false);
@@ -188,21 +187,22 @@ private static IEnumerable<string> GetFiles(string directory)
188187
}
189188

190189
/// <summary>
191-
/// Gets the file mode for a given file.
190+
/// Gets the Unix file mode of the file on the path.
192191
/// </summary>
193-
/// <param name="path">The path to the file.</param>
194-
/// <returns>The file mode for the <see cref="TarEntry"/></returns>
195-
private static int GetFileMode(string path)
192+
/// <param name="filePath">The path to the file.</param>
193+
/// <returns>The Unix file mode of the file on the path.</returns>
194+
private static int GetUnixFileMode(string filePath)
196195
{
197196
#if NET7_0_OR_GREATER
198-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
197+
if (!OperatingSystem.IsWindows())
199198
{
200-
return (int)File.GetUnixFileMode(path);
199+
return (int)File.GetUnixFileMode(filePath);
201200
}
202201
#endif
203202

204-
// Default to 755 for Windows and fallback to 755 for Unix when
205-
// `GetUnixFileMode` is not available.
203+
// Default to 755 for Windows and fall back to 755 for Unix when `GetUnixFileMode`
204+
// is not available.
205+
_ = filePath;
206206
return (int)Unix.FileMode755;
207207
}
208208
}

0 commit comments

Comments
 (0)