Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Jens Geyer | 72a714e | 2025-08-26 22:12:07 +0200 | [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 | # |
| 21 | |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 22 | import argparse |
| 23 | import socket |
| 24 | import sys |
| 25 | |
| 26 | from util import add_common_args |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 27 | from local_thrift import thrift # noqa |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 28 | from thrift.Thrift import TMessageType, TType |
| 29 | from thrift.transport.TSocket import TSocket |
| 30 | from thrift.transport.TTransport import TBufferedTransport, TFramedTransport |
| 31 | from thrift.protocol.TBinaryProtocol import TBinaryProtocol |
Nobuaki Sukegawa | 95c628e | 2016-01-24 01:03:28 +0900 | [diff] [blame] | 32 | from thrift.protocol.TCompactProtocol import TCompactProtocol |
| 33 | |
| 34 | |
| 35 | def test_void(proto): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 36 | proto.writeMessageBegin('testVoid', TMessageType.CALL, 3) |
| 37 | proto.writeStructBegin('testVoid_args') |
| 38 | proto.writeFieldStop() |
| 39 | proto.writeStructEnd() |
| 40 | proto.writeMessageEnd() |
| 41 | proto.trans.flush() |
Nobuaki Sukegawa | 95c628e | 2016-01-24 01:03:28 +0900 | [diff] [blame] | 42 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 43 | _, mtype, _ = proto.readMessageBegin() |
| 44 | assert mtype == TMessageType.REPLY |
| 45 | proto.readStructBegin() |
| 46 | _, ftype, _ = proto.readFieldBegin() |
| 47 | assert ftype == TType.STOP |
| 48 | proto.readStructEnd() |
| 49 | proto.readMessageEnd() |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 50 | |
| 51 | |
| 52 | # THeader stack should accept binary protocol with optionally framed transport |
| 53 | def main(argv): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 54 | p = argparse.ArgumentParser() |
| 55 | add_common_args(p) |
| 56 | # Since THeaderTransport acts as framed transport when detected frame, we |
| 57 | # cannot use --transport=framed as it would result in 2 layered frames. |
| 58 | p.add_argument('--override-transport') |
| 59 | p.add_argument('--override-protocol') |
| 60 | args = p.parse_args() |
| 61 | assert args.protocol == 'header' |
| 62 | assert args.transport == 'buffered' |
| 63 | assert not args.ssl |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 64 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 65 | sock = TSocket(args.host, args.port, socket_family=socket.AF_INET) |
| 66 | if not args.override_transport or args.override_transport == 'buffered': |
| 67 | trans = TBufferedTransport(sock) |
| 68 | elif args.override_transport == 'framed': |
| 69 | print('TFRAMED') |
| 70 | trans = TFramedTransport(sock) |
| 71 | else: |
| 72 | raise ValueError('invalid transport') |
| 73 | trans.open() |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 74 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | if not args.override_protocol or args.override_protocol == 'binary': |
| 76 | proto = TBinaryProtocol(trans) |
| 77 | elif args.override_protocol == 'compact': |
| 78 | proto = TCompactProtocol(trans) |
| 79 | else: |
| 80 | raise ValueError('invalid transport') |
Nobuaki Sukegawa | 95c628e | 2016-01-24 01:03:28 +0900 | [diff] [blame] | 81 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 82 | test_void(proto) |
| 83 | test_void(proto) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 84 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 85 | trans.close() |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 89 | sys.exit(main(sys.argv[1:])) |