Skip to content

Commit

Permalink
feat(#715): Prepare HTTP wait strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Dec 20, 2022
1 parent 768dfd2 commit 1922cc1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Threading.Tasks;
using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging;

public sealed class HttpWaitStrategy : IWaitUntil
{
private readonly UriBuilder uriBuilder = new UriBuilder(Uri.UriSchemeHttp, "127.0.0.1");

public Task<bool> Until(ITestcontainersContainer testcontainers, ILogger logger)
{
return Task.FromResult(false);
}

public HttpWaitStrategy ForPath(string path)
{
this.uriBuilder.Path = path;
return this;
}

public HttpWaitStrategy UsingTls(bool tlsEnabled = true)
{
this.uriBuilder.Scheme = tlsEnabled ? Uri.UriSchemeHttps : Uri.UriSchemeHttp;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace DotNet.Testcontainers.Configurations
using JetBrains.Annotations;

/// <summary>
/// Collection of pre-configured strategies to wait until the Testcontainer is up and running.
/// Waits until all wait strategies are completed.
/// Collection of pre-configured strategies to wait until the container is up and running.
/// </summary>
[PublicAPI]
public interface IWaitForContainerOS
Expand Down Expand Up @@ -42,6 +41,14 @@ public interface IWaitForContainerOS
[PublicAPI]
IWaitForContainerOS UntilCommandIsCompleted(params string[] command);

/// <summary>
/// Waits until the port is available.
/// </summary>
/// <param name="port">The port to be checked.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilPortIsAvailable(int port);

/// <summary>
/// Waits until the file exists.
/// </summary>
Expand Down Expand Up @@ -70,12 +77,12 @@ public interface IWaitForContainerOS
IWaitForContainerOS UntilOperationIsSucceeded(Func<bool> operation, int maxCallCount);

/// <summary>
/// Waits until the port is available.
/// Waits until the http request is completed successfully.
/// </summary>
/// <param name="port">The port to be checked.</param>
/// <param name="request">The http request to be executed.</param>
/// <returns>A configured instance of <see cref="IWaitForContainerOS" />.</returns>
[PublicAPI]
IWaitForContainerOS UntilPortIsAvailable(int port);
IWaitForContainerOS UntilHttpRequestIsSucceeded(Func<HttpWaitStrategy, HttpWaitStrategy> request);

/// <summary>
/// Waits until the container is healthy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ public virtual IWaitForContainerOS UntilOperationIsSucceeded(Func<bool> operatio
}

/// <inheritdoc />
public virtual IWaitForContainerOS UntilContainerIsHealthy(long failingStreak = 20)
public virtual IWaitForContainerOS UntilHttpRequestIsSucceeded(Func<HttpWaitStrategy, HttpWaitStrategy> request)
{
return this.AddCustomWaitStrategy(request.Invoke(new HttpWaitStrategy()));
}

/// <inheritdoc />
public virtual IWaitForContainerOS UntilContainerIsHealthy(long failingStreak = 3)
{
return this.AddCustomWaitStrategy(new UntilContainerIsHealthy(failingStreak));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ internal sealed class WaitForContainerUnix : WaitForContainerOS
/// <inheritdoc />
public override IWaitForContainerOS UntilCommandIsCompleted(string command)
{
this.AddCustomWaitStrategy(new UntilUnixCommandIsCompleted(command));
return this;
return this.AddCustomWaitStrategy(new UntilUnixCommandIsCompleted(command));
}

/// <inheritdoc />
public override IWaitForContainerOS UntilCommandIsCompleted(params string[] command)
{
this.AddCustomWaitStrategy(new UntilUnixCommandIsCompleted(command));
return this;
return this.AddCustomWaitStrategy(new UntilUnixCommandIsCompleted(command));
}

/// <inheritdoc />
public override IWaitForContainerOS UntilPortIsAvailable(int port)
{
this.AddCustomWaitStrategy(new UntilUnixPortIsAvailable(port));
return this;
return this.AddCustomWaitStrategy(new UntilUnixPortIsAvailable(port));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ internal sealed class WaitForContainerWindows : WaitForContainerOS
/// <inheritdoc />
public override IWaitForContainerOS UntilCommandIsCompleted(string command)
{
this.AddCustomWaitStrategy(new UntilWindowsCommandIsCompleted(command));
return this;
return this.AddCustomWaitStrategy(new UntilWindowsCommandIsCompleted(command));
}

/// <inheritdoc />
public override IWaitForContainerOS UntilCommandIsCompleted(params string[] command)
{
this.AddCustomWaitStrategy(new UntilWindowsCommandIsCompleted(command));
return this;
return this.AddCustomWaitStrategy(new UntilWindowsCommandIsCompleted(command));
}

/// <inheritdoc />
public override IWaitForContainerOS UntilPortIsAvailable(int port)
{
this.AddCustomWaitStrategy(new UntilWindowsPortIsAvailable(port));
return this;
return this.AddCustomWaitStrategy(new UntilWindowsPortIsAvailable(port));
}
}
}

0 comments on commit 1922cc1

Please sign in to comment.