Skip to content

Commit

Permalink
pytest-server-fixtures: Explicitly close initial Mongo client
Browse files Browse the repository at this point in the history
When checking the Mongo server has come up properly, the `MongoTestServer`
class first creates a `pymongo.MongoClient` with a short timeout to avoid
waiting a long time in the failure case. After this it creates another
client with the default timeouts, but prior to this change, the initial
client is not closed.

This change explicitly closes the initial client after use.
  • Loading branch information
lwfitzgerald committed Jan 25, 2022
1 parent d02ad10 commit df2eb58
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pytest-server-fixtures/pytest_server_fixtures/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def check_server_up(self):

log.info("Connecting to Mongo at %s:%s" % (self.hostname, self.port))
try:
self.api = pymongo.MongoClient(self.hostname, self.port,
serverselectiontimeoutms=200)
self.api.list_database_names()
with pymongo.MongoClient(self.hostname, self.port, serverselectiontimeoutms=200) as initial_api:
initial_api.list_database_names()

# Configure the client with default timeouts in case the server goes slow
self.api = pymongo.MongoClient(self.hostname, self.port)
return True
Expand Down

0 comments on commit df2eb58

Please sign in to comment.