Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(EventHubs, ServiceBus): Replace wait strategy with HTTP health check #1398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Testcontainers.Db2/Db2Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<ExecResult> ExecScriptAsync(string scriptContent, Cancellation
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
.ConfigureAwait(false);

return await ExecAsync(new [] { "/bin/sh", "-c", db2ShellCommand}, ct)
return await ExecAsync(new[] { "/bin/sh", "-c", db2ShellCommand}, ct)
.ConfigureAwait(false);
}
}
26 changes: 5 additions & 21 deletions src/Testcontainers.EventHubs/EventHubsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public sealed class EventHubsBuilder : ContainerBuilder<EventHubsBuilder, EventH

public const ushort EventHubsPort = 5672;

public const ushort EventHubsHttpPort = 5300;

/// <summary>
/// Initializes a new instance of the <see cref="EventHubsBuilder" /> class.
/// </summary>
Expand Down Expand Up @@ -135,9 +137,9 @@ protected override EventHubsBuilder Init()
return base.Init()
.WithImage(EventHubsImage)
.WithPortBinding(EventHubsPort, true)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilMessageIsLogged("Emulator Service is Successfully Up!")
.AddCustomWaitStrategy(new WaitTwoSeconds()));
.WithPortBinding(EventHubsHttpPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPort(EventHubsHttpPort).ForPath("/health")));
}

/// <inheritdoc />
Expand All @@ -157,22 +159,4 @@ protected override EventHubsBuilder Merge(EventHubsConfiguration oldValue, Event
{
return new EventHubsBuilder(new EventHubsConfiguration(oldValue, newValue));
}

/// <inheritdoc cref="IWaitUntil" />
/// <remarks>
/// This is a workaround to ensure that the wait strategy does not indicate
/// readiness too early:
/// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533.
/// </remarks>
private sealed class WaitTwoSeconds : IWaitUntil
{
/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
await Task.Delay(TimeSpan.FromSeconds(2))
.ConfigureAwait(false);

return true;
}
}
}
27 changes: 6 additions & 21 deletions src/Testcontainers.ServiceBus/ServiceBusBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public sealed class ServiceBusBuilder : ContainerBuilder<ServiceBusBuilder, Serv

public const ushort ServiceBusPort = 5672;

public const ushort ServiceBusHttpPort = 5300;

/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusBuilder" /> class.
/// </summary>
Expand Down Expand Up @@ -115,9 +117,10 @@ protected override ServiceBusBuilder Init()
return base.Init()
.WithImage(ServiceBusImage)
.WithPortBinding(ServiceBusPort, true)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilMessageIsLogged("Emulator Service is Successfully Up!")
.AddCustomWaitStrategy(new WaitTwoSeconds()));
.WithPortBinding(ServiceBusHttpPort, true)
.WithEnvironment("SQL_WAIT_INTERVAL", "0")
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPort(ServiceBusHttpPort).ForPath("/health")));
}

/// <inheritdoc />
Expand All @@ -137,22 +140,4 @@ protected override ServiceBusBuilder Merge(ServiceBusConfiguration oldValue, Ser
{
return new ServiceBusBuilder(new ServiceBusConfiguration(oldValue, newValue));
}

/// <inheritdoc cref="IWaitUntil" />
/// <remarks>
/// This is a workaround to ensure that the wait strategy does not indicate
/// readiness too early:
/// https://github.com/Azure/azure-service-bus-emulator-installer/issues/35#issuecomment-2497164533.
/// </remarks>
private sealed class WaitTwoSeconds : IWaitUntil
{
/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
await Task.Delay(TimeSpan.FromSeconds(2))
.ConfigureAwait(false);

return true;
}
}
}
2 changes: 1 addition & 1 deletion src/Testcontainers/Containers/DockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ await _client.AttachAsync(_container.ID, _configuration.OutputConsumer, ct)
await _client.StartAsync(_container.ID, ct)
.ConfigureAwait(false);

_ = await CheckReadinessAsync(new [] { portBindingsMapped }, ct)
_ = await CheckReadinessAsync(new[] { portBindingsMapped }, ct)
.ConfigureAwait(false);

Starting?.Invoke(this, EventArgs.Empty);
Expand Down
20 changes: 10 additions & 10 deletions src/Testcontainers/Images/DockerfileArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace DotNet.Testcontainers.Images
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -150,7 +149,7 @@ public async Task<string> Tar(CancellationToken ct = default)
{
var entry = TarEntry.CreateTarEntry(relativeFilePath);
entry.TarHeader.Size = inputStream.Length;
entry.TarHeader.Mode = GetFileMode(absoluteFilePath);
entry.TarHeader.Mode = GetUnixFileMode(absoluteFilePath);

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

/// <summary>
/// Gets the file mode for a given file.
/// Gets the Unix file mode of the file on the path.
/// </summary>
/// <param name="path">The path to the file.</param>
/// <returns>The file mode for the <see cref="TarEntry"/></returns>
private static int GetFileMode(string path)
/// <param name="filePath">The path to the file.</param>
/// <returns>The Unix file mode of the file on the path.</returns>
private static int GetUnixFileMode(string filePath)
{
#if NET7_0_OR_GREATER
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
return (int)File.GetUnixFileMode(path);
return (int)File.GetUnixFileMode(filePath);
}
#endif

// Default to 755 for Windows and fallback to 755 for Unix when
// `GetUnixFileMode` is not available.
// Default to 755 for Windows and fall back to 755 for Unix when `GetUnixFileMode`
// is not available.
_ = filePath;
return (int)Unix.FileMode755;
}
}
Expand Down