2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
24 "multiplexedprotocoltest"
31 func FindAvailableTCPServerPort() net.Addr {
32 if l, err := net.Listen("tcp", "127.0.0.1:0"); err != nil {
33 panic("Could not find available server port")
40 type FirstImpl struct{}
42 func (f *FirstImpl) ReturnOne(ctx context.Context) (r int64, err error) {
46 type SecondImpl struct{}
48 func (s *SecondImpl) ReturnTwo(ctx context.Context) (r int64, err error) {
52 func createTransport(addr net.Addr) (thrift.TTransport, error) {
53 socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT)
54 transport := thrift.NewTFramedTransport(socket)
55 err := transport.Open()
62 func TestMultiplexedProtocolFirst(t *testing.T) {
63 processor := thrift.NewTMultiplexedProcessor()
64 protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
65 transportFactory := thrift.NewTTransportFactory()
66 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
67 addr := FindAvailableTCPServerPort()
68 serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT)
70 t.Fatal("Unable to create server socket", err)
72 server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
74 firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
75 processor.RegisterProcessor("FirstService", firstProcessor)
77 secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
78 processor.RegisterProcessor("SecondService", secondProcessor)
82 time.Sleep(10 * time.Millisecond)
84 transport, err := createTransport(addr)
88 defer transport.Close()
89 protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "FirstService")
91 client := multiplexedprotocoltest.NewFirstClient(thrift.NewTStandardClient(protocol, protocol))
93 ret, err := client.ReturnOne(defaultCtx)
95 t.Fatal("Unable to call first server:", err)
97 t.Fatal("Unexpected result from server: ", ret)
101 func TestMultiplexedProtocolSecond(t *testing.T) {
102 processor := thrift.NewTMultiplexedProcessor()
103 protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
104 transportFactory := thrift.NewTTransportFactory()
105 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
106 addr := FindAvailableTCPServerPort()
107 serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT)
109 t.Fatal("Unable to create server socket", err)
111 server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
113 firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
114 processor.RegisterProcessor("FirstService", firstProcessor)
116 secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
117 processor.RegisterProcessor("SecondService", secondProcessor)
121 time.Sleep(10 * time.Millisecond)
123 transport, err := createTransport(addr)
127 defer transport.Close()
128 protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "SecondService")
130 client := multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol))
132 ret, err := client.ReturnTwo(defaultCtx)
134 t.Fatal("Unable to call second server:", err)
136 t.Fatal("Unexpected result from server: ", ret)
140 func TestMultiplexedProtocolLegacy(t *testing.T) {
141 processor := thrift.NewTMultiplexedProcessor()
142 protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
143 transportFactory := thrift.NewTTransportFactory()
144 transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
145 addr := FindAvailableTCPServerPort()
146 serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT)
148 t.Fatal("Unable to create server socket", err)
150 server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
152 firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
153 processor.RegisterProcessor("FirstService", firstProcessor)
155 secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
156 processor.RegisterProcessor("SecondService", secondProcessor)
160 time.Sleep(10 * time.Millisecond)
162 transport, err := createTransport(addr)
167 defer transport.Close()
169 protocol := thrift.NewTBinaryProtocolTransport(transport)
170 client := multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol))
172 ret, err := client.ReturnTwo(defaultCtx)
173 //expect error since default processor is not registered
175 t.Fatal("Expecting error")
178 //register default processor and call again
179 processor.RegisterDefault(multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{}))
180 transport, err = createTransport(addr)
185 defer transport.Close()
187 protocol = thrift.NewTBinaryProtocolTransport(transport)
188 client = multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol))
190 ret, err = client.ReturnTwo(defaultCtx)
192 t.Fatal("Unable to call legacy server:", err)
195 t.Fatal("Unexpected result from server: ", ret)