| 93 | |
| 94 | |
| 95 | class MockClient(MongoClient): |
| 96 | def __init__( |
| 97 | self, |
| 98 | standalones, |
| 99 | members, |
| 100 | mongoses, |
| 101 | hello_hosts=None, |
| 102 | arbiters=None, |
| 103 | down_hosts=None, |
| 104 | *args, |
| 105 | **kwargs, |
| 106 | ): |
| 107 | """A MongoClient connected to the default server, with a mock topology. |
| 108 | |
| 109 | standalones, members, mongoses, arbiters, and down_hosts determine the |
| 110 | configuration of the topology. They are formatted like ['a:1', 'b:2']. |
| 111 | hello_hosts provides an alternative host list for the server's |
| 112 | mocked hello response; see test_connect_with_internal_ips. |
| 113 | """ |
| 114 | self.mock_standalones = standalones[:] |
| 115 | self.mock_members = members[:] |
| 116 | |
| 117 | if self.mock_members: |
| 118 | self.mock_primary = self.mock_members[0] |
| 119 | else: |
| 120 | self.mock_primary = None |
| 121 | |
| 122 | # Hosts that should be considered an arbiter. |
| 123 | self.mock_arbiters = arbiters[:] if arbiters else [] |
| 124 | |
| 125 | if hello_hosts is not None: |
| 126 | self.mock_hello_hosts = hello_hosts |
| 127 | else: |
| 128 | self.mock_hello_hosts = members[:] |
| 129 | |
| 130 | self.mock_mongoses = mongoses[:] |
| 131 | |
| 132 | # Hosts that should raise socket errors. |
| 133 | self.mock_down_hosts = down_hosts[:] if down_hosts else [] |
| 134 | |
| 135 | # Hostname -> (min wire version, max wire version) |
| 136 | self.mock_wire_versions = {} |
| 137 | |
| 138 | # Hostname -> max write batch size |
| 139 | self.mock_max_write_batch_sizes = {} |
| 140 | |
| 141 | # Hostname -> round trip time |
| 142 | self.mock_rtts = {} |
| 143 | |
| 144 | kwargs["_pool_class"] = partial(MockPool, self) |
| 145 | kwargs["_monitor_class"] = partial(SyncMockMonitor, self) |
| 146 | |
| 147 | client_options = client_context.default_client_options.copy() |
| 148 | client_options.update(kwargs) |
| 149 | |
| 150 | super().__init__(*args, **client_options) |
| 151 | |
| 152 | @classmethod |
no outgoing calls