29
29
from web3 ._utils .caching import (
30
30
generate_cache_key ,
31
31
)
32
- from web3 ._utils .request import (
33
- async_cache_and_return_session ,
34
- cache_and_return_session ,
35
- )
36
32
from web3 .utils .caching import (
37
33
SimpleCache ,
38
34
)
39
35
36
+ request_session_manager = request .RequestSessionManager ()
37
+
40
38
41
39
class MockedResponse :
42
40
def __init__ (self , text = "" , status_code = 200 ):
@@ -72,7 +70,7 @@ def check_adapters_mounted(session: Session):
72
70
73
71
74
72
def _simulate_call (uri ):
75
- _session = cache_and_return_session (uri )
73
+ _session = request_session_manager . cache_and_return_session (uri )
76
74
77
75
# simulate a call taking 0.01s to return a response
78
76
time .sleep (0.01 )
@@ -81,24 +79,22 @@ def _simulate_call(uri):
81
79
82
80
@pytest .fixture (autouse = True )
83
81
def setup_and_teardown ():
84
- # clear session caches before and after each test
85
- request ._session_cache .clear ()
86
- request ._async_session_cache .clear ()
82
+ # clear session cache before and after each test
83
+ request_session_manager .session_cache .clear ()
87
84
yield
88
- request ._session_cache .clear ()
89
- request ._async_session_cache .clear ()
85
+ request_session_manager .session_cache .clear ()
90
86
91
87
92
88
def test_json_make_get_request (mocker ):
93
89
mocker .patch ("requests.Session.get" , return_value = MockedResponse ())
94
90
95
91
# Submit a first request to create a session with default parameters
96
- assert len (request . _session_cache ) == 0
97
- response = request .json_make_get_request (TEST_URI )
92
+ assert len (request_session_manager . session_cache ) == 0
93
+ response = request_session_manager .json_make_get_request (TEST_URI )
98
94
assert response == json .dumps ({"data" : "content" })
99
- assert len (request . _session_cache ) == 1
95
+ assert len (request_session_manager . session_cache ) == 1
100
96
cache_key = generate_cache_key (f"{ threading .get_ident ()} :{ TEST_URI } " )
101
- session = request . _session_cache .get_cache_entry (cache_key )
97
+ session = request_session_manager . session_cache .get_cache_entry (cache_key )
102
98
session .get .assert_called_once_with (TEST_URI , timeout = 30 )
103
99
104
100
# Ensure the adapter was created with default values
@@ -113,12 +109,12 @@ def test_make_post_request_no_args(mocker):
113
109
mocker .patch ("requests.Session.post" , return_value = MockedResponse ())
114
110
115
111
# Submit a first request to create a session with default parameters
116
- assert len (request . _session_cache ) == 0
117
- response = request .make_post_request (TEST_URI , data = b"request" )
112
+ assert len (request_session_manager . session_cache ) == 0
113
+ response = request_session_manager .make_post_request (TEST_URI , data = b"request" )
118
114
assert response == "content"
119
- assert len (request . _session_cache ) == 1
115
+ assert len (request_session_manager . session_cache ) == 1
120
116
cache_key = generate_cache_key (f"{ threading .get_ident ()} :{ TEST_URI } " )
121
- session = request . _session_cache .get_cache_entry (cache_key )
117
+ session = request_session_manager . session_cache .get_cache_entry (cache_key )
122
118
session .post .assert_called_once_with (TEST_URI , data = b"request" , timeout = 30 )
123
119
124
120
# Ensure the adapter was created with default values
@@ -137,16 +133,18 @@ def test_precached_session(mocker):
137
133
session = Session ()
138
134
session .mount ("http://" , adapter )
139
135
session .mount ("https://" , adapter )
140
- request .cache_and_return_session (TEST_URI , session )
136
+ request_session_manager .cache_and_return_session (TEST_URI , session )
141
137
142
138
# Submit a second request with different arguments
143
- assert len (request ._session_cache ) == 1
144
- response = request .make_post_request (TEST_URI , data = b"request" , timeout = 60 )
139
+ assert len (request_session_manager .session_cache ) == 1
140
+ response = request_session_manager .make_post_request (
141
+ TEST_URI , data = b"request" , timeout = 60
142
+ )
145
143
assert response == "content"
146
- assert len (request . _session_cache ) == 1
144
+ assert len (request_session_manager . session_cache ) == 1
147
145
148
146
# Ensure the timeout was passed to the request
149
- session = request .cache_and_return_session (TEST_URI )
147
+ session = request_session_manager .cache_and_return_session (TEST_URI )
150
148
session .post .assert_called_once_with (TEST_URI , data = b"request" , timeout = 60 )
151
149
152
150
# Ensure the adapter parameters match those we specified
@@ -187,19 +185,19 @@ def test_cache_session_class():
187
185
188
186
def test_cache_does_not_close_session_before_a_call_when_multithreading ():
189
187
# save default values
190
- session_cache_default = request . _session_cache
188
+ session_cache_default = request_session_manager . session_cache
191
189
timeout_default = request .DEFAULT_TIMEOUT
192
190
193
191
# set cache size to 1 + set future session close thread time to 0.01s
194
- request . _session_cache = SimpleCache (1 )
192
+ request_session_manager . session_cache = SimpleCache (1 )
195
193
_timeout_for_testing = 0.01
196
194
request .DEFAULT_TIMEOUT = _timeout_for_testing
197
195
198
196
with ThreadPoolExecutor (max_workers = len (UNIQUE_URIS )) as exc :
199
197
all_sessions = [exc .submit (_simulate_call , uri ) for uri in UNIQUE_URIS ]
200
198
201
199
# assert last session remains in cache, all others evicted
202
- cache_data = request . _session_cache ._data
200
+ cache_data = request_session_manager . session_cache ._data
203
201
assert len (cache_data ) == 1
204
202
_key , cached_session = cache_data .popitem ()
205
203
assert cached_session == all_sessions [- 1 ].result () # result of the `Future`
@@ -210,7 +208,7 @@ def test_cache_does_not_close_session_before_a_call_when_multithreading():
210
208
cached_session .close ()
211
209
212
210
# reset default values
213
- request . _session_cache = session_cache_default
211
+ request_session_manager . session_cache = session_cache_default
214
212
request .DEFAULT_TIMEOUT = timeout_default
215
213
216
214
@@ -221,7 +219,7 @@ def test_unique_cache_keys_created_per_thread_with_same_uri():
221
219
test_sessions = [exc .submit (_simulate_call , TEST_URI ) for _ in range (2 )]
222
220
223
221
# assert unique keys are generated per thread for the same uri
224
- assert len (request . _session_cache ._data ) == 2
222
+ assert len (request_session_manager . session_cache ._data ) == 2
225
223
226
224
# -- teardown -- #
227
225
@@ -257,12 +255,12 @@ async def test_async_json_make_get_request(mocker):
257
255
mocker .patch ("aiohttp.ClientSession.get" , return_value = AsyncMockedResponse ())
258
256
259
257
# Submit a first request to create a session with default parameters
260
- assert len (request . _async_session_cache ) == 0
261
- response = await request .async_json_make_get_request (TEST_URI )
258
+ assert len (request_session_manager . session_cache ) == 0
259
+ response = await request_session_manager .async_json_make_get_request (TEST_URI )
262
260
assert response == json .dumps ({"data" : "content" })
263
- assert len (request . _async_session_cache ) == 1
261
+ assert len (request_session_manager . session_cache ) == 1
264
262
cache_key = generate_cache_key (f"{ threading .get_ident ()} :{ TEST_URI } " )
265
- session = request . _async_session_cache .get_cache_entry (cache_key )
263
+ session = request_session_manager . session_cache .get_cache_entry (cache_key )
266
264
assert isinstance (session , ClientSession )
267
265
session .get .assert_called_once_with (
268
266
TEST_URI ,
@@ -278,12 +276,14 @@ async def test_async_make_post_request(mocker):
278
276
mocker .patch ("aiohttp.ClientSession.post" , return_value = AsyncMockedResponse ())
279
277
280
278
# Submit a first request to create a session with default parameters
281
- assert len (request ._async_session_cache ) == 0
282
- response = await request .async_make_post_request (TEST_URI , data = b"request" )
279
+ assert len (request_session_manager .session_cache ) == 0
280
+ response = await request_session_manager .async_make_post_request (
281
+ TEST_URI , data = b"request"
282
+ )
283
283
assert response == "content"
284
- assert len (request . _async_session_cache ) == 1
284
+ assert len (request_session_manager . session_cache ) == 1
285
285
cache_key = generate_cache_key (f"{ threading .get_ident ()} :{ TEST_URI } " )
286
- session = request . _async_session_cache .get_cache_entry (cache_key )
286
+ session = request_session_manager . session_cache .get_cache_entry (cache_key )
287
287
assert isinstance (session , ClientSession )
288
288
session .post .assert_called_once_with (
289
289
TEST_URI ,
@@ -299,36 +299,41 @@ async def test_async_make_post_request(mocker):
299
299
async def test_async_precached_session ():
300
300
# Add a session
301
301
session = ClientSession ()
302
- await request .async_cache_and_return_session (TEST_URI , session )
303
- assert len (request . _async_session_cache ) == 1
302
+ await request_session_manager .async_cache_and_return_session (TEST_URI , session )
303
+ assert len (request_session_manager . session_cache ) == 1
304
304
305
305
# Make sure the session isn't duplicated
306
- await request .async_cache_and_return_session (TEST_URI , session )
307
- assert len (request . _async_session_cache ) == 1
306
+ await request_session_manager .async_cache_and_return_session (TEST_URI , session )
307
+ assert len (request_session_manager . session_cache ) == 1
308
308
309
309
# Make sure a request with a different URI adds another cached session
310
- await request .async_cache_and_return_session (URI (f"{ TEST_URI } /test" ), session )
311
- assert len (request ._async_session_cache ) == 2
310
+ await request_session_manager .async_cache_and_return_session (
311
+ URI (f"{ TEST_URI } /test" ), session
312
+ )
313
+ assert len (request_session_manager .session_cache ) == 2
312
314
313
315
# -- teardown -- #
314
316
315
317
# appropriately close the cached sessions
316
- [await session .close () for session in request ._async_session_cache ._data .values ()]
318
+ [
319
+ await session .close ()
320
+ for session in request_session_manager .session_cache ._data .values ()
321
+ ]
317
322
318
323
319
324
@pytest .mark .asyncio
320
325
async def test_async_cache_does_not_close_session_before_a_call_when_multithreading ():
321
326
# save default values
322
- session_cache_default = request . _async_session_cache
327
+ session_cache_default = request_session_manager . session_cache
323
328
timeout_default = request .DEFAULT_TIMEOUT
324
329
325
330
# set cache size to 1 + set future session close thread time to 0.01s
326
- request . _async_session_cache = SimpleCache (1 )
331
+ request_session_manager . session_cache = SimpleCache (1 )
327
332
_timeout_for_testing = 0.01
328
333
request .DEFAULT_TIMEOUT = _timeout_for_testing
329
334
330
335
async def cache_uri_and_return_session (uri ):
331
- _session = await async_cache_and_return_session (uri )
336
+ _session = await request_session_manager . async_cache_and_return_session (uri )
332
337
333
338
# simulate a call taking 0.01s to return a response
334
339
await asyncio .sleep (0.01 )
@@ -343,7 +348,7 @@ async def cache_uri_and_return_session(uri):
343
348
assert all (isinstance (s , ClientSession ) for s in all_sessions )
344
349
345
350
# last session remains in cache, all others evicted
346
- cache_data = request . _async_session_cache ._data
351
+ cache_data = request_session_manager . session_cache ._data
347
352
assert len (cache_data ) == 1
348
353
_key , cached_session = cache_data .popitem ()
349
354
assert cached_session == all_sessions [- 1 ]
@@ -358,8 +363,8 @@ async def cache_uri_and_return_session(uri):
358
363
await cached_session .close ()
359
364
360
365
# reset default values
361
- request . _async_session_cache = session_cache_default
362
- request .DEFAULT_TIMEOUT = timeout_default
366
+ request_session_manager . session_cache = session_cache_default
367
+ request_session_manager .DEFAULT_TIMEOUT = timeout_default
363
368
364
369
365
370
@pytest .mark .asyncio
@@ -373,7 +378,7 @@ async def test_async_unique_cache_keys_created_per_thread_with_same_uri():
373
378
def target_function (endpoint_uri ):
374
379
event_loop = asyncio .new_event_loop ()
375
380
unique_session = event_loop .run_until_complete (
376
- async_cache_and_return_session (endpoint_uri )
381
+ request_session_manager . async_cache_and_return_session (endpoint_uri )
377
382
)
378
383
event_loop .close ()
379
384
test_sessions .append (unique_session )
@@ -393,7 +398,7 @@ def target_function(endpoint_uri):
393
398
394
399
# assert unique keys are generated per thread, w/ unique event loops,
395
400
# for the same uri
396
- assert len (request . _async_session_cache ._data ) == 2
401
+ assert len (request_session_manager . session_cache ._data ) == 2
397
402
398
403
# -- teardown -- #
399
404
@@ -409,29 +414,31 @@ async def test_async_use_new_session_if_loop_closed_for_cached_session():
409
414
session1 = ClientSession (raise_for_status = True )
410
415
session1 ._loop = loop1
411
416
412
- await async_cache_and_return_session (TEST_URI , session = session1 )
417
+ await request_session_manager .async_cache_and_return_session (
418
+ TEST_URI , session = session1
419
+ )
413
420
414
421
# assert session1 was cached
415
422
cache_key = generate_cache_key (f"{ threading .get_ident ()} :{ TEST_URI } " )
416
423
417
- assert len (request . _async_session_cache ) == 1
418
- cached_session = request . _async_session_cache .get_cache_entry (cache_key )
424
+ assert len (request_session_manager . session_cache ) == 1
425
+ cached_session = request_session_manager . session_cache .get_cache_entry (cache_key )
419
426
assert cached_session == session1
420
427
421
428
# close loop that was used with session1
422
429
loop1 .close ()
423
430
424
431
# assert we create a new session when trying to retrieve the session at the
425
432
# cache key for TEST_URI
426
- session2 = await async_cache_and_return_session (TEST_URI )
433
+ session2 = await request_session_manager . async_cache_and_return_session (TEST_URI )
427
434
assert not session2 ._loop .is_closed ()
428
435
assert session2 != session1
429
436
430
437
# assert we appropriately closed session1, evicted it from the cache, and cached
431
438
# the new session2 at the cache key
432
439
assert session1 .closed
433
- assert len (request . _async_session_cache ) == 1
434
- cached_session = request . _async_session_cache .get_cache_entry (cache_key )
440
+ assert len (request_session_manager . session_cache ) == 1
441
+ cached_session = request_session_manager . session_cache .get_cache_entry (cache_key )
435
442
assert cached_session == session2
436
443
437
444
# -- teardown -- #
@@ -445,24 +452,26 @@ async def test_async_use_new_session_if_session_closed_for_cached_session():
445
452
# create a session, close it, and cache it at the cache key for TEST_URI
446
453
session1 = ClientSession (raise_for_status = True )
447
454
await session1 .close ()
448
- await async_cache_and_return_session (TEST_URI , session = session1 )
455
+ await request_session_manager .async_cache_and_return_session (
456
+ TEST_URI , session = session1
457
+ )
449
458
450
459
# assert session1 was cached
451
460
cache_key = generate_cache_key (f"{ threading .get_ident ()} :{ TEST_URI } " )
452
461
453
- assert len (request . _async_session_cache ) == 1
454
- cached_session = request . _async_session_cache .get_cache_entry (cache_key )
462
+ assert len (request_session_manager . session_cache ) == 1
463
+ cached_session = request_session_manager . session_cache .get_cache_entry (cache_key )
455
464
assert cached_session == session1
456
465
457
466
# assert we create a new session when trying to retrieve closed session from cache
458
- session2 = await async_cache_and_return_session (TEST_URI )
467
+ session2 = await request_session_manager . async_cache_and_return_session (TEST_URI )
459
468
assert not session2 .closed
460
469
assert session2 != session1
461
470
462
471
# assert we evicted session1 from the cache, and cached the new session2
463
472
# at the cache key
464
- assert len (request . _async_session_cache ) == 1
465
- cached_session = request . _async_session_cache .get_cache_entry (cache_key )
473
+ assert len (request_session_manager . session_cache ) == 1
474
+ cached_session = request_session_manager . session_cache .get_cache_entry (cache_key )
466
475
assert cached_session == session2
467
476
468
477
# -- teardown -- #
0 commit comments