Unsure if there is a bug when using ".WithPortBinding()" or if I'm not using TestContainers correctly #1345
-
Hello Everyone! I am trying to Use TestContainers in a DotNet project, specifically to run a PostgresSql instance for some Test. In order to simplify debugging I am trying to fix the selected port of the container using I get an error however after the first test runs. The second test class throws an exception. Then the third test works, The forth throws an exception, and so on. ` Message: Stack Trace: I'm not sure if this is a bug or if I am doing something wrong? I tried to create a minimal example to showcase the behavior here: https://github.com/stefanteodoropol/Testcontainer_Port_Bug_Demo/ I appreciate any help in advance regarding this issue! I have searched and have seen that there might be some issues with the PortBinding method, but I was under the impression, that those issues only regard running tests in parallel, but for me it seems this issue also happens when test are run sequentially.. Any ideas / thoughts / comments? Thank you! Cheers! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
This is not a bug. You cannot bind a host port more than once. If you create multiple containers and bind them to the same host port, the containers will fail to start. That's why we recommend using random assigned host ports: https://dotnet.testcontainers.org/api/create_docker_network/#exposing-container-ports-to-the-host. Edit: Simply do not assign fixed host ports. BTW, that's how modules are pre-configured. Your example simply needs:
|
Beta Was this translation helpful? Give feedback.
I see, then test fixtures behave differently than the collections in xUnit.net. I haven't used NUnit in a long time (and just assumed that).
I checked out your reproducer and took a look at it. It seems that the PostgreSQL client library caches the connection (using a connection pool). I'm not sure if there are other ways to disable pooling, but you can clear the pool using:
I noticed another issue in your reproducer. Remove the
[TestFixture]
attribute from the base class. Here's my updated diff that works:diff --git a/TestContainer_Port_Bug_Demo/BaseTest.cs b/TestContainer_Port_Bug_Demo/BaseTest.cs index b…