I have an integration test project (xunit) which currently relies on a hosted SQL database, and we would like to move this dependency into TestContainers so it can be 100% self-contained. We have a number of tests already running with it just fine, however TestContainers uses a
resource cleaning container called Ryuk to monitor and clean up after the tests have run. In Visual Studio and Rider's native test runners this works as expected: The Ryuk container is started, the SQL TestContainer(s) is initialized in the class fixture, tests finish running, SQL TestContainers are stopped/removed, then after 10 seconds the Ryuk container is stopped/removed. The last step is triggered when Ryuk stops detecting the host process and will stop/remove itself after a 10 seconds, otherwise it assumed more containers could be started that it needs to monitor.
However when I run these in NCrunch I noticed that A) a new instance of Ryuk are started each time the tests are run, and B) every instances of Ryuk is never get stopped/removed, so they will continue growing and consuming more resources.
I looked at the
documentation for atomicity, but that seems to be advice for atomicity between tests during a test run and does not track the state between test runs. I tried the "public static int TimesEnvironmentSetUp = 0;" example but each time the tests are run it resets it back to 0 which doesn't seem helpful in this scenario.. but maybe I am missing something here as well?
Here is a simplified example of my issue. Running this test multiple times will result in multiple docker containers being created that will not go away until NCrunch is restarted
Code:
using Microsoft.Data.SqlClient;
using Testcontainers.MsSql;
using Xunit;
namespace MyTestProject;
public class ApplicationFactory : IAsyncLifetime
{
public MsSqlContainer SqlContainer;
public async Task InitializeAsync()
{
SqlContainer = new MsSqlBuilder().Build();
await SqlContainer.StartAsync();
}
public async Task DisposeAsync() => await SqlContainer.DisposeAsync();
}
public class TestClass(ApplicationFactory factory) : IClassFixture<ApplicationFactory>
{
[Fact]
public void Should_verify_connection()
{
using var conn = new SqlConnection(factory.SqlContainer.GetConnectionString());
conn.Open();
conn.Close();
}
}
Package and environment versions:
- NCrunch 4.16.0.4
- Visual Studio 17.9.0
- xunit 2.7.0
- Testcontainers.MsSql 3.7.0
- MsSql docker image tag 2019-CU18-ubuntu-20.04