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