-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_shared_pool.py
More file actions
319 lines (229 loc) · 9.39 KB
/
test_shared_pool.py
File metadata and controls
319 lines (229 loc) · 9.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""Tests for shared HTTP transport pool behavior.
Verifies that SDK clients share (or don't share) the underlying httpx
transport, and that refcounting correctly manages the transport lifecycle.
"""
from __future__ import annotations
import os
import asyncio
from typing import Any, Iterator
import httpx
import pytest
import runloop_api_client._base_client as _base_mod
from runloop_api_client import Runloop, AsyncRunloop
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
bearer_token = "My Bearer Token"
@pytest.fixture(autouse=True)
def _reset_shared_pool() -> Iterator[None]: # pyright: ignore[reportUnusedFunction]
_clear_pool_state()
yield
_clear_pool_state()
def _clear_pool_state() -> None:
with _base_mod._pool_lock:
old_sync = _base_mod._shared_sync_transport
_base_mod._shared_sync_transport = None
_base_mod._shared_async_transports.clear()
if old_sync is not None:
try:
old_sync._transport.close()
except Exception:
pass
def _make_client(**kwargs: Any) -> Runloop:
kwargs.setdefault("base_url", base_url)
kwargs.setdefault("bearer_token", bearer_token)
return Runloop(**kwargs)
def _make_async_client(**kwargs: Any) -> AsyncRunloop:
kwargs.setdefault("base_url", base_url)
kwargs.setdefault("bearer_token", bearer_token)
return AsyncRunloop(**kwargs)
def _get_transport(client: Runloop | AsyncRunloop) -> Any:
return client._client._transport # type: ignore[union-attr]
# ---------------------------------------------------------------------------
# Sync: sharing behavior
# ---------------------------------------------------------------------------
class TestSyncSharedPool:
def test_shared_pool_uses_same_transport(self):
c1 = _make_client(shared_http_pool=True)
c2 = _make_client(shared_http_pool=True)
assert _get_transport(c1) is _get_transport(c2)
assert c1._client is not c2._client
assert c1._uses_shared_pool is True
assert c2._uses_shared_pool is True
c1.close()
c2.close()
def test_private_pool_uses_different_transports(self):
c1 = _make_client(shared_http_pool=False)
c2 = _make_client(shared_http_pool=False)
assert _get_transport(c1) is not _get_transport(c2)
assert c1._uses_shared_pool is False
assert c2._uses_shared_pool is False
c1.close()
c2.close()
def test_custom_http_client_bypasses_sharing(self):
custom = httpx.Client()
c1 = _make_client(http_client=custom, shared_http_pool=True)
assert c1._client is custom
assert c1._uses_shared_pool is False
c1.close()
custom.close()
def test_default_is_shared(self):
c1 = _make_client()
assert c1._uses_shared_pool is True
c1.close()
def test_cookie_isolation(self):
c1 = _make_client(shared_http_pool=True)
c2 = _make_client(shared_http_pool=True)
c1._client.cookies.set("session", "secret-123")
assert "session" not in c2._client.cookies
c1.close()
c2.close()
class TestSyncRefcounting:
def test_close_one_keeps_transport_alive(self):
c1 = _make_client(shared_http_pool=True)
c2 = _make_client(shared_http_pool=True)
transport = _get_transport(c1)
assert transport.refcount == 2
c1.close()
assert transport.refcount == 1
assert not c2.is_closed()
c2.close()
assert transport.refcount == 0
def test_double_close_is_safe(self):
c1 = _make_client(shared_http_pool=True)
transport = _get_transport(c1)
c1.close()
c1.close() # should not raise or double-decrement
assert transport.refcount == 0
def test_three_clients_refcount(self):
c1 = _make_client(shared_http_pool=True)
c2 = _make_client(shared_http_pool=True)
c3 = _make_client(shared_http_pool=True)
transport = _get_transport(c1)
assert transport.refcount == 3
c1.close()
assert transport.refcount == 2
c2.close()
assert transport.refcount == 1
c3.close()
assert transport.refcount == 0
def test_transport_recreated_after_full_release(self):
c1 = _make_client(shared_http_pool=True)
t1 = _get_transport(c1)
c1.close()
c2 = _make_client(shared_http_pool=True)
t2 = _get_transport(c2)
assert t2 is not t1
assert t2.refcount == 1
c2.close()
class TestSyncCopy:
def test_copy_inherits_shared_pool(self):
c1 = _make_client(shared_http_pool=True)
c2 = c1.copy()
transport = _get_transport(c1)
assert c2._uses_shared_pool is True
assert _get_transport(c2) is transport
assert transport.refcount == 2
c1.close()
c2.close()
def test_copy_with_custom_client_disables_sharing(self):
c1 = _make_client(shared_http_pool=True)
custom = httpx.Client()
c2 = c1.copy(http_client=custom)
assert c2._uses_shared_pool is False
assert c2._client is custom
c1.close()
c2.close()
custom.close()
def test_copy_of_non_shared_stays_non_shared(self):
c1 = _make_client(shared_http_pool=False)
c2 = c1.copy()
assert c2._uses_shared_pool is False
assert _get_transport(c2) is not _get_transport(c1)
c1.close()
c2.close()
# ---------------------------------------------------------------------------
# Async: sharing behavior
# ---------------------------------------------------------------------------
class TestAsyncSharedPool:
async def test_shared_pool_uses_same_transport(self):
c1 = _make_async_client(shared_http_pool=True)
c2 = _make_async_client(shared_http_pool=True)
assert _get_transport(c1) is _get_transport(c2)
assert c1._client is not c2._client
assert c1._uses_shared_pool is True
assert c2._uses_shared_pool is True
def test_private_pool_uses_different_transports(self):
c1 = _make_async_client(shared_http_pool=False)
c2 = _make_async_client(shared_http_pool=False)
assert _get_transport(c1) is not _get_transport(c2)
assert c1._uses_shared_pool is False
def test_custom_http_client_bypasses_sharing(self):
custom = httpx.AsyncClient()
c1 = _make_async_client(http_client=custom, shared_http_pool=True)
assert c1._client is custom
assert c1._uses_shared_pool is False
async def test_default_is_shared(self):
c1 = _make_async_client()
assert c1._uses_shared_pool is True
def test_no_loop_creates_private_client(self):
c1 = _make_async_client(shared_http_pool=True)
assert c1._uses_shared_pool is False
class TestAsyncRefcounting:
async def test_close_one_keeps_transport_alive(self):
c1 = _make_async_client(shared_http_pool=True)
c2 = _make_async_client(shared_http_pool=True)
transport = _get_transport(c1)
assert transport.refcount == 2
await c1.close()
assert transport.refcount == 1
assert not c2.is_closed()
await c2.close()
assert transport.refcount == 0
async def test_double_close_is_safe(self):
c1 = _make_async_client(shared_http_pool=True)
transport = _get_transport(c1)
await c1.close()
await c1.close() # should not raise or double-decrement
assert transport.refcount == 0
def test_no_loop_client_closes_properly(self):
"""Client created without a running loop should close without leaking."""
c1 = _make_async_client(shared_http_pool=True)
assert c1._uses_shared_pool is False
asyncio.run(c1.close())
assert c1.is_closed()
class TestAsyncCopy:
async def test_copy_inherits_shared_pool(self):
c1 = _make_async_client(shared_http_pool=True)
c2 = c1.copy()
transport = _get_transport(c1)
assert c2._uses_shared_pool is True
assert _get_transport(c2) is transport
assert transport.refcount == 2
async def test_copy_with_custom_client_disables_sharing(self):
c1 = _make_async_client(shared_http_pool=True)
custom = httpx.AsyncClient()
c2 = c1.copy(http_client=custom)
assert c2._uses_shared_pool is False
assert c2._client is custom
class TestAsyncCrossLoop:
def test_separate_loops_get_separate_transports(self):
"""Clients created in different asyncio.run() calls must not share a transport."""
async def create_client() -> Any:
c = _make_async_client(shared_http_pool=True)
transport = _get_transport(c)
await c.close()
return transport
t1 = asyncio.run(create_client())
t2 = asyncio.run(create_client())
assert t1 is not t2, "each loop should get its own transport"
def test_same_loop_shares_transport(self):
"""Clients created in the same asyncio.run() must share a transport."""
async def create_two() -> tuple[int, int]:
c1 = _make_async_client(shared_http_pool=True)
c2 = _make_async_client(shared_http_pool=True)
id1 = id(_get_transport(c1))
id2 = id(_get_transport(c2))
await c1.close()
await c2.close()
return id1, id2
id1, id2 = asyncio.run(create_two())
assert id1 == id2