Skip to content

Commit

Permalink
chore: Add CancellationToken to ExposeHostPortsAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Jun 21, 2023
1 parent 07f1808 commit b88f626
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Testcontainers/Configurations/TestcontainersSettings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -184,12 +185,16 @@ public static WaitHandle SettingsInitialized
=> ManualResetEvent.WaitHandle;

/// <inheritdoc cref="PortForwardingContainer.ExposeHostPortsAsync" />
public static async Task ExposeHostPortsAsync(params ushort[] ports)
public static Task ExposeHostPortsAsync(ushort port, CancellationToken ct = default)
=> ExposeHostPortsAsync(new[] { port }, ct);

/// <inheritdoc cref="PortForwardingContainer.ExposeHostPortsAsync" />
public static async Task ExposeHostPortsAsync(IEnumerable<ushort> ports, CancellationToken ct = default)
{
await PortForwardingContainer.Instance.StartAsync()
await PortForwardingContainer.Instance.StartAsync(ct)
.ConfigureAwait(false);

await PortForwardingContainer.Instance.ExposeHostPortsAsync(ports)
await PortForwardingContainer.Instance.ExposeHostPortsAsync(ports, ct)
.ConfigureAwait(false);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Testcontainers/Containers/PortForwarding.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace DotNet.Testcontainers.Containers
{
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Builders;
Expand Down Expand Up @@ -41,8 +43,9 @@ private PortForwardingContainer(PortForwardingConfiguration configuration, ILogg
/// Exposes the host ports using SSH port forwarding.
/// </summary>
/// <param name="ports">The host ports to forward.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A task that completes when the host ports are forwarded.</returns>
public Task ExposeHostPortsAsync(params ushort[] ports)
public Task ExposeHostPortsAsync(IEnumerable<ushort> ports, CancellationToken ct = default)
{
var sshClient = new SshClient(Hostname, GetMappedPublicPort(PortForwardingBuilder.SshdPort), _configuration.Username, _configuration.Password);
sshClient.Connect();
Expand Down

0 comments on commit b88f626

Please sign in to comment.