blob: 5ef47ef8406545208ddc57ce3d3f2bd9614a3d93 [file] [log] [blame]
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +09001#!/usr/bin/env python
2
Jens Geyer72a714e2025-08-26 22:12:07 +02003#
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 Sukegawa378b7272016-01-03 17:04:50 +090022import argparse
23import socket
24import sys
25
26from util import add_common_args
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090027from local_thrift import thrift # noqa
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090028from thrift.Thrift import TMessageType, TType
29from thrift.transport.TSocket import TSocket
30from thrift.transport.TTransport import TBufferedTransport, TFramedTransport
31from thrift.protocol.TBinaryProtocol import TBinaryProtocol
Nobuaki Sukegawa95c628e2016-01-24 01:03:28 +090032from thrift.protocol.TCompactProtocol import TCompactProtocol
33
34
35def test_void(proto):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090036 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 Sukegawa95c628e2016-01-24 01:03:28 +090042
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090043 _, 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 Sukegawa378b7272016-01-03 17:04:50 +090050
51
52# THeader stack should accept binary protocol with optionally framed transport
53def main(argv):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090054 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 Sukegawa378b7272016-01-03 17:04:50 +090064
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090065 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 Sukegawa378b7272016-01-03 17:04:50 +090074
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090075 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 Sukegawa95c628e2016-01-24 01:03:28 +090081
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090082 test_void(proto)
83 test_void(proto)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090084
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090085 trans.close()
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090086
87
88if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090089 sys.exit(main(sys.argv[1:]))