Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 1 | # Test suite for SocketServer.py |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test import test_support |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 4 | from test.test_support import (verbose, verify, TESTFN, TestSkipped, |
| 5 | reap_children) |
Tim Peters | a86f0c1 | 2001-09-18 02:18:57 +0000 | [diff] [blame] | 6 | test_support.requires('network') |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 7 | |
| 8 | from SocketServer import * |
| 9 | import socket |
Neal Norwitz | 909eb12 | 2006-06-12 02:13:21 +0000 | [diff] [blame] | 10 | import errno |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 11 | import select |
| 12 | import time |
| 13 | import threading |
| 14 | import os |
| 15 | |
| 16 | NREQ = 3 |
| 17 | DELAY = 0.5 |
| 18 | |
| 19 | class MyMixinHandler: |
| 20 | def handle(self): |
| 21 | time.sleep(DELAY) |
| 22 | line = self.rfile.readline() |
| 23 | time.sleep(DELAY) |
| 24 | self.wfile.write(line) |
| 25 | |
| 26 | class MyStreamHandler(MyMixinHandler, StreamRequestHandler): |
| 27 | pass |
| 28 | |
| 29 | class MyDatagramHandler(MyMixinHandler, DatagramRequestHandler): |
| 30 | pass |
| 31 | |
| 32 | class MyMixinServer: |
| 33 | def serve_a_few(self): |
| 34 | for i in range(NREQ): |
| 35 | self.handle_request() |
| 36 | def handle_error(self, request, client_address): |
| 37 | self.close_request(request) |
| 38 | self.server_close() |
| 39 | raise |
| 40 | |
| 41 | teststring = "hello world\n" |
| 42 | |
| 43 | def receive(sock, n, timeout=20): |
| 44 | r, w, x = select.select([sock], [], [], timeout) |
| 45 | if sock in r: |
| 46 | return sock.recv(n) |
| 47 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 48 | raise RuntimeError, "timed out on %r" % (sock,) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 49 | |
| 50 | def testdgram(proto, addr): |
| 51 | s = socket.socket(proto, socket.SOCK_DGRAM) |
| 52 | s.sendto(teststring, addr) |
| 53 | buf = data = receive(s, 100) |
| 54 | while data and '\n' not in buf: |
| 55 | data = receive(s, 100) |
| 56 | buf += data |
| 57 | verify(buf == teststring) |
| 58 | s.close() |
| 59 | |
| 60 | def teststream(proto, addr): |
| 61 | s = socket.socket(proto, socket.SOCK_STREAM) |
| 62 | s.connect(addr) |
Guido van Rossum | 9bd1401 | 2001-10-29 07:18:02 +0000 | [diff] [blame] | 63 | s.sendall(teststring) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 64 | buf = data = receive(s, 100) |
| 65 | while data and '\n' not in buf: |
| 66 | data = receive(s, 100) |
| 67 | buf += data |
| 68 | verify(buf == teststring) |
| 69 | s.close() |
| 70 | |
| 71 | class ServerThread(threading.Thread): |
| 72 | def __init__(self, addr, svrcls, hdlrcls): |
| 73 | threading.Thread.__init__(self) |
| 74 | self.__addr = addr |
| 75 | self.__svrcls = svrcls |
| 76 | self.__hdlrcls = hdlrcls |
| 77 | def run(self): |
| 78 | class svrcls(MyMixinServer, self.__svrcls): |
| 79 | pass |
| 80 | if verbose: print "thread: creating server" |
| 81 | svr = svrcls(self.__addr, self.__hdlrcls) |
Neal Norwitz | 909eb12 | 2006-06-12 02:13:21 +0000 | [diff] [blame] | 82 | # pull the address out of the server in case it changed |
| 83 | # this can happen if another process is using the port |
| 84 | addr = getattr(svr, 'server_address') |
| 85 | if addr: |
| 86 | self.__addr = addr |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 87 | if verbose: print "thread: serving three times" |
| 88 | svr.serve_a_few() |
| 89 | if verbose: print "thread: done" |
| 90 | |
| 91 | seed = 0 |
| 92 | def pickport(): |
| 93 | global seed |
| 94 | seed += 1 |
| 95 | return 10000 + (os.getpid() % 1000)*10 + seed |
| 96 | |
Guido van Rossum | 05be1a0 | 2001-07-10 15:46:34 +0000 | [diff] [blame] | 97 | host = "localhost" |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 98 | testfiles = [] |
| 99 | def pickaddr(proto): |
| 100 | if proto == socket.AF_INET: |
| 101 | return (host, pickport()) |
| 102 | else: |
| 103 | fn = TESTFN + str(pickport()) |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 104 | if os.name == 'os2': |
| 105 | # AF_UNIX socket names on OS/2 require a specific prefix |
| 106 | # which can't include a drive letter and must also use |
| 107 | # backslashes as directory separators |
| 108 | if fn[1] == ':': |
| 109 | fn = fn[2:] |
| 110 | if fn[0] in (os.sep, os.altsep): |
| 111 | fn = fn[1:] |
| 112 | fn = os.path.join('\socket', fn) |
| 113 | if os.sep == '/': |
| 114 | fn = fn.replace(os.sep, os.altsep) |
| 115 | else: |
| 116 | fn = fn.replace(os.altsep, os.sep) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 117 | testfiles.append(fn) |
| 118 | return fn |
| 119 | |
| 120 | def cleanup(): |
| 121 | for fn in testfiles: |
| 122 | try: |
| 123 | os.remove(fn) |
| 124 | except os.error: |
| 125 | pass |
| 126 | testfiles[:] = [] |
| 127 | |
| 128 | def testloop(proto, servers, hdlrcls, testfunc): |
| 129 | for svrcls in servers: |
| 130 | addr = pickaddr(proto) |
| 131 | if verbose: |
| 132 | print "ADDR =", addr |
| 133 | print "CLASS =", svrcls |
| 134 | t = ServerThread(addr, svrcls, hdlrcls) |
| 135 | if verbose: print "server created" |
| 136 | t.start() |
| 137 | if verbose: print "server running" |
| 138 | for i in range(NREQ): |
| 139 | time.sleep(DELAY) |
| 140 | if verbose: print "test client", i |
| 141 | testfunc(proto, addr) |
| 142 | if verbose: print "waiting for server" |
| 143 | t.join() |
| 144 | if verbose: print "done" |
| 145 | |
Neal Norwitz | 909eb12 | 2006-06-12 02:13:21 +0000 | [diff] [blame] | 146 | class ForgivingTCPServer(TCPServer): |
| 147 | # prevent errors if another process is using the port we want |
| 148 | def server_bind(self): |
| 149 | host, default_port = self.server_address |
| 150 | # this code shamelessly stolen from test.test_support |
| 151 | # the ports were changed to protect the innocent |
| 152 | import sys |
| 153 | for port in [default_port, 3434, 8798, 23833]: |
| 154 | try: |
| 155 | self.server_address = host, port |
| 156 | TCPServer.server_bind(self) |
| 157 | break |
| 158 | except socket.error, (err, msg): |
| 159 | if err != errno.EADDRINUSE: |
| 160 | raise |
| 161 | print >>sys.__stderr__, \ |
| 162 | ' WARNING: failed to listen on port %d, trying another' % port |
| 163 | |
| 164 | tcpservers = [ForgivingTCPServer, ThreadingTCPServer] |
Andrew MacIntyre | c6fff89 | 2003-01-02 12:49:00 +0000 | [diff] [blame] | 165 | if hasattr(os, 'fork') and os.name not in ('os2',): |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 166 | tcpservers.append(ForkingTCPServer) |
| 167 | udpservers = [UDPServer, ThreadingUDPServer] |
Andrew MacIntyre | c6fff89 | 2003-01-02 12:49:00 +0000 | [diff] [blame] | 168 | if hasattr(os, 'fork') and os.name not in ('os2',): |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 169 | udpservers.append(ForkingUDPServer) |
| 170 | |
| 171 | if not hasattr(socket, 'AF_UNIX'): |
| 172 | streamservers = [] |
| 173 | dgramservers = [] |
| 174 | else: |
| 175 | class ForkingUnixStreamServer(ForkingMixIn, UnixStreamServer): pass |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 176 | streamservers = [UnixStreamServer, ThreadingUnixStreamServer] |
| 177 | if hasattr(os, 'fork') and os.name not in ('os2',): |
| 178 | streamservers.append(ForkingUnixStreamServer) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 179 | class ForkingUnixDatagramServer(ForkingMixIn, UnixDatagramServer): pass |
Andrew MacIntyre | daedf21 | 2004-04-11 12:03:57 +0000 | [diff] [blame] | 180 | dgramservers = [UnixDatagramServer, ThreadingUnixDatagramServer] |
| 181 | if hasattr(os, 'fork') and os.name not in ('os2',): |
| 182 | dgramservers.append(ForkingUnixDatagramServer) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 183 | |
Neal Norwitz | b476fdf | 2006-08-15 04:58:28 +0000 | [diff] [blame^] | 184 | def sloppy_cleanup(): |
| 185 | # See http://python.org/sf/1540386 |
| 186 | # We need to reap children here otherwise a child from one server |
| 187 | # can be left running for the next server and cause a test failure. |
| 188 | time.sleep(DELAY) |
| 189 | reap_children() |
| 190 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 191 | def testall(): |
| 192 | testloop(socket.AF_INET, tcpservers, MyStreamHandler, teststream) |
Neal Norwitz | b476fdf | 2006-08-15 04:58:28 +0000 | [diff] [blame^] | 193 | sloppy_cleanup() |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 194 | testloop(socket.AF_INET, udpservers, MyDatagramHandler, testdgram) |
Guido van Rossum | 05be1a0 | 2001-07-10 15:46:34 +0000 | [diff] [blame] | 195 | if hasattr(socket, 'AF_UNIX'): |
Neal Norwitz | b476fdf | 2006-08-15 04:58:28 +0000 | [diff] [blame^] | 196 | sloppy_cleanup() |
Guido van Rossum | 05be1a0 | 2001-07-10 15:46:34 +0000 | [diff] [blame] | 197 | testloop(socket.AF_UNIX, streamservers, MyStreamHandler, teststream) |
| 198 | # Alas, on Linux (at least) recvfrom() doesn't return a meaningful |
| 199 | # client address so this cannot work: |
| 200 | ##testloop(socket.AF_UNIX, dgramservers, MyDatagramHandler, testdgram) |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 201 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 202 | def test_main(): |
| 203 | import imp |
| 204 | if imp.lock_held(): |
| 205 | # If the import lock is held, the threads will hang. |
| 206 | raise TestSkipped("can't run when import lock is held") |
| 207 | |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 208 | try: |
| 209 | testall() |
| 210 | finally: |
| 211 | cleanup() |
Neal Norwitz | b15ac31 | 2006-06-29 04:10:08 +0000 | [diff] [blame] | 212 | reap_children() |
Guido van Rossum | 39f1b36 | 2001-07-10 11:52:38 +0000 | [diff] [blame] | 213 | |
Tim Peters | a9f6f22 | 2001-09-17 23:56:20 +0000 | [diff] [blame] | 214 | if __name__ == "__main__": |
| 215 | test_main() |