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 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6 | import errno |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 7 | import imp |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 8 | import os |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 9 | import select |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 10 | import signal |
| 11 | import socket |
| 12 | import tempfile |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 13 | import threading |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 14 | import time |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 15 | import unittest |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 16 | import socketserver |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 17 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | import test.support |
| 19 | from test.support import reap_children, verbose, TestSkipped |
| 20 | from test.support import TESTFN as TEST_FILE |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 21 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 22 | test.support.requires("network") |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 23 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 24 | TEST_STR = b"hello world\n" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 25 | HOST = test.support.HOST |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 26 | |
| 27 | HAVE_UNIX_SOCKETS = hasattr(socket, "AF_UNIX") |
| 28 | HAVE_FORKING = hasattr(os, "fork") and os.name != "os2" |
| 29 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 30 | def signal_alarm(n): |
| 31 | """Call signal.alarm when it exists (i.e. not on Windows).""" |
| 32 | if hasattr(signal, 'alarm'): |
| 33 | signal.alarm(n) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 34 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 35 | def receive(sock, n, timeout=20): |
| 36 | r, w, x = select.select([sock], [], [], timeout) |
| 37 | if sock in r: |
| 38 | return sock.recv(n) |
| 39 | else: |
| 40 | raise RuntimeError("timed out on %r" % (sock,)) |
| 41 | |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 42 | if HAVE_UNIX_SOCKETS: |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 43 | class ForkingUnixStreamServer(socketserver.ForkingMixIn, |
| 44 | socketserver.UnixStreamServer): |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 45 | pass |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 46 | |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 47 | class ForkingUnixDatagramServer(socketserver.ForkingMixIn, |
| 48 | socketserver.UnixDatagramServer): |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 49 | pass |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 50 | |
| 51 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 52 | @contextlib.contextmanager |
| 53 | def simple_subprocess(testcase): |
| 54 | pid = os.fork() |
| 55 | if pid == 0: |
| 56 | # Don't throw an exception; it would be caught by the test harness. |
| 57 | os._exit(72) |
| 58 | yield None |
| 59 | pid2, status = os.waitpid(pid, 0) |
| 60 | testcase.assertEquals(pid2, pid) |
| 61 | testcase.assertEquals(72 << 8, status) |
| 62 | |
| 63 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 64 | class SocketServerTest(unittest.TestCase): |
| 65 | """Test all socket servers.""" |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 66 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 67 | def setUp(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 68 | signal_alarm(20) # Kill deadlocks after 20 seconds. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 69 | self.port_seed = 0 |
| 70 | self.test_files = [] |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 71 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 72 | def tearDown(self): |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 73 | signal_alarm(0) # Didn't deadlock. |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 74 | reap_children() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 75 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 76 | for fn in self.test_files: |
| 77 | try: |
| 78 | os.remove(fn) |
| 79 | except os.error: |
| 80 | pass |
| 81 | self.test_files[:] = [] |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 82 | |
| 83 | def pickaddr(self, proto): |
| 84 | if proto == socket.AF_INET: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 85 | return (HOST, 0) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 86 | else: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 87 | # XXX: We need a way to tell AF_UNIX to pick its own name |
| 88 | # like AF_INET provides port==0. |
| 89 | dir = None |
| 90 | if os.name == 'os2': |
| 91 | dir = '\socket' |
| 92 | fn = tempfile.mktemp(prefix='unix_socket.', dir=dir) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 93 | if os.name == 'os2': |
| 94 | # AF_UNIX socket names on OS/2 require a specific prefix |
| 95 | # which can't include a drive letter and must also use |
| 96 | # backslashes as directory separators |
| 97 | if fn[1] == ':': |
| 98 | fn = fn[2:] |
| 99 | if fn[0] in (os.sep, os.altsep): |
| 100 | fn = fn[1:] |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 101 | if os.sep == '/': |
| 102 | fn = fn.replace(os.sep, os.altsep) |
| 103 | else: |
| 104 | fn = fn.replace(os.altsep, os.sep) |
| 105 | self.test_files.append(fn) |
| 106 | return fn |
| 107 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 108 | def make_server(self, addr, svrcls, hdlrbase): |
| 109 | class MyServer(svrcls): |
| 110 | def handle_error(self, request, client_address): |
| 111 | self.close_request(request) |
| 112 | self.server_close() |
| 113 | raise |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 114 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 115 | class MyHandler(hdlrbase): |
| 116 | def handle(self): |
| 117 | line = self.rfile.readline() |
| 118 | self.wfile.write(line) |
| 119 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 120 | if verbose: print("creating server") |
| 121 | server = MyServer(addr, MyHandler) |
| 122 | self.assertEquals(server.server_address, server.socket.getsockname()) |
| 123 | return server |
| 124 | |
| 125 | def run_server(self, svrcls, hdlrbase, testfunc): |
| 126 | server = self.make_server(self.pickaddr(svrcls.address_family), |
| 127 | svrcls, hdlrbase) |
| 128 | # We had the OS pick a port, so pull the real address out of |
| 129 | # the server. |
| 130 | addr = server.server_address |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 131 | if verbose: |
| 132 | print("ADDR =", addr) |
| 133 | print("CLASS =", svrcls) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 134 | |
| 135 | t = threading.Thread( |
| 136 | name='%s serving' % svrcls, |
| 137 | target=server.serve_forever, |
| 138 | # Short poll interval to make the test finish quickly. |
| 139 | # Time between requests is short enough that we won't wake |
| 140 | # up spuriously too many times. |
| 141 | kwargs={'poll_interval':0.01}) |
| 142 | t.setDaemon(True) # In case this function raises. |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 143 | t.start() |
| 144 | if verbose: print("server running") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 145 | for i in range(3): |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 146 | if verbose: print("test client", i) |
| 147 | testfunc(svrcls.address_family, addr) |
| 148 | if verbose: print("waiting for server") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 149 | server.shutdown() |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 150 | t.join() |
| 151 | if verbose: print("done") |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 152 | |
| 153 | def stream_examine(self, proto, addr): |
| 154 | s = socket.socket(proto, socket.SOCK_STREAM) |
| 155 | s.connect(addr) |
| 156 | s.sendall(TEST_STR) |
| 157 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 158 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 159 | data = receive(s, 100) |
| 160 | buf += data |
| 161 | self.assertEquals(buf, TEST_STR) |
| 162 | s.close() |
| 163 | |
| 164 | def dgram_examine(self, proto, addr): |
| 165 | s = socket.socket(proto, socket.SOCK_DGRAM) |
| 166 | s.sendto(TEST_STR, addr) |
| 167 | buf = data = receive(s, 100) |
Christian Heimes | fdb6bb5 | 2008-02-03 17:32:13 +0000 | [diff] [blame] | 168 | while data and b'\n' not in buf: |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 169 | data = receive(s, 100) |
| 170 | buf += data |
| 171 | self.assertEquals(buf, TEST_STR) |
| 172 | s.close() |
| 173 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 174 | def test_TCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 175 | self.run_server(socketserver.TCPServer, |
| 176 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 177 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 178 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 179 | def test_ThreadingTCPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 180 | self.run_server(socketserver.ThreadingTCPServer, |
| 181 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 182 | self.stream_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 183 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 184 | if HAVE_FORKING: |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 185 | def test_ForkingTCPServer(self): |
| 186 | with simple_subprocess(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 187 | self.run_server(socketserver.ForkingTCPServer, |
| 188 | socketserver.StreamRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 189 | self.stream_examine) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 190 | |
| 191 | if HAVE_UNIX_SOCKETS: |
| 192 | def test_UnixStreamServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 193 | self.run_server(socketserver.UnixStreamServer, |
| 194 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 195 | self.stream_examine) |
| 196 | |
| 197 | def test_ThreadingUnixStreamServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 198 | self.run_server(socketserver.ThreadingUnixStreamServer, |
| 199 | socketserver.StreamRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 200 | self.stream_examine) |
| 201 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 202 | if HAVE_FORKING: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 203 | def test_ForkingUnixStreamServer(self): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 204 | with simple_subprocess(self): |
| 205 | self.run_server(ForkingUnixStreamServer, |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 206 | socketserver.StreamRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 207 | self.stream_examine) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 208 | |
| 209 | def test_UDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 210 | self.run_server(socketserver.UDPServer, |
| 211 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 212 | self.dgram_examine) |
| 213 | |
| 214 | def test_ThreadingUDPServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 215 | self.run_server(socketserver.ThreadingUDPServer, |
| 216 | socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 217 | self.dgram_examine) |
| 218 | |
| 219 | if HAVE_FORKING: |
| 220 | def test_ForkingUDPServer(self): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 221 | with simple_subprocess(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 222 | self.run_server(socketserver.ForkingUDPServer, |
| 223 | socketserver.DatagramRequestHandler, |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 224 | self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 225 | |
| 226 | # Alas, on Linux (at least) recvfrom() doesn't return a meaningful |
| 227 | # client address so this cannot work: |
| 228 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 229 | # if HAVE_UNIX_SOCKETS: |
| 230 | # def test_UnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 231 | # self.run_server(socketserver.UnixDatagramServer, |
| 232 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 233 | # self.dgram_examine) |
| 234 | # |
| 235 | # def test_ThreadingUnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 236 | # self.run_server(socketserver.ThreadingUnixDatagramServer, |
| 237 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 238 | # self.dgram_examine) |
| 239 | # |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 240 | # if HAVE_FORKING: |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 241 | # def test_ForkingUnixDatagramServer(self): |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 242 | # self.run_server(socketserver.ForkingUnixDatagramServer, |
| 243 | # socketserver.DatagramRequestHandler, |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 244 | # self.dgram_examine) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 246 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 247 | def test_main(): |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 248 | if imp.lock_held(): |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 249 | # If the import lock is held, the threads will hang |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 250 | raise TestSkipped("can't run when import lock is held") |
| 251 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 252 | test.support.run_unittest(SocketServerTest) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 253 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 254 | if __name__ == "__main__": |
| 255 | test_main() |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 256 | signal_alarm(3) # Shutdown shouldn't take more than 3 seconds. |