Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 22 | from __future__ import division |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 23 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 24 | import subprocess |
| 25 | import sys |
| 26 | import os |
| 27 | import signal |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 28 | from optparse import OptionParser |
| 29 | |
| 30 | parser = OptionParser() |
| 31 | parser.add_option("--port", type="int", dest="port", default=9090, |
| 32 | help="port number for server to listen on") |
| 33 | options, args = parser.parse_args() |
| 34 | |
| 35 | FRAMED = ["TNonblockingServer"] |
| 36 | EXTRA_DELAY = ['TProcessPoolServer'] |
| 37 | EXTRA_SLEEP = 3.5 |
| 38 | |
| 39 | PROTOS= [ |
| 40 | 'accel', |
| 41 | 'binary', |
| 42 | 'compact' ] |
| 43 | |
| 44 | SERVERS = [ |
| 45 | "TSimpleServer", |
| 46 | "TThreadedServer", |
| 47 | "TThreadPoolServer", |
| 48 | "TProcessPoolServer", # new! |
| 49 | "TForkingServer", |
| 50 | "TNonblockingServer", |
| 51 | "THttpServer" ] |
| 52 | |
| 53 | # Test for presence of multiprocessing module, and if it is not present, then |
| 54 | # remove it from the list of available servers. |
| 55 | try: |
| 56 | import multiprocessing |
| 57 | except: |
| 58 | print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer' |
| 59 | SERVERS.remove('TProcessPoolServer') |
| 60 | |
| 61 | |
| 62 | # commandline permits a single class name to be specified to override SERVERS=[...] |
| 63 | if len(args) == 1: |
| 64 | if args[0] in SERVERS: |
| 65 | SERVERS = args |
| 66 | else: |
| 67 | print 'Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS) |
| 68 | sys.exit(0) |
| 69 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 70 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 71 | def relfile(fname): |
| 72 | return os.path.join(os.path.dirname(__file__), fname) |
| 73 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 74 | def runTest(server_class, proto, port): |
| 75 | server_args = [sys.executable, # /usr/bin/python or similar |
| 76 | relfile('TestServer.py'), # ./TestServer.py |
| 77 | '--proto=%s' % proto, # accel, binary or compact |
| 78 | '--port=%d' % port, # usually 9090, given on cmdline |
| 79 | server_class] # name of class to test, from SERVERS[] or cmdline |
| 80 | print "Testing server %s: %s" % (server_class, ' '.join(server_args)) |
| 81 | serverproc = subprocess.Popen(server_args) |
David Reiss | 3166141 | 2009-01-31 21:39:08 +0000 | [diff] [blame] | 82 | time.sleep(0.25) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 83 | try: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 84 | argv = [sys.executable, relfile("TestClient.py"), |
| 85 | '--proto=%s' % (proto), '--port=%d' % (port) ] |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 86 | if server_class in FRAMED: |
| 87 | argv.append('--framed') |
David Reiss | f78ec2b | 2009-01-31 21:59:32 +0000 | [diff] [blame] | 88 | if server_class == 'THttpServer': |
| 89 | argv.append('--http=/') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 90 | print 'Testing client %s: %s' % (server_class, ' '.join(argv)) |
David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 91 | ret = subprocess.call(argv) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 92 | if ret != 0: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 93 | raise Exception("subprocess %s failed, args: %s" % (server_class, ' '.join(argv))) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 94 | finally: |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 95 | # check that server didn't die |
| 96 | time.sleep(0.05) |
| 97 | serverproc.poll() |
| 98 | if serverproc.returncode is not None: |
| 99 | print 'Server process (%s) failed with retcode %d' % (' '.join(server_args), serverproc.returncode) |
| 100 | raise Exception('subprocess %s died, args: %s' % (server_class, ' '.join(server_args))) |
| 101 | else: |
| 102 | if server_class in EXTRA_DELAY: |
| 103 | print 'Giving %s (proto=%s) an extra %d seconds for child processes to terminate via alarm' % (server_class, proto, EXTRA_SLEEP) |
| 104 | time.sleep(EXTRA_SLEEP) |
| 105 | os.kill(serverproc.pid, signal.SIGKILL) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 106 | # wait for shutdown |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 107 | time.sleep(0.5) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 108 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 109 | for try_server in SERVERS: |
| 110 | for try_proto in PROTOS: |
| 111 | runTest(try_server, try_proto, options.port) |