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

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

Issue 26404: socketserver context manager
Patch Set: Created 4 years, 3 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())
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 271
273 def test_tcpserver_bind_leak(self): 272 def test_tcpserver_bind_leak(self):
274 # 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()
275 # failed. 274 # failed.
276 # 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
277 # in FD exhaustion. 276 # in FD exhaustion.
278 for i in range(1024): 277 for i in range(1024):
279 with self.assertRaises(OverflowError): 278 with self.assertRaises(OverflowError):
280 socketserver.TCPServer((HOST, -1), 279 socketserver.TCPServer((HOST, -1),
281 socketserver.StreamRequestHandler) 280 socketserver.StreamRequestHandler)
281
282 def test_context_manager(self):
283 with socketserver.TCPServer((HOST, 0),
284 socketserver.StreamRequestHandler) as server :
285 pass
286 self.assertEqual(-1, server.socket.fileno())
282 287
283 288
284 class ErrorHandlerTest(unittest.TestCase): 289 class ErrorHandlerTest(unittest.TestCase):
285 """Test that the servers pass normal exceptions from the handler to 290 """Test that the servers pass normal exceptions from the handler to
286 handle_error(), and that exiting exceptions like SystemExit and 291 handle_error(), and that exiting exceptions like SystemExit and
287 KeyboardInterrupt are not passed.""" 292 KeyboardInterrupt are not passed."""
288 293
289 def tearDown(self): 294 def tearDown(self):
290 test.support.unlink(test.support.TESTFN) 295 test.support.unlink(test.support.TESTFN)
291 296
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 s = socket.socket(server.address_family, socket.SOCK_STREAM) 406 s = socket.socket(server.address_family, socket.SOCK_STREAM)
402 s.connect(server.server_address) 407 s.connect(server.server_address)
403 s.close() 408 s.close()
404 server.handle_request() 409 server.handle_request()
405 self.assertEqual(server.shutdown_called, 1) 410 self.assertEqual(server.shutdown_called, 1)
406 server.server_close() 411 server.server_close()
407 412
408 413
409 if __name__ == "__main__": 414 if __name__ == "__main__":
410 unittest.main() 415 unittest.main()
OLDNEW
« Doc/whatsnew/3.6.rst ('K') | « Lib/socketserver.py ('k') | Lib/wsgiref/simple_server.py » ('j') | no next file with comments »

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