|
15 | 15 | """Synchronous test suite for pymongo, bson, and gridfs.""" |
16 | 16 | from __future__ import annotations |
17 | 17 |
|
18 | | -import asyncio |
19 | 18 | import gc |
20 | | -import logging |
21 | 19 | import multiprocessing |
22 | 20 | import os |
23 | 21 | import signal |
24 | 22 | import socket |
25 | 23 | import subprocess |
26 | 24 | import sys |
27 | | -import threading |
28 | 25 | import time |
29 | | -import traceback |
30 | 26 | import unittest |
31 | 27 | import warnings |
32 | 28 | from asyncio import iscoroutinefunction |
|
53 | 49 | sanitize_reply, |
54 | 50 | ) |
55 | 51 |
|
| 52 | +from pymongo.lock import _create_lock |
56 | 53 | from pymongo.uri_parser import parse_uri |
57 | 54 |
|
58 | 55 | try: |
@@ -116,7 +113,7 @@ def __init__(self): |
116 | 113 | self.default_client_options: Dict = {} |
117 | 114 | self.sessions_enabled = False |
118 | 115 | self.client = None # type: ignore |
119 | | - self.conn_lock = threading.Lock() |
| 116 | + self.conn_lock = _create_lock() |
120 | 117 | self.is_data_lake = False |
121 | 118 | self.load_balancer = TEST_LOADBALANCER |
122 | 119 | self.serverless = TEST_SERVERLESS |
@@ -518,6 +515,12 @@ def require_data_lake(self, func): |
518 | 515 | func=func, |
519 | 516 | ) |
520 | 517 |
|
| 518 | + @property |
| 519 | + def is_not_mmap(self): |
| 520 | + if self.is_mongos: |
| 521 | + return True |
| 522 | + return self.storage_engine != "mmapv1" |
| 523 | + |
521 | 524 | def require_no_mmap(self, func): |
522 | 525 | """Run a test only if the server is not using the MMAPv1 storage |
523 | 526 | engine. Only works for standalone and replica sets; tests are |
@@ -571,6 +574,10 @@ def require_replica_set(self, func): |
571 | 574 | """Run a test only if the client is connected to a replica set.""" |
572 | 575 | return self._require(lambda: self.is_rs, "Not connected to a replica set", func=func) |
573 | 576 |
|
| 577 | + @property |
| 578 | + def secondaries_count(self): |
| 579 | + return 0 if not self.client else len(self.client.secondaries) |
| 580 | + |
574 | 581 | def require_secondaries_count(self, count): |
575 | 582 | """Run a test only if the client is connected to a replica set that has |
576 | 583 | `count` secondaries. |
@@ -690,7 +697,7 @@ def is_topology_type(self, topologies): |
690 | 697 | if "sharded" in topologies and self.is_mongos: |
691 | 698 | return True |
692 | 699 | 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() |
694 | 701 | for shard in shards: |
695 | 702 | # For a 3-member RS-backed sharded cluster, shard['host'] |
696 | 703 | # will be 'replicaName/ip1:port1,ip2:port2,ip3:port3' |
@@ -864,6 +871,18 @@ def max_message_size_bytes(self): |
864 | 871 | client_context = ClientContext() |
865 | 872 |
|
866 | 873 |
|
| 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 | + |
867 | 886 | class PyMongoTestCase(unittest.TestCase): |
868 | 887 | def assertEqualCommand(self, expected, actual, msg=None): |
869 | 888 | self.assertEqual(sanitize_cmd(expected), sanitize_cmd(actual), msg) |
|
0 commit comments