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") |
| 30 | HAVE_FORKING = hasattr(os, "fork") and os.name != "os2" |
| 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 |
| 96 | if os.name == 'os2': |
| 97 | dir = '\socket' |
| 98 | fn = tempfile.mktemp(prefix='unix_socket.', dir=dir) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 99 | if os.name == 'os2': |
| 100 | # AF_UNIX socket names on OS/2 require a specific prefix |
| 101 | # which can't include a drive letter and must also use |
| 102 | # backslashes as directory separators |
| 103 | if fn[1] == ':': |
| 104 | fn = fn[2:] |
| 105 | if fn[0] in (os.sep, os.altsep): |
| 106 | fn = fn[1:] |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 107 | if os.sep == '/': |
| 108 | fn = fn.replace(os.sep, os.altsep) |
| 109 | else: |
| 110 | fn = fn.replace(os.altsep, os.sep) |
| 111 | self.test_files.append(fn) |
| 112 | return fn |
| 113 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 114 | def make_server(self, addr, svrcls, hdlrbase): |
| 115 | class MyServer(svrcls): |
| 116 | def handle_error(self, request, client_address): |
| 117 | self.close_request(request) |
| 118 | self.server_close() |
| 119 | raise |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 120 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 121 | class MyHandler(hdlrbase): |
| 122 | def handle(self): |
| 123 | line = self.rfile.readline() |
| 124 | self.wfile.write(line) |
| 125 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 126 | if verbose: print("creating server") |
| 127 | server = MyServer(addr, MyHandler) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 128 | self.assertEqual(server.server_address, server.socket.getsockname()) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 129 | return server |
| 130 | |
Antoine Pitrou | c00d4b4 | 2009-10-27 21:32:38 +0000 | [diff] [blame] | 131 | @reap_threads |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 132 | def run_server(self, svrcls, hdlrbase, testfunc): |
| 133 | server = self.make_server(self.pickaddr(svrcls.address_family), |
| 134 | svrcls, hdlrbase) |
| 135 | # We had the OS pick a port, so pull the real address out of |
| 136 | # the server. |
| 137 | addr = server.server_address |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 138 | if verbose: |
| 139 | print("ADDR =", addr) |
| 140 | print("CLASS =", svrcls) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 141 | |
| 142 | t = threading.Thread( |
| 143 | name='%s serving' % svrcls, |
| 144 | target=server.serve_forever, |
| 145 | # Short poll interval to make the test finish quickly. |
| 146 | # Time between requests is short enough that we won't wake |
| 147 | # up spuriously too many times. |
| 148 | kwargs={'poll_interval':0.01}) |
Benjamin Peterson | 7275370 | 2008-08-18 18:09:21 +0000 | [diff] [blame] | 149 | t.daemon = True # In case this function raises. |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 150 | t.start() |
| 151 | if verbose: print("server running") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 152 | for i in range(3): |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 153 | if verbose: print("test client", i) |
| 154 | testfunc(svrcls.address_family, addr) |
| 155 | if verbose: print("waiting for server") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 156 | server.shutdown() |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 157 | t.join() |
Victor Stinner | 109761b | 2011-01-03 14:30:39 +0000 | [diff] [blame] | 158 | server.server_close() |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 159 | if verbose: print("done") |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 160 | |
| 161 | def stream_examine(self, proto, addr): |
| 162 | s = socket.socket(proto, socket.SOCK_STREAM) |
| 163 | s.connect(addr) |
| 164 | s.sendall(TEST_STR) |
| 165 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 166 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 167 | data = receive(s, 100) |
| 168 | buf += data |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 169 | self.assertEqual(buf, TEST_STR) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 170 | s.close() |
| 171 | |
| 172 | def dgram_examine(self, proto, addr): |
| 173 | s = socket.socket(proto, socket.SOCK_DGRAM) |
| 174 | s.sendto(TEST_STR, addr) |
| 175 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 176 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 177 | data = receive(s, 100) |
| 178 | buf += data |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 179 | self.assertEqual(buf, TEST_STR) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 180 | s.close() |
| 181 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 182 | def test_TCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 183 | self.run_server(socketserver.TCPServer, |
| 184 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 185 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 186 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 187 | def test_ThreadingTCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 188 | self.run_server(socketserver.ThreadingTCPServer, |
| 189 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 190 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 191 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 192 | if HAVE_FORKING: |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 193 | def test_ForkingTCPServer(self): |
| 194 | with simple_subprocess(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 195 | self.run_server(socketserver.ForkingTCPServer, |
| 196 | socketserver.StreamRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 197 | self.stream_examine) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 198 | |
| 199 | if HAVE_UNIX_SOCKETS: |
| 200 | def test_UnixStreamServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 201 | self.run_server(socketserver.UnixStreamServer, |
| 202 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 203 | self.stream_examine) |
| 204 | |
| 205 | def test_ThreadingUnixStreamServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 206 | self.run_server(socketserver.ThreadingUnixStreamServer, |
| 207 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 208 | self.stream_examine) |
| 209 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 210 | if HAVE_FORKING: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 211 | def test_ForkingUnixStreamServer(self): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 212 | with simple_subprocess(self): |
| 213 | self.run_server(ForkingUnixStreamServer, |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 214 | socketserver.StreamRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 215 | self.stream_examine) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 216 | |
| 217 | def test_UDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 218 | self.run_server(socketserver.UDPServer, |
| 219 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 220 | self.dgram_examine) |
| 221 | |
| 222 | def test_ThreadingUDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 223 | self.run_server(socketserver.ThreadingUDPServer, |
| 224 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 225 | self.dgram_examine) |
| 226 | |
| 227 | if HAVE_FORKING: |
| 228 | def test_ForkingUDPServer(self): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 229 | with simple_subprocess(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 230 | self.run_server(socketserver.ForkingUDPServer, |
| 231 | socketserver.DatagramRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 232 | self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | b0a9c66 | 2012-04-09 00:47:24 +0200 | [diff] [blame] | 234 | @contextlib.contextmanager |
| 235 | def mocked_select_module(self): |
| 236 | """Mocks the select.select() call to raise EINTR for first call""" |
| 237 | old_select = select.select |
| 238 | |
| 239 | class MockSelect: |
| 240 | def __init__(self): |
| 241 | self.called = 0 |
| 242 | |
| 243 | def __call__(self, *args): |
| 244 | self.called += 1 |
| 245 | if self.called == 1: |
| 246 | # raise the exception on first call |
Antoine Pitrou | c0aa9ee | 2012-04-09 01:37:19 +0200 | [diff] [blame] | 247 | raise select.error(errno.EINTR, os.strerror(errno.EINTR)) |
Antoine Pitrou | b0a9c66 | 2012-04-09 00:47:24 +0200 | [diff] [blame] | 248 | else: |
| 249 | # Return real select value for consecutive calls |
| 250 | return old_select(*args) |
| 251 | |
| 252 | select.select = MockSelect() |
| 253 | try: |
| 254 | yield select.select |
| 255 | finally: |
| 256 | select.select = old_select |
| 257 | |
| 258 | def test_InterruptServerSelectCall(self): |
| 259 | with self.mocked_select_module() as mock_select: |
| 260 | pid = self.run_server(socketserver.TCPServer, |
| 261 | socketserver.StreamRequestHandler, |
| 262 | self.stream_examine) |
| 263 | # Make sure select was called again: |
| 264 | self.assertGreater(mock_select.called, 1) |
| 265 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 266 | # Alas, on Linux (at least) recvfrom() doesn't return a meaningful |
| 267 | # client address so this cannot work: |
| 268 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 269 | # if HAVE_UNIX_SOCKETS: |
| 270 | # def test_UnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 271 | # self.run_server(socketserver.UnixDatagramServer, |
| 272 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 273 | # self.dgram_examine) |
| 274 | # |
| 275 | # def test_ThreadingUnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 276 | # self.run_server(socketserver.ThreadingUnixDatagramServer, |
| 277 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 278 | # self.dgram_examine) |
| 279 | # |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 280 | # if HAVE_FORKING: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 281 | # def test_ForkingUnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 282 | # self.run_server(socketserver.ForkingUnixDatagramServer, |
| 283 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 284 | # self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | 3bcba8e | 2010-04-25 22:01:43 +0000 | [diff] [blame] | 286 | @reap_threads |
| 287 | def test_shutdown(self): |
| 288 | # Issue #2302: shutdown() should always succeed in making an |
| 289 | # other thread leave serve_forever(). |
| 290 | class MyServer(socketserver.TCPServer): |
| 291 | pass |
| 292 | |
| 293 | class MyHandler(socketserver.StreamRequestHandler): |
| 294 | pass |
| 295 | |
| 296 | threads = [] |
| 297 | for i in range(20): |
| 298 | s = MyServer((HOST, 0), MyHandler) |
| 299 | t = threading.Thread( |
| 300 | name='MyServer serving', |
| 301 | target=s.serve_forever, |
| 302 | kwargs={'poll_interval':0.01}) |
| 303 | t.daemon = True # In case this function raises. |
| 304 | threads.append((t, s)) |
| 305 | for t, s in threads: |
| 306 | t.start() |
| 307 | s.shutdown() |
| 308 | for t, s in threads: |
| 309 | t.join() |
Victor Stinner | 109761b | 2011-01-03 14:30:39 +0000 | [diff] [blame] | 310 | s.server_close() |
Antoine Pitrou | 3bcba8e | 2010-04-25 22:01:43 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 312 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 313 | def test_main(): |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 314 | if imp.lock_held(): |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 315 | # If the import lock is held, the threads will hang |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 316 | raise unittest.SkipTest("can't run when import lock is held") |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 317 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 318 | test.support.run_unittest(SocketServerTest) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 319 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 320 | if __name__ == "__main__": |
| 321 | test_main() |