blob: 451399aa7baba6396b9a586dcc668967b461ee29 [file] [log] [blame]
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +09001#!/usr/bin/env python
2
3import argparse
4import socket
5import sys
6
7from util import add_common_args
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09008from local_thrift import thrift # noqa
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +09009from thrift.Thrift import TMessageType, TType
10from thrift.transport.TSocket import TSocket
11from thrift.transport.TTransport import TBufferedTransport, TFramedTransport
12from thrift.protocol.TBinaryProtocol import TBinaryProtocol
Nobuaki Sukegawa95c628e2016-01-24 01:03:28 +090013from thrift.protocol.TCompactProtocol import TCompactProtocol
14
15
16def test_void(proto):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090017 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 Sukegawa95c628e2016-01-24 01:03:28 +090023
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090024 _, 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 Sukegawa378b7272016-01-03 17:04:50 +090031
32
33# THeader stack should accept binary protocol with optionally framed transport
34def main(argv):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090035 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 Sukegawa378b7272016-01-03 17:04:50 +090045
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090046 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 Sukegawa378b7272016-01-03 17:04:50 +090055
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 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 Sukegawa95c628e2016-01-24 01:03:28 +090062
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 test_void(proto)
64 test_void(proto)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090065
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090066 trans.close()
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090067
68
69if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090070 sys.exit(main(sys.argv[1:]))