Skip to content

Commit

Permalink
docs: Add reuse docs for Network and VolumeBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Jan 22, 2024
1 parent d339d16 commit 1736001
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
10 changes: 9 additions & 1 deletion docs/api/resource_reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ _ = new ContainerBuilder()
.WithReuse(true);
```

The reuse implementation does not currently consider (support) all builder APIs when calculating the hash value. Therefore, collisions may occur. To prevent collisions, simply use a distinct label to identify the resource.
The reuse implementation does currently not consider (support) all builder APIs when calculating the hash value. Therefore, collisions may occur. To prevent collisions, simply use a distinct label to identify the resource.

```csharp title="Label container resource to identify it"
_ = new ContainerBuilder()
Expand All @@ -20,3 +20,11 @@ _ = new ContainerBuilder()
Reuse does not replace singleton implementations to improve test performance. Prefer proper shared instances according to your chosen test framework.

Calling `Dispose()` on a reusable container will stop it. Testcontainers will automatically start it in the next test run. This will assign a new random host port. Some services (e.g. Kafka) require the random assigned host port on the initial configuration. This may interfere with the new random assigned host port.

Keep in mind that reuse depends on stable builder and resource configurations. If a configuration changes during a test run, the reuse hash will also change, and Testcontainers cannot pick up the resource again. For example, the network and volume builder assign a random name to the resource; this default configuration will not work without overriding and setting the name to a fixed value.

```csharp title="Override NetworkBuilder's default random network name configuration"
_ = new NetworkBuilder()
.WithReuse(true)
.WithName("WeatherForecast");
```
1 change: 1 addition & 0 deletions src/Testcontainers/Networks/DockerNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ await _client.Network.DeleteAsync(_network.ID, ct)
}
catch (DockerApiException)
{
// Ignore exception for resources that do not exist anymore.
}
finally
{
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers/Volumes/DockerVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ await _client.Volume.DeleteAsync(Name, ct)
}
catch (DockerApiException)
{
// Ignore exception for resources that do not exist anymore.
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ public sealed class WaitUntilHttpRequestIsSucceededTest : IAsyncLifetime
.WithPortBinding(HttpPort, true)
.Build();

public static IEnumerable<object[]> GetHttpWaitStrategies()
public static TheoryData<HttpWaitStrategy> GetHttpWaitStrategies()
{
yield return new object[] { new HttpWaitStrategy() };
yield return new object[] { new HttpWaitStrategy().ForPort(HttpPort) };
yield return new object[] { new HttpWaitStrategy().ForStatusCode(HttpStatusCode.OK) };
yield return new object[] { new HttpWaitStrategy().ForStatusCodeMatching(statusCode => HttpStatusCode.OK.Equals(statusCode)) };
yield return new object[] { new HttpWaitStrategy().ForResponseMessageMatching(response => Task.FromResult(response.IsSuccessStatusCode)) };
yield return new object[] { new HttpWaitStrategy().ForStatusCode(HttpStatusCode.MovedPermanently).ForStatusCodeMatching(statusCode => HttpStatusCode.OK.Equals(statusCode)) };
var theoryData = new TheoryData<HttpWaitStrategy>();
theoryData.Add(new HttpWaitStrategy());
theoryData.Add(new HttpWaitStrategy().ForPort(HttpPort));
theoryData.Add(new HttpWaitStrategy().ForStatusCode(HttpStatusCode.OK));
theoryData.Add(new HttpWaitStrategy().ForStatusCodeMatching(statusCode => HttpStatusCode.OK.Equals(statusCode)));
theoryData.Add(new HttpWaitStrategy().ForResponseMessageMatching(response => Task.FromResult(response.IsSuccessStatusCode)));
theoryData.Add(new HttpWaitStrategy().ForStatusCode(HttpStatusCode.MovedPermanently).ForStatusCodeMatching(statusCode => HttpStatusCode.OK.Equals(statusCode)));
return theoryData;
}

public Task InitializeAsync()
Expand Down

0 comments on commit 1736001

Please sign in to comment.