11import contextlib
2- import time
32from typing import List , Optional , Type
43
54import pytest
5+ from tests import concurrency
66
77from httpcore import (
88 ConnectionPool ,
99 ConnectError ,
1010 PoolTimeout ,
11- ReadTimeout ,
1211 ReadError ,
12+ ReadTimeout ,
1313 UnsupportedProtocol ,
1414)
1515from httpcore .backends .base import NetworkStream
16- from httpcore .backends .mock import MockBackend , HangingStream
17- from tests import concurrency
16+ from httpcore .backends .mock import HangingStream , MockBackend
17+
1818
1919
2020def test_connection_pool_with_keepalive ():
@@ -511,21 +511,22 @@ def test_pool_under_load():
511511 """
512512 network_backend = MockBackend ([], resp_stream_cls = HangingStream )
513513
514- def fetch (_pool : ConnectionPool , * exceptions : Type [BaseException ]):
514+ def fetch (
515+ _pool : ConnectionPool , * exceptions : Type [BaseException ]
516+ ) -> None :
515517 with contextlib .suppress (* exceptions ):
516- with pool .stream (
517- "GET" ,
518- "http://a.com/" ,
519- extensions = {
520- "timeout" : {
521- "connect" : 0.1 ,
522- "read" : 0.1 ,
523- "pool" : 0.1 ,
524- "write" : 0.1 ,
525- },
518+ pool .request (
519+ "GET" ,
520+ "http://a.com/" ,
521+ extensions = {
522+ "timeout" : {
523+ "connect" : 0.1 ,
524+ "read" : 0.1 ,
525+ "pool" : 0.1 ,
526+ "write" : 0.1 ,
526527 },
527- ) as response :
528- response . read ( )
528+ },
529+ )
529530
530531 with ConnectionPool (
531532 max_connections = 1 , network_backend = network_backend
@@ -535,9 +536,10 @@ def fetch(_pool: ConnectionPool, *exceptions: Type[BaseException]):
535536 # Sending many requests to the same url. All of them but one will have PoolTimeout. One will
536537 # be finished with ReadTimeout
537538 nursery .start_soon (fetch , pool , PoolTimeout , ReadTimeout )
538- if pool .connections : # There is one connection in pool in "CONNECTING" state
539- assert pool .connections [0 ].is_connecting ()
540- with pytest .raises (ReadTimeout ): # ReadTimeout indicates that connection could be retrieved
539+ assert pool .connections [0 ].is_connecting () if pool .connections else True
540+ with pytest .raises (
541+ ReadTimeout
542+ ): # ReadTimeout indicates that connection could be retrieved
541543 fetch (pool )
542544
543545
@@ -554,11 +556,12 @@ def test_pool_timeout_connection_cleanup():
554556 b"Content-Length: 13\r \n " ,
555557 b"\r \n " ,
556558 b"Hello, world!" ,
557- ] * 2 ,
559+ ]
560+ * 2 ,
558561 )
559562
560563 with ConnectionPool (
561- network_backend = network_backend , max_connections = 2
564+ network_backend = network_backend , max_connections = 1
562565 ) as pool :
563566 timeout = {
564567 "connect" : 0.1 ,
@@ -567,12 +570,18 @@ def test_pool_timeout_connection_cleanup():
567570 "write" : 0.1 ,
568571 }
569572 with contextlib .suppress (PoolTimeout ):
570- pool .request ("GET" , "https://example.com/" , extensions = {"timeout" : timeout })
573+ pool .request (
574+ "GET" , "https://example.com/" , extensions = {"timeout" : timeout }
575+ )
571576
572577 # wait for a considerable amount of time to make sure all requests time out
573- time .sleep (0.1 )
578+ concurrency .sleep (0.1 )
574579
575- pool .request ("GET" , "https://example.com/" , extensions = {"timeout" : {** timeout , 'pool' : 0.1 }})
580+ pool .request (
581+ "GET" ,
582+ "https://example.com/" ,
583+ extensions = {"timeout" : {** timeout , "pool" : 0.1 }},
584+ )
576585
577586 if pool .connections :
578587 for conn in pool .connections :
0 commit comments