@@ -45,11 +45,11 @@ def __init__(self, wal_directory):
4545
4646 def try_startup (self ):
4747 # get a port to bind to
48- s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
49- s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
50- s .bind (('localhost' , 0 ))
51- host , port = s .getsockname ()
52- s .close ()
48+ with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
49+ s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
50+ s .bind (('localhost' , 0 ))
51+ host , port = s .getsockname ()
52+ s .close ()
5353 self .host = host
5454 self .port = port
5555 program = os .path .expanduser (os .environ .get ('BEANSTALKD_PATH' , 'beanstalkd' ))
@@ -77,21 +77,23 @@ def stop(self):
7777 self .p .wait ()
7878
7979 def status (self ):
80- s = socket .create_connection ((self .host , self .port ), timeout = 0.25 )
81- s .sendall (b'stats\r \n ' )
82- top , rest = s .recv (1024 ).split (b'\r \n ' , 1 )
83- status , count = top .split (b' ' )
84- count = int (count ) + 2
85- bio = io .BytesIO ()
86- bytes_read = len (rest )
87- bio .write (rest )
88- while bytes_read < count :
89- message = s .recv (count - bytes_read )
90- if not message :
91- break
92- bio .write (message )
93- bio .seek (0 , 0 )
94- return yaml .safe_load (bio )
80+ with socket .create_connection ((self .host , self .port ), timeout = 0.25 ) as s :
81+ s .sendall (b'stats\r \n ' )
82+ raw = s .recv (1024 )
83+ top , rest = raw .split (b'\r \n ' , 1 )
84+ status , count = top .split (b' ' )
85+ count = int (count ) + 2
86+ bio = io .BytesIO ()
87+ bytes_read = len (rest )
88+ bio .write (rest )
89+ while bytes_read < count :
90+ message = s .recv (count - bytes_read )
91+ if not message :
92+ break
93+ bio .write (message )
94+ bytes_read += len (message )
95+ bio .seek (0 , 0 )
96+ return yaml .safe_load (bio )
9597
9698
9799class IntegrationBaseTestCase (TestCase ):
0 commit comments