Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Running TestContainers leaving dangling containers every test run
cheil
#1 Posted : Monday, March 4, 2024 5:47:43 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/5/2019(UTC)
Posts: 7
Location: United States of America

Thanks: 4 times
Was thanked: 2 time(s) in 2 post(s)
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
Remco
#2 Posted : Monday, March 4, 2024 11:40:25 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 6,976

Thanks: 931 times
Was thanked: 1257 time(s) in 1170 post(s)
Hi, thanks for sharing this issue and for providing so much detail.

From how you've described this issue, I think the test containers are tied to the lifespan of the NCrunch test host processes. Because we often re-use these processes after test runs are completed, they tend to hang around for a while and the engine can often clean them up quite late (only when absolutely needed).

In your situation, it may be worth turning on the 'Terminate test runner tasks when all test execution is complete' setting (in your NCrunch performance settings). This will stop the engine from leaving the test runners hanging when the queue is clear. I'd be interested to hear if this solves the issue for you.

1 user thanked Remco for this useful post.
cheil on 3/5/2024(UTC)
cheil
#3 Posted : Tuesday, March 5, 2024 2:52:48 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/5/2019(UTC)
Posts: 7
Location: United States of America

Thanks: 4 times
Was thanked: 2 time(s) in 2 post(s)
That did the trick, thanks!
1 user thanked cheil for this useful post.
Remco on 3/5/2024(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.031 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download