'Parallel test execution and growing active connections to mongodb

I have an application with thousands of tests through MockMvc. The tests are running in band, so 1 at a time and whole process takes hours. I decided to try parallel test run with

systemProperties["junit.jupiter.execution.parallel.enabled"] = true
systemProperties["junit.jupiter.execution.parallel.mode.default"] = "concurrent"

It runs relativly ok (some minor issues to solve later), but over time number of active connections to mongodb grows. Once it reaches 170 (171st) connections, mongo fails.

I cant find the reason for the connections to stay active. So I decided to tackle the issue with some limits to connection pool and added ?maxLifeTimeMS=1000&maxPoolSize=5&maxIdleTimeMS=1000 settings to connection string. It helped a lot, instead of mongo failure in 2 minutes, it fails after 12 minutes. I believe the only affecting parameter here is the maxLifeTimeMS but any lower value just slows down the tests significantly and does not help. There are still growing number of active connections. I can try to "fix" it on the other side and add some more connections in mongo. But I am curious, why are there so many active connections.

I suspect the failed test somehows still keeps the connection open, so I wanted to close connection in AfterEach method with

mongoTemplate.getDb().getMongo().close()

From StackOverflow. But getDb() does not return object with getMongo() and all my attepts to access connection pool through available objects to close the connections failed. Do you know how to reduce the number of connection? Properly close them? Or how to approach this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source