Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Unified Diff: Lib/test/test_socketserver.py

Issue 26404: socketserver context manager
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
--- a/Lib/test/test_socketserver.py Tue Feb 23 22:10:06 2016 +1100
+++ b/Lib/test/test_socketserver.py Wed Feb 24 11:00:00 2016 +0200
@@ -104,7 +104,6 @@
class MyServer(svrcls):
def handle_error(self, request, client_address):
self.close_request(request)
- self.server_close()
raise
class MyHandler(hdlrbase):
@@ -119,32 +118,31 @@
@reap_threads
def run_server(self, svrcls, hdlrbase, testfunc):
- server = self.make_server(self.pickaddr(svrcls.address_family),
berkerpeksag 2016/02/24 14:09:38 Please don't change test code. It will make using
palaviv 2016/02/25 18:27:05 Done.
- svrcls, hdlrbase)
- # We had the OS pick a port, so pull the real address out of
- # the server.
- addr = server.server_address
- if verbose:
- print("ADDR =", addr)
- print("CLASS =", svrcls)
+ with self.make_server(self.pickaddr(svrcls.address_family),
+ svrcls, hdlrbase) as server:
+ # We had the OS pick a port, so pull the real address out of
+ # the server.
+ addr = server.server_address
+ if verbose:
+ print("ADDR =", addr)
+ print("CLASS =", svrcls)
- t = threading.Thread(
- name='%s serving' % svrcls,
- target=server.serve_forever,
- # Short poll interval to make the test finish quickly.
- # Time between requests is short enough that we won't wake
- # up spuriously too many times.
- kwargs={'poll_interval':0.01})
- t.daemon = True # In case this function raises.
- t.start()
- if verbose: print("server running")
- for i in range(3):
- if verbose: print("test client", i)
- testfunc(svrcls.address_family, addr)
- if verbose: print("waiting for server")
- server.shutdown()
- t.join()
- server.server_close()
+ t = threading.Thread(
+ name='%s serving' % svrcls,
+ target=server.serve_forever,
+ # Short poll interval to make the test finish quickly.
+ # Time between requests is short enough that we won't wake
+ # up spuriously too many times.
+ kwargs={'poll_interval':0.01})
+ t.daemon = True # In case this function raises.
+ t.start()
+ if verbose: print("server running")
+ for i in range(3):
+ if verbose: print("test client", i)
+ testfunc(svrcls.address_family, addr)
+ if verbose: print("waiting for server")
+ server.shutdown()
+ t.join()
self.assertEqual(-1, server.socket.fileno())
if verbose: print("done")
@@ -281,6 +279,12 @@
socketserver.TCPServer((HOST, -1),
socketserver.StreamRequestHandler)
+ def test_server_close(self):
+ server = socketserver.TCPServer((HOST, 0),
+ socketserver.StreamRequestHandler)
+ server.server_close()
+ self.assertEqual(-1, server.socket.fileno())
+
class ErrorHandlerTest(unittest.TestCase):
"""Test that the servers pass normal exceptions from the handler to

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+