Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 1 | """ |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 2 | Test suite for socketserver. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 3 | """ |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 4 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 5 | import contextlib |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 6 | import os |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 7 | import select |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 8 | import signal |
| 9 | import socket |
Antoine Pitrou | b0a9c66 | 2012-04-09 00:47:24 +0200 | [diff] [blame] | 10 | import select |
| 11 | import errno |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 12 | import tempfile |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 13 | import unittest |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 14 | import socketserver |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 15 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 16 | import test.support |
Antoine Pitrou | c00d4b4 | 2009-10-27 21:32:38 +0000 | [diff] [blame] | 17 | from test.support import reap_children, reap_threads, verbose |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 18 | try: |
| 19 | import threading |
| 20 | except ImportError: |
| 21 | threading = None |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 22 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 23 | test.support.requires("network") |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 24 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 25 | TEST_STR = b"hello world\n" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 26 | HOST = test.support.HOST |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 27 | |
| 28 | HAVE_UNIX_SOCKETS = hasattr(socket, "AF_UNIX") |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 29 | requires_unix_sockets = unittest.skipUnless(HAVE_UNIX_SOCKETS, |
| 30 | 'requires Unix sockets') |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 31 | HAVE_FORKING = hasattr(os, "fork") |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 32 | requires_forking = unittest.skipUnless(HAVE_FORKING, 'requires forking') |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 33 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 34 | def signal_alarm(n): |
| 35 | """Call signal.alarm when it exists (i.e. not on Windows).""" |
| 36 | if hasattr(signal, 'alarm'): |
| 37 | signal.alarm(n) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 38 | |
Antoine Pitrou | f18d6f3 | 2012-04-09 01:16:47 +0200 | [diff] [blame] | 39 | # Remember real select() to avoid interferences with mocking |
| 40 | _real_select = select.select |
| 41 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 42 | def receive(sock, n, timeout=20): |
Antoine Pitrou | f18d6f3 | 2012-04-09 01:16:47 +0200 | [diff] [blame] | 43 | r, w, x = _real_select([sock], [], [], timeout) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 44 | if sock in r: |
| 45 | return sock.recv(n) |
| 46 | else: |
| 47 | raise RuntimeError("timed out on %r" % (sock,)) |
| 48 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 49 | if HAVE_UNIX_SOCKETS: |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 50 | class ForkingUnixStreamServer(socketserver.ForkingMixIn, |
| 51 | socketserver.UnixStreamServer): |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 52 | pass |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 53 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 54 | class ForkingUnixDatagramServer(socketserver.ForkingMixIn, |
| 55 | socketserver.UnixDatagramServer): |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 56 | pass |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 57 | |
| 58 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 59 | @contextlib.contextmanager |
| 60 | def simple_subprocess(testcase): |
| 61 | pid = os.fork() |
| 62 | if pid == 0: |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 63 | # Don't raise an exception; it would be caught by the test harness. |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 64 | os._exit(72) |
| 65 | yield None |
| 66 | pid2, status = os.waitpid(pid, 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 67 | testcase.assertEqual(pid2, pid) |
| 68 | testcase.assertEqual(72 << 8, status) |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 69 | |
| 70 | |
Victor Stinner | 5f22e72 | 2010-05-26 17:33:03 +0000 | [diff] [blame] | 71 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 72 | class SocketServerTest(unittest.TestCase): |
| 73 | """Test all socket servers.""" |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 74 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 75 | def setUp(self): |
Victor Stinner | 5702ae6 | 2011-07-14 14:53:24 +0200 | [diff] [blame] | 76 | signal_alarm(60) # Kill deadlocks after 60 seconds. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 77 | self.port_seed = 0 |
| 78 | self.test_files = [] |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 79 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 80 | def tearDown(self): |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 81 | signal_alarm(0) # Didn't deadlock. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 82 | reap_children() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 83 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 84 | for fn in self.test_files: |
| 85 | try: |
| 86 | os.remove(fn) |
Andrew Svetlov | ad28c7f | 2012-12-18 22:02:39 +0200 | [diff] [blame] | 87 | except OSError: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 88 | pass |
| 89 | self.test_files[:] = [] |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 90 | |
| 91 | def pickaddr(self, proto): |
| 92 | if proto == socket.AF_INET: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 93 | return (HOST, 0) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 94 | else: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 95 | # XXX: We need a way to tell AF_UNIX to pick its own name |
| 96 | # like AF_INET provides port==0. |
| 97 | dir = None |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 98 | fn = tempfile.mktemp(prefix='unix_socket.', dir=dir) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 99 | self.test_files.append(fn) |
| 100 | return fn |
| 101 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 102 | def make_server(self, addr, svrcls, hdlrbase): |
| 103 | class MyServer(svrcls): |
| 104 | def handle_error(self, request, client_address): |
| 105 | self.close_request(request) |
| 106 | self.server_close() |
| 107 | raise |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 108 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 109 | class MyHandler(hdlrbase): |
| 110 | def handle(self): |
| 111 | line = self.rfile.readline() |
| 112 | self.wfile.write(line) |
| 113 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 114 | if verbose: print("creating server") |
| 115 | server = MyServer(addr, MyHandler) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 116 | self.assertEqual(server.server_address, server.socket.getsockname()) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 117 | return server |
| 118 | |
Antoine Pitrou | c00d4b4 | 2009-10-27 21:32:38 +0000 | [diff] [blame] | 119 | @reap_threads |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 120 | def run_server(self, svrcls, hdlrbase, testfunc): |
| 121 | server = self.make_server(self.pickaddr(svrcls.address_family), |
| 122 | svrcls, hdlrbase) |
| 123 | # We had the OS pick a port, so pull the real address out of |
| 124 | # the server. |
| 125 | addr = server.server_address |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 126 | if verbose: |
| 127 | print("ADDR =", addr) |
| 128 | print("CLASS =", svrcls) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 129 | |
| 130 | t = threading.Thread( |
| 131 | name='%s serving' % svrcls, |
| 132 | target=server.serve_forever, |
| 133 | # Short poll interval to make the test finish quickly. |
| 134 | # Time between requests is short enough that we won't wake |
| 135 | # up spuriously too many times. |
| 136 | kwargs={'poll_interval':0.01}) |
Benjamin Peterson | 7275370 | 2008-08-18 18:09:21 +0000 | [diff] [blame] | 137 | t.daemon = True # In case this function raises. |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 138 | t.start() |
| 139 | if verbose: print("server running") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 140 | for i in range(3): |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 141 | if verbose: print("test client", i) |
| 142 | testfunc(svrcls.address_family, addr) |
| 143 | if verbose: print("waiting for server") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 144 | server.shutdown() |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 145 | t.join() |
Victor Stinner | 109761b | 2011-01-03 14:30:39 +0000 | [diff] [blame] | 146 | server.server_close() |
Robert Collins | 1ee9283 | 2015-07-29 12:52:40 +1200 | [diff] [blame] | 147 | self.assertEqual(-1, server.socket.fileno()) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 148 | if verbose: print("done") |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 149 | |
| 150 | def stream_examine(self, proto, addr): |
| 151 | s = socket.socket(proto, socket.SOCK_STREAM) |
| 152 | s.connect(addr) |
| 153 | s.sendall(TEST_STR) |
| 154 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 155 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 156 | data = receive(s, 100) |
| 157 | buf += data |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 158 | self.assertEqual(buf, TEST_STR) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 159 | s.close() |
| 160 | |
| 161 | def dgram_examine(self, proto, addr): |
| 162 | s = socket.socket(proto, socket.SOCK_DGRAM) |
Martin Panter | 1827eff | 2016-02-24 05:12:59 +0000 | [diff] [blame] | 163 | if HAVE_UNIX_SOCKETS and proto == socket.AF_UNIX: |
| 164 | s.bind(self.pickaddr(proto)) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 165 | s.sendto(TEST_STR, addr) |
| 166 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 167 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 168 | data = receive(s, 100) |
| 169 | buf += data |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 170 | self.assertEqual(buf, TEST_STR) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 171 | s.close() |
| 172 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 173 | def test_TCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 174 | self.run_server(socketserver.TCPServer, |
| 175 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 176 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 177 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 178 | def test_ThreadingTCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 179 | self.run_server(socketserver.ThreadingTCPServer, |
| 180 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 181 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 182 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 183 | @requires_forking |
| 184 | def test_ForkingTCPServer(self): |
| 185 | with simple_subprocess(self): |
| 186 | self.run_server(socketserver.ForkingTCPServer, |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 187 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 188 | self.stream_examine) |
| 189 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 190 | @requires_unix_sockets |
| 191 | def test_UnixStreamServer(self): |
| 192 | self.run_server(socketserver.UnixStreamServer, |
| 193 | socketserver.StreamRequestHandler, |
| 194 | self.stream_examine) |
| 195 | |
| 196 | @requires_unix_sockets |
| 197 | def test_ThreadingUnixStreamServer(self): |
| 198 | self.run_server(socketserver.ThreadingUnixStreamServer, |
| 199 | socketserver.StreamRequestHandler, |
| 200 | self.stream_examine) |
| 201 | |
| 202 | @requires_unix_sockets |
| 203 | @requires_forking |
| 204 | def test_ForkingUnixStreamServer(self): |
| 205 | with simple_subprocess(self): |
| 206 | self.run_server(ForkingUnixStreamServer, |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 207 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 208 | self.stream_examine) |
| 209 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 210 | def test_UDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 211 | self.run_server(socketserver.UDPServer, |
| 212 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 213 | self.dgram_examine) |
| 214 | |
| 215 | def test_ThreadingUDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 216 | self.run_server(socketserver.ThreadingUDPServer, |
| 217 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 218 | self.dgram_examine) |
| 219 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 220 | @requires_forking |
| 221 | def test_ForkingUDPServer(self): |
| 222 | with simple_subprocess(self): |
| 223 | self.run_server(socketserver.ForkingUDPServer, |
| 224 | socketserver.DatagramRequestHandler, |
| 225 | self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 226 | |
Martin Panter | 1827eff | 2016-02-24 05:12:59 +0000 | [diff] [blame] | 227 | @requires_unix_sockets |
| 228 | def test_UnixDatagramServer(self): |
| 229 | self.run_server(socketserver.UnixDatagramServer, |
| 230 | socketserver.DatagramRequestHandler, |
| 231 | self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 232 | |
Martin Panter | 1827eff | 2016-02-24 05:12:59 +0000 | [diff] [blame] | 233 | @requires_unix_sockets |
| 234 | def test_ThreadingUnixDatagramServer(self): |
| 235 | self.run_server(socketserver.ThreadingUnixDatagramServer, |
| 236 | socketserver.DatagramRequestHandler, |
| 237 | self.dgram_examine) |
| 238 | |
| 239 | @requires_unix_sockets |
| 240 | @requires_forking |
| 241 | def test_ForkingUnixDatagramServer(self): |
| 242 | self.run_server(ForkingUnixDatagramServer, |
| 243 | socketserver.DatagramRequestHandler, |
| 244 | self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | 3bcba8e | 2010-04-25 22:01:43 +0000 | [diff] [blame] | 246 | @reap_threads |
| 247 | def test_shutdown(self): |
| 248 | # Issue #2302: shutdown() should always succeed in making an |
| 249 | # other thread leave serve_forever(). |
| 250 | class MyServer(socketserver.TCPServer): |
| 251 | pass |
| 252 | |
| 253 | class MyHandler(socketserver.StreamRequestHandler): |
| 254 | pass |
| 255 | |
| 256 | threads = [] |
| 257 | for i in range(20): |
| 258 | s = MyServer((HOST, 0), MyHandler) |
| 259 | t = threading.Thread( |
| 260 | name='MyServer serving', |
| 261 | target=s.serve_forever, |
| 262 | kwargs={'poll_interval':0.01}) |
| 263 | t.daemon = True # In case this function raises. |
| 264 | threads.append((t, s)) |
| 265 | for t, s in threads: |
| 266 | t.start() |
| 267 | s.shutdown() |
| 268 | for t, s in threads: |
| 269 | t.join() |
Victor Stinner | 109761b | 2011-01-03 14:30:39 +0000 | [diff] [blame] | 270 | s.server_close() |
Antoine Pitrou | 3bcba8e | 2010-04-25 22:01:43 +0000 | [diff] [blame] | 271 | |
Charles-François Natali | 0f4f048 | 2014-10-13 19:19:26 +0100 | [diff] [blame] | 272 | def test_tcpserver_bind_leak(self): |
| 273 | # Issue #22435: the server socket wouldn't be closed if bind()/listen() |
| 274 | # failed. |
| 275 | # Create many servers for which bind() will fail, to see if this result |
| 276 | # in FD exhaustion. |
| 277 | for i in range(1024): |
| 278 | with self.assertRaises(OverflowError): |
| 279 | socketserver.TCPServer((HOST, -1), |
| 280 | socketserver.StreamRequestHandler) |
| 281 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 282 | |
Berker Peksag | 3265344 | 2015-02-03 11:55:09 +0200 | [diff] [blame] | 283 | class MiscTestCase(unittest.TestCase): |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 284 | |
Berker Peksag | 3265344 | 2015-02-03 11:55:09 +0200 | [diff] [blame] | 285 | def test_all(self): |
| 286 | # objects defined in the module should be in __all__ |
| 287 | expected = [] |
| 288 | for name in dir(socketserver): |
| 289 | if not name.startswith('_'): |
| 290 | mod_object = getattr(socketserver, name) |
| 291 | if getattr(mod_object, '__module__', None) == 'socketserver': |
| 292 | expected.append(name) |
| 293 | self.assertCountEqual(socketserver.__all__, expected) |
| 294 | |
Martin Panter | 4bf4274 | 2016-02-19 02:16:42 +0000 | [diff] [blame] | 295 | def test_shutdown_request_called_if_verify_request_false(self): |
| 296 | # Issue #26309: BaseServer should call shutdown_request even if |
| 297 | # verify_request is False |
| 298 | |
| 299 | class MyServer(socketserver.TCPServer): |
| 300 | def verify_request(self, request, client_address): |
| 301 | return False |
| 302 | |
| 303 | shutdown_called = 0 |
| 304 | def shutdown_request(self, request): |
| 305 | self.shutdown_called += 1 |
| 306 | socketserver.TCPServer.shutdown_request(self, request) |
| 307 | |
| 308 | server = MyServer((HOST, 0), socketserver.StreamRequestHandler) |
| 309 | s = socket.socket(server.address_family, socket.SOCK_STREAM) |
| 310 | s.connect(server.server_address) |
| 311 | s.close() |
| 312 | server.handle_request() |
| 313 | self.assertEqual(server.shutdown_called, 1) |
| 314 | server.server_close() |
| 315 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 316 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 317 | if __name__ == "__main__": |
Berker Peksag | 3265344 | 2015-02-03 11:55:09 +0200 | [diff] [blame] | 318 | unittest.main() |