Skip to content

Commit 3fa1552

Browse files
committed
Convert test.test_monitor to async
1 parent c8d3afd commit 3fa1552

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

test/asynchronous/test_monitor.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2014-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test the monitor module."""
16+
from __future__ import annotations
17+
18+
import gc
19+
import subprocess
20+
import sys
21+
import warnings
22+
from functools import partial
23+
24+
sys.path[0:0] = [""]
25+
26+
from test.asynchronous import AsyncIntegrationTest, connected, unittest
27+
from test.utils import (
28+
ServerAndTopologyEventListener,
29+
async_wait_until,
30+
)
31+
32+
from pymongo.periodic_executor import _EXECUTORS
33+
34+
_IS_SYNC = False
35+
36+
def unregistered(ref):
37+
gc.collect()
38+
return ref not in _EXECUTORS
39+
40+
41+
def get_executors(client):
42+
executors = []
43+
for server in client._topology._servers.values():
44+
executors.append(server._monitor._executor)
45+
executors.append(server._monitor._rtt_monitor._executor)
46+
executors.append(client._kill_cursors_executor)
47+
executors.append(client._topology._Topology__events_executor)
48+
return [e for e in executors if e is not None]
49+
50+
51+
class TestMonitor(AsyncIntegrationTest):
52+
async def create_client(self):
53+
listener = ServerAndTopologyEventListener()
54+
client = await self.unmanaged_async_single_client(event_listeners=[listener])
55+
await connected(client)
56+
return client
57+
58+
async def test_cleanup_executors_on_client_del(self):
59+
with warnings.catch_warnings():
60+
warnings.simplefilter("ignore")
61+
client = await self.create_client()
62+
executors = get_executors(client)
63+
self.assertEqual(len(executors), 4)
64+
65+
# Each executor stores a weakref to itself in _EXECUTORS.
66+
executor_refs = [(r, r()._name) for r in _EXECUTORS.copy() if r() in executors]
67+
68+
del executors
69+
del client
70+
71+
for ref, name in executor_refs:
72+
await async_wait_until(partial(unregistered, ref), f"unregister executor: {name}", timeout=5)
73+
74+
async def test_cleanup_executors_on_client_close(self):
75+
client = await self.create_client()
76+
executors = get_executors(client)
77+
self.assertEqual(len(executors), 4)
78+
79+
await client.close()
80+
81+
for executor in executors:
82+
await async_wait_until(lambda: executor._stopped, f"closed executor: {executor._name}", timeout=5)
83+
84+
async def test_no_thread_start_runtime_err_on_shutdown(self):
85+
"""Test we silence noisy runtime errors fired when the AsyncMongoClient spawns a new thread
86+
on process shutdown."""
87+
command = [sys.executable, "-c", "from pymongo import AsyncMongoClient; c = AsyncMongoClient()"]
88+
completed_process: subprocess.CompletedProcess = subprocess.run(
89+
command, capture_output=True
90+
)
91+
92+
self.assertFalse(completed_process.stderr)
93+
self.assertFalse(completed_process.stdout)
94+
95+
96+
if __name__ == "__main__":
97+
unittest.main()

test/test_monitor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from pymongo.periodic_executor import _EXECUTORS
3333

34+
_IS_SYNC = True
3435

3536
def unregistered(ref):
3637
gc.collect()

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def async_only_test(f: str) -> bool:
214214
"test_json_util_integration.py",
215215
"test_gridfs_spec.py",
216216
"test_logger.py",
217+
"test_monitor.py",
217218
"test_monitoring.py",
218219
"test_raw_bson.py",
219220
"test_retryable_reads.py",

0 commit comments

Comments
 (0)