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
13 changes: 12 additions & 1 deletion EstateReportingAPI.IntegrationTests/ControllerTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ public virtual async ValueTask InitializeAsync()
await this.SetupStandingData();
}

public virtual ValueTask DisposeAsync() => default;
public virtual async ValueTask DisposeAsync()
{
this.Dispose();

if (this.context != null)
{
await this.context.DisposeAsync();
}

this.Client?.Dispose();
this.factory?.Dispose();
}

protected EstateManagementContext context;

Expand Down
54 changes: 51 additions & 3 deletions EstateReportingAPI.IntegrationTests/IntegrationTestEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
using Shared.IntegrationTesting;
Expand All @@ -12,6 +14,8 @@ internal sealed class IntegrationTestEnvironment
{
private static readonly SemaphoreSlim InitialisationLock = new(1, 1);
private static IntegrationTestEnvironment? Instance;
private static readonly string ScenarioName = $"IntegrationTests-{Process.GetCurrentProcess().Id}-{Guid.NewGuid():N}";
private static bool CleanupRegistered;

private IntegrationTestEnvironment(TestDockerHelper dockerHelper, int sqlPort)
{
Expand All @@ -38,15 +42,16 @@ public static async Task<IntegrationTestEnvironment> GetAsync()
return Instance;
}

string scenarioName = "IntegrationTests";
RegisterCleanupHandlers();

NlogLogger logger = new();
logger.Initialise(LogManager.GetLogger(scenarioName), scenarioName);
logger.Initialise(LogManager.GetLogger(ScenarioName), ScenarioName);
LogManager.AddHiddenAssembly(typeof(NlogLogger).Assembly);

TestDockerHelper dockerHelper = new();
dockerHelper.Logger = logger;

await dockerHelper.StartContainersForScenarioRun(scenarioName, DockerServices.SqlServer);
await dockerHelper.StartContainersForScenarioRun(ScenarioName, DockerServices.SqlServer);

int sqlPort = dockerHelper.GetSqlPort()
?? throw new InvalidOperationException("SQL Server container did not expose port 1433.");
Expand All @@ -59,4 +64,47 @@ public static async Task<IntegrationTestEnvironment> GetAsync()
InitialisationLock.Release();
}
}

private static void RegisterCleanupHandlers()
{
if (CleanupRegistered)
{
return;
}

CleanupRegistered = true;
AppDomain.CurrentDomain.ProcessExit += (_, _) => CleanupSafely().GetAwaiter().GetResult();
AssemblyLoadContext.Default.Unloading += _ => CleanupSafely().GetAwaiter().GetResult();
}

private static async Task CleanupSafely()
{
if (Instance == null)
{
return;
}

await InitialisationLock.WaitAsync().ConfigureAwait(false);
try
{
if (Instance == null)
{
return;
}

try
{
await Instance.DockerHelper.StopContainersForScenarioRun(DockerServices.None).ConfigureAwait(false);
}
catch (Exception ex)
{
LogManager.GetLogger(ScenarioName).Error(ex, "Failed to stop integration test containers during shutdown.");
}
}
finally
{
Instance = null;
InitialisationLock.Release();
}
}
}
Loading