Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
"semanticCommitScope": "docker",
"versioning": "loose"
},
{
"description": "Track SQL Server CU/GDR releases on the ubuntu-22.04 base only; preview/RC/CTP tags never match. See microsoft/mssql-docker#974.",
"matchDatasources": ["docker"],
"matchPackageNames": ["mcr.microsoft.com/mssql/server"],
"versioning": "regex:^(?<major>\\d+)-CU(?<minor>\\d+)(-GDR(?<patch>\\d+))?-ubuntu-(?<compatibility>\\d+\\.\\d+)$"
},
{
"groupName": "Testcontainers packages",
"groupSlug": "testcontainers-packages",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
namespace NetEvolve.Pulse.Tests.Integration.Internals;

using DotNet.Testcontainers.Containers;
using Microsoft.Extensions.Logging.Abstractions;
using Testcontainers.MsSql;
using TUnit.Core.Interfaces;

public sealed class SqlServerContainerFixture : IAsyncDisposable, IAsyncInitializer
{
private readonly MsSqlContainer _container = new MsSqlBuilder(
/*dockerimage*/"mcr.microsoft.com/mssql/server:2025-RTM-ubuntu-24.04-preview"
)
.WithLogger(NullLogger.Instance)
.Build();
// SQL Server 2025 containers crash intermittently during startup (sqlpal/LSASS fatal error, Reason 0x00000002),
// more often when several start concurrently, as they do here (one per target framework). This happens on the
// ubuntu-24.04 and the ubuntu-22.04 base, see https://github.com/microsoft/mssql-docker/issues/974. A crashed
// container is replaced by a fresh one (bounded), and every retry is reported.
private const int MaxStartAttempts = 3;

public string ConnectionString => _container.GetConnectionString() + ";MultipleActiveResultSets=True;";
private MsSqlContainer? _container;

public ValueTask DisposeAsync() => _container.DisposeAsync();
public string ConnectionString =>
(
_container ?? throw new InvalidOperationException("The SQL Server container has not been started.")
).GetConnectionString() + ";MultipleActiveResultSets=True;";

public async Task InitializeAsync() => await _container.StartAsync().ConfigureAwait(false);
public ValueTask DisposeAsync() => _container?.DisposeAsync() ?? ValueTask.CompletedTask;

public async Task InitializeAsync()
{
for (var attempt = 1; attempt < MaxStartAttempts; attempt++)
{
var container = CreateContainer();
try
{
await container.StartAsync().ConfigureAwait(false);
_container = container;
return;
}
catch (ContainerNotRunningException ex)
{
await ReportCrashAsync(attempt, ex).ConfigureAwait(false);
await container.DisposeAsync().ConfigureAwait(false);
}
}

// Last attempt: let a further crash fail the tests with the container output.
_container = CreateContainer();
await _container.StartAsync().ConfigureAwait(false);
}

// Test output of passing runs is not printed by `dotnet test`, so a retry is also written to the GitHub Actions job
// summary (when running there) to keep the upstream crash rate observable.
private static async Task ReportCrashAsync(int attempt, ContainerNotRunningException ex)
{
var message =
$"[SqlServerContainerFixture] SQL Server container crashed during startup (attempt {attempt}/{MaxStartAttempts}, {TestHelper.TargetFramework}), starting a new one.";
await Console.Error.WriteLineAsync($"{message}{Environment.NewLine}{ex.Message}").ConfigureAwait(false);

var summary = Environment.GetEnvironmentVariable("GITHUB_STEP_SUMMARY");
if (!string.IsNullOrEmpty(summary))
{
await File.AppendAllTextAsync(
summary,
$"> [!WARNING]{Environment.NewLine}> {message}{Environment.NewLine}{Environment.NewLine}"
)
.ConfigureAwait(false);
}
}

private static MsSqlContainer CreateContainer()
{
var builder = new MsSqlBuilder(
/*dockerimage*/"mcr.microsoft.com/mssql/server:2025-CU9-ubuntu-22.04"
);
return builder.WithLogger(NullLogger.Instance).Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ await _adminChannel
ExchangeName,
ExchangeType.Fanout,
durable: false,
autoDelete: true,
// Not auto-delete: each test gets its own instance and connection, and closing the previous
// one removes its exclusive queue's binding, which would asynchronously delete the exchange
// between this declare and the next test's QueueBind (404 NOT_FOUND).
autoDelete: false,
cancellationToken: cancellationToken
)
.ConfigureAwait(false);
Expand Down
Loading