Skip to content

Commit 84211e7

Browse files
committed
Lots of cleanup
1 parent 048edf2 commit 84211e7

17 files changed

+2707
-5289
lines changed

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ lint-manual:
6262
test *args="-v --durations=5 --maxfail=10":
6363
{{uv_run}} --extra test pytest {{args}}
6464

65+
[group('test')]
66+
test-async *args="-v --durations=5 --maxfail=10 -m asyncio":
67+
{{uv_run}} --extra test pytest {{args}}
68+
6569
[group('test')]
6670
test-mockupdb *args:
6771
{{uv_run}} -v --extra test --group mockupdb pytest -m mockupdb {{args}}

pymongo/asynchronous/mongo_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,8 @@ def __next__(self) -> NoReturn:
14181418
raise TypeError("'AsyncMongoClient' object is not iterable")
14191419

14201420
next = __next__
1421-
anext = next
1421+
if not _IS_SYNC:
1422+
anext = next
14221423

14231424
async def _server_property(self, attr_name: str) -> Any:
14241425
"""An attribute of the current server's description.

pymongo/synchronous/mongo_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,8 @@ def __next__(self) -> NoReturn:
14141414
raise TypeError("'MongoClient' object is not iterable")
14151415

14161416
next = __next__
1417+
if not _IS_SYNC:
1418+
next = next
14171419

14181420
def _server_property(self, attr_name: str) -> Any:
14191421
"""An attribute of the current server's description.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ zstd = ["requirements/zstd.txt"]
9494

9595
[tool.pytest.ini_options]
9696
minversion = "7"
97-
addopts = ["-ra", "--strict-config", "--strict-markers", "--junitxml=xunit-results/TEST-results.xml", "-m default or default_async or asyncio"]
97+
addopts = ["-ra", "--strict-config", "--strict-markers", "--junitxml=xunit-results/TEST-results.xml", "-m default or default_async"]
9898
testpaths = ["test"]
9999
log_cli_level = "INFO"
100100
faulthandler_timeout = 1500
@@ -135,6 +135,8 @@ markers = [
135135
"mockupdb: tests that rely on mockupdb",
136136
"default: default test suite",
137137
"default_async: default async test suite",
138+
"unit: tests that don't require a connection to MongoDB",
139+
"integration: tests that require a connection to MongoDB",
138140
]
139141

140142
[tool.mypy]

test/__init__.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
"""Synchronous test suite for pymongo, bson, and gridfs."""
1616
from __future__ import annotations
1717

18-
import asyncio
1918
import gc
20-
import logging
2119
import multiprocessing
2220
import os
2321
import signal
2422
import socket
2523
import subprocess
2624
import sys
27-
import threading
2825
import time
29-
import traceback
3026
import unittest
3127
import warnings
3228
from asyncio import iscoroutinefunction
@@ -53,6 +49,7 @@
5349
sanitize_reply,
5450
)
5551

52+
from pymongo.lock import _create_lock
5653
from pymongo.uri_parser import parse_uri
5754

5855
try:
@@ -116,7 +113,7 @@ def __init__(self):
116113
self.default_client_options: Dict = {}
117114
self.sessions_enabled = False
118115
self.client = None # type: ignore
119-
self.conn_lock = threading.Lock()
116+
self.conn_lock = _create_lock()
120117
self.is_data_lake = False
121118
self.load_balancer = TEST_LOADBALANCER
122119
self.serverless = TEST_SERVERLESS
@@ -518,6 +515,12 @@ def require_data_lake(self, func):
518515
func=func,
519516
)
520517

518+
@property
519+
def is_not_mmap(self):
520+
if self.is_mongos:
521+
return True
522+
return self.storage_engine != "mmapv1"
523+
521524
def require_no_mmap(self, func):
522525
"""Run a test only if the server is not using the MMAPv1 storage
523526
engine. Only works for standalone and replica sets; tests are
@@ -571,6 +574,10 @@ def require_replica_set(self, func):
571574
"""Run a test only if the client is connected to a replica set."""
572575
return self._require(lambda: self.is_rs, "Not connected to a replica set", func=func)
573576

577+
@property
578+
def secondaries_count(self):
579+
return 0 if not self.client else len(self.client.secondaries)
580+
574581
def require_secondaries_count(self, count):
575582
"""Run a test only if the client is connected to a replica set that has
576583
`count` secondaries.
@@ -690,7 +697,7 @@ def is_topology_type(self, topologies):
690697
if "sharded" in topologies and self.is_mongos:
691698
return True
692699
if "sharded-replicaset" in topologies and self.is_mongos:
693-
shards = client_context.client.config.shards.find().to_list()
700+
shards = self.client.config.shards.find().to_list()
694701
for shard in shards:
695702
# For a 3-member RS-backed sharded cluster, shard['host']
696703
# will be 'replicaName/ip1:port1,ip2:port2,ip3:port3'
@@ -864,6 +871,18 @@ def max_message_size_bytes(self):
864871
client_context = ClientContext()
865872

866873

874+
class PyMongoTestCasePyTest:
875+
@contextmanager
876+
def fail_point(self, client, command_args):
877+
cmd_on = SON([("configureFailPoint", "failCommand")])
878+
cmd_on.update(command_args)
879+
client.admin.command(cmd_on)
880+
try:
881+
yield
882+
finally:
883+
client.admin.command("configureFailPoint", cmd_on["configureFailPoint"], mode="off")
884+
885+
867886
class PyMongoTestCase(unittest.TestCase):
868887
def assertEqualCommand(self, expected, actual, msg=None):
869888
self.assertEqual(sanitize_cmd(expected), sanitize_cmd(actual), msg)

test/asynchronous/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,7 @@ async def max_message_size_bytes(self):
872872
# Reusable client context
873873
async_client_context = AsyncClientContext()
874874

875+
875876
class AsyncPyMongoTestCasePyTest:
876877
@asynccontextmanager
877878
async def fail_point(self, client, command_args):
@@ -1212,6 +1213,7 @@ async def asyncTearDown(self) -> None:
12121213

12131214

12141215
async def async_setup():
1216+
await async_client_context.init()
12151217
warnings.resetwarnings()
12161218
warnings.simplefilter("always")
12171219
global_knobs.enable()
@@ -1226,16 +1228,16 @@ async def async_teardown():
12261228
garbage.append(f" gc.get_referrers: {gc.get_referrers(g)!r}")
12271229
if garbage:
12281230
raise AssertionError("\n".join(garbage))
1229-
# c = async_client_context.client
1230-
# if c:
1231-
# if not async_client_context.is_data_lake:
1232-
# await c.drop_database("pymongo-pooling-tests")
1233-
# await c.drop_database("pymongo_test")
1234-
# await c.drop_database("pymongo_test1")
1235-
# await c.drop_database("pymongo_test2")
1236-
# await c.drop_database("pymongo_test_mike")
1237-
# await c.drop_database("pymongo_test_bernie")
1238-
# await c.close()
1231+
c = async_client_context.client
1232+
if c:
1233+
if not async_client_context.is_data_lake:
1234+
await c.drop_database("pymongo-pooling-tests")
1235+
await c.drop_database("pymongo_test")
1236+
await c.drop_database("pymongo_test1")
1237+
await c.drop_database("pymongo_test2")
1238+
await c.drop_database("pymongo_test_mike")
1239+
await c.drop_database("pymongo_test_bernie")
1240+
await c.close()
12391241
print_running_clients()
12401242

12411243

0 commit comments

Comments
 (0)