@@ -593,7 +593,7 @@ def rs_or_single_client(h=None, p=None, **kwargs):
593
593
return _mongo_client (h , p , ** kwargs )
594
594
595
595
596
- def ensure_all_connected (client ) :
596
+ def ensure_all_connected (client : MongoClient ) -> None :
597
597
"""Ensure that the client's connection pool has socket connections to all
598
598
members of a replica set. Raises ConfigurationError when called with a
599
599
non-replica set client.
@@ -605,14 +605,26 @@ def ensure_all_connected(client):
605
605
if "setName" not in hello :
606
606
raise ConfigurationError ("cluster is not a replica set" )
607
607
608
- target_host_list = set (hello ["hosts" ])
608
+ target_host_list = set (hello ["hosts" ] + hello . get ( "passives" , []) )
609
609
connected_host_list = set ([hello ["me" ]])
610
- admindb = client .get_database ("admin" )
611
610
612
611
# 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
+ )
616
628
617
629
618
630
def one (s ):
0 commit comments