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

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),
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.
123 svrcls, hdlrbase) 122 svrcls, hdlrbase) as server:
124 # 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
125 # the server. 124 # the server.
126 addr = server.server_address 125 addr = server.server_address
127 if verbose: 126 if verbose:
128 print("ADDR =", addr) 127 print("ADDR =", addr)
129 print("CLASS =", svrcls) 128 print("CLASS =", svrcls)
130 129
131 t = threading.Thread( 130 t = threading.Thread(
132 name='%s serving' % svrcls, 131 name='%s serving' % svrcls,
133 target=server.serve_forever, 132 target=server.serve_forever,
134 # Short poll interval to make the test finish quickly. 133 # Short poll interval to make the test finish quickly.
135 # Time between requests is short enough that we won't wake 134 # Time between requests is short enough that we won't wake
136 # up spuriously too many times. 135 # up spuriously too many times.
137 kwargs={'poll_interval':0.01}) 136 kwargs={'poll_interval':0.01})
138 t.daemon = True # In case this function raises. 137 t.daemon = True # In case this function raises.
139 t.start() 138 t.start()
140 if verbose: print("server running") 139 if verbose: print("server running")
141 for i in range(3): 140 for i in range(3):
142 if verbose: print("test client", i) 141 if verbose: print("test client", i)
143 testfunc(svrcls.address_family, addr) 142 testfunc(svrcls.address_family, addr)
144 if verbose: print("waiting for server") 143 if verbose: print("waiting for server")
145 server.shutdown() 144 server.shutdown()
146 t.join() 145 t.join()
147 server.server_close()
148 self.assertEqual(-1, server.socket.fileno()) 146 self.assertEqual(-1, server.socket.fileno())
149 if verbose: print("done") 147 if verbose: print("done")
150 148
151 def stream_examine(self, proto, addr): 149 def stream_examine(self, proto, addr):
152 s = socket.socket(proto, socket.SOCK_STREAM) 150 s = socket.socket(proto, socket.SOCK_STREAM)
153 s.connect(addr) 151 s.connect(addr)
154 s.sendall(TEST_STR) 152 s.sendall(TEST_STR)
155 buf = data = receive(s, 100) 153 buf = data = receive(s, 100)
156 while data and b'\n' not in buf: 154 while data and b'\n' not in buf:
157 data = receive(s, 100) 155 data = receive(s, 100)
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 271
274 def test_tcpserver_bind_leak(self): 272 def test_tcpserver_bind_leak(self):
275 # 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()
276 # failed. 274 # failed.
277 # 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
278 # in FD exhaustion. 276 # in FD exhaustion.
279 for i in range(1024): 277 for i in range(1024):
280 with self.assertRaises(OverflowError): 278 with self.assertRaises(OverflowError):
281 socketserver.TCPServer((HOST, -1), 279 socketserver.TCPServer((HOST, -1),
282 socketserver.StreamRequestHandler) 280 socketserver.StreamRequestHandler)
281
282 def test_server_close(self):
283 server = socketserver.TCPServer((HOST, 0),
284 socketserver.StreamRequestHandler)
285 server.server_close()
286 self.assertEqual(-1, server.socket.fileno())
283 287
284 288
285 class ErrorHandlerTest(unittest.TestCase): 289 class ErrorHandlerTest(unittest.TestCase):
286 """Test that the servers pass normal exceptions from the handler to 290 """Test that the servers pass normal exceptions from the handler to
287 handle_error(), and that exiting exceptions like SystemExit and 291 handle_error(), and that exiting exceptions like SystemExit and
288 KeyboardInterrupt are not passed.""" 292 KeyboardInterrupt are not passed."""
289 293
290 def tearDown(self): 294 def tearDown(self):
291 test.support.unlink(test.support.TESTFN) 295 test.support.unlink(test.support.TESTFN)
292 296
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 s = socket.socket(server.address_family, socket.SOCK_STREAM) 406 s = socket.socket(server.address_family, socket.SOCK_STREAM)
403 s.connect(server.server_address) 407 s.connect(server.server_address)
404 s.close() 408 s.close()
405 server.handle_request() 409 server.handle_request()
406 self.assertEqual(server.shutdown_called, 1) 410 self.assertEqual(server.shutdown_called, 1)
407 server.server_close() 411 server.server_close()
408 412
409 413
410 if __name__ == "__main__": 414 if __name__ == "__main__":
411 unittest.main() 415 unittest.main()
OLDNEW

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