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

Delta Between Two Patch Sets: Lib/test/test_socketserver.py

Issue 26404: socketserver context manager
Left Patch Set: Created 3 years, 12 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 line = self.rfile.readline() 111 line = self.rfile.readline()
112 self.wfile.write(line) 112 self.wfile.write(line)
113 113
114 if verbose: print("creating server") 114 if verbose: print("creating server")
115 server = MyServer(addr, MyHandler) 115 server = MyServer(addr, MyHandler)
116 self.assertEqual(server.server_address, server.socket.getsockname()) 116 self.assertEqual(server.server_address, server.socket.getsockname())
117 return server 117 return server
118 118
119 @reap_threads 119 @reap_threads
120 def run_server(self, svrcls, hdlrbase, testfunc): 120 def run_server(self, svrcls, hdlrbase, testfunc):
121 with self.make_server(self.pickaddr(svrcls.address_family), svrcls, hdlr base) as server: 121 server = self.make_server(self.pickaddr(svrcls.address_family),
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
122 # We had the OS pick a port, so pull the real address out of 122 svrcls, hdlrbase)
123 # the server. 123 # We had the OS pick a port, so pull the real address out of
124 addr = server.server_address 124 # the server.
125 if verbose: 125 addr = server.server_address
126 print("ADDR =", addr) 126 if verbose:
127 print("CLASS =", svrcls) 127 print("ADDR =", addr)
128 128 print("CLASS =", svrcls)
129 t = threading.Thread( 129
130 name='%s serving' % svrcls, 130 t = threading.Thread(
131 target=server.serve_forever, 131 name='%s serving' % svrcls,
132 # Short poll interval to make the test finish quickly. 132 target=server.serve_forever,
133 # Time between requests is short enough that we won't wake 133 # Short poll interval to make the test finish quickly.
134 # up spuriously too many times. 134 # Time between requests is short enough that we won't wake
135 kwargs={'poll_interval':0.01}) 135 # up spuriously too many times.
136 t.daemon = True # In case this function raises. 136 kwargs={'poll_interval':0.01})
137 t.start() 137 t.daemon = True # In case this function raises.
138 if verbose: print("server running") 138 t.start()
139 for i in range(3): 139 if verbose: print("server running")
140 if verbose: print("test client", i) 140 for i in range(3):
141 testfunc(svrcls.address_family, addr) 141 if verbose: print("test client", i)
142 if verbose: print("waiting for server") 142 testfunc(svrcls.address_family, addr)
143 server.shutdown() 143 if verbose: print("waiting for server")
144 t.join() 144 server.shutdown()
145 t.join()
146 server.server_close()
145 self.assertEqual(-1, server.socket.fileno()) 147 self.assertEqual(-1, server.socket.fileno())
146 if verbose: print("done") 148 if verbose: print("done")
147 149
148 def stream_examine(self, proto, addr): 150 def stream_examine(self, proto, addr):
149 s = socket.socket(proto, socket.SOCK_STREAM) 151 s = socket.socket(proto, socket.SOCK_STREAM)
150 s.connect(addr) 152 s.connect(addr)
151 s.sendall(TEST_STR) 153 s.sendall(TEST_STR)
152 buf = data = receive(s, 100) 154 buf = data = receive(s, 100)
153 while data and b'\n' not in buf: 155 while data and b'\n' not in buf:
154 data = receive(s, 100) 156 data = receive(s, 100)
155 buf += data 157 buf += data
156 self.assertEqual(buf, TEST_STR) 158 self.assertEqual(buf, TEST_STR)
157 s.close() 159 s.close()
158 160
159 def dgram_examine(self, proto, addr): 161 def dgram_examine(self, proto, addr):
160 s = socket.socket(proto, socket.SOCK_DGRAM) 162 s = socket.socket(proto, socket.SOCK_DGRAM)
163 if HAVE_UNIX_SOCKETS and proto == socket.AF_UNIX:
164 s.bind(self.pickaddr(proto))
161 s.sendto(TEST_STR, addr) 165 s.sendto(TEST_STR, addr)
162 buf = data = receive(s, 100) 166 buf = data = receive(s, 100)
163 while data and b'\n' not in buf: 167 while data and b'\n' not in buf:
164 data = receive(s, 100) 168 data = receive(s, 100)
165 buf += data 169 buf += data
166 self.assertEqual(buf, TEST_STR) 170 self.assertEqual(buf, TEST_STR)
167 s.close() 171 s.close()
168 172
169 def test_TCPServer(self): 173 def test_TCPServer(self):
170 self.run_server(socketserver.TCPServer, 174 self.run_server(socketserver.TCPServer,
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 socketserver.DatagramRequestHandler, 217 socketserver.DatagramRequestHandler,
214 self.dgram_examine) 218 self.dgram_examine)
215 219
216 @requires_forking 220 @requires_forking
217 def test_ForkingUDPServer(self): 221 def test_ForkingUDPServer(self):
218 with simple_subprocess(self): 222 with simple_subprocess(self):
219 self.run_server(socketserver.ForkingUDPServer, 223 self.run_server(socketserver.ForkingUDPServer,
220 socketserver.DatagramRequestHandler, 224 socketserver.DatagramRequestHandler,
221 self.dgram_examine) 225 self.dgram_examine)
222 226
223 # Alas, on Linux (at least) recvfrom() doesn't return a meaningful 227 @requires_unix_sockets
224 # client address so this cannot work: 228 def test_UnixDatagramServer(self):
225 229 self.run_server(socketserver.UnixDatagramServer,
226 # @requires_unix_sockets 230 socketserver.DatagramRequestHandler,
227 # def test_UnixDatagramServer(self): 231 self.dgram_examine)
228 # self.run_server(socketserver.UnixDatagramServer, 232
229 # socketserver.DatagramRequestHandler, 233 @requires_unix_sockets
230 # self.dgram_examine) 234 def test_ThreadingUnixDatagramServer(self):
231 # 235 self.run_server(socketserver.ThreadingUnixDatagramServer,
232 # @requires_unix_sockets 236 socketserver.DatagramRequestHandler,
233 # def test_ThreadingUnixDatagramServer(self): 237 self.dgram_examine)
234 # self.run_server(socketserver.ThreadingUnixDatagramServer, 238
235 # socketserver.DatagramRequestHandler, 239 @requires_unix_sockets
236 # self.dgram_examine) 240 @requires_forking
237 # 241 def test_ForkingUnixDatagramServer(self):
238 # @requires_unix_sockets 242 self.run_server(ForkingUnixDatagramServer,
239 # @requires_forking 243 socketserver.DatagramRequestHandler,
240 # def test_ForkingUnixDatagramServer(self): 244 self.dgram_examine)
241 # self.run_server(socketserver.ForkingUnixDatagramServer,
242 # socketserver.DatagramRequestHandler,
243 # self.dgram_examine)
244 245
245 @reap_threads 246 @reap_threads
246 def test_shutdown(self): 247 def test_shutdown(self):
247 # Issue #2302: shutdown() should always succeed in making an 248 # Issue #2302: shutdown() should always succeed in making an
248 # other thread leave serve_forever(). 249 # other thread leave serve_forever().
249 class MyServer(socketserver.TCPServer): 250 class MyServer(socketserver.TCPServer):
250 pass 251 pass
251 252
252 class MyHandler(socketserver.StreamRequestHandler): 253 class MyHandler(socketserver.StreamRequestHandler):
253 pass 254 pass
(...skipping 17 matching lines...) Expand all
271 def test_tcpserver_bind_leak(self): 272 def test_tcpserver_bind_leak(self):
272 # Issue #22435: the server socket wouldn't be closed if bind()/listen() 273 # Issue #22435: the server socket wouldn't be closed if bind()/listen()
273 # failed. 274 # failed.
274 # Create many servers for which bind() will fail, to see if this result 275 # Create many servers for which bind() will fail, to see if this result
275 # in FD exhaustion. 276 # in FD exhaustion.
276 for i in range(1024): 277 for i in range(1024):
277 with self.assertRaises(OverflowError): 278 with self.assertRaises(OverflowError):
278 socketserver.TCPServer((HOST, -1), 279 socketserver.TCPServer((HOST, -1),
279 socketserver.StreamRequestHandler) 280 socketserver.StreamRequestHandler)
280 281
281 def test_server_close(self): 282 def test_context_manager(self):
282 server = socketserver.TCPServer((HOST, 0), 283 with socketserver.TCPServer((HOST, 0),
283 socketserver.StreamRequestHandler) 284 socketserver.StreamRequestHandler) as server :
284 server.server_close() 285 pass
285 self.assertEqual(-1, server.socket.fileno()) 286 self.assertEqual(-1, server.socket.fileno())
286 287
287 288
288 class ErrorHandlerTest(unittest.TestCase): 289 class ErrorHandlerTest(unittest.TestCase):
289 """Test that the servers pass normal exceptions from the handler to 290 """Test that the servers pass normal exceptions from the handler to
290 handle_error(), and that exiting exceptions like SystemExit and 291 handle_error(), and that exiting exceptions like SystemExit and
291 KeyboardInterrupt are not passed.""" 292 KeyboardInterrupt are not passed."""
292 293
293 def tearDown(self): 294 def tearDown(self):
294 test.support.unlink(test.support.TESTFN) 295 test.support.unlink(test.support.TESTFN)
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 s = socket.socket(server.address_family, socket.SOCK_STREAM) 406 s = socket.socket(server.address_family, socket.SOCK_STREAM)
406 s.connect(server.server_address) 407 s.connect(server.server_address)
407 s.close() 408 s.close()
408 server.handle_request() 409 server.handle_request()
409 self.assertEqual(server.shutdown_called, 1) 410 self.assertEqual(server.shutdown_called, 1)
410 server.server_close() 411 server.server_close()
411 412
412 413
413 if __name__ == "__main__": 414 if __name__ == "__main__":
414 unittest.main() 415 unittest.main()
LEFTRIGHT

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