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