Ensure that the client's connection pool has socket connections to all members of a replica set. Raises ConfigurationError when called with a non-replica set client. Depending on the use-case, the caller may need to clear any event listeners that are configured on the client.
(client: AsyncMongoClient)
| 91 | |
| 92 | |
| 93 | async def async_ensure_all_connected(client: AsyncMongoClient) -> None: |
| 94 | """Ensure that the client's connection pool has socket connections to all |
| 95 | members of a replica set. Raises ConfigurationError when called with a |
| 96 | non-replica set client. |
| 97 | |
| 98 | Depending on the use-case, the caller may need to clear any event listeners |
| 99 | that are configured on the client. |
| 100 | """ |
| 101 | hello: dict = await client.admin.command(HelloCompat.LEGACY_CMD) |
| 102 | if "setName" not in hello: |
| 103 | raise ConfigurationError("cluster is not a replica set") |
| 104 | |
| 105 | target_host_list = set(hello["hosts"] + hello.get("passives", [])) |
| 106 | connected_host_list = {hello["me"]} |
| 107 | |
| 108 | # Run hello until we have connected to each host at least once. |
| 109 | async def discover(): |
| 110 | i = 0 |
| 111 | while i < 100 and connected_host_list != target_host_list: |
| 112 | hello: dict = await client.admin.command( |
| 113 | HelloCompat.LEGACY_CMD, read_preference=ReadPreference.SECONDARY |
| 114 | ) |
| 115 | connected_host_list.update([hello["me"]]) |
| 116 | i += 1 |
| 117 | return connected_host_list |
| 118 | |
| 119 | try: |
| 120 | |
| 121 | async def predicate(): |
| 122 | return target_host_list == await discover() |
| 123 | |
| 124 | await async_wait_until(predicate, "connected to all hosts") |
| 125 | except AssertionError as exc: |
| 126 | raise AssertionError( |
| 127 | f"{exc}, {connected_host_list} != {target_host_list}, {client.topology_description}" |
| 128 | ) |
| 129 | |
| 130 | |
| 131 | async def asyncAssertRaisesExactly(cls, fn, *args, **kwargs): |