Skip to content

Commit d7b4f14

Browse files
committed
fix: Remove Sonar findings, enhance reuse docs
1 parent 894319e commit d7b4f14

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

docs/api/resource_reuse.md

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ _ = new ContainerBuilder()
1515
.WithLabel("reuse-id", "WeatherForecast");
1616
```
1717

18+
The current implementation considers the following resource configurations and their corresponding builder APIs when calculating the hash value.
19+
20+
- [ContainerConfiguration](https://github.com/testcontainers/testcontainers-dotnet/blob/develop/src/Testcontainers/Configurations/Containers/ContainerConfiguration.cs)
21+
- Image
22+
- Entrypoint
23+
- Command
24+
- Environments
25+
- ExposedPorts
26+
- PortBindings
27+
- NetworkAliases
28+
- ExtraHosts
29+
- Labels
30+
- [NetworkConfiguration](https://github.com/testcontainers/testcontainers-dotnet/blob/develop/src/Testcontainers/Configurations/Networks/NetworkConfiguration.cs)
31+
- Name
32+
- Labels
33+
- [VolumeConfiguration](https://github.com/testcontainers/testcontainers-dotnet/blob/develop/src/Testcontainers/Configurations/Volumes/VolumeConfiguration.cs)
34+
- Name
35+
- Labels
36+
37+
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.
38+
1839
!!!warning
1940

2041
Reuse does not replace singleton implementations to improve test performance. Prefer proper shared instances according to your chosen test framework.

src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs

+4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ public virtual string GetReuseHash()
8181
{
8282
var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(this, GetType());
8383

84+
#if NET6_0_OR_GREATER
85+
return Convert.ToBase64String(SHA1.HashData(jsonUtf8Bytes));
86+
#else
8487
using (var sha1 = SHA1.Create())
8588
{
8689
return Convert.ToBase64String(sha1.ComputeHash(jsonUtf8Bytes));
8790
}
91+
#endif
8892
}
8993
}
9094
}

src/Testcontainers/Configurations/Volumes/UriResourceMapping.cs

+5
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ public async Task<byte[]> GetAllBytesAsync(CancellationToken ct = default)
5252
{
5353
using (var httpClient = new HttpClient())
5454
{
55+
#if NET6_0_OR_GREATER
56+
return await httpClient.GetByteArrayAsync(_uri, ct)
57+
.ConfigureAwait(false);
58+
#else
5559
return await httpClient.GetByteArrayAsync(_uri)
5660
.ConfigureAwait(false);
61+
#endif
5762
}
5863
}
5964
}

src/Testcontainers/Containers/ResourceReaper.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private async Task MaintainRyukConnection(TaskCompletionSource<bool> ryukInitial
279279
{
280280
if (!TryGetEndpoint(out var host, out var port))
281281
{
282-
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), default(CancellationToken))
282+
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.None)
283283
.ConfigureAwait(false);
284284

285285
continue;
@@ -389,14 +389,14 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), ct)
389389
{
390390
_resourceReaperContainer.Logger.CanNotConnectToResourceReaper(SessionId, host, port, e);
391391

392-
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), default(CancellationToken))
392+
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.None)
393393
.ConfigureAwait(false);
394394
}
395395
catch (Exception e)
396396
{
397397
_resourceReaperContainer.Logger.LostConnectionToResourceReaper(SessionId, host, port, e);
398398

399-
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), default(CancellationToken))
399+
await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.None)
400400
.ConfigureAwait(false);
401401
}
402402
}

src/Testcontainers/Images/IgnoreFile.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private interface ISearchAndReplace<TToReplace>
136136
public bool Accepts(string file)
137137
{
138138
var matches = _ignorePatterns.AsParallel().Where(ignorePattern => ignorePattern.Key.IsMatch(file)).ToArray();
139-
return !matches.Any() || matches[matches.Length - 1].Value;
139+
return matches.Length == 0 || matches[matches.Length - 1].Value;
140140
}
141141

142142
/// <summary>

0 commit comments

Comments
 (0)