33
33
SimpleCache ,
34
34
)
35
35
36
- request_session_manager = request .RequestSessionManager ()
37
-
38
36
39
37
class MockedResponse :
40
38
def __init__ (self , text = "" , status_code = 200 ):
@@ -69,23 +67,20 @@ def check_adapters_mounted(session: Session):
69
67
assert len (session .adapters ) == 2
70
68
71
69
72
- def _simulate_call (uri ):
70
+ def _simulate_call (request_session_manager , uri ):
73
71
_session = request_session_manager .cache_and_return_session (uri )
74
72
75
73
# simulate a call taking 0.01s to return a response
76
74
time .sleep (0.01 )
77
75
return _session
78
76
79
77
80
- @pytest .fixture (autouse = True )
81
- def setup_and_teardown ():
82
- # clear session cache before and after each test
83
- request_session_manager .session_cache .clear ()
84
- yield
85
- request_session_manager .session_cache .clear ()
78
+ @pytest .fixture
79
+ def request_session_manager ():
80
+ return request .RequestSessionManager ()
86
81
87
82
88
- def test_json_make_get_request (mocker ):
83
+ def test_session_manager_json_make_get_request (mocker , request_session_manager ):
89
84
mocker .patch ("requests.Session.get" , return_value = MockedResponse ())
90
85
91
86
# Submit a first request to create a session with default parameters
@@ -105,7 +100,7 @@ def test_json_make_get_request(mocker):
105
100
assert adapter ._pool_maxsize == DEFAULT_POOLSIZE
106
101
107
102
108
- def test_make_post_request_no_args (mocker ):
103
+ def test_session_manager_make_post_request_no_args (mocker , request_session_manager ):
109
104
mocker .patch ("requests.Session.post" , return_value = MockedResponse ())
110
105
111
106
# Submit a first request to create a session with default parameters
@@ -125,7 +120,7 @@ def test_make_post_request_no_args(mocker):
125
120
assert adapter ._pool_maxsize == DEFAULT_POOLSIZE
126
121
127
122
128
- def test_precached_session (mocker ):
123
+ def test_session_manager_precached_session (mocker , request_session_manager ):
129
124
mocker .patch ("requests.Session.post" , return_value = MockedResponse ())
130
125
131
126
# Update the cache with a handcrafted session
@@ -155,7 +150,7 @@ def test_precached_session(mocker):
155
150
assert adapter ._pool_maxsize == 100
156
151
157
152
158
- def test_cache_session_class ():
153
+ def test_simple_cache_cache_session ():
159
154
cache = SimpleCache (2 )
160
155
_ , evicted_items = cache .cache ("1" , "Hello1" )
161
156
assert cache .get_cache_entry ("1" ) == "Hello1"
@@ -183,7 +178,9 @@ def test_cache_session_class():
183
178
assert cache .get_cache_entry ("1" ) is None
184
179
185
180
186
- def test_cache_does_not_close_session_before_a_call_when_multithreading ():
181
+ def test_session_manager_cache_does_not_close_session_before_a_call_when_multithreading (
182
+ request_session_manager ,
183
+ ):
187
184
# save default values
188
185
session_cache_default = request_session_manager .session_cache
189
186
timeout_default = request .DEFAULT_TIMEOUT
@@ -194,7 +191,10 @@ def test_cache_does_not_close_session_before_a_call_when_multithreading():
194
191
request .DEFAULT_TIMEOUT = _timeout_for_testing
195
192
196
193
with ThreadPoolExecutor (max_workers = len (UNIQUE_URIS )) as exc :
197
- all_sessions = [exc .submit (_simulate_call , uri ) for uri in UNIQUE_URIS ]
194
+ all_sessions = [
195
+ exc .submit (_simulate_call , request_session_manager , uri )
196
+ for uri in UNIQUE_URIS
197
+ ]
198
198
199
199
# assert last session remains in cache, all others evicted
200
200
cache_data = request_session_manager .session_cache ._data
@@ -212,11 +212,16 @@ def test_cache_does_not_close_session_before_a_call_when_multithreading():
212
212
request .DEFAULT_TIMEOUT = timeout_default
213
213
214
214
215
- def test_unique_cache_keys_created_per_thread_with_same_uri ():
215
+ def test_session_manager_unique_cache_keys_created_per_thread_with_same_uri (
216
+ request_session_manager ,
217
+ ):
216
218
# somewhat inspired by issue #2680
217
219
218
220
with ThreadPoolExecutor (max_workers = 2 ) as exc :
219
- test_sessions = [exc .submit (_simulate_call , TEST_URI ) for _ in range (2 )]
221
+ test_sessions = [
222
+ exc .submit (_simulate_call , request_session_manager , TEST_URI )
223
+ for _ in range (2 )
224
+ ]
220
225
221
226
# assert unique keys are generated per thread for the same uri
222
227
assert len (request_session_manager .session_cache ._data ) == 2
@@ -251,7 +256,9 @@ def raise_for_status() -> None:
251
256
252
257
253
258
@pytest .mark .asyncio
254
- async def test_async_json_make_get_request (mocker ):
259
+ async def test_session_manager_async_json_make_get_request (
260
+ mocker , request_session_manager
261
+ ):
255
262
mocker .patch ("aiohttp.ClientSession.get" , return_value = AsyncMockedResponse ())
256
263
257
264
# Submit a first request to create a session with default parameters
@@ -272,7 +279,7 @@ async def test_async_json_make_get_request(mocker):
272
279
273
280
274
281
@pytest .mark .asyncio
275
- async def test_async_make_post_request (mocker ):
282
+ async def test_session_manager_async_make_post_request (mocker , request_session_manager ):
276
283
mocker .patch ("aiohttp.ClientSession.post" , return_value = AsyncMockedResponse ())
277
284
278
285
# Submit a first request to create a session with default parameters
@@ -296,7 +303,7 @@ async def test_async_make_post_request(mocker):
296
303
297
304
298
305
@pytest .mark .asyncio
299
- async def test_async_precached_session ( ):
306
+ async def test_session_manager_async_precached_session ( request_session_manager ):
300
307
# Add a session
301
308
session = ClientSession ()
302
309
await request_session_manager .async_cache_and_return_session (TEST_URI , session )
@@ -322,7 +329,9 @@ async def test_async_precached_session():
322
329
323
330
324
331
@pytest .mark .asyncio
325
- async def test_async_cache_does_not_close_session_before_a_call_when_multithreading ():
332
+ async def test_session_manager_async_cache_does_not_close_session_before_a_call_when_multithreading ( # noqa: E501
333
+ request_session_manager ,
334
+ ):
326
335
# save default values
327
336
session_cache_default = request_session_manager .session_cache
328
337
timeout_default = request .DEFAULT_TIMEOUT
@@ -368,7 +377,9 @@ async def cache_uri_and_return_session(uri):
368
377
369
378
370
379
@pytest .mark .asyncio
371
- async def test_async_unique_cache_keys_created_per_thread_with_same_uri ():
380
+ async def test_session_manager_async_unique_cache_keys_created_per_thread_with_same_uri (
381
+ request_session_manager ,
382
+ ):
372
383
# inspired by issue #2680
373
384
# Note: unique event loops for each thread are important here
374
385
@@ -407,7 +418,9 @@ def target_function(endpoint_uri):
407
418
408
419
409
420
@pytest .mark .asyncio
410
- async def test_async_use_new_session_if_loop_closed_for_cached_session ():
421
+ async def test_session_manager_async_use_new_session_if_loop_closed_for_cached_session (
422
+ request_session_manager ,
423
+ ):
411
424
# create new loop, cache a session wihin the loop, close the loop
412
425
loop1 = asyncio .new_event_loop ()
413
426
@@ -448,7 +461,9 @@ async def test_async_use_new_session_if_loop_closed_for_cached_session():
448
461
449
462
450
463
@pytest .mark .asyncio
451
- async def test_async_use_new_session_if_session_closed_for_cached_session ():
464
+ async def test_session_manager_async_use_new_session_if_session_closed_for_cached_session ( # noqa: E501
465
+ request_session_manager ,
466
+ ):
452
467
# create a session, close it, and cache it at the cache key for TEST_URI
453
468
session1 = ClientSession (raise_for_status = True )
454
469
await session1 .close ()
0 commit comments