Repository navigation
Update NuGet packages and share SQL Server across integration tests #584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Shared.IntegrationTesting; | ||
| using Shared.IntegrationTesting.TestContainers; | ||
| using Shared.Logger; | ||
| using NLog; | ||
|
|
||
| namespace EstateReportingAPI.IntegrationTests; | ||
|
|
||
| internal sealed class IntegrationTestEnvironment | ||
| { | ||
| private static readonly SemaphoreSlim InitialisationLock = new(1, 1); | ||
| private static IntegrationTestEnvironment? Instance; | ||
|
|
||
| private IntegrationTestEnvironment(TestDockerHelper dockerHelper, int sqlPort) | ||
| { | ||
| this.DockerHelper = dockerHelper; | ||
| this.SqlPort = sqlPort; | ||
| } | ||
|
|
||
| public TestDockerHelper DockerHelper { get; } | ||
|
|
||
| public int SqlPort { get; } | ||
|
|
||
| public static async Task<IntegrationTestEnvironment> GetAsync() | ||
| { | ||
| if (Instance != null) | ||
| { | ||
| return Instance; | ||
| } | ||
|
|
||
| await InitialisationLock.WaitAsync().ConfigureAwait(false); | ||
| try | ||
| { | ||
| if (Instance != null) | ||
| { | ||
| return Instance; | ||
| } | ||
|
|
||
| string scenarioName = "IntegrationTests"; | ||
| NlogLogger logger = new(); | ||
| logger.Initialise(LogManager.GetLogger(scenarioName), scenarioName); | ||
| LogManager.AddHiddenAssembly(typeof(NlogLogger).Assembly); | ||
|
|
||
| TestDockerHelper dockerHelper = new(); | ||
| dockerHelper.Logger = logger; | ||
|
|
||
| await dockerHelper.StartContainersForScenarioRun(scenarioName, DockerServices.SqlServer); | ||
|
|
||
| int sqlPort = dockerHelper.GetSqlPort() | ||
| ?? throw new InvalidOperationException("SQL Server container did not expose port 1433."); | ||
|
|
||
| Instance = new IntegrationTestEnvironment(dockerHelper, sqlPort); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an integration-test process finishes, this static instance remains the sole owner of the Docker helper, but no assembly-level or process-level teardown calls Useful? React with 👍 / 👎. |
||
| return Instance; | ||
| } | ||
| finally | ||
| { | ||
| InitialisationLock.Release(); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each test creates a GUID-named database through
EnsureCreated, but xUnit invokes thisIAsyncLifetime.DisposeAsyncmethod rather than the separateDispose()method below, so making it a no-op leaves all 47 per-test databases in the shared SQL Server for the remainder of the run. Invoke the existing database cleanup (and dispose the client, factory, and context) here so a full suite does not continually accumulate databases and connections.Useful? React with 👍 / 👎.