blob: 8a7fda64eb738a35fc393bb740cf1eef911375a3 [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()
Roger Meierf4eec7a2011-09-11 18:16:21 +000031parser.add_option('--genpydirs', type='string', dest='genpydirs',
32 default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots',
33 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000034parser.add_option("--port", type="int", dest="port", default=9090,
35 help="port number for server to listen on")
Bryan Duxbury16066592011-03-22 18:06:04 +000036parser.add_option('-v', '--verbose', action="store_const",
37 dest="verbose", const=2,
38 help="verbose output")
39parser.add_option('-q', '--quiet', action="store_const",
40 dest="verbose", const=0,
41 help="minimal output")
42parser.set_defaults(verbose=1)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000043options, args = parser.parse_args()
44
Roger Meierf4eec7a2011-09-11 18:16:21 +000045generated_dirs = []
46for gp_dir in options.genpydirs.split(','):
47 generated_dirs.append('gen-py-%s' % (gp_dir))
48
49SCRIPTS = ['SerializationTest.py', 'TestEof.py', 'TestSyntax.py', 'TestSocket.py']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000050FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000051SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
52SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meierf4eec7a2011-09-11 18:16:21 +000053EXTRA_DELAY = dict(TProcessPoolServer=3.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000054
55PROTOS= [
56 'accel',
57 'binary',
58 'compact' ]
59
60SERVERS = [
61 "TSimpleServer",
62 "TThreadedServer",
63 "TThreadPoolServer",
64 "TProcessPoolServer", # new!
65 "TForkingServer",
66 "TNonblockingServer",
67 "THttpServer" ]
68
69# Test for presence of multiprocessing module, and if it is not present, then
70# remove it from the list of available servers.
71try:
72 import multiprocessing
73except:
74 print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer'
75 SERVERS.remove('TProcessPoolServer')
76
Bryan Duxbury16066592011-03-22 18:06:04 +000077try:
78 import ssl
79except:
80 print 'Warning, no ssl module available. Skipping all SSL tests.'
81 SKIP_SSL.extend(SERVERS)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000082
83# commandline permits a single class name to be specified to override SERVERS=[...]
84if len(args) == 1:
85 if args[0] in SERVERS:
86 SERVERS = args
87 else:
88 print 'Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS)
89 sys.exit(0)
90
Mark Slee5299a952007-10-05 00:13:24 +000091
David Reiss2a4bfd62008-04-07 23:45:00 +000092def relfile(fname):
93 return os.path.join(os.path.dirname(__file__), fname)
94
Roger Meierf4eec7a2011-09-11 18:16:21 +000095def runScriptTest(genpydir, script):
96 script_args = [sys.executable, relfile(script) ]
97 script_args.append('--genpydir=%s' % genpydir)
98 serverproc = subprocess.Popen(script_args)
99 print '\nTesting script: %s\n----' % (' '.join(script_args))
100 ret = subprocess.call(script_args)
101 if ret != 0:
102 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
103
104def runServiceTest(genpydir, server_class, proto, port, use_zlib, use_ssl):
Bryan Duxbury16066592011-03-22 18:06:04 +0000105 # Build command line arguments
106 server_args = [sys.executable, relfile('TestServer.py') ]
107 cli_args = [sys.executable, relfile('TestClient.py') ]
108 for which in (server_args, cli_args):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000109 which.append('--genpydir=%s' % genpydir)
Bryan Duxbury16066592011-03-22 18:06:04 +0000110 which.append('--proto=%s' % proto) # accel, binary or compact
111 which.append('--port=%d' % port) # default to 9090
112 if use_zlib:
113 which.append('--zlib')
114 if use_ssl:
115 which.append('--ssl')
116 if options.verbose == 0:
117 which.append('-q')
118 if options.verbose == 2:
119 which.append('-v')
120 # server-specific option to select server class
121 server_args.append(server_class)
122 # client-specific cmdline options
123 if server_class in FRAMED:
124 cli_args.append('--framed')
125 if server_class == 'THttpServer':
126 cli_args.append('--http=/')
127 if options.verbose > 0:
128 print 'Testing server %s: %s' % (server_class, ' '.join(server_args))
129 serverproc = subprocess.Popen(server_args)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000130 time.sleep(0.15)
Bryan Duxbury16066592011-03-22 18:06:04 +0000131 try:
132 if options.verbose > 0:
133 print 'Testing client: %s' % (' '.join(cli_args))
134 ret = subprocess.call(cli_args)
135 if ret != 0:
136 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
137 finally:
138 # check that server didn't die
139 serverproc.poll()
140 if serverproc.returncode is not None:
141 print 'FAIL: Server process (%s) failed with retcode %d' % (' '.join(server_args), serverproc.returncode)
142 raise Exception('Server subprocess %s died, args: %s' % (server_class, ' '.join(server_args)))
143 else:
Roger Meierf4eec7a2011-09-11 18:16:21 +0000144 extra_sleep = EXTRA_DELAY.get(server_class, 0)
145 if extra_sleep > 0 and options.verbose > 0:
146 print 'Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child processes to terminate via alarm' % (server_class,
147 proto, use_zlib, use_ssl, extra_sleep)
148 time.sleep(extra_sleep)
Bryan Duxbury16066592011-03-22 18:06:04 +0000149 os.kill(serverproc.pid, signal.SIGKILL)
150 # wait for shutdown
Roger Meierf4eec7a2011-09-11 18:16:21 +0000151 time.sleep(0.05)
David Reissbcaa2ad2008-06-10 22:55:26 +0000152
Bryan Duxbury16066592011-03-22 18:06:04 +0000153test_count = 0
Roger Meierf4eec7a2011-09-11 18:16:21 +0000154# run tests without a client/server first
155print '----------------'
156print ' Executing individual test scripts with various generated code directories'
157print ' Directories to be tested: ' + ', '.join(generated_dirs)
158print ' Scripts to be tested: ' + ', '.join(SCRIPTS)
159print '----------------'
160for genpydir in generated_dirs:
161 for script in SCRIPTS:
162 runScriptTest(genpydir, script)
163
164print '----------------'
165print ' Executing Client/Server tests with various generated code directories'
166print ' Servers to be tested: ' + ', '.join(SERVERS)
167print ' Directories to be tested: ' + ', '.join(generated_dirs)
168print ' Protocols to be tested: ' + ', '.join(PROTOS)
169print ' Options to be tested: ZLIB(yes/no), SSL(yes/no)'
170print '----------------'
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000171for try_server in SERVERS:
Roger Meierf4eec7a2011-09-11 18:16:21 +0000172 for genpydir in generated_dirs:
173 for try_proto in PROTOS:
174 for with_zlib in (False, True):
175 # skip any servers that don't work with the Zlib transport
176 if with_zlib and try_server in SKIP_ZLIB:
Bryan Duxbury16066592011-03-22 18:06:04 +0000177 continue
Roger Meierf4eec7a2011-09-11 18:16:21 +0000178 for with_ssl in (False, True):
179 # skip any servers that don't work with SSL
180 if with_ssl and try_server in SKIP_SSL:
181 continue
182 test_count += 1
183 if options.verbose > 0:
184 print '\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)
185 runServiceTest(genpydir, try_server, try_proto, options.port, with_zlib, with_ssl)
186 if options.verbose > 0:
187 print 'OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)