|
19 | 19 | @contextmanager |
20 | 20 | def _offline_pool(): |
21 | 21 | connection = MagicMock() |
| 22 | + connection.sock = None |
22 | 23 | connection.close.return_value = None |
23 | 24 | connection.getresponse.side_effect = lambda: SimpleHttpResponse(200, {}) |
24 | 25 | with HTTPSConnectionPool("example.invalid") as pool, patch.object( |
@@ -86,3 +87,44 @@ def test_background_telemetry_does_not_receive_retry_errors(pending_telemetry): |
86 | 87 | with pytest.raises(MaxRetryError): |
87 | 88 | pool.urlopen("POST", "/sql", retries=Retry(total=1, allowed_methods=None)) |
88 | 89 | assert validate.call_count == 2 |
| 90 | + |
| 91 | + |
| 92 | +def test_mocked_connections_do_not_leak_into_a_shared_pool(): |
| 93 | + ready, release = Event(), Event() |
| 94 | + responses = [ |
| 95 | + {"status": 429, "headers": {"Retry-After": "0"}, "redirect_location": None}, |
| 96 | + {"status": 503, "headers": {}, "redirect_location": None}, |
| 97 | + ] |
| 98 | + with _offline_pool() as pool, ThreadPoolExecutor(max_workers=1) as executor: |
| 99 | + connection = pool._new_conn.return_value |
| 100 | + |
| 101 | + def new_connection(): |
| 102 | + # Hold the real pool's slot while the test sends mocked requests. |
| 103 | + ready.set() |
| 104 | + assert release.wait(timeout=5) |
| 105 | + return connection |
| 106 | + |
| 107 | + def send(): |
| 108 | + return [ |
| 109 | + pool.urlopen("POST", "/telemetry-ext", retries=False).status |
| 110 | + for _ in range(2) |
| 111 | + ] |
| 112 | + |
| 113 | + pool._new_conn.side_effect = new_connection |
| 114 | + future = executor.submit(send) |
| 115 | + assert ready.wait(timeout=5) |
| 116 | + try: |
| 117 | + with mock_sequential_server_responses(responses) as mocked_connection: |
| 118 | + with pytest.raises(MaxRetryError): |
| 119 | + pool.urlopen( |
| 120 | + "POST", |
| 121 | + "/sql", |
| 122 | + retries=Retry( |
| 123 | + total=1, status_forcelist=[429, 503], allowed_methods=None |
| 124 | + ), |
| 125 | + ) |
| 126 | + release.set() |
| 127 | + assert future.result(timeout=5) == [200, 200] |
| 128 | + assert mocked_connection.return_value.getresponse.call_count == 2 |
| 129 | + finally: |
| 130 | + release.set() |
0 commit comments