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

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

Issue 26404: socketserver context manager
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:
View unified diff | Download patch
« Doc/library/socketserver.rst ('K') | « Lib/socketserver.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 if proto == socket.AF_INET: 93 if proto == socket.AF_INET:
94 return (HOST, 0) 94 return (HOST, 0)
95 else: 95 else:
96 # XXX: We need a way to tell AF_UNIX to pick its own name 96 # XXX: We need a way to tell AF_UNIX to pick its own name
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 @reap_threads
104 def run_server(self, svrcls, hdlrbase, testfunc):
104 class MyServer(svrcls): 105 class MyServer(svrcls):
105 def handle_error(self, request, client_address): 106 def handle_error(self, request, client_address):
106 self.close_request(request) 107 self.close_request(request)
107 self.server_close()
108 raise 108 raise
109 109
110 class MyHandler(hdlrbase): 110 class MyHandler(hdlrbase):
111 def handle(self): 111 def handle(self):
112 line = self.rfile.readline() 112 line = self.rfile.readline()
113 self.wfile.write(line) 113 self.wfile.write(line)
114 114
115 if verbose: print("creating server") 115 if verbose: print("creating server")
116 server = MyServer(addr, MyHandler) 116 with MyServer(self.pickaddr(svrcls.address_family), MyHandler) as server :
117 self.assertEqual(server.server_address, server.socket.getsockname()) 117 self.assertEqual(server.server_address, server.socket.getsockname())
118 return server 118 # We had the OS pick a port, so pull the real address out of
119 # the server.
120 addr = server.server_address
121 if verbose:
122 print("ADDR =", addr)
123 print("CLASS =", svrcls)
119 124
120 @reap_threads 125 t = threading.Thread(
121 def run_server(self, svrcls, hdlrbase, testfunc): 126 name='%s serving' % svrcls,
122 server = self.make_server(self.pickaddr(svrcls.address_family), 127 target=server.serve_forever,
Martin Panter 2016/02/22 11:57:06 Just write “with self.make_server(...) as server”
123 svrcls, hdlrbase) 128 # Short poll interval to make the test finish quickly.
124 # We had the OS pick a port, so pull the real address out of 129 # Time between requests is short enough that we won't wake
125 # the server. 130 # up spuriously too many times.
126 addr = server.server_address 131 kwargs={'poll_interval':0.01})
127 if verbose: 132 t.daemon = True # In case this function raises.
128 print("ADDR =", addr) 133 t.start()
129 print("CLASS =", svrcls) 134 if verbose: print("server running")
130 135 for i in range(3):
131 t = threading.Thread( 136 if verbose: print("test client", i)
132 name='%s serving' % svrcls, 137 testfunc(svrcls.address_family, addr)
133 target=server.serve_forever, 138 if verbose: print("waiting for server")
134 # Short poll interval to make the test finish quickly. 139 server.shutdown()
135 # Time between requests is short enough that we won't wake 140 t.join()
136 # up spuriously too many times.
137 kwargs={'poll_interval':0.01})
138 t.daemon = True # In case this function raises.
139 t.start()
140 if verbose: print("server running")
141 for i in range(3):
142 if verbose: print("test client", i)
143 testfunc(svrcls.address_family, addr)
144 if verbose: print("waiting for server")
145 server.shutdown()
146 t.join()
147 server.server_close()
Martin Panter 2016/02/22 11:57:06 Would be nice to keep testing that server_close()
148 self.assertEqual(-1, server.socket.fileno()) 141 self.assertEqual(-1, server.socket.fileno())
149 if verbose: print("done") 142 if verbose: print("done")
150 143
151 def stream_examine(self, proto, addr): 144 def stream_examine(self, proto, addr):
152 s = socket.socket(proto, socket.SOCK_STREAM) 145 s = socket.socket(proto, socket.SOCK_STREAM)
153 s.connect(addr) 146 s.connect(addr)
154 s.sendall(TEST_STR) 147 s.sendall(TEST_STR)
155 buf = data = receive(s, 100) 148 buf = data = receive(s, 100)
156 while data and b'\n' not in buf: 149 while data and b'\n' not in buf:
157 data = receive(s, 100) 150 data = receive(s, 100)
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 s = socket.socket(server.address_family, socket.SOCK_STREAM) 395 s = socket.socket(server.address_family, socket.SOCK_STREAM)
403 s.connect(server.server_address) 396 s.connect(server.server_address)
404 s.close() 397 s.close()
405 server.handle_request() 398 server.handle_request()
406 self.assertEqual(server.shutdown_called, 1) 399 self.assertEqual(server.shutdown_called, 1)
407 server.server_close() 400 server.server_close()
408 401
409 402
410 if __name__ == "__main__": 403 if __name__ == "__main__":
411 unittest.main() 404 unittest.main()
OLDNEW
« Doc/library/socketserver.rst ('K') | « Lib/socketserver.py ('k') | no next file » | no next file with comments »

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