From e8c01bcfa37bdfb77706cdad050b484e52cc44e8 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:12:44 +0100 Subject: [PATCH 1/2] chore: Prepare next release cycle --- Directory.Build.props | 2 +- .../ElasticsearchContainer.cs | 2 +- .../WebDriverContainer.cs | 6 +- .../DbContainerFixture.cs | 2 +- src/Testcontainers.Xunit/DbContainerTest.cs | 2 +- .../DbContainerTestMethods.cs | 2 +- .../IDbContainerTestMethods.cs | 2 +- .../MTlsEndpointAuthenticationProvider.cs | 8 +-- .../Clients/TestcontainersClient.cs | 5 -- .../Commons/ResourceConfiguration.cs | 2 +- .../Volumes/UriResourceMapping.cs | 2 +- .../WaitStrategies/WaitStrategy.cs | 5 ++ .../Containers/ResourceReaper.cs | 33 ++------- .../Containers/TarOutputMemoryStream.cs | 9 +-- .../Polyfills/StreamExtensions.cs | 67 +++++++++++++++++++ 15 files changed, 95 insertions(+), 54 deletions(-) create mode 100644 src/Testcontainers/Polyfills/StreamExtensions.cs diff --git a/Directory.Build.props b/Directory.Build.props index b71381331..9fa624452 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ $(AssemblyName) - 4.17.0 + 4.18.0 $(Version) $(Version) Testcontainers diff --git a/src/Testcontainers.Elasticsearch/ElasticsearchContainer.cs b/src/Testcontainers.Elasticsearch/ElasticsearchContainer.cs index 74cfe51e1..ff713aa77 100644 --- a/src/Testcontainers.Elasticsearch/ElasticsearchContainer.cs +++ b/src/Testcontainers.Elasticsearch/ElasticsearchContainer.cs @@ -84,7 +84,7 @@ public async Task GetCertificateAsync(CancellationToken ct = d var certificateBytes = await ReadFileAsync(ElasticsearchBuilder.ElasticsearchHttpCaCertificateFilePath, ct) .ConfigureAwait(false); -#if NET5_0_OR_GREATER +#if NET6_0_OR_GREATER return X509Certificate2.CreateFromPem(Encoding.Default.GetString(certificateBytes)); #else return new X509Certificate2(certificateBytes); diff --git a/src/Testcontainers.WebDriver/WebDriverContainer.cs b/src/Testcontainers.WebDriver/WebDriverContainer.cs index 59e1185f0..b3b3e8fc4 100644 --- a/src/Testcontainers.WebDriver/WebDriverContainer.cs +++ b/src/Testcontainers.WebDriver/WebDriverContainer.cs @@ -57,11 +57,11 @@ public async Task ExportVideoAsync(string target, CancellationToken ct = default var bytes = await _ffmpegContainer.ReadFileAsync(WebDriverBuilder.VideoFilePath, ct) .ConfigureAwait(false); -#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER +#if NETSTANDARD2_0 + File.WriteAllBytes(target, bytes); +#else await File.WriteAllBytesAsync(target, bytes, ct) .ConfigureAwait(false); -#else - File.WriteAllBytes(target, bytes); #endif } diff --git a/src/Testcontainers.Xunit/DbContainerFixture.cs b/src/Testcontainers.Xunit/DbContainerFixture.cs index 33d8b0d08..8a1651a40 100644 --- a/src/Testcontainers.Xunit/DbContainerFixture.cs +++ b/src/Testcontainers.Xunit/DbContainerFixture.cs @@ -50,7 +50,7 @@ await base.DisposeAsyncCore() /// public DbConnection CreateConnection() => _testMethods.CreateConnection(); -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER /// public DbConnection OpenConnection() => _testMethods.OpenConnection(); diff --git a/src/Testcontainers.Xunit/DbContainerTest.cs b/src/Testcontainers.Xunit/DbContainerTest.cs index 4761e995c..7e97f0a30 100644 --- a/src/Testcontainers.Xunit/DbContainerTest.cs +++ b/src/Testcontainers.Xunit/DbContainerTest.cs @@ -49,7 +49,7 @@ await base.DisposeAsyncCore() /// public DbConnection CreateConnection() => _testMethods.CreateConnection(); -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER /// public DbConnection OpenConnection() => _testMethods.OpenConnection(); diff --git a/src/Testcontainers.Xunit/DbContainerTestMethods.cs b/src/Testcontainers.Xunit/DbContainerTestMethods.cs index ec798553b..ae793356d 100644 --- a/src/Testcontainers.Xunit/DbContainerTestMethods.cs +++ b/src/Testcontainers.Xunit/DbContainerTestMethods.cs @@ -5,7 +5,7 @@ internal sealed class DbContainerTestMethods(DbProviderFactory dbProviderFactory private readonly DbProviderFactory _dbProviderFactory = dbProviderFactory ?? throw new ArgumentNullException(nameof(dbProviderFactory)); private readonly Lazy _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER [CanBeNull] private DbDataSource _dbDataSource; private DbDataSource DbDataSource diff --git a/src/Testcontainers.Xunit/IDbContainerTestMethods.cs b/src/Testcontainers.Xunit/IDbContainerTestMethods.cs index a45e032c2..110b95401 100644 --- a/src/Testcontainers.Xunit/IDbContainerTestMethods.cs +++ b/src/Testcontainers.Xunit/IDbContainerTestMethods.cs @@ -17,7 +17,7 @@ internal interface IDbContainerTestMethods /// A new, closed connection to the database. DbConnection CreateConnection(); -#if NET8_0_OR_GREATER +#if NET7_0_OR_GREATER /// /// Returns a new, open connection to the database. /// diff --git a/src/Testcontainers/Builders/MTlsEndpointAuthenticationProvider.cs b/src/Testcontainers/Builders/MTlsEndpointAuthenticationProvider.cs index 7beb6d400..321157b02 100644 --- a/src/Testcontainers/Builders/MTlsEndpointAuthenticationProvider.cs +++ b/src/Testcontainers/Builders/MTlsEndpointAuthenticationProvider.cs @@ -52,14 +52,14 @@ protected override X509Certificate2 GetClientCertificate() // The certificate must be exported to PFX on Windows to avoid "No credentials are available in the security package": // https://stackoverflow.com/questions/72096812/loading-x509certificate2-from-pem-file-results-in-no-credentials-are-available/72101855#72101855. -#if NETSTANDARD - return Polyfills.X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath); -#elif NET9_0_OR_GREATER +#if NET9_0_OR_GREATER var certificate = X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath); return OperatingSystem.IsWindows() ? X509CertificateLoader.LoadPkcs12(certificate.Export(X509ContentType.Pfx), null) : certificate; -#elif NET6_0_OR_GREATER +#elif NET5_0_OR_GREATER var certificate = X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath); return OperatingSystem.IsWindows() ? new X509Certificate2(certificate.Export(X509ContentType.Pfx)) : certificate; +#else + return Polyfills.X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath); #endif } } diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index 9f2c561a1..c00188ea5 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -297,13 +297,8 @@ public async Task ReadFileAsync(string id, string filePath, Cancellation var readBytes = new byte[entry.Size]; -#if NETSTANDARD2_0 - _ = await tarInputStream.ReadAsync(readBytes, 0, readBytes.Length, ct) - .ConfigureAwait(false); -#else _ = await tarInputStream.ReadAsync(readBytes, ct) .ConfigureAwait(false); -#endif return readBytes; } diff --git a/src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs b/src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs index 05e203af9..1d4ccfcdf 100644 --- a/src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs +++ b/src/Testcontainers/Configurations/Commons/ResourceConfiguration.cs @@ -103,7 +103,7 @@ public virtual string GetReuseHash() var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(this, GetType(), DefaultJsonSerializerOptions.Instance); -#if NET6_0_OR_GREATER +#if NET5_0_OR_GREATER return Convert.ToBase64String(SHA1.HashData(jsonUtf8Bytes)); #else using (var sha1 = SHA1.Create()) diff --git a/src/Testcontainers/Configurations/Volumes/UriResourceMapping.cs b/src/Testcontainers/Configurations/Volumes/UriResourceMapping.cs index f29688fbf..c44e15e87 100644 --- a/src/Testcontainers/Configurations/Volumes/UriResourceMapping.cs +++ b/src/Testcontainers/Configurations/Volumes/UriResourceMapping.cs @@ -66,7 +66,7 @@ public async Task GetAllBytesAsync(CancellationToken ct = default) { using (var httpClient = new HttpClient()) { -#if NET6_0_OR_GREATER +#if NET5_0_OR_GREATER return await httpClient.GetByteArrayAsync(_uri, ct) .ConfigureAwait(false); #else diff --git a/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs b/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs index 82e03f14a..8af7c79cf 100644 --- a/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs +++ b/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs @@ -199,7 +199,12 @@ await waitTask { // Stop the task that is still pending. Either the timeout, or the // evaluation of the condition that did not complete in time. +#if NET8_0_OR_GREATER + await linkedCts.CancelAsync() + .ConfigureAwait(false); +#else linkedCts.Cancel(); +#endif } } diff --git a/src/Testcontainers/Containers/ResourceReaper.cs b/src/Testcontainers/Containers/ResourceReaper.cs index d3cfce079..c65a9baa1 100644 --- a/src/Testcontainers/Containers/ResourceReaper.cs +++ b/src/Testcontainers/Containers/ResourceReaper.cs @@ -211,7 +211,7 @@ public async ValueTask DisposeAsync() try { -#if NET6_0_OR_GREATER +#if NET8_0_OR_GREATER await _maintainConnectionCts.CancelAsync() .ConfigureAwait(false); #else @@ -314,13 +314,8 @@ private static async Task TryRegisterFilterAsync(NetworkStream stream, str var readBytes = new byte[AcknowledgementBufferSizeInBytes]; -#if NETSTANDARD2_0 - await stream.WriteAsync(sendBytes, 0, sendBytes.Length, ct) - .ConfigureAwait(false); -#else await stream.WriteAsync(sendBytes, ct) .ConfigureAwait(false); -#endif await stream.FlushAsync(ct) .ConfigureAwait(false); @@ -331,13 +326,8 @@ await stream.FlushAsync(ct) do { -#if NETSTANDARD2_0 - var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, ct) - .ConfigureAwait(false); -#else var numberOfBytes = await stream.ReadAsync(readBytes, ct) .ConfigureAwait(false); -#endif if (numberOfBytes == 0) { @@ -352,25 +342,15 @@ await stream.FlushAsync(ct) if (indexOfNewLine == -1) { // We have not received the entire message yet. Read from stream again. -#if NETSTANDARD2_0 - await messageBuffer.WriteAsync(readBytes, 0, numberOfBytes, ct) - .ConfigureAwait(false); -#else await messageBuffer.WriteAsync(readBytes.AsMemory(0, numberOfBytes), ct) .ConfigureAwait(false); -#endif hasAcknowledge = false; } else { -#if NETSTANDARD2_0 - await messageBuffer.WriteAsync(readBytes, 0, indexOfNewLine, ct) - .ConfigureAwait(false); -#else await messageBuffer.WriteAsync(readBytes.AsMemory(0, indexOfNewLine), ct) .ConfigureAwait(false); -#endif var buffer = messageBuffer.GetBuffer(); @@ -408,7 +388,7 @@ private async Task TryConnectAndRegisterFilterAsync(string filter, Cancell try { -#if NET6_0_OR_GREATER +#if NET5_0_OR_GREATER await tcpClient.ConnectAsync(host, port, ct) .ConfigureAwait(false); #else @@ -485,10 +465,12 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken. try { -#if NET6_0_OR_GREATER +#if NET5_0_OR_GREATER await tcpClient.ConnectAsync(host, port, ct) .ConfigureAwait(false); #else + // The overload that takes a cancellation token is not available. + // The loop checks the cancellation token before each connection attempt. await tcpClient.ConnectAsync(host, port) .ConfigureAwait(false); #endif @@ -520,13 +502,8 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), ct) while (!_maintainConnectionCts.IsCancellationRequested) { // Keep the connection to Ryuk up. -#if NETSTANDARD2_0 - var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, _maintainConnectionCts.Token) - .ConfigureAwait(false); -#else var numberOfBytes = await stream.ReadAsync(readBytes, _maintainConnectionCts.Token) .ConfigureAwait(false); -#endif if (numberOfBytes == 0) { diff --git a/src/Testcontainers/Containers/TarOutputMemoryStream.cs b/src/Testcontainers/Containers/TarOutputMemoryStream.cs index cc702d869..7285b70ef 100644 --- a/src/Testcontainers/Containers/TarOutputMemoryStream.cs +++ b/src/Testcontainers/Containers/TarOutputMemoryStream.cs @@ -79,13 +79,10 @@ public async Task AddAsync(IResourceMapping resourceMapping, CancellationToken c await PutNextEntryAsync(tarEntry, ct) .ConfigureAwait(false); -#if NETSTANDARD2_0 - await WriteAsync(fileContent, 0, fileContent.Length, ct) + // Keep the explicit `this`. C# binds extension methods only to an explicit + // receiver, and .NET Standard 2.0 resolves `WriteAsync` to the polyfill. + await this.WriteAsync(fileContent, ct) .ConfigureAwait(false); -#else - await WriteAsync(fileContent, ct) - .ConfigureAwait(false); -#endif await CloseEntryAsync(ct) .ConfigureAwait(false); diff --git a/src/Testcontainers/Polyfills/StreamExtensions.cs b/src/Testcontainers/Polyfills/StreamExtensions.cs new file mode 100644 index 000000000..98a692476 --- /dev/null +++ b/src/Testcontainers/Polyfills/StreamExtensions.cs @@ -0,0 +1,67 @@ +#if NETSTANDARD2_0 +namespace DotNet.Testcontainers +{ + using System; + using System.IO; + using System.Runtime.InteropServices; + using System.Threading; + using System.Threading.Tasks; + + /// + /// Provides extension methods for the class + /// that .NET Standard 2.0 does not support. + /// + internal static class StreamExtensions + { + /// + /// Asynchronously reads a sequence of bytes from the stream into the buffer. + /// + /// The stream to read from. + /// The buffer to write the data into. + /// Cancellation token. + /// + /// A task that completes when the bytes have been read, returning the number + /// of bytes read into the buffer. + /// + public static async ValueTask ReadAsync(this Stream stream, Memory buffer, CancellationToken ct = default) + { + if (MemoryMarshal.TryGetArray(buffer, out var segment)) + { + return await stream.ReadAsync(segment.Array, segment.Offset, segment.Count, ct) + .ConfigureAwait(false); + } + + var bytes = new byte[buffer.Length]; + + var numberOfBytes = await stream.ReadAsync(bytes, 0, bytes.Length, ct) + .ConfigureAwait(false); + + bytes.AsSpan(0, numberOfBytes).CopyTo(buffer.Span); + return numberOfBytes; + } + + /// + /// Asynchronously writes a sequence of bytes from the buffer to the stream. + /// + /// The stream to write to. + /// The buffer to read the data from. + /// Cancellation token. + /// A task that completes when the bytes have been written. + public static async ValueTask WriteAsync(this Stream stream, ReadOnlyMemory buffer, CancellationToken ct = default) + { + if (MemoryMarshal.TryGetArray(buffer, out var segment)) + { + await stream.WriteAsync(segment.Array, segment.Offset, segment.Count, ct) + .ConfigureAwait(false); + } + else + { + var bytes = buffer.ToArray(); + + await stream.WriteAsync(bytes, 0, bytes.Length, ct) + .ConfigureAwait(false); + } + } + } +} +#endif From 3a51a22e3e38f0be79e0806f7dca509515997f37 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:24:19 +0100 Subject: [PATCH 2/2] docs: Align StreamExtensions XML comments with repository style --- src/Testcontainers/Polyfills/StreamExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Testcontainers/Polyfills/StreamExtensions.cs b/src/Testcontainers/Polyfills/StreamExtensions.cs index 98a692476..817af9e29 100644 --- a/src/Testcontainers/Polyfills/StreamExtensions.cs +++ b/src/Testcontainers/Polyfills/StreamExtensions.cs @@ -20,8 +20,8 @@ internal static class StreamExtensions /// The buffer to write the data into. /// Cancellation token. /// - /// A task that completes when the bytes have been read, returning the number - /// of bytes read into the buffer. + /// Task that completes when the bytes have been read, + /// returning the number of bytes read into the buffer. /// public static async ValueTask ReadAsync(this Stream stream, Memory buffer, CancellationToken ct = default) { @@ -46,7 +46,7 @@ public static async ValueTask ReadAsync(this Stream stream, Memory bu /// The stream to write to. /// The buffer to read the data from. /// Cancellation token. - /// A task that completes when the bytes have been written. + /// Task that completes when the bytes have been written. public static async ValueTask WriteAsync(this Stream stream, ReadOnlyMemory buffer, CancellationToken ct = default) { if (MemoryMarshal.TryGetArray(buffer, out var segment))