| 184 | |
| 185 | |
| 186 | def create_test(scenario_def): |
| 187 | def run_scenario(self): |
| 188 | with client_knobs(events_queue_frequency=0.05, min_heartbeat_interval=0.05): |
| 189 | _run_scenario(self) |
| 190 | |
| 191 | def _run_scenario(self): |
| 192 | class NoopMonitor(Monitor): |
| 193 | """Override the _run method to do nothing.""" |
| 194 | |
| 195 | def _run(self): |
| 196 | time.sleep(0.05) |
| 197 | |
| 198 | m = MongoClient( |
| 199 | host=scenario_def["uri"], |
| 200 | port=27017, |
| 201 | event_listeners=[self.all_listener], |
| 202 | _monitor_class=NoopMonitor, |
| 203 | ) |
| 204 | topology = m._get_topology() |
| 205 | |
| 206 | try: |
| 207 | for phase in scenario_def["phases"]: |
| 208 | for source, response in phase.get("responses", []): |
| 209 | source_address = clean_node(source) |
| 210 | topology.on_change( |
| 211 | ServerDescription( |
| 212 | address=source_address, hello=Hello(response), round_trip_time=0 |
| 213 | ) |
| 214 | ) |
| 215 | |
| 216 | expected_results = phase["outcome"]["events"] |
| 217 | expected_len = len(expected_results) |
| 218 | wait_until( |
| 219 | lambda: len(self.all_listener.results) >= expected_len, |
| 220 | "publish all events", |
| 221 | timeout=15, |
| 222 | ) |
| 223 | |
| 224 | # Wait some time to catch possible lagging extra events. |
| 225 | wait_until(lambda: topology._events.empty(), "publish lagging events") |
| 226 | |
| 227 | i = 0 |
| 228 | while i < expected_len: |
| 229 | result = ( |
| 230 | self.all_listener.results[i] if len(self.all_listener.results) > i else None |
| 231 | ) |
| 232 | # The order of ServerOpening/ClosedEvents doesn't matter |
| 233 | if isinstance( |
| 234 | result, (monitoring.ServerOpeningEvent, monitoring.ServerClosedEvent) |
| 235 | ): |
| 236 | i, passed, message = compare_multiple_events( |
| 237 | i, expected_results, self.all_listener.results |
| 238 | ) |
| 239 | self.assertTrue(passed, message) |
| 240 | else: |
| 241 | self.assertTrue(*compare_events(expected_results[i], result)) |
| 242 | i += 1 |
| 243 | |