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