blob: 633856f527c3d8bab89dc5b85b98c015f08291b1 [file] [log] [blame]
Mark Slee5299a952007-10-05 00:13:24 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
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 Duxbury59d4efd2011-03-21 17:38:22 +000022from __future__ import division
David Reissbcaa2ad2008-06-10 22:55:26 +000023import time
Mark Slee5299a952007-10-05 00:13:24 +000024import subprocess
25import sys
26import os
27import signal
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000028from optparse import OptionParser
29
30parser = OptionParser()
31parser.add_option("--port", type="int", dest="port", default=9090,
32 help="port number for server to listen on")
Bryan Duxbury16066592011-03-22 18:06:04 +000033parser.add_option('-v', '--verbose', action="store_const",
34 dest="verbose", const=2,
35 help="verbose output")
36parser.add_option('-q', '--quiet', action="store_const",
37 dest="verbose", const=0,
38 help="minimal output")
39parser.set_defaults(verbose=1)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000040options, args = parser.parse_args()
41
42FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000043SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
44SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000045EXTRA_DELAY = ['TProcessPoolServer']
46EXTRA_SLEEP = 3.5
47
48PROTOS= [
49 'accel',
50 'binary',
51 'compact' ]
52
53SERVERS = [
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.
64try:
65 import multiprocessing
66except:
67 print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer'
68 SERVERS.remove('TProcessPoolServer')
69
Bryan Duxbury16066592011-03-22 18:06:04 +000070try:
71 import ssl
72except:
73 print 'Warning, no ssl module available. Skipping all SSL tests.'
74 SKIP_SSL.extend(SERVERS)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000075
76# commandline permits a single class name to be specified to override SERVERS=[...]
77if 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 Slee5299a952007-10-05 00:13:24 +000084
David Reiss2a4bfd62008-04-07 23:45:00 +000085def relfile(fname):
86 return os.path.join(os.path.dirname(__file__), fname)
87
Bryan Duxbury16066592011-03-22 18:06:04 +000088def 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 Reissbcaa2ad2008-06-10 22:55:26 +0000135
Bryan Duxbury16066592011-03-22 18:06:04 +0000136test_count = 0
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000137for try_server in SERVERS:
138 for try_proto in PROTOS:
Bryan Duxbury16066592011-03-22 18:06:04 +0000139 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)