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

Side by Side Diff: Lib/test/test_socketserver.py

Issue 26404: socketserver context manager
Patch Set: Created 3 years, 12 months ago
Left:
Right:
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 unified diff | Download patch
OLDNEW
1 """ 1 """
2 Test suite for socketserver. 2 Test suite for socketserver.
3 """ 3 """
4 4
5 import contextlib 5 import contextlib
6 import os 6 import os
7 import select 7 import select
8 import signal 8 import signal
9 import socket 9 import socket
10 import select 10 import select
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 # like AF_INET provides port==0. 97 # like AF_INET provides port==0.
98 dir = None 98 dir = None
99 fn = tempfile.mktemp(prefix='unix_socket.', dir=dir) 99 fn = tempfile.mktemp(prefix='unix_socket.', dir=dir)
100 self.test_files.append(fn) 100 self.test_files.append(fn)
101 return fn 101 return fn
102 102
103 def make_server(self, addr, svrcls, hdlrbase): 103 def make_server(self, addr, svrcls, hdlrbase):
104 class MyServer(svrcls): 104 class MyServer(svrcls):
105 def handle_error(self, request, client_address): 105 def handle_error(self, request, client_address):
106 self.close_request(request) 106 self.close_request(request)
107 self.server_close()
108 raise 107 raise
109 108
110 class MyHandler(hdlrbase): 109 class MyHandler(hdlrbase):
111 def handle(self): 110 def handle(self):
112 line = self.rfile.readline() 111 line = self.rfile.readline()
113 self.wfile.write(line) 112 self.wfile.write(line)
114 113
115 if verbose: print("creating server") 114 if verbose: print("creating server")
116 server = MyServer(addr, MyHandler) 115 server = MyServer(addr, MyHandler)
117 self.assertEqual(server.server_address, server.socket.getsockname()) 116 self.assertEqual(server.server_address, server.socket.getsockname())
118 return server 117 return server
119 118
120 @reap_threads 119 @reap_threads
121 def run_server(self, svrcls, hdlrbase, testfunc): 120 def run_server(self, svrcls, hdlrbase, testfunc):
122 server = self.make_server(self.pickaddr(svrcls.address_family), 121 with self.make_server(self.pickaddr(svrcls.address_family), svrcls, hdlr base) as server:
Martin Panter 2016/02/24 02:55:12 Might be better to leave the original wrapped line
palaviv 2016/02/24 10:05:34 I thinking breaking the like as before will have t
123 svrcls, hdlrbase) 122 # We had the OS pick a port, so pull the real address out of
124 # We had the OS pick a port, so pull the real address out of 123 # the server.
125 # the server. 124 addr = server.server_address
126 addr = server.server_address 125 if verbose:
127 if verbose: 126 print("ADDR =", addr)
128 print("ADDR =", addr) 127 print("CLASS =", svrcls)
129 print("CLASS =", svrcls)
130 128
131 t = threading.Thread( 129 t = threading.Thread(
132 name='%s serving' % svrcls, 130 name='%s serving' % svrcls,
133 target=server.serve_forever, 131 target=server.serve_forever,
134 # Short poll interval to make the test finish quickly. 132 # Short poll interval to make the test finish quickly.
135 # Time between requests is short enough that we won't wake 133 # Time between requests is short enough that we won't wake
136 # up spuriously too many times. 134 # up spuriously too many times.
137 kwargs={'poll_interval':0.01}) 135 kwargs={'poll_interval':0.01})
138 t.daemon = True # In case this function raises. 136 t.daemon = True # In case this function raises.
139 t.start() 137 t.start()
140 if verbose: print("server running") 138 if verbose: print("server running")
141 for i in range(3): 139 for i in range(3):
142 if verbose: print("test client", i) 140 if verbose: print("test client", i)
143 testfunc(svrcls.address_family, addr) 141 testfunc(svrcls.address_family, addr)
144 if verbose: print("waiting for server") 142 if verbose: print("waiting for server")
145 server.shutdown() 143 server.shutdown()
146 t.join() 144 t.join()
147 server.server_close()
148 self.assertEqual(-1, server.socket.fileno()) 145 self.assertEqual(-1, server.socket.fileno())
149 if verbose: print("done") 146 if verbose: print("done")
150 147
151 def stream_examine(self, proto, addr): 148 def stream_examine(self, proto, addr):
152 s = socket.socket(proto, socket.SOCK_STREAM) 149 s = socket.socket(proto, socket.SOCK_STREAM)
153 s.connect(addr) 150 s.connect(addr)
154 s.sendall(TEST_STR) 151 s.sendall(TEST_STR)
155 buf = data = receive(s, 100) 152 buf = data = receive(s, 100)
156 while data and b'\n' not in buf: 153 while data and b'\n' not in buf:
157 data = receive(s, 100) 154 data = receive(s, 100)
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 270
274 def test_tcpserver_bind_leak(self): 271 def test_tcpserver_bind_leak(self):
275 # Issue #22435: the server socket wouldn't be closed if bind()/listen() 272 # Issue #22435: the server socket wouldn't be closed if bind()/listen()
276 # failed. 273 # failed.
277 # Create many servers for which bind() will fail, to see if this result 274 # Create many servers for which bind() will fail, to see if this result
278 # in FD exhaustion. 275 # in FD exhaustion.
279 for i in range(1024): 276 for i in range(1024):
280 with self.assertRaises(OverflowError): 277 with self.assertRaises(OverflowError):
281 socketserver.TCPServer((HOST, -1), 278 socketserver.TCPServer((HOST, -1),
282 socketserver.StreamRequestHandler) 279 socketserver.StreamRequestHandler)
280
281 def test_server_close(self):
282 server = socketserver.TCPServer((HOST, 0),
283 socketserver.StreamRequestHandler)
284 server.server_close()
285 self.assertEqual(-1, server.socket.fileno())
283 286
284 287
285 class ErrorHandlerTest(unittest.TestCase): 288 class ErrorHandlerTest(unittest.TestCase):
286 """Test that the servers pass normal exceptions from the handler to 289 """Test that the servers pass normal exceptions from the handler to
287 handle_error(), and that exiting exceptions like SystemExit and 290 handle_error(), and that exiting exceptions like SystemExit and
288 KeyboardInterrupt are not passed.""" 291 KeyboardInterrupt are not passed."""
289 292
290 def tearDown(self): 293 def tearDown(self):
291 test.support.unlink(test.support.TESTFN) 294 test.support.unlink(test.support.TESTFN)
292 295
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 s = socket.socket(server.address_family, socket.SOCK_STREAM) 405 s = socket.socket(server.address_family, socket.SOCK_STREAM)
403 s.connect(server.server_address) 406 s.connect(server.server_address)
404 s.close() 407 s.close()
405 server.handle_request() 408 server.handle_request()
406 self.assertEqual(server.shutdown_called, 1) 409 self.assertEqual(server.shutdown_called, 1)
407 server.server_close() 410 server.server_close()
408 411
409 412
410 if __name__ == "__main__": 413 if __name__ == "__main__":
411 unittest.main() 414 unittest.main()
OLDNEW
« Lib/http/server.py ('K') | « Lib/socketserver.py ('k') | Lib/xmlrpc/server.py » ('j') | no next file with comments »

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