blob: dced91adafb67432d340f334f423b4f5dc1d5ded [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")
33options, args = parser.parse_args()
34
35FRAMED = ["TNonblockingServer"]
36EXTRA_DELAY = ['TProcessPoolServer']
37EXTRA_SLEEP = 3.5
38
39PROTOS= [
40 'accel',
41 'binary',
42 'compact' ]
43
44SERVERS = [
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.
55try:
56 import multiprocessing
57except:
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=[...]
63if 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 Slee5299a952007-10-05 00:13:24 +000070
David Reiss2a4bfd62008-04-07 23:45:00 +000071def relfile(fname):
72 return os.path.join(os.path.dirname(__file__), fname)
73
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000074def 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 Reiss31661412009-01-31 21:39:08 +000082 time.sleep(0.25)
David Reissbcaa2ad2008-06-10 22:55:26 +000083 try:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000084 argv = [sys.executable, relfile("TestClient.py"),
85 '--proto=%s' % (proto), '--port=%d' % (port) ]
David Reiss74421272008-11-07 23:09:31 +000086 if server_class in FRAMED:
87 argv.append('--framed')
David Reissf78ec2b2009-01-31 21:59:32 +000088 if server_class == 'THttpServer':
89 argv.append('--http=/')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000090 print 'Testing client %s: %s' % (server_class, ' '.join(argv))
David Reiss74421272008-11-07 23:09:31 +000091 ret = subprocess.call(argv)
David Reissbcaa2ad2008-06-10 22:55:26 +000092 if ret != 0:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000093 raise Exception("subprocess %s failed, args: %s" % (server_class, ' '.join(argv)))
David Reissbcaa2ad2008-06-10 22:55:26 +000094 finally:
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000095 # 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 Reissbcaa2ad2008-06-10 22:55:26 +0000106 # wait for shutdown
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000107 time.sleep(0.5)
David Reissbcaa2ad2008-06-10 22:55:26 +0000108
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000109for try_server in SERVERS:
110 for try_proto in PROTOS:
111 runTest(try_server, try_proto, options.port)