Skip to content

Commit 9ada654

Browse files
authored
PYTHON-3174 Remove noisy running Topology check for main test client (#898)
1 parent 087950d commit 9ada654

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

test/__init__.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,8 @@ def hello(self):
286286
return self._hello
287287

288288
def _connect(self, host, port, **kwargs):
289-
# Jython takes a long time to connect.
290-
if sys.platform.startswith("java"):
291-
timeout_ms = 10000
292-
else:
293-
timeout_ms = 5000
294289
kwargs.update(self.default_client_options)
295-
client = pymongo.MongoClient(host, port, serverSelectionTimeoutMS=timeout_ms, **kwargs)
290+
client = pymongo.MongoClient(host, port, serverSelectionTimeoutMS=5000, **kwargs)
296291
try:
297292
try:
298293
client.admin.command(HelloCompat.LEGACY_CMD) # Can we connect?
@@ -1037,21 +1032,26 @@ def _get_executors(topology):
10371032
return [e for e in executors if e is not None]
10381033

10391034

1040-
def all_executors_stopped(topology):
1035+
def print_running_topology(topology):
10411036
running = [e for e in _get_executors(topology) if not e._stopped]
10421037
if running:
10431038
print(
1044-
" Topology %s has THREADS RUNNING: %s, created at: %s"
1045-
% (topology, running, topology._settings._stack)
1039+
"WARNING: found Topology with running threads:\n"
1040+
" Threads: %s\n"
1041+
" Topology: %s\n"
1042+
" Creation traceback:\n%s" % (running, topology, topology._settings._stack)
10461043
)
1047-
return False
1048-
return True
10491044

10501045

1051-
def print_unclosed_clients():
1046+
def print_running_clients():
10521047
from pymongo.topology import Topology
10531048

10541049
processed = set()
1050+
# Avoid false positives on the main test client.
1051+
# XXX: Can be removed after PYTHON-1634 or PYTHON-1896.
1052+
c = client_context.client
1053+
if c:
1054+
processed.add(c._topology._topology_id)
10551055
# Call collect to manually cleanup any would-be gc'd clients to avoid
10561056
# false positives.
10571057
gc.collect()
@@ -1061,7 +1061,7 @@ def print_unclosed_clients():
10611061
# Avoid printing the same Topology multiple times.
10621062
if obj._topology_id in processed:
10631063
continue
1064-
all_executors_stopped(obj)
1064+
print_running_topology(obj)
10651065
processed.add(obj._topology_id)
10661066
except ReferenceError:
10671067
pass
@@ -1086,9 +1086,7 @@ def teardown():
10861086
c.drop_database("pymongo_test_bernie")
10871087
c.close()
10881088

1089-
# Jython does not support gc.get_objects.
1090-
if not sys.platform.startswith("java"):
1091-
print_unclosed_clients()
1089+
print_running_clients()
10921090

10931091

10941092
class PymongoTestRunner(unittest.TextTestRunner):

test/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ def test_list_databases(self):
759759
for doc in helper_docs:
760760
self.assertIs(type(doc), dict)
761761
client = rs_or_single_client(document_class=SON)
762+
self.addCleanup(client.close)
762763
for doc in client.list_databases():
763764
self.assertIs(type(doc), dict)
764765

@@ -979,6 +980,7 @@ def test_unix_socket(self):
979980
uri = "mongodb://%s" % encoded_socket
980981
# Confirm we can do operations via the socket.
981982
client = rs_or_single_client(uri)
983+
self.addCleanup(client.close)
982984
client.pymongo_test.test.insert_one({"dummy": "object"})
983985
dbs = client.list_database_names()
984986
self.assertTrue("pymongo_test" in dbs)
@@ -1002,6 +1004,7 @@ def test_document_class(self):
10021004
self.assertFalse(isinstance(db.test.find_one(), SON))
10031005

10041006
c = rs_or_single_client(document_class=SON)
1007+
self.addCleanup(c.close)
10051008
db = c.pymongo_test
10061009

10071010
self.assertEqual(SON, c.codec_options.document_class)
@@ -1040,6 +1043,7 @@ def test_socket_timeout(self):
10401043
no_timeout = self.client
10411044
timeout_sec = 1
10421045
timeout = rs_or_single_client(socketTimeoutMS=1000 * timeout_sec)
1046+
self.addCleanup(timeout.close)
10431047

10441048
no_timeout.pymongo_test.drop_collection("test")
10451049
no_timeout.pymongo_test.test.insert_one({"x": 1})
@@ -1095,6 +1099,7 @@ def test_tz_aware(self):
10951099
self.assertRaises(ValueError, MongoClient, tz_aware="foo")
10961100

10971101
aware = rs_or_single_client(tz_aware=True)
1102+
self.addCleanup(aware.close)
10981103
naive = self.client
10991104
aware.pymongo_test.drop_collection("test")
11001105

@@ -1124,6 +1129,7 @@ def test_ipv6(self):
11241129
uri += "/?replicaSet=" + (client_context.replica_set_name or "")
11251130

11261131
client = rs_or_single_client_noauth(uri)
1132+
self.addCleanup(client.close)
11271133
client.pymongo_test.test.insert_one({"dummy": "object"})
11281134
client.pymongo_test_bernie.test.insert_one({"dummy": "object"})
11291135

@@ -1222,6 +1228,7 @@ def test_operation_failure(self):
12221228
# to avoid race conditions caused by replica set failover or idle
12231229
# socket reaping.
12241230
client = single_client()
1231+
self.addCleanup(client.close)
12251232
client.pymongo_test.test.find_one()
12261233
pool = get_pool(client)
12271234
socket_count = len(pool.sockets)
@@ -1245,18 +1252,21 @@ def test_lazy_connect_w0(self):
12451252
self.addCleanup(client_context.client.drop_database, "test_lazy_connect_w0")
12461253

12471254
client = rs_or_single_client(connect=False, w=0)
1255+
self.addCleanup(client.close)
12481256
client.test_lazy_connect_w0.test.insert_one({})
12491257
wait_until(
12501258
lambda: client.test_lazy_connect_w0.test.count_documents({}) == 1, "find one document"
12511259
)
12521260

12531261
client = rs_or_single_client(connect=False, w=0)
1262+
self.addCleanup(client.close)
12541263
client.test_lazy_connect_w0.test.update_one({}, {"$set": {"x": 1}})
12551264
wait_until(
12561265
lambda: client.test_lazy_connect_w0.test.find_one().get("x") == 1, "update one document"
12571266
)
12581267

12591268
client = rs_or_single_client(connect=False, w=0)
1269+
self.addCleanup(client.close)
12601270
client.test_lazy_connect_w0.test.delete_one({})
12611271
wait_until(
12621272
lambda: client.test_lazy_connect_w0.test.count_documents({}) == 0, "delete one document"
@@ -1267,6 +1277,7 @@ def test_exhaust_network_error(self):
12671277
# When doing an exhaust query, the socket stays checked out on success
12681278
# but must be checked in on error to avoid semaphore leaks.
12691279
client = rs_or_single_client(maxPoolSize=1, retryReads=False)
1280+
self.addCleanup(client.close)
12701281
collection = client.pymongo_test.test
12711282
pool = get_pool(client)
12721283
pool._check_interval_seconds = None # Never check.

0 commit comments

Comments
 (0)