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

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

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

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