Skip to content

Commit

Permalink
fix: Remove Sonar findings, enhance reuse docs
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Mar 3, 2024
1 parent 894319e commit d7b4f14
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
21 changes: 21 additions & 0 deletions docs/api/resource_reuse.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ _ = new ContainerBuilder()
.WithLabel("reuse-id", "WeatherForecast");
```

The current implementation considers the following resource configurations and their corresponding builder APIs when calculating the hash value.

- [ContainerConfiguration](https://github.com/testcontainers/testcontainers-dotnet/blob/develop/src/Testcontainers/Configurations/Containers/ContainerConfiguration.cs)
- Image
- Entrypoint
- Command
- Environments
- ExposedPorts
- PortBindings
- NetworkAliases
- ExtraHosts
- Labels
- [NetworkConfiguration](https://github.com/testcontainers/testcontainers-dotnet/blob/develop/src/Testcontainers/Configurations/Networks/NetworkConfiguration.cs)
- Name
- Labels
- [VolumeConfiguration](https://github.com/testcontainers/testcontainers-dotnet/blob/develop/src/Testcontainers/Configurations/Volumes/VolumeConfiguration.cs)
- Name
- Labels

By default, all module resource configurations are included. This works well for simple value and reference types that can be serialized and deserialized to JSON without custom converters. However, more complex resource configurations may require a custom converter to properly serialize and deserialize their values.

!!!warning

Reuse does not replace singleton implementations to improve test performance. Prefer proper shared instances according to your chosen test framework.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ public virtual string GetReuseHash()
{
var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(this, GetType());

#if NET6_0_OR_GREATER
return Convert.ToBase64String(SHA1.HashData(jsonUtf8Bytes));
#else
using (var sha1 = SHA1.Create())
{
return Convert.ToBase64String(sha1.ComputeHash(jsonUtf8Bytes));
}
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ public async Task<byte[]> GetAllBytesAsync(CancellationToken ct = default)
{
using (var httpClient = new HttpClient())
{
#if NET6_0_OR_GREATER
return await httpClient.GetByteArrayAsync(_uri, ct)
.ConfigureAwait(false);
#else
return await httpClient.GetByteArrayAsync(_uri)
.ConfigureAwait(false);
#endif
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Testcontainers/Containers/ResourceReaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private async Task MaintainRyukConnection(TaskCompletionSource<bool> ryukInitial
{
if (!TryGetEndpoint(out var host, out var port))
{
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), default(CancellationToken))
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.None)
.ConfigureAwait(false);

continue;
Expand Down Expand Up @@ -389,14 +389,14 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), ct)
{
_resourceReaperContainer.Logger.CanNotConnectToResourceReaper(SessionId, host, port, e);

await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), default(CancellationToken))
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.None)
.ConfigureAwait(false);
}
catch (Exception e)
{
_resourceReaperContainer.Logger.LostConnectionToResourceReaper(SessionId, host, port, e);

await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), default(CancellationToken))
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.None)
.ConfigureAwait(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Images/IgnoreFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private interface ISearchAndReplace<TToReplace>
public bool Accepts(string file)
{
var matches = _ignorePatterns.AsParallel().Where(ignorePattern => ignorePattern.Key.IsMatch(file)).ToArray();
return !matches.Any() || matches[matches.Length - 1].Value;
return matches.Length == 0 || matches[matches.Length - 1].Value;
}

/// <summary>
Expand Down

0 comments on commit d7b4f14

Please sign in to comment.