@@ -39,11 +39,10 @@ def gzip_bytes(self, response_bytes):
39
39
"""
40
40
some web servers will send back gzipped files to save bandwidth
41
41
"""
42
- bio = BytesIO ()
43
- zipper = gzip .GzipFile (fileobj = bio , mode = "w" )
44
- zipper .write (response_bytes )
45
- zipper .close ()
46
- response_bytes = bio .getvalue ()
42
+ with BytesIO () as bio :
43
+ with gzip .GzipFile (fileobj = bio , mode = "w" ) as zipper :
44
+ zipper .write (response_bytes )
45
+ response_bytes = bio .getvalue ()
47
46
return response_bytes
48
47
49
48
def write_back_bytes (self , response_bytes ):
@@ -205,17 +204,16 @@ def test_server_and_default_headers(responder, read_method, parquet_engine):
205
204
206
205
# passing 0 for the port will let the system find an unused port
207
206
with http .server .HTTPServer (("localhost" , 0 ), responder ) as server :
208
- server_thread = threading .Thread (target = server .serve_forever )
207
+ server_thread = threading .Thread (target = server .handle_request )
209
208
server_thread .start ()
210
209
211
210
port = server .server_port
212
211
if parquet_engine is None :
213
212
df_http = read_method (f"http://localhost:{ port } " )
214
213
else :
215
214
df_http = read_method (f"http://localhost:{ port } " , engine = parquet_engine )
216
- server .shutdown ()
217
215
server .server_close ()
218
- server_thread .join ()
216
+ server_thread .join (timeout = 2 )
219
217
assert not df_http .empty
220
218
221
219
@@ -249,7 +247,7 @@ def test_server_and_custom_headers(responder, read_method, parquet_engine):
249
247
250
248
# passing 0 for the port will let the system find an unused port
251
249
with http .server .HTTPServer (("localhost" , 0 ), responder ) as server :
252
- server_thread = threading .Thread (target = server .serve_forever )
250
+ server_thread = threading .Thread (target = server .handle_request )
253
251
server_thread .start ()
254
252
255
253
port = server .server_port
@@ -264,10 +262,8 @@ def test_server_and_custom_headers(responder, read_method, parquet_engine):
264
262
storage_options = {"User-Agent" : custom_user_agent },
265
263
engine = parquet_engine ,
266
264
)
267
- server .shutdown ()
268
-
269
265
server .server_close ()
270
- server_thread .join ()
266
+ server_thread .join (timeout = 2 )
271
267
272
268
tm .assert_frame_equal (df_true , df_http )
273
269
@@ -288,17 +284,16 @@ def test_server_and_all_custom_headers(responder, read_method):
288
284
289
285
# passing 0 for the port will let the system find an unused port
290
286
with http .server .HTTPServer (("localhost" , 0 ), responder ) as server :
291
- server_thread = threading .Thread (target = server .serve_forever )
287
+ server_thread = threading .Thread (target = server .handle_request )
292
288
server_thread .start ()
293
289
294
290
port = server .server_port
295
291
df_http = read_method (
296
292
f"http://localhost:{ port } " ,
297
293
storage_options = storage_options ,
298
294
)
299
- server .shutdown ()
300
295
server .server_close ()
301
- server_thread .join ()
296
+ server_thread .join (timeout = 2 )
302
297
303
298
df_http = df_http [df_http ["0" ].isin (storage_options .keys ())]
304
299
df_http = df_http .sort_values (["0" ]).reset_index ()
0 commit comments