@@ -319,29 +319,29 @@ def test_socket_options(self):
319
319
"""Test that connections accept socket options."""
320
320
# This test needs to be here in order to be run. socket.create_connection actually tries to
321
321
# connect to the host provided so we need a dummyserver to be running.
322
- pool = HTTPConnectionPool (
322
+ with HTTPConnectionPool (
323
323
self .host ,
324
324
self .port ,
325
325
socket_options = [(socket .SOL_SOCKET , socket .SO_KEEPALIVE , 1 )],
326
- )
327
- conn = pool ._new_conn ()
328
- conn .connect ()
329
- s = conn ._sock
330
- using_keepalive = s .getsockopt (socket .SOL_SOCKET , socket .SO_KEEPALIVE ) > 0
331
- assert using_keepalive
332
- s .close ()
326
+ ) as pool :
327
+ conn = pool ._new_conn ()
328
+ conn .connect ()
329
+ s = conn ._sock
330
+ using_keepalive = s .getsockopt (socket .SOL_SOCKET , socket .SO_KEEPALIVE ) > 0
331
+ assert using_keepalive
332
+ s .close ()
333
333
334
334
def test_disable_default_socket_options (self ):
335
335
"""Test that passing None disables all socket options."""
336
336
# This test needs to be here in order to be run. socket.create_connection actually tries
337
337
# to connect to the host provided so we need a dummyserver to be running.
338
- pool = HTTPConnectionPool (self .host , self .port , socket_options = None )
339
- conn = pool ._new_conn ()
340
- conn .connect ()
341
- s = conn ._sock
342
- using_nagle = s .getsockopt (socket .IPPROTO_TCP , socket .TCP_NODELAY ) == 0
343
- assert using_nagle
344
- s .close ()
338
+ with HTTPConnectionPool (self .host , self .port , socket_options = None ) as pool :
339
+ conn = pool ._new_conn ()
340
+ conn .connect ()
341
+ s = conn ._sock
342
+ using_nagle = s .getsockopt (socket .IPPROTO_TCP , socket .TCP_NODELAY ) == 0
343
+ assert using_nagle
344
+ s .close ()
345
345
346
346
def test_defaults_are_applied (self ):
347
347
"""Test that modifying the default socket options works."""
@@ -372,12 +372,12 @@ def test_defaults_are_applied(self):
372
372
def test_connection_error_retries (self ):
373
373
""" ECONNREFUSED error should raise a connection error, with retries """
374
374
port = find_unused_port ()
375
- pool = HTTPConnectionPool (self .host , port )
376
- try :
377
- pool .request ("GET" , "/" , retries = Retry (connect = 3 ))
378
- self .fail ("Should have failed with a connection error." )
379
- except MaxRetryError as e :
380
- assert type (e .reason ) == NewConnectionError
375
+ with HTTPConnectionPool (self .host , port ) as pool :
376
+ try :
377
+ pool .request ("GET" , "/" , retries = Retry (connect = 3 ))
378
+ self .fail ("Should have failed with a connection error." )
379
+ except MaxRetryError as e :
380
+ assert type (e .reason ) == NewConnectionError
381
381
382
382
def test_timeout_success (self ):
383
383
timeout = Timeout (connect = 3 , read = 5 , total = None )
@@ -395,12 +395,12 @@ def test_timeout_success(self):
395
395
pool .request ("GET" , "/" )
396
396
397
397
def test_bad_connect (self ):
398
- pool = HTTPConnectionPool ("badhost.invalid" , self .port )
399
- try :
400
- pool .request ("GET" , "/" , retries = 5 )
401
- self .fail ("should raise timeout exception here" )
402
- except MaxRetryError as e :
403
- assert type (e .reason ) == NewConnectionError
398
+ with HTTPConnectionPool ("badhost.invalid" , self .port ) as pool :
399
+ try :
400
+ pool .request ("GET" , "/" , retries = 5 )
401
+ self .fail ("should raise timeout exception here" )
402
+ except MaxRetryError as e :
403
+ assert type (e .reason ) == NewConnectionError
404
404
405
405
def test_keepalive (self ):
406
406
with HTTPConnectionPool (self .host , self .port , block = True , maxsize = 1 ) as pool :
@@ -567,59 +567,58 @@ def test_lazy_load_twice(self):
567
567
# This test is sad and confusing. Need to figure out what's
568
568
# going on with partial reads and socket reuse.
569
569
570
- pool = HTTPConnectionPool (
570
+ with HTTPConnectionPool (
571
571
self .host , self .port , block = True , maxsize = 1 , timeout = 2
572
- )
573
-
574
- payload_size = 1024 * 2
575
- first_chunk = 512
572
+ ) as pool :
573
+ payload_size = 1024 * 2
574
+ first_chunk = 512
576
575
577
- boundary = "foo"
576
+ boundary = "foo"
578
577
579
- req_data = {"count" : "a" * payload_size }
580
- resp_data = encode_multipart_formdata (req_data , boundary = boundary )[0 ]
578
+ req_data = {"count" : "a" * payload_size }
579
+ resp_data = encode_multipart_formdata (req_data , boundary = boundary )[0 ]
581
580
582
- req2_data = {"count" : "b" * payload_size }
583
- resp2_data = encode_multipart_formdata (req2_data , boundary = boundary )[0 ]
581
+ req2_data = {"count" : "b" * payload_size }
582
+ resp2_data = encode_multipart_formdata (req2_data , boundary = boundary )[0 ]
584
583
585
- r1 = pool .request (
586
- "POST" ,
587
- "/echo" ,
588
- fields = req_data ,
589
- multipart_boundary = boundary ,
590
- preload_content = False ,
591
- )
592
-
593
- first_data = r1 .read (first_chunk )
594
- assert len (first_data ) > 0
595
- assert first_data == resp_data [: len (first_data )]
596
-
597
- try :
598
- r2 = pool .request (
584
+ r1 = pool .request (
599
585
"POST" ,
600
586
"/echo" ,
601
- fields = req2_data ,
587
+ fields = req_data ,
602
588
multipart_boundary = boundary ,
603
589
preload_content = False ,
604
- pool_timeout = 0.001 ,
605
590
)
606
591
607
- # This branch should generally bail here, but maybe someday it will
608
- # work? Perhaps by some sort of magic. Consider it a TODO.
592
+ first_data = r1 .read (first_chunk )
593
+ assert len (first_data ) > 0
594
+ assert first_data == resp_data [: len (first_data )]
595
+
596
+ try :
597
+ r2 = pool .request (
598
+ "POST" ,
599
+ "/echo" ,
600
+ fields = req2_data ,
601
+ multipart_boundary = boundary ,
602
+ preload_content = False ,
603
+ pool_timeout = 0.001 ,
604
+ )
609
605
610
- second_data = r2 .read (first_chunk )
611
- assert len (second_data ) > 0
612
- assert second_data == resp2_data [: len (second_data )]
606
+ # This branch should generally bail here, but maybe someday it will
607
+ # work? Perhaps by some sort of magic. Consider it a TODO.
613
608
614
- assert r1 . read () == resp_data [ len ( first_data ) :]
615
- assert r2 . read () == resp2_data [ len (second_data ) :]
616
- assert pool . num_requests == 2
609
+ second_data = r2 . read ( first_chunk )
610
+ assert len (second_data ) > 0
611
+ assert second_data == resp2_data [: len ( second_data )]
617
612
618
- except EmptyPoolError :
619
- assert r1 .read () == resp_data [len (first_data ) :]
620
- assert pool .num_requests == 1
613
+ assert r1 . read () == resp_data [ len ( first_data ) :]
614
+ assert r2 .read () == resp2_data [len (second_data ) :]
615
+ assert pool .num_requests == 2
621
616
622
- assert pool .num_connections == 1
617
+ except EmptyPoolError :
618
+ assert r1 .read () == resp_data [len (first_data ) :]
619
+ assert pool .num_requests == 1
620
+
621
+ assert pool .num_connections == 1
623
622
624
623
def test_for_double_release (self ):
625
624
MAXSIZE = 5
@@ -653,11 +652,11 @@ def test_for_double_release(self):
653
652
654
653
def test_connections_arent_released (self ):
655
654
MAXSIZE = 5
656
- pool = HTTPConnectionPool (self .host , self .port , maxsize = MAXSIZE )
657
- assert pool .pool .qsize () == MAXSIZE
655
+ with HTTPConnectionPool (self .host , self .port , maxsize = MAXSIZE ) as pool :
656
+ assert pool .pool .qsize () == MAXSIZE
658
657
659
- pool .request ("GET" , "/" , preload_content = False )
660
- assert pool .pool .qsize () == MAXSIZE - 1
658
+ pool .request ("GET" , "/" , preload_content = False )
659
+ assert pool .pool .qsize () == MAXSIZE - 1
661
660
662
661
def test_dns_error (self ):
663
662
pool = HTTPConnectionPool (
@@ -684,11 +683,11 @@ def test_source_address(self):
684
683
685
684
def test_source_address_error (self ):
686
685
for addr in INVALID_SOURCE_ADDRESSES :
687
- pool = HTTPConnectionPool (
686
+ with HTTPConnectionPool (
688
687
self .host , self .port , source_address = addr , retries = False
689
- )
690
- with pytest .raises (NewConnectionError ):
691
- pool .request ("GET" , "/source_address?{0}" .format (addr ))
688
+ ) as pool :
689
+ with pytest .raises (NewConnectionError ):
690
+ pool .request ("GET" , "/source_address?{0}" .format (addr ))
692
691
693
692
def test_stream_keepalive (self ):
694
693
x = 2
0 commit comments