7
7
import ssl
8
8
import sys
9
9
import uuid
10
+ from collections import defaultdict , deque
10
11
from concurrent import futures
11
12
from contextlib import closing , suppress
12
- from typing import Any , List , Literal , Optional , Sequence , Tuple
13
+ from typing import Any , DefaultDict , Deque , List , Literal , Optional , Sequence , Tuple
13
14
from unittest import mock
14
15
15
16
import pytest
@@ -194,7 +195,7 @@ async def test_del_with_scheduled_cleanup(loop) -> None:
194
195
loop .set_debug (True )
195
196
conn = aiohttp .BaseConnector (loop = loop , keepalive_timeout = 0.01 )
196
197
transp = mock .Mock ()
197
- conn ._conns ["a" ] = [(transp , 123 )]
198
+ conn ._conns ["a" ] = deque ( [(transp , 123 )])
198
199
199
200
conns_impl = conn ._conns
200
201
exc_handler = mock .Mock ()
@@ -224,7 +225,7 @@ async def make_conn():
224
225
225
226
conn = loop .run_until_complete (make_conn ())
226
227
transp = mock .Mock ()
227
- conn ._conns ["a" ] = [(transp , 123 )]
228
+ conn ._conns ["a" ] = deque ( [(transp , 123 )])
228
229
229
230
conns_impl = conn ._conns
230
231
exc_handler = mock .Mock ()
@@ -283,7 +284,7 @@ async def test_close(loop) -> None:
283
284
284
285
conn = aiohttp .BaseConnector (loop = loop )
285
286
assert not conn .closed
286
- conn ._conns [("host" , 8080 , False )] = [(proto , object ())]
287
+ conn ._conns [("host" , 8080 , False )] = deque ( [(proto , object ())])
287
288
await conn .close ()
288
289
289
290
assert not conn ._conns
@@ -296,7 +297,7 @@ async def test_get(loop: asyncio.AbstractEventLoop, key: ConnectionKey) -> None:
296
297
assert await conn ._get (key , []) is None
297
298
298
299
proto = create_mocked_conn (loop )
299
- conn ._conns [key ] = [(proto , loop .time ())]
300
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
300
301
connection = await conn ._get (key , [])
301
302
assert connection is not None
302
303
assert connection .protocol == proto
@@ -310,14 +311,14 @@ async def test_get_unconnected_proto(loop) -> None:
310
311
assert await conn ._get (key , []) is None
311
312
312
313
proto = create_mocked_conn (loop )
313
- conn ._conns [key ] = [(proto , loop .time ())]
314
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
314
315
connection = await conn ._get (key , [])
315
316
assert connection is not None
316
317
assert connection .protocol == proto
317
318
connection .close ()
318
319
319
320
assert await conn ._get (key , []) is None
320
- conn ._conns [key ] = [(proto , loop .time ())]
321
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
321
322
proto .is_connected = lambda * args : False
322
323
assert await conn ._get (key , []) is None
323
324
await conn .close ()
@@ -329,14 +330,14 @@ async def test_get_unconnected_proto_ssl(loop) -> None:
329
330
assert await conn ._get (key , []) is None
330
331
331
332
proto = create_mocked_conn (loop )
332
- conn ._conns [key ] = [(proto , loop .time ())]
333
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
333
334
connection = await conn ._get (key , [])
334
335
assert connection is not None
335
336
assert connection .protocol == proto
336
337
connection .close ()
337
338
338
339
assert await conn ._get (key , []) is None
339
- conn ._conns [key ] = [(proto , loop .time ())]
340
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
340
341
proto .is_connected = lambda * args : False
341
342
assert await conn ._get (key , []) is None
342
343
await conn .close ()
@@ -348,7 +349,7 @@ async def test_get_expired(loop: asyncio.AbstractEventLoop) -> None:
348
349
assert await conn ._get (key , []) is None
349
350
350
351
proto = mock .Mock ()
351
- conn ._conns [key ] = [(proto , loop .time () - 1000 )]
352
+ conn ._conns [key ] = deque ( [(proto , loop .time () - 1000 )])
352
353
assert await conn ._get (key , []) is None
353
354
assert not conn ._conns
354
355
await conn .close ()
@@ -361,7 +362,7 @@ async def test_get_expired_ssl(loop: asyncio.AbstractEventLoop) -> None:
361
362
362
363
proto = mock .Mock ()
363
364
transport = proto .transport
364
- conn ._conns [key ] = [(proto , loop .time () - 1000 )]
365
+ conn ._conns [key ] = deque ( [(proto , loop .time () - 1000 )])
365
366
assert await conn ._get (key , []) is None
366
367
assert not conn ._conns
367
368
assert conn ._cleanup_closed_transports == [transport ]
@@ -1411,12 +1412,12 @@ async def test_release_close_do_not_delete_existing_connections(key) -> None:
1411
1412
proto1 = mock .Mock ()
1412
1413
1413
1414
conn = aiohttp .BaseConnector ()
1414
- conn ._conns [key ] = [(proto1 , 1 )]
1415
+ conn ._conns [key ] = deque ( [(proto1 , 1 )])
1415
1416
1416
1417
proto = mock .Mock (should_close = True )
1417
1418
conn ._acquired .add (proto )
1418
1419
conn ._release (key , proto )
1419
- assert conn ._conns [key ] == [(proto1 , 1 )]
1420
+ assert conn ._conns [key ] == deque ( [(proto1 , 1 )])
1420
1421
assert proto .close .called
1421
1422
await conn .close ()
1422
1423
@@ -1451,7 +1452,7 @@ async def test_connect(loop, key) -> None:
1451
1452
req = ClientRequest ("GET" , URL ("http://localhost:80" ), loop = loop )
1452
1453
1453
1454
conn = aiohttp .BaseConnector (loop = loop )
1454
- conn ._conns [key ] = [(proto , loop .time ())]
1455
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
1455
1456
conn ._create_connection = mock .Mock ()
1456
1457
conn ._create_connection .return_value = loop .create_future ()
1457
1458
conn ._create_connection .return_value .set_result (proto )
@@ -1687,12 +1688,15 @@ async def test_ctor_cleanup() -> None:
1687
1688
assert conn ._cleanup_closed_handle is not None
1688
1689
1689
1690
1690
- async def test_cleanup (key ) -> None :
1691
- testset = {
1692
- key : [(mock .Mock (), 10 ), (mock .Mock (), 300 )],
1693
- }
1694
- testset [key ][0 ][0 ].is_connected .return_value = True
1695
- testset [key ][1 ][0 ].is_connected .return_value = False
1691
+ async def test_cleanup (key : ConnectionKey ) -> None :
1692
+ m1 = mock .Mock ()
1693
+ m2 = mock .Mock ()
1694
+ m1 .is_connected .return_value = True
1695
+ m2 .is_connected .return_value = False
1696
+ testset : DefaultDict [ConnectionKey , Deque [Tuple [ResponseHandler , float ]]] = (
1697
+ defaultdict (deque )
1698
+ )
1699
+ testset [key ] = deque ([(m1 , 10 ), (m2 , 300 )])
1696
1700
1697
1701
loop = mock .Mock ()
1698
1702
loop .time .return_value = 300
@@ -1710,7 +1714,10 @@ async def test_cleanup(key) -> None:
1710
1714
async def test_cleanup_close_ssl_transport (ssl_key ) -> None :
1711
1715
proto = mock .Mock ()
1712
1716
transport = proto .transport
1713
- testset = {ssl_key : [(proto , 10 )]}
1717
+ testset : DefaultDict [ConnectionKey , Deque [Tuple [ResponseHandler , float ]]] = (
1718
+ defaultdict (deque )
1719
+ )
1720
+ testset [ssl_key ] = deque ([(proto , 10 )])
1714
1721
1715
1722
loop = mock .Mock ()
1716
1723
new_time = asyncio .get_event_loop ().time () + 300
@@ -1727,9 +1734,13 @@ async def test_cleanup_close_ssl_transport(ssl_key) -> None:
1727
1734
assert conn ._cleanup_closed_transports == [transport ]
1728
1735
1729
1736
1730
- async def test_cleanup2 () -> None :
1731
- testset = {1 : [(mock .Mock (), 300 )]}
1732
- testset [1 ][0 ][0 ].is_connected .return_value = True
1737
+ async def test_cleanup2 (loop : asyncio .AbstractEventLoop , key : ConnectionKey ) -> None :
1738
+ m = create_mocked_conn ()
1739
+ m .is_connected .return_value = True
1740
+ testset : DefaultDict [ConnectionKey , Deque [Tuple [ResponseHandler , float ]]] = (
1741
+ defaultdict (deque )
1742
+ )
1743
+ testset [key ] = deque ([(m , 300 )])
1733
1744
1734
1745
conn = aiohttp .BaseConnector (keepalive_timeout = 10 )
1735
1746
conn ._loop = mock .Mock ()
@@ -1744,9 +1755,13 @@ async def test_cleanup2() -> None:
1744
1755
await conn .close ()
1745
1756
1746
1757
1747
- async def test_cleanup3 (key ) -> None :
1748
- testset = {key : [(mock .Mock (), 290.1 ), (mock .Mock (), 305.1 )]}
1749
- testset [key ][0 ][0 ].is_connected .return_value = True
1758
+ async def test_cleanup3 (loop : asyncio .AbstractEventLoop , key : ConnectionKey ) -> None :
1759
+ m = create_mocked_conn (loop )
1760
+ m .is_connected .return_value = True
1761
+ testset : DefaultDict [ConnectionKey , Deque [Tuple [ResponseHandler , float ]]] = (
1762
+ defaultdict (deque )
1763
+ )
1764
+ testset [key ] = deque ([(m , 290.1 ), (create_mocked_conn (loop ), 305.1 )])
1750
1765
1751
1766
loop = mock .Mock ()
1752
1767
loop .time .return_value = 308.5
@@ -1757,7 +1772,7 @@ async def test_cleanup3(key) -> None:
1757
1772
with mock .patch ("aiohttp.connector.monotonic" , return_value = 308.5 ):
1758
1773
conn ._cleanup ()
1759
1774
1760
- assert conn ._conns == {key : [testset [key ][1 ]]}
1775
+ assert conn ._conns == {key : deque ( [testset [key ][1 ]]) }
1761
1776
1762
1777
assert conn ._cleanup_handle is not None
1763
1778
loop .call_at .assert_called_with (319 , mock .ANY , mock .ANY )
@@ -1927,7 +1942,7 @@ async def test_close_twice(loop) -> None:
1927
1942
proto = mock .Mock ()
1928
1943
1929
1944
conn = aiohttp .BaseConnector (loop = loop )
1930
- conn ._conns [1 ] = [(proto , object ())]
1945
+ conn ._conns [1 ] = deque ( [(proto , object ())])
1931
1946
await conn .close ()
1932
1947
1933
1948
assert not conn ._conns
@@ -2324,7 +2339,7 @@ async def test_connect_with_limit(
2324
2339
)
2325
2340
2326
2341
conn = aiohttp .BaseConnector (loop = loop , limit = 1 )
2327
- conn ._conns [key ] = [(proto , loop .time ())]
2342
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2328
2343
conn ._create_connection = mock .Mock ()
2329
2344
conn ._create_connection .return_value = loop .create_future ()
2330
2345
conn ._create_connection .return_value .set_result (proto )
@@ -2380,7 +2395,7 @@ async def test_connect_queued_operation_tracing(loop, key) -> None:
2380
2395
)
2381
2396
2382
2397
conn = aiohttp .BaseConnector (loop = loop , limit = 1 )
2383
- conn ._conns [key ] = [(proto , loop .time ())]
2398
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2384
2399
conn ._create_connection = mock .Mock ()
2385
2400
conn ._create_connection .return_value = loop .create_future ()
2386
2401
conn ._create_connection .return_value .set_result (proto )
@@ -2424,7 +2439,7 @@ async def test_connect_reuseconn_tracing(loop, key) -> None:
2424
2439
)
2425
2440
2426
2441
conn = aiohttp .BaseConnector (loop = loop , limit = 1 )
2427
- conn ._conns [key ] = [(proto , loop .time ())]
2442
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2428
2443
conn2 = await conn .connect (req , traces , ClientTimeout ())
2429
2444
conn2 .release ()
2430
2445
@@ -2441,7 +2456,7 @@ async def test_connect_with_limit_and_limit_per_host(loop, key) -> None:
2441
2456
req = ClientRequest ("GET" , URL ("http://localhost:80" ), loop = loop )
2442
2457
2443
2458
conn = aiohttp .BaseConnector (loop = loop , limit = 1000 , limit_per_host = 1 )
2444
- conn ._conns [key ] = [(proto , loop .time ())]
2459
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2445
2460
conn ._create_connection = mock .Mock ()
2446
2461
conn ._create_connection .return_value = loop .create_future ()
2447
2462
conn ._create_connection .return_value .set_result (proto )
@@ -2475,7 +2490,7 @@ async def test_connect_with_no_limit_and_limit_per_host(loop, key) -> None:
2475
2490
req = ClientRequest ("GET" , URL ("http://localhost1:80" ), loop = loop )
2476
2491
2477
2492
conn = aiohttp .BaseConnector (loop = loop , limit = 0 , limit_per_host = 1 )
2478
- conn ._conns [key ] = [(proto , loop .time ())]
2493
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2479
2494
conn ._create_connection = mock .Mock ()
2480
2495
conn ._create_connection .return_value = loop .create_future ()
2481
2496
conn ._create_connection .return_value .set_result (proto )
@@ -2507,7 +2522,7 @@ async def test_connect_with_no_limits(loop, key) -> None:
2507
2522
req = ClientRequest ("GET" , URL ("http://localhost:80" ), loop = loop )
2508
2523
2509
2524
conn = aiohttp .BaseConnector (loop = loop , limit = 0 , limit_per_host = 0 )
2510
- conn ._conns [key ] = [(proto , loop .time ())]
2525
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2511
2526
conn ._create_connection = mock .Mock ()
2512
2527
conn ._create_connection .return_value = loop .create_future ()
2513
2528
conn ._create_connection .return_value .set_result (proto )
@@ -2541,7 +2556,7 @@ async def test_connect_with_limit_cancelled(loop) -> None:
2541
2556
2542
2557
conn = aiohttp .BaseConnector (loop = loop , limit = 1 )
2543
2558
key = ("host" , 80 , False )
2544
- conn ._conns [key ] = [(proto , loop .time ())]
2559
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2545
2560
conn ._create_connection = mock .Mock ()
2546
2561
conn ._create_connection .return_value = loop .create_future ()
2547
2562
conn ._create_connection .return_value .set_result (proto )
@@ -2687,7 +2702,7 @@ async def test_close_with_acquired_connection(loop) -> None:
2687
2702
2688
2703
conn = aiohttp .BaseConnector (loop = loop , limit = 1 )
2689
2704
key = ("host" , 80 , False )
2690
- conn ._conns [key ] = [(proto , loop .time ())]
2705
+ conn ._conns [key ] = deque ( [(proto , loop .time ())])
2691
2706
conn ._create_connection = mock .Mock ()
2692
2707
conn ._create_connection .return_value = loop .create_future ()
2693
2708
conn ._create_connection .return_value .set_result (proto )
@@ -3291,7 +3306,7 @@ async def allow_connection_and_add_dummy_waiter() -> None:
3291
3306
spec_set = True ,
3292
3307
side_effect = [0 , 1 , 1 , 1 ],
3293
3308
):
3294
- connector ._conns [key ] = [(proto , loop .time ())]
3309
+ connector ._conns [key ] = deque ( [(proto , loop .time ())])
3295
3310
with mock .patch .object (
3296
3311
connector ,
3297
3312
"_create_connection" ,
0 commit comments