]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/go/test/tests/multiplexed_protocol_test.go
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / go / test / tests / multiplexed_protocol_test.go
1 /*
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 * under the License.
18 */
19
20 package tests
21
22 import (
23 "context"
24 "multiplexedprotocoltest"
25 "net"
26 "testing"
27 "thrift"
28 "time"
29 )
30
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")
34 } else {
35 defer l.Close()
36 return l.Addr()
37 }
38 }
39
40 type FirstImpl struct{}
41
42 func (f *FirstImpl) ReturnOne(ctx context.Context) (r int64, err error) {
43 return 1, nil
44 }
45
46 type SecondImpl struct{}
47
48 func (s *SecondImpl) ReturnTwo(ctx context.Context) (r int64, err error) {
49 return 2, nil
50 }
51
52 func createTransport(addr net.Addr) (thrift.TTransport, error) {
53 socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT)
54 transport := thrift.NewTFramedTransport(socket)
55 err := transport.Open()
56 if err != nil {
57 return nil, err
58 }
59 return transport, nil
60 }
61
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)
69 if err != nil {
70 t.Fatal("Unable to create server socket", err)
71 }
72 server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
73
74 firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
75 processor.RegisterProcessor("FirstService", firstProcessor)
76
77 secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
78 processor.RegisterProcessor("SecondService", secondProcessor)
79
80 defer server.Stop()
81 go server.Serve()
82 time.Sleep(10 * time.Millisecond)
83
84 transport, err := createTransport(addr)
85 if err != nil {
86 t.Fatal(err)
87 }
88 defer transport.Close()
89 protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "FirstService")
90
91 client := multiplexedprotocoltest.NewFirstClient(thrift.NewTStandardClient(protocol, protocol))
92
93 ret, err := client.ReturnOne(defaultCtx)
94 if err != nil {
95 t.Fatal("Unable to call first server:", err)
96 } else if ret != 1 {
97 t.Fatal("Unexpected result from server: ", ret)
98 }
99 }
100
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)
108 if err != nil {
109 t.Fatal("Unable to create server socket", err)
110 }
111 server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
112
113 firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
114 processor.RegisterProcessor("FirstService", firstProcessor)
115
116 secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
117 processor.RegisterProcessor("SecondService", secondProcessor)
118
119 defer server.Stop()
120 go server.Serve()
121 time.Sleep(10 * time.Millisecond)
122
123 transport, err := createTransport(addr)
124 if err != nil {
125 t.Fatal(err)
126 }
127 defer transport.Close()
128 protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "SecondService")
129
130 client := multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol))
131
132 ret, err := client.ReturnTwo(defaultCtx)
133 if err != nil {
134 t.Fatal("Unable to call second server:", err)
135 } else if ret != 2 {
136 t.Fatal("Unexpected result from server: ", ret)
137 }
138 }
139
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)
147 if err != nil {
148 t.Fatal("Unable to create server socket", err)
149 }
150 server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
151
152 firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
153 processor.RegisterProcessor("FirstService", firstProcessor)
154
155 secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
156 processor.RegisterProcessor("SecondService", secondProcessor)
157
158 defer server.Stop()
159 go server.Serve()
160 time.Sleep(10 * time.Millisecond)
161
162 transport, err := createTransport(addr)
163 if err != nil {
164 t.Error(err)
165 return
166 }
167 defer transport.Close()
168
169 protocol := thrift.NewTBinaryProtocolTransport(transport)
170 client := multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol))
171
172 ret, err := client.ReturnTwo(defaultCtx)
173 //expect error since default processor is not registered
174 if err == nil {
175 t.Fatal("Expecting error")
176 }
177
178 //register default processor and call again
179 processor.RegisterDefault(multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{}))
180 transport, err = createTransport(addr)
181 if err != nil {
182 t.Error(err)
183 return
184 }
185 defer transport.Close()
186
187 protocol = thrift.NewTBinaryProtocolTransport(transport)
188 client = multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol))
189
190 ret, err = client.ReturnTwo(defaultCtx)
191 if err != nil {
192 t.Fatal("Unable to call legacy server:", err)
193 }
194 if ret != 2 {
195 t.Fatal("Unexpected result from server: ", ret)
196 }
197 }