Skip to content

Commit a0a5c71

Browse files
authored
PYTHON-3405/PYTHON-2531 Fix tests for primary step down (#1041)
1 parent 09aeef0 commit a0a5c71

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

test/unified_format.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
from pymongo.server_selectors import Selection, writable_server_selector
115115
from pymongo.server_type import SERVER_TYPE
116116
from pymongo.topology_description import TopologyDescription
117+
from pymongo.typings import _Address
117118
from pymongo.write_concern import WriteConcern
118119

119120
JSON_OPTS = json_util.JSONOptions(tz_aware=False)
@@ -1442,21 +1443,21 @@ def _testOperation_assertTopologyType(self, spec):
14421443
self.assertIsInstance(description, TopologyDescription)
14431444
self.assertEqual(description.topology_type_name, spec["topologyType"])
14441445

1445-
def _testOperation_waitForPrimaryChange(self, spec):
1446+
def _testOperation_waitForPrimaryChange(self, spec: dict) -> None:
14461447
"""Run the waitForPrimaryChange test operation."""
14471448
client = self.entity_map[spec["client"]]
14481449
old_description: TopologyDescription = self.entity_map[spec["priorTopologyDescription"]]
14491450
timeout = spec["timeoutMS"] / 1000.0
14501451

1451-
def get_primary(td: TopologyDescription) -> Optional[ServerDescription]:
1452+
def get_primary(td: TopologyDescription) -> Optional[_Address]:
14521453
servers = writable_server_selector(Selection.from_topology_description(td))
14531454
if servers and servers[0].server_type == SERVER_TYPE.RSPrimary:
1454-
return servers[0]
1455+
return servers[0].address
14551456
return None
14561457

14571458
old_primary = get_primary(old_description)
14581459

1459-
def primary_changed():
1460+
def primary_changed() -> bool:
14601461
primary = client.primary
14611462
if primary is None:
14621463
return False

test/utils.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ def rs_or_single_client(h=None, p=None, **kwargs):
593593
return _mongo_client(h, p, **kwargs)
594594

595595

596-
def ensure_all_connected(client):
596+
def ensure_all_connected(client: MongoClient) -> None:
597597
"""Ensure that the client's connection pool has socket connections to all
598598
members of a replica set. Raises ConfigurationError when called with a
599599
non-replica set client.
@@ -605,14 +605,26 @@ def ensure_all_connected(client):
605605
if "setName" not in hello:
606606
raise ConfigurationError("cluster is not a replica set")
607607

608-
target_host_list = set(hello["hosts"])
608+
target_host_list = set(hello["hosts"] + hello.get("passives", []))
609609
connected_host_list = set([hello["me"]])
610-
admindb = client.get_database("admin")
611610

612611
# Run hello until we have connected to each host at least once.
613-
while connected_host_list != target_host_list:
614-
hello = admindb.command(HelloCompat.LEGACY_CMD, read_preference=ReadPreference.SECONDARY)
615-
connected_host_list.update([hello["me"]])
612+
def discover():
613+
i = 0
614+
while i < 100 and connected_host_list != target_host_list:
615+
hello = client.admin.command(
616+
HelloCompat.LEGACY_CMD, read_preference=ReadPreference.SECONDARY
617+
)
618+
connected_host_list.update([hello["me"]])
619+
i += 1
620+
return connected_host_list
621+
622+
try:
623+
wait_until(lambda: target_host_list == discover(), "connected to all hosts")
624+
except AssertionError as exc:
625+
raise AssertionError(
626+
f"{exc}, {connected_host_list} != {target_host_list}, {client.topology_description}"
627+
)
616628

617629

618630
def one(s):

0 commit comments

Comments
 (0)