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") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 33 | parser.add_option('-v', '--verbose', action="store_const", |
| 34 | dest="verbose", const=2, |
| 35 | help="verbose output") |
| 36 | parser.add_option('-q', '--quiet', action="store_const", |
| 37 | dest="verbose", const=0, |
| 38 | help="minimal output") |
| 39 | parser.set_defaults(verbose=1) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 40 | options, args = parser.parse_args() |
| 41 | |
| 42 | FRAMED = ["TNonblockingServer"] |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 43 | SKIP_ZLIB = ['TNonblockingServer', 'THttpServer'] |
| 44 | SKIP_SSL = ['TNonblockingServer', 'THttpServer'] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 45 | EXTRA_DELAY = ['TProcessPoolServer'] |
| 46 | EXTRA_SLEEP = 3.5 |
| 47 | |
| 48 | PROTOS= [ |
| 49 | 'accel', |
| 50 | 'binary', |
| 51 | 'compact' ] |
| 52 | |
| 53 | SERVERS = [ |
| 54 | "TSimpleServer", |
| 55 | "TThreadedServer", |
| 56 | "TThreadPoolServer", |
| 57 | "TProcessPoolServer", # new! |
| 58 | "TForkingServer", |
| 59 | "TNonblockingServer", |
| 60 | "THttpServer" ] |
| 61 | |
| 62 | # Test for presence of multiprocessing module, and if it is not present, then |
| 63 | # remove it from the list of available servers. |
| 64 | try: |
| 65 | import multiprocessing |
| 66 | except: |
| 67 | print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer' |
| 68 | SERVERS.remove('TProcessPoolServer') |
| 69 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 70 | try: |
| 71 | import ssl |
| 72 | except: |
| 73 | print 'Warning, no ssl module available. Skipping all SSL tests.' |
| 74 | SKIP_SSL.extend(SERVERS) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 75 | |
| 76 | # commandline permits a single class name to be specified to override SERVERS=[...] |
| 77 | if len(args) == 1: |
| 78 | if args[0] in SERVERS: |
| 79 | SERVERS = args |
| 80 | else: |
| 81 | print 'Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS) |
| 82 | sys.exit(0) |
| 83 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 84 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 85 | def relfile(fname): |
| 86 | return os.path.join(os.path.dirname(__file__), fname) |
| 87 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 88 | def runTest(server_class, proto, port, use_zlib, use_ssl): |
| 89 | # Build command line arguments |
| 90 | server_args = [sys.executable, relfile('TestServer.py') ] |
| 91 | cli_args = [sys.executable, relfile('TestClient.py') ] |
| 92 | for which in (server_args, cli_args): |
| 93 | which.append('--proto=%s' % proto) # accel, binary or compact |
| 94 | which.append('--port=%d' % port) # default to 9090 |
| 95 | if use_zlib: |
| 96 | which.append('--zlib') |
| 97 | if use_ssl: |
| 98 | which.append('--ssl') |
| 99 | if options.verbose == 0: |
| 100 | which.append('-q') |
| 101 | if options.verbose == 2: |
| 102 | which.append('-v') |
| 103 | # server-specific option to select server class |
| 104 | server_args.append(server_class) |
| 105 | # client-specific cmdline options |
| 106 | if server_class in FRAMED: |
| 107 | cli_args.append('--framed') |
| 108 | if server_class == 'THttpServer': |
| 109 | cli_args.append('--http=/') |
| 110 | if options.verbose > 0: |
| 111 | print 'Testing server %s: %s' % (server_class, ' '.join(server_args)) |
| 112 | serverproc = subprocess.Popen(server_args) |
| 113 | time.sleep(0.2) |
| 114 | try: |
| 115 | if options.verbose > 0: |
| 116 | print 'Testing client: %s' % (' '.join(cli_args)) |
| 117 | ret = subprocess.call(cli_args) |
| 118 | if ret != 0: |
| 119 | raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) |
| 120 | finally: |
| 121 | # check that server didn't die |
| 122 | serverproc.poll() |
| 123 | if serverproc.returncode is not None: |
| 124 | print 'FAIL: Server process (%s) failed with retcode %d' % (' '.join(server_args), serverproc.returncode) |
| 125 | raise Exception('Server subprocess %s died, args: %s' % (server_class, ' '.join(server_args))) |
| 126 | else: |
| 127 | if server_class in EXTRA_DELAY: |
| 128 | if options.verbose > 0: |
| 129 | print 'Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child processes to terminate via alarm' % (server_class, |
| 130 | proto, use_zlib, use_ssl, EXTRA_SLEEP) |
| 131 | time.sleep(EXTRA_SLEEP) |
| 132 | os.kill(serverproc.pid, signal.SIGKILL) |
| 133 | # wait for shutdown |
| 134 | time.sleep(0.1) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 135 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 136 | test_count = 0 |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 137 | for try_server in SERVERS: |
| 138 | for try_proto in PROTOS: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 139 | for with_zlib in (False, True): |
| 140 | # skip any servers that don't work with the Zlib transport |
| 141 | if with_zlib and try_server in SKIP_ZLIB: |
| 142 | continue |
| 143 | for with_ssl in (False, True): |
| 144 | # skip any servers that don't work with SSL |
| 145 | if with_ssl and try_server in SKIP_SSL: |
| 146 | continue |
| 147 | test_count += 1 |
| 148 | if options.verbose > 0: |
| 149 | print '\nTest run #%d: Server=%s, Proto=%s, zlib=%s, SSL=%s' % (test_count, try_server, try_proto, with_zlib, with_ssl) |
| 150 | runTest(try_server, try_proto, options.port, with_zlib, with_ssl) |
| 151 | if options.verbose > 0: |
| 152 | print 'OK: Finished %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' % (try_server, try_proto, with_zlib, with_ssl, test_count) |