(loop)
| 3478 | |
| 3479 | |
| 3480 | async def test_connect_with_limit_concurrent(loop) -> None: |
| 3481 | proto = create_mocked_conn() |
| 3482 | proto.should_close = False |
| 3483 | proto.is_connected.return_value = True |
| 3484 | |
| 3485 | req = ClientRequest("GET", URL("http://host:80"), loop=loop) |
| 3486 | |
| 3487 | max_connections = 2 |
| 3488 | num_connections = 0 |
| 3489 | |
| 3490 | conn = aiohttp.BaseConnector(limit=max_connections, loop=loop) |
| 3491 | |
| 3492 | # Use a real coroutine for _create_connection; a mock would mask |
| 3493 | # problems that only happen when the method yields. |
| 3494 | |
| 3495 | async def create_connection(req, traces, timeout): |
| 3496 | nonlocal num_connections |
| 3497 | num_connections += 1 |
| 3498 | await asyncio.sleep(0) |
| 3499 | |
| 3500 | # Make a new transport mock each time because acquired |
| 3501 | # transports are stored in a set. Reusing the same object |
| 3502 | # messes with the count. |
| 3503 | proto = create_mocked_conn(should_close=False) |
| 3504 | proto.is_connected.return_value = True |
| 3505 | |
| 3506 | return proto |
| 3507 | |
| 3508 | conn._create_connection = create_connection |
| 3509 | |
| 3510 | # Simulate something like a crawler. It opens a connection, does |
| 3511 | # something with it, closes it, then creates tasks that make more |
| 3512 | # connections and waits for them to finish. The crawler is started |
| 3513 | # with multiple concurrent requests and stops when it hits a |
| 3514 | # predefined maximum number of requests. |
| 3515 | |
| 3516 | max_requests = 50 |
| 3517 | num_requests = 0 |
| 3518 | start_requests = max_connections + 1 |
| 3519 | |
| 3520 | async def f(start=True): |
| 3521 | nonlocal num_requests |
| 3522 | if num_requests == max_requests: |
| 3523 | return |
| 3524 | num_requests += 1 |
| 3525 | if not start: |
| 3526 | connection = await conn.connect(req, None, ClientTimeout()) |
| 3527 | await asyncio.sleep(0) |
| 3528 | connection.release() |
| 3529 | await asyncio.sleep(0) |
| 3530 | tasks = [loop.create_task(f(start=False)) for i in range(start_requests)] |
| 3531 | await asyncio.wait(tasks) |
| 3532 | |
| 3533 | await f() |
| 3534 | await conn.close() |
| 3535 | |
| 3536 | assert max_connections == num_connections |
| 3537 |
nothing calls this directly
no test coverage detected
searching dependent graphs…