| 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 | """ |
| Dmytro Shteflyuk | acbcf10 | 2026-02-13 18:25:55 -0500 | [diff] [blame^] | 209 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 210 | def __init__(self, protocol): |
| 211 | # TProtocolDecorator.__new__ does all the heavy lifting |
| 212 | pass |
| 213 | |
| 214 | def readMessageBegin(self): |
| 215 | global LAST_SEQID |
| 216 | (name, type, seqid) =\ |
| 217 | super(TPedanticSequenceIdProtocolWrapper, self).readMessageBegin() |
| 218 | if LAST_SEQID is not None and LAST_SEQID == seqid: |
| 219 | raise TProtocol.TProtocolException( |
| 220 | TProtocol.TProtocolException.INVALID_DATA, |
| 221 | "We received the same seqid {0} twice in a row".format(seqid)) |
| 222 | LAST_SEQID = seqid |
| 223 | return (name, type, seqid) |
| 224 | |
| 225 | |
| 226 | def make_pedantic(proto): |
| 227 | """ Wrap a protocol in the pedantic sequence ID wrapper. """ |
| 228 | # NOTE: this is disabled for now as many clients send seqid |
| 229 | # of zero and that is okay, need a way to identify |
| 230 | # clients that MUST send seqid unique to function right |
| 231 | # or just force all implementations to send unique seqids (preferred) |
| 232 | return proto # TPedanticSequenceIdProtocolWrapper(proto) |
| 233 | |
| 234 | |
| 235 | class TPedanticSequenceIdProtocolFactory(TProtocol.TProtocolFactory): |
| 236 | def __init__(self, encapsulated): |
| 237 | super(TPedanticSequenceIdProtocolFactory, self).__init__() |
| 238 | self.encapsulated = encapsulated |
| 239 | |
| 240 | def getProtocol(self, trans): |
| 241 | return make_pedantic(self.encapsulated.getProtocol(trans)) |
| 242 | |
| 243 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 244 | def main(options): |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 245 | # common header allowed client types |
| 246 | allowed_client_types = [ |
| 247 | THeaderTransport.THeaderClientType.HEADERS, |
| 248 | THeaderTransport.THeaderClientType.FRAMED_BINARY, |
| 249 | THeaderTransport.THeaderClientType.UNFRAMED_BINARY, |
| 250 | THeaderTransport.THeaderClientType.FRAMED_COMPACT, |
| 251 | THeaderTransport.THeaderClientType.UNFRAMED_COMPACT, |
| 252 | ] |
| 253 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 254 | # set up the protocol factory form the --protocol option |
| 255 | prot_factories = { |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 256 | 'accel': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 257 | 'multia': TBinaryProtocol.TBinaryProtocolAcceleratedFactory(), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 258 | 'accelc': TCompactProtocol.TCompactProtocolAcceleratedFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 259 | 'multiac': TCompactProtocol.TCompactProtocolAcceleratedFactory(), |
| 260 | 'binary': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), |
| 261 | 'multi': TPedanticSequenceIdProtocolFactory(TBinaryProtocol.TBinaryProtocolFactory()), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 262 | 'compact': TCompactProtocol.TCompactProtocolFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 263 | 'multic': TCompactProtocol.TCompactProtocolFactory(), |
| 264 | 'header': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), |
| 265 | 'multih': THeaderProtocol.THeaderProtocolFactory(allowed_client_types), |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 266 | 'json': TJSONProtocol.TJSONProtocolFactory(), |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 267 | 'multij': TJSONProtocol.TJSONProtocolFactory(), |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 268 | } |
| Neil Williams | 66a44c5 | 2018-08-13 16:12:24 -0700 | [diff] [blame] | 269 | pfactory = prot_factories.get(options.proto, None) |
| 270 | if pfactory is None: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 271 | raise AssertionError('Unknown --protocol option: %s' % options.proto) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 272 | try: |
| 273 | pfactory.string_length_limit = options.string_limit |
| 274 | pfactory.container_length_limit = options.container_limit |
| James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 275 | except Exception: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 276 | # Ignore errors for those protocols that does not support length limit |
| 277 | pass |
| Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 278 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 279 | # get the server type (TSimpleServer, TNonblockingServer, etc...) |
| 280 | if len(args) > 1: |
| 281 | raise AssertionError('Only one server type may be specified, not multiple types.') |
| 282 | server_type = args[0] |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 283 | if options.trans == 'http': |
| 284 | server_type = 'THttpServer' |
| David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 285 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 286 | # Set up the handler and processor objects |
| Carel Combrink | 5abe53f | 2025-11-13 06:16:41 +0100 | [diff] [blame] | 287 | handler = TestHandler(options) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 288 | processor = ThriftTest.Processor(handler) |
| Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 289 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 290 | if options.proto.startswith('multi'): |
| 291 | secondHandler = SecondHandler() |
| 292 | secondProcessor = SecondService.Processor(secondHandler) |
| 293 | |
| 294 | multiplexedProcessor = TMultiplexedProcessor() |
| 295 | multiplexedProcessor.registerDefault(processor) |
| 296 | multiplexedProcessor.registerProcessor('ThriftTest', processor) |
| 297 | multiplexedProcessor.registerProcessor('SecondService', secondProcessor) |
| 298 | processor = multiplexedProcessor |
| 299 | |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 300 | global server |
| 301 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 302 | # Handle THttpServer as a special case |
| 303 | if server_type == 'THttpServer': |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 304 | if options.ssl: |
| 305 | __certfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.crt") |
| 306 | __keyfile = os.path.join(os.path.dirname(SCRIPT_DIR), "keys", "server.key") |
| 307 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory, cert_file=__certfile, key_file=__keyfile) |
| 308 | else: |
| 309 | server = THttpServer.THttpServer(processor, ('', options.port), pfactory) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 310 | server.serve() |
| 311 | sys.exit(0) |
| 312 | |
| 313 | # set up server transport and transport factory |
| 314 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 315 | host = None |
| 316 | if options.ssl: |
| 317 | from thrift.transport import TSSLSocket |
| Dmytro Shteflyuk | 0b68228 | 2026-02-04 16:26:46 -0500 | [diff] [blame] | 318 | keys_dir = os.path.join(os.path.dirname(SCRIPT_DIR), 'keys') |
| 319 | ca_certs = os.path.join(keys_dir, 'client.pem') |
| 320 | certfile = os.path.join(keys_dir, 'server.crt') |
| 321 | keyfile = os.path.join(keys_dir, 'server.key') |
| 322 | ssl_version = getattr(ssl, 'PROTOCOL_TLS_SERVER', ssl.PROTOCOL_TLSv1) |
| 323 | transport = TSSLSocket.TSSLServerSocket( |
| 324 | host, |
| 325 | options.port, |
| 326 | certfile=certfile, |
| 327 | keyfile=keyfile, |
| 328 | ca_certs=ca_certs, |
| 329 | cert_reqs=ssl.CERT_REQUIRED, |
| 330 | ssl_version=ssl_version, |
| 331 | ) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 332 | else: |
| Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 333 | transport = TSocket.TServerSocket(host, options.port, options.domain_socket) |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 334 | tfactory = TTransport.TBufferedTransportFactory() |
| 335 | if options.trans == 'buffered': |
| 336 | tfactory = TTransport.TBufferedTransportFactory() |
| 337 | elif options.trans == 'framed': |
| 338 | tfactory = TTransport.TFramedTransportFactory() |
| 339 | elif options.trans == '': |
| 340 | raise AssertionError('Unknown --transport option: %s' % options.trans) |
| 341 | else: |
| 342 | tfactory = TTransport.TBufferedTransportFactory() |
| 343 | # if --zlib, then wrap server transport, and use a different transport factory |
| 344 | if options.zlib: |
| Gregg Donovan | 62ec929 | 2026-01-29 16:51:37 -0500 | [diff] [blame] | 345 | if server_type != "TProcessPoolServer": |
| 346 | transport = TZlibTransport.TZlibTransport(transport) # wrap with zlib |
| 347 | # 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] | 348 | tfactory = TZlibTransport.TZlibTransportFactory() |
| 349 | |
| 350 | # do server-specific setup here: |
| 351 | if server_type == "TNonblockingServer": |
| 352 | server = TNonblockingServer.TNonblockingServer(processor, transport, inputProtocolFactory=pfactory) |
| 353 | elif server_type == "TProcessPoolServer": |
| 354 | import signal |
| 355 | from thrift.server import TProcessPoolServer |
| 356 | server = TProcessPoolServer.TProcessPoolServer(processor, transport, tfactory, pfactory) |
| 357 | server.setNumWorkers(5) |
| 358 | |
| 359 | def set_alarm(): |
| 360 | def clean_shutdown(signum, frame): |
| 361 | for worker in server.workers: |
| 362 | if options.verbose > 0: |
| 363 | logging.info('Terminating worker: %s' % worker) |
| 364 | worker.terminate() |
| 365 | if options.verbose > 0: |
| 366 | logging.info('Requesting server to stop()') |
| 367 | try: |
| 368 | server.stop() |
| James E. King, III | 350fe75 | 2017-10-25 09:57:18 -0400 | [diff] [blame] | 369 | except Exception: |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 370 | pass |
| 371 | signal.signal(signal.SIGALRM, clean_shutdown) |
| 372 | signal.alarm(4) |
| 373 | set_alarm() |
| 374 | else: |
| 375 | # look up server class dynamically to instantiate server |
| 376 | ServerClass = getattr(TServer, server_type) |
| 377 | server = ServerClass(processor, transport, tfactory, pfactory) |
| 378 | # enter server main loop |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 379 | server.serve() |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 380 | |
| James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 381 | |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 382 | def exit_gracefully(signum, frame): |
| 383 | print("SIGINT received\n") |
| 384 | server.shutdown() # doesn't work properly, yet |
| 385 | sys.exit(0) |
| 386 | |
| 387 | |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 388 | if __name__ == '__main__': |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 389 | signal.signal(signal.SIGINT, exit_gracefully) |
| 390 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 391 | parser = OptionParser() |
| 392 | parser.add_option('--libpydir', type='string', dest='libpydir', |
| 393 | help='include this directory to sys.path for locating library code') |
| 394 | parser.add_option('--genpydir', type='string', dest='genpydir', |
| 395 | default='gen-py', |
| 396 | help='include this directory to sys.path for locating generated code') |
| 397 | parser.add_option("--port", type="int", dest="port", |
| 398 | help="port number for server to listen on") |
| 399 | parser.add_option("--zlib", action="store_true", dest="zlib", |
| 400 | help="use zlib wrapper for compressed transport") |
| 401 | parser.add_option("--ssl", action="store_true", dest="ssl", |
| 402 | help="use SSL for encrypted transport") |
| 403 | parser.add_option('-v', '--verbose', action="store_const", |
| 404 | dest="verbose", const=2, |
| 405 | help="verbose output") |
| 406 | parser.add_option('-q', '--quiet', action="store_const", |
| 407 | dest="verbose", const=0, |
| 408 | help="minimal output") |
| 409 | parser.add_option('--protocol', dest="proto", type="string", |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 410 | 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] | 411 | parser.add_option('--transport', dest="trans", type="string", |
| James E. King III | 6f8c99e | 2018-03-24 16:32:02 -0400 | [diff] [blame] | 412 | help="transport to use, one of: buffered, framed, http") |
| Kengo Seki | f1c5341 | 2019-12-13 08:09:36 +0900 | [diff] [blame] | 413 | parser.add_option('--domain-socket', dest="domain_socket", type="string", |
| 414 | help="Unix domain socket path") |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 415 | parser.add_option('--container-limit', dest='container_limit', type='int', default=None) |
| 416 | 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] | 417 | parser.set_defaults(port=9090, verbose=1, proto='binary', transport='buffered') |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 418 | options, args = parser.parse_args() |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 419 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 420 | # Print TServer log to stdout so that the test-runner can redirect it to log files |
| 421 | logging.basicConfig(level=options.verbose) |
| Nobuaki Sukegawa | 7b545b5 | 2016-01-11 13:46:04 +0900 | [diff] [blame] | 422 | |
| Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 423 | sys.path.insert(0, os.path.join(SCRIPT_DIR, options.genpydir)) |
| Nobuaki Sukegawa | a185d7e | 2015-11-06 21:24:24 +0900 | [diff] [blame] | 424 | |
| James E. King III | 9804ab9 | 2019-02-07 16:59:05 -0500 | [diff] [blame] | 425 | from ThriftTest import ThriftTest, SecondService |
| 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)) |