| Mark Slee | 57cc25e | 2007-02-28 21:43:54 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 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 | # |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 21 | import logging |
| 22 | import os |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 23 | import signal |
| Dmytro Shteflyuk | 0b68228 | 2026-02-04 16:26:46 -0500 | [diff] [blame^] | 24 | import ssl |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 25 | import sys |
| 26 | import time |
| Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 27 | from optparse import OptionParser |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 28 | |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 29 | from util import local_libpath |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 30 | sys.path.insert(0, local_libpath()) |
| 31 | from thrift.protocol import TProtocol, TProtocolDecorator |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame] | 32 | from thrift.Thrift import TException |
| Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 33 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 34 | SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
| Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 35 | |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 36 | |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 37 | class TestHandler(object): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 38 | def __init__(self, options): |
| 39 | self.options = options |
| 40 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | def testVoid(self): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 42 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 43 | logging.info('testVoid()') |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 44 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 45 | def testString(self, str): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 46 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | logging.info('testString(%s)' % str) |
| 48 | return str |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 49 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 50 | def testBool(self, boolean): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 51 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 52 | logging.info('testBool(%s)' % str(boolean).lower()) |
| 53 | return boolean |
| Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 54 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 55 | def testByte(self, byte): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 56 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 57 | logging.info('testByte(%d)' % byte) |
| 58 | return byte |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 59 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 60 | def testI16(self, i16): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 61 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 62 | logging.info('testI16(%d)' % i16) |
| 63 | return i16 |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 64 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 65 | def testI32(self, i32): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 66 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 67 | logging.info('testI32(%d)' % i32) |
| 68 | return i32 |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 69 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | def testI64(self, i64): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 71 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 72 | logging.info('testI64(%d)' % i64) |
| 73 | return i64 |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 74 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | def testDouble(self, dub): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 76 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 77 | logging.info('testDouble(%f)' % dub) |
| 78 | return dub |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 79 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 80 | def testBinary(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 81 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 82 | logging.info('testBinary()') # TODO: hex output |
| 83 | return thing |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 84 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 85 | def testStruct(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 86 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 87 | logging.info('testStruct({%s, %s, %s, %s})' % (thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing)) |
| 88 | return thing |
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 89 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 90 | def testException(self, arg): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 91 | from ThriftTest.ttypes import Xception |
| 92 | # if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 93 | logging.info('testException(%s)' % arg) |
| 94 | if arg == 'Xception': |
| 95 | raise Xception(errorCode=1001, message=arg) |
| 96 | elif arg == 'TException': |
| 97 | raise TException(message='This is a TException') |
| Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 98 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 99 | def testMultiException(self, arg0, arg1): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 100 | from ThriftTest.ttypes import Xtruct, Xception, Xception2 |
| 101 | |
| 102 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 103 | logging.info('testMultiException(%s, %s)' % (arg0, arg1)) |
| 104 | if arg0 == 'Xception': |
| 105 | raise Xception(errorCode=1001, message='This is an Xception') |
| 106 | elif arg0 == 'Xception2': |
| 107 | raise Xception2( |
| 108 | errorCode=2002, |
| 109 | struct_thing=Xtruct(string_thing='This is an Xception2')) |
| 110 | return Xtruct(string_thing=arg1) |
| Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 111 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 112 | def testOneway(self, seconds): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 113 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 114 | logging.info('testOneway(%d) => sleeping...' % seconds) |
| 115 | time.sleep(seconds / 3) # be quick |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 116 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 117 | logging.info('done sleeping') |
| David Reiss | db893b6 | 2008-02-18 02:11:48 +0000 | [diff] [blame] | 118 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 119 | def testNest(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 120 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 121 | logging.info('testNest(%s)' % thing) |
| 122 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 123 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 124 | def testMap(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 125 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 126 | logging.info('testMap(%s)' % thing) |
| 127 | return thing |
| Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 128 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 129 | def testStringMap(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 130 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 131 | logging.info('testStringMap(%s)' % thing) |
| 132 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 133 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 134 | def testSet(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 135 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 136 | logging.info('testSet(%s)' % thing) |
| 137 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 138 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 139 | def testList(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 140 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 141 | logging.info('testList(%s)' % thing) |
| 142 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 143 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 144 | def testEnum(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 145 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 146 | logging.info('testEnum(%s)' % thing) |
| 147 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 148 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | def testTypedef(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 150 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 151 | logging.info('testTypedef(%s)' % thing) |
| 152 | return thing |
| David Reiss | 7442127 | 2008-11-07 23:09:31 +0000 | [diff] [blame] | 153 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 154 | def testMapMap(self, thing): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 155 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 156 | logging.info('testMapMap(%s)' % thing) |
| 157 | return { |
| 158 | -4: { |
| 159 | -4: -4, |
| 160 | -3: -3, |
| 161 | -2: -2, |
| 162 | -1: -1, |
| 163 | }, |
| 164 | 4: { |
| 165 | 4: 4, |
| 166 | 3: 3, |
| 167 | 2: 2, |
| 168 | 1: 1, |
| 169 | }, |
| 170 | } |
| Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 171 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 172 | def testInsanity(self, argument): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 173 | from ThriftTest.ttypes import Insanity |
| 174 | |
| 175 | if self.options.verbose > 1: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 176 | logging.info('testInsanity(%s)' % argument) |
| 177 | return { |
| 178 | 1: { |
| 179 | 2: argument, |
| 180 | 3: argument, |
| 181 | }, |
| 182 | 2: {6: Insanity()}, |
| 183 | } |
| Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 184 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 185 | def testMulti(self, arg0, arg1, arg2, arg3, arg4, arg5): |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 186 | from ThriftTest.ttypes import Xtruct |
| 187 | |
| 188 | if self.options.verbose > 1: |
| Jens Geyer | 506e311 | 2020-05-17 22:48:51 +0200 | [diff] [blame] | 189 | logging.info('testMulti(%s, %s, %s, %s, %s, %s)' % (arg0, arg1, arg2, arg3, arg4, arg5)) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 190 | return Xtruct(string_thing='Hello2', |
| 191 | byte_thing=arg0, i32_thing=arg1, i64_thing=arg2) |
| Roger Meier | 1f554e1 | 2013-01-05 20:38:35 +0100 | [diff] [blame] | 192 | |
| Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 193 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 194 | class SecondHandler(object): |
| 195 | def secondtestString(self, argument): |
| 196 | return "testString(\"" + argument + "\")" |
| 197 | |
| 198 | |
| 199 | # LAST_SEQID is a global because we have one transport and multiple protocols |
| 200 | # running on it (when multiplexed) |
| 201 | LAST_SEQID = None |
| 202 | |
| 203 | |
| 204 | class TPedanticSequenceIdProtocolWrapper(TProtocolDecorator.TProtocolDecorator): |
| 205 | """ |
| 206 | Wraps any protocol with sequence ID checking: looks for outbound |
| 207 | uniqueness as well as request/response alignment. |
| 208 | """ |
| 209 | def __init__(self, protocol): |
| 210 | # TProtocolDecorator.__new__ does all the heavy lifting |
| 211 | pass |
| 212 | |
| 213 | def readMessageBegin(self): |
| 214 | global LAST_SEQID |
| 215 | (name, type, seqid) =\ |
| 216 | super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin() |
| 217 | if LAST_SEQID is not None and LAST_SEQID == seqid: |
| 218 | raise TProtocol.TProtocolException( |
| 219 | TProtocol.TProtocolException.INVALID_DATA, |
| 220 | "We received the same seqid {0} twice in a row".format(seqid)) |
| 221 | LAST_SEQID = seqid |
| 222 | return (name, type, seqid) |
| 223 | |
| 224 | |
| 225 | def make_pedantic(proto): |
| 226 | """ Wrap a protocol in the pedantic sequence ID wrapper. """ |
| 227 | # NOTE: this is disabled for now as many clients send seqid |
| 228 | # of zero and that is okay, need a way to identify |
| 229 | # clients that MUST send seqid unique to function right |
| 230 | # or just force all implementations to send unique seqids (preferred) |
| 231 | return proto # TPedanticSequenceIdProtocolWrapper(proto) |
| 232 | |
| 233 | |
| 234 | class TPedanticSequenceIdProtocolFactory(TProtocol.TProtocolFactory): |
| 235 | def __init__(self, encapsulated): |
| 236 | super(TPedanticSequenceIdProtocolFactory, self).__init__() |
| 237 | self.encapsulated = encapsulated |
| 238 | |
| 239 | def getProtocol(self, trans): |
| 240 | return make_pedantic(self.encapsulated.getProtocol(trans)) |
| 241 | |
| 242 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 243 | def main(options): |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 244 | # common header allowed client types |
| 245 | allowed_client_types = [ |
| 246 | THeaderTransport.THeaderClientType.HEADERS, |
| 247 | THeaderTransport.THeaderClientType.FRAMED_BINARY, |
| 248 | THeaderTransport.THeaderClientType.UNFRAMED_BINARY, |
| 249 | THeaderTransport.THeaderClientType.FRAMED_COMPACT, |
| 250 | THeaderTransport.THeaderClientType.UNFRAMED_COMPACT, |
| 251 | ] |
| 252 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 253 | # set up the protocol factory form the --protocol option |
| 254 | prot_factories = { |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 255 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 256 | 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 257 | 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 258 | 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(), |
| 259 | 'binary': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), |
| 260 | 'multi': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 261 | 'compact': TCompactProtocol.TCompactProtocolFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 262 | 'multic': TCompactProtocol.TCompactProtocolFactory(), |
| 263 | 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), |
| 264 | 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 265 | 'json': TJSONProtocol.TJSONProtocolFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 266 | 'multij': TJSONProtocol.TJSONProtocolFactory(), |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 267 | } |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 268 | pfactory = prot_factories.get(options.proto, None) |
| 269 | if pfactory is None: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 270 | raise AssertionError('Unknown --protocol option: %s' % options.proto) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 271 | try: |
| 272 | pfactory.string_length_limit = options.string_limit |
| 273 | pfactory.container_length_limit = options.container_limit |
| James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 274 | except Exception: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 275 | # Ignore errors for those protocols that does not support length limit |
| 276 | pass |
| Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 277 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 278 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 279 | if len(args) > 1: |
| 280 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 281 | server_type = args[0] |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 282 | if options.trans == 'http': |
| 283 | server_type = 'THttpServer' |
| David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 284 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 285 | # Set up the handler and processor objects |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 286 | handler = TestHandler(options) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 287 | processor = ThriftTest.Processor(handler) |
| Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 288 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 289 | if options.proto.startswith('multi'): |
| 290 | secondHandler = SecondHandler() |
| 291 | secondProcessor = SecondService.Processor(secondHandler) |
| 292 | |
| 293 | multiplexedProcessor = TMultiplexedProcessor() |
| 294 | multiplexedProcessor.registerDefault(processor) |
| 295 | multiplexedProcessor.registerProcessor('ThriftTest', processor) |
| 296 | multiplexedProcessor.registerProcessor('SecondService', secondProcessor) |
| 297 | processor = multiplexedProcessor |
| 298 | |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 299 | global server |
| 300 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 301 | # Handle THttpServer as a special case |
| 302 | if server_type == 'THttpServer': |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 303 | if options.ssl: |
| 304 | __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt") |
| 305 | __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key") |
| 306 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile) |
| 307 | else: |
| 308 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 309 | server.serve() |
| 310 | sys.exit(0) |
| 311 | |
| 312 | # set up server transport and transport factory |
| 313 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 314 | host = None |
| 315 | if options.ssl: |
| 316 | from thrift.transport import TSSLSocket |
| Dmytro Shteflyuk | 0b68228 | 2026-02-04 16:26:46 -0500 | [diff] [blame^] | 317 | keys_dir = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys') |
| 318 | ca_certs = os.path.join(keys_dir, 'client.pem') |
| 319 | certfile = os.path.join(keys_dir, 'server.crt') |
| 320 | keyfile = os.path.join(keys_dir, 'server.key') |
| 321 | ssl_version = getattr(ssl, 'PROTOCOL_TLS_SERVER', ssl.PROTOCOL_TLSv1) |
| 322 | transport = TSSLSocket.TSSLServerSocket( |
| 323 | host, |
| 324 | options.port, |
| 325 | certfile=certfile, |
| 326 | keyfile=keyfile, |
| 327 | ca_certs=ca_certs, |
| 328 | cert_reqs=ssl.CERT_REQUIRED, |
| 329 | ssl_version=ssl_version, |
| 330 | ) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 331 | else: |
| Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 332 | transport = TSocket.TServerSocket(host, options.port, options.domain_socket) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 333 | tfactory = TTransport.TBufferedTransportFactory() |
| 334 | if options.trans == 'buffered': |
| 335 | tfactory = TTransport.TBufferedTransportFactory() |
| 336 | elif options.trans == 'framed': |
| 337 | tfactory = TTransport.TFramedTransportFactory() |
| 338 | elif options.trans == '': |
| 339 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
| 340 | else: |
| 341 | tfactory = TTransport.TBufferedTransportFactory() |
| 342 | # if --zlib, then wrap server transport, and use a different transport factory |
| 343 | if options.zlib: |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame] | 344 | if server_type != "TProcessPoolServer": |
| 345 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
| 346 | # Avoid wrapping the server transport for process pools; TZlibTransport isn't picklable on spawn. |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 347 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 348 | |
| 349 | # do server-specific setup here: |
| 350 | if server_type == "TNonblockingServer": |
| 351 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 352 | elif server_type == "TProcessPoolServer": |
| 353 | import signal |
| 354 | from thrift.server import TProcessPoolServer |
| 355 | server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) |
| 356 | server.setNumWorkers(5) |
| 357 | |
| 358 | def set_alarm(): |
| 359 | def clean_shutdown(signum, frame): |
| 360 | for worker in server.workers: |
| 361 | if options.verbose > 0: |
| 362 | logging.info('Terminating worker: %s' % worker) |
| 363 | worker.terminate() |
| 364 | if options.verbose > 0: |
| 365 | logging.info('Requesting server to stop()') |
| 366 | try: |
| 367 | server.stop() |
| James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 368 | except Exception: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 369 | pass |
| 370 | signal.signal(signal.SIGALRM, clean_shutdown) |
| 371 | signal.alarm(4) |
| 372 | set_alarm() |
| 373 | else: |
| 374 | # look up server class dynamically to instantiate server |
| 375 | ServerClass = getattr(TServer, server_type) |
| 376 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 377 | # enter server main loop |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 378 | server.serve() |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 379 | |
| James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 380 | |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 381 | def exit_gracefully(signum, frame): |
| 382 | print("SIGINT received\n") |
| 383 | server.shutdown() # doesn't work properly, yet |
| 384 | sys.exit(0) |
| 385 | |
| 386 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 387 | if __name__ == '__main__': |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 388 | signal.signal(signal.SIGINT, exit_gracefully) |
| 389 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 390 | parser = OptionParser() |
| 391 | parser.add_option('--libpydir', type='string', dest='libpydir', |
| 392 | help='include this directory to sys.path for locating library code') |
| 393 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 394 | default='gen-py', |
| 395 | help='include this directory to sys.path for locating generated code') |
| 396 | parser.add_option("--port", type="int", dest="port", |
| 397 | help="port number for server to listen on") |
| 398 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 399 | help="use zlib wrapper for compressed transport") |
| 400 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 401 | help="use SSL for encrypted transport") |
| 402 | parser.add_option('-v', '--verbose', action="store_const", |
| 403 | dest="verbose", const=2, |
| 404 | help="verbose output") |
| 405 | parser.add_option('-q', '--quiet', action="store_const", |
| 406 | dest="verbose", const=0, |
| 407 | help="minimal output") |
| 408 | parser.add_option('--protocol', dest="proto", type="string", |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 409 | help="protocol to use, one of: accel, accelc, binary, compact, json, multi, multia, multiac, multic, multih, multij") |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 410 | parser.add_option('--transport', dest="trans", type="string", |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 411 | help="transport to use, one of: buffered, framed, http") |
| Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 412 | parser.add_option('--domain-socket', dest="domain_socket", type="string", |
| 413 | help="Unix domain socket path") |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 414 | parser.add_option('--container-limit', dest='container_limit', type='int', default=None) |
| 415 | parser.add_option('--string-limit', dest='string_limit', type='int', default=None) |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 416 | parser.set_defaults(port=9090, verbose=1, proto='binary', transport='buffered') |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 417 | options, args = parser.parse_args() |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 418 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 419 | # Print TServer log to stdout so that the test-runner can redirect it to log files |
| 420 | logging.basicConfig(level=options.verbose) |
| Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 421 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 422 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 423 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 424 | from ThriftTest import ThriftTest, SecondService |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 425 | from thrift.Thrift import TException |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 426 | from thrift.TMultiplexedProcessor import TMultiplexedProcessor |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 427 | from thrift.transport import THeaderTransport |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 428 | from thrift.transport import TTransport |
| 429 | from thrift.transport import TSocket |
| 430 | from thrift.transport import TZlibTransport |
| 431 | from thrift.protocol import TBinaryProtocol |
| 432 | from thrift.protocol import TCompactProtocol |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 433 | from thrift.protocol import THeaderProtocol |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 434 | from thrift.protocol import TJSONProtocol |
| 435 | from thrift.server import TServer, TNonblockingServer, THttpServer |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 436 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 437 | sys.exit(main(options)) |