/
map_test.go
198 lines (178 loc) · 4.9 KB
/
map_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Copyright 2023 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test
import (
"context"
"errors"
"math/rand"
"net"
"strconv"
"testing"
"time"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/client/callopt"
"github.com/cloudwego/kitex/client/genericclient"
"github.com/cloudwego/kitex/internal/test"
"github.com/cloudwego/kitex/pkg/generic"
"github.com/cloudwego/kitex/server"
"github.com/cloudwego/kitex/server/genericserver"
)
var mcli genericclient.Client
func TestThriftMapExample(t *testing.T) {
testThriftMapExample(t)
}
func BenchmarkThriftMapExample(b *testing.B) {
for i:=0; i<b.N; i++ {
testThriftMapExample(b)
}
}
func testThriftMapExample(t testing.TB) {
log_id := strconv.Itoa(rand.Int())
req := MakeExampleReqMap(true, ReqMsg, log_id)
out, err := mcli.GenericCall(context.Background(), Method, req, callopt.WithRPCTimeout(100*time.Second))
test.Assert(t, err == nil, err)
resp, ok := out.(map[string]interface{})
test.Assert(t, ok)
msg, ok := resp["Msg"].(string)
if !ok || msg != RespMsg {
t.Fail()
}
require_field, ok := resp["required_field"].(string)
if !ok || require_field != ReqMsg{
t.Fail()
}
logid, ok := resp["BaseResp"].(map[string]interface{})["StatusMessage"]
if !ok || log_id != logid {
t.Fail()
}
}
func initThriftMapClient(addr, idl string) genericclient.Client {
p, err := generic.NewThriftFileProvider(idl)
if err != nil {
panic(err)
}
g, err := generic.MapThriftGeneric(p)
if err != nil {
panic(err)
}
cli := newGenericClient("destServiceName", g, addr)
if err != nil {
panic(err)
}
return cli
}
func newGenericClient(destService string, g generic.Generic, targetIPPort string) genericclient.Client {
var opts []client.Option
opts = append(opts, client.WithHostPorts(targetIPPort))
genericCli, _ := genericclient.NewClient(destService, g, opts...)
return genericCli
}
func initThriftMapServer(address string, idl string, handler generic.Service) server.Server {
addr, _ := net.ResolveTCPAddr("tcp", address)
p, err := generic.NewThriftFileProvider(idl)
if err != nil {
panic(err)
}
g, err := generic.MapThriftGeneric(p)
if err != nil {
panic(err)
}
svr := newGenericServer(g, addr, handler)
if err != nil {
panic(err)
}
time.Sleep(500 * time.Millisecond)
return svr
}
func newGenericServer(g generic.Generic, addr net.Addr, handler generic.Service) server.Server {
var opts []server.Option
opts = append(opts, server.WithServiceAddr(addr))
svr := genericserver.NewServer(handler, g, opts...)
go func() {
err := svr.Run()
if err != nil {
panic(err)
}
}()
return svr
}
type ExampeServerImpl struct{}
// GenericCall ...
func (g *ExampeServerImpl) GenericCall(ctx context.Context, method string, request interface{}) (response interface{}, err error) {
buf := request.(map[string]interface{})
required_field := ""
logid := ""
if b, ok := buf["B"].(bool); ok && b {
if base, ok := buf["Base"].(map[string]interface{}); !ok {
return nil, errors.New("empty base field")
} else {
logid, _ = base["LogID"].(string)
}
if testmap, ok := buf["TestMap"].(map[interface{}]interface{}); !ok {
return nil, errors.New("empty TestMap")
} else if a, ok := testmap["a"].(map[string]interface{}); !ok {
return nil, errors.New("key 'a' not found")
} else {
required_field, _ = a["Bar"].(string)
}
}
return MakeExampleRespMap(RespMsg, required_field, logid)
}
func MakeExampleRespMap(msg string, require_field string, logid string) (map[string]interface{}, error) {
return map[string]interface{}{
"Msg": msg,
"required_field": require_field,
"BaseResp": map[string]interface{}{
"StatusMessage": logid,
},
}, nil
}
func MakeExampleReqMap(B bool, A string, logid string) map[string]interface{} {
list := make([]interface{}, SampleListSize+1)
list[0] = map[string]interface{}{
"Bar": A,
}
for i:=1; i<len(list); i++ {
list[i] = map[string]interface{}{
"Bar": A,
}
}
m := make(map[string]interface{}, SampleListSize+1)
m["a"] = map[string]interface{}{
"Bar": A,
}
for i:=1; i<len(list); i++ {
m[strconv.Itoa(i)] = map[string]interface{}{
"Bar": A,
}
}
return map[string]interface{} {
"Msg": "Hello",
"Foo": int32(1),
"TestList": list,
"TestMap": m,
"I64List": []interface{}{
int64(1), int64(2), int64(3),
},
"B": B,
"Base": map[string]interface{}{
"LogID": logid,
"Caller": "d.e.f",
"Addr": "127.0.0.1",
"Client": "dynamicgo",
},
}
}