Skip to content

Commit 2c4f171

Browse files
alexanderankinShaishav Parekh
and
Shaishav Parekh
authoredMar 9, 2024··
fix(mongodb): waiting for container to start (it was not waiting at all before?) (#461)
we were using this code to test if it was online or not:`MongoClient(self.get_connection_url())`, but that doesn't actually perform any connection, instead you have to do something like: ```python @wait_container_is_ready() def _connect(self): client = self.get_connection_client() # will raise pymongo.errors.ServerSelectionTimeoutError if no connection is established client.admin.command('ismaster') ``` thanks to @smparekh for pointing this out, in his PR: https://github.com/testcontainers/testcontainers-python/pull/80/files#diff-cf09f76f44db0af04c58ddb456ccae39f7e29ce1d9208acd5f514c0a7dccb646R78 this PR implements the workaround described in the PR: ```python @pytest.fixture(scope="session") def test_client(): # init mongo mongo_container = MongoDbContainer("mongo:4").start() wait_for_logs(mongo_container, 'waiting for connections on port 27017') ... Co-authored-by: Shaishav Parekh <sparekh@oneweb.net>
1 parent 5bef18a commit 2c4f171

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed
 

‎modules/mongodb/testcontainers/mongodb/__init__.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from testcontainers.core.generic import DbContainer
1919
from testcontainers.core.utils import raise_for_deprecated_parameter
20-
from testcontainers.core.waiting_utils import wait_container_is_ready
20+
from testcontainers.core.waiting_utils import wait_for_logs
2121

2222

2323
class MongoDbContainer(DbContainer):
@@ -81,9 +81,8 @@ def get_connection_url(self) -> str:
8181
port=self.port,
8282
)
8383

84-
@wait_container_is_ready()
85-
def _connect(self) -> MongoClient:
86-
return MongoClient(self.get_connection_url())
84+
def _connect(self) -> None:
85+
wait_for_logs(self, "Waiting for connections")
8786

8887
def get_connection_client(self) -> MongoClient:
89-
return self._connect()
88+
return MongoClient(self.get_connection_url())

0 commit comments

Comments
 (0)
Please sign in to comment.