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 |
| 8 | from local_thrift import thrift |
| 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 |
| 13 | |
| 14 | |
| 15 | # THeader stack should accept binary protocol with optionally framed transport |
| 16 | def main(argv): |
| 17 | p = argparse.ArgumentParser() |
| 18 | add_common_args(p) |
| 19 | # Since THeaderTransport acts as framed transport when detected frame, we |
| 20 | # cannot use --transport=framed as it would result in 2 layered frames. |
| 21 | p.add_argument('--override-transport') |
| 22 | args = p.parse_args() |
| 23 | assert args.protocol == 'header' |
| 24 | assert args.transport == 'buffered' |
| 25 | assert not args.ssl |
| 26 | |
| 27 | sock = TSocket(args.host, args.port, socket_family=socket.AF_INET) |
| 28 | if not args.override_transport or args.override_transport == 'buffered': |
| 29 | trans = TBufferedTransport(sock) |
| 30 | elif args.override_transport == 'framed': |
| 31 | trans = TFramedTransport(sock) |
| 32 | else: |
| 33 | raise ValueError('invalid transport') |
| 34 | trans.open() |
| 35 | proto = TBinaryProtocol(trans) |
| 36 | proto.writeMessageBegin('testVoid', TMessageType.CALL, 3) |
| 37 | proto.writeStructBegin('testVoid_args') |
| 38 | proto.writeFieldStop() |
| 39 | proto.writeStructEnd() |
| 40 | proto.writeMessageEnd() |
| 41 | trans.flush() |
| 42 | |
| 43 | _, mtype, _ = proto.readMessageBegin() |
| 44 | assert mtype == TMessageType.REPLY |
| 45 | proto.readStructBegin() |
| 46 | _, ftype, _ = proto.readFieldBegin() |
| 47 | assert ftype == TType.STOP |
| 48 | proto.readFieldEnd() |
| 49 | proto.readStructEnd() |
| 50 | proto.readMessageEnd() |
| 51 | |
| 52 | trans.close() |
| 53 | |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | sys.exit(main(sys.argv[1:])) |