-
Notifications
You must be signed in to change notification settings - Fork 62
/
service_client_test.go
370 lines (316 loc) · 7.27 KB
/
service_client_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package goroslib
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type TestServiceReq struct {
A float64
B string
}
type TestServiceRes struct {
C float64
}
type TestService struct {
TestServiceReq
TestServiceRes
}
func TestServiceClientRequestAfterProvider(t *testing.T) {
for _, provider := range []string{
"cpp",
"go",
} {
t.Run(provider, func(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
var p *container
var nsp *Node
var sp *ServiceProvider
switch provider {
case "cpp":
p = newContainer(t, "node-serviceprovider", m.IP())
case "go":
var err error
nsp, err = NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib_sp",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
sp, err = NewServiceProvider(ServiceProviderConf{
Node: nsp,
Name: "test_srv",
Srv: &TestService{},
Callback: func(req *TestServiceReq) (*TestServiceRes, bool) {
c := float64(0)
if req.A == 123 && req.B == "456" {
c = 123
}
return &TestServiceRes{C: c}, true
},
})
require.NoError(t, err)
}
n, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer n.Close()
sc, err := NewServiceClient(ServiceClientConf{
Node: n,
Name: "test_srv",
Srv: &TestService{},
})
require.NoError(t, err)
defer sc.Close()
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
go func() {
defer wg.Done()
req := TestServiceReq{
A: 123,
B: "456",
}
var res TestServiceRes
err2 := sc.Call(&req, &res)
require.NoError(t, err2)
expected := TestServiceRes{C: 123}
require.Equal(t, expected, res)
}()
}
wg.Wait()
switch provider {
case "cpp":
p.close()
case "go":
sp.Close()
nsp.Close()
}
req := TestServiceReq{
A: 123,
B: "456",
}
var res TestServiceRes
err = sc.Call(&req, &res)
require.Error(t, err)
})
}
}
func TestServiceClientRequestBeforeProvider(t *testing.T) {
for _, provider := range []string{
"cpp",
"go",
} {
t.Run(provider, func(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
n, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer n.Close()
sc, err := NewServiceClient(ServiceClientConf{
Node: n,
Name: "test_srv",
Srv: &TestService{},
})
require.NoError(t, err)
defer sc.Close()
req := TestServiceReq{
A: 123,
B: "456",
}
var res TestServiceRes
err = sc.Call(&req, &res)
require.Error(t, err)
switch provider {
case "cpp":
p := newContainer(t, "node-serviceprovider", m.IP())
defer p.close()
case "go":
nsp, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib_sp",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer nsp.Close()
sp, err := NewServiceProvider(ServiceProviderConf{
Node: nsp,
Name: "test_srv",
Srv: &TestService{},
Callback: func(req *TestServiceReq) (*TestServiceRes, bool) {
c := float64(0)
if req.A == 123 && req.B == "456" {
c = 123
}
return &TestServiceRes{C: c}, true
},
})
require.NoError(t, err)
defer sp.Close()
}
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
go func() {
defer wg.Done()
req := TestServiceReq{
A: 123,
B: "456",
}
var res TestServiceRes
err := sc.Call(&req, &res)
require.NoError(t, err)
expected := TestServiceRes{C: 123}
require.Equal(t, expected, res)
}()
}
wg.Wait()
})
}
}
func TestServiceClientContext(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
nsp, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib_sp",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer nsp.Close()
spTerminate := make(chan struct{})
clientCreated := make(chan struct{})
sp, err := NewServiceProvider(ServiceProviderConf{
Node: nsp,
Name: "test_srv",
Srv: &TestService{},
Callback: func(_ *TestServiceReq) (*TestServiceRes, bool) {
<-spTerminate
return &TestServiceRes{C: 123}, true
},
onClient: func() {
close(clientCreated)
},
})
require.NoError(t, err)
defer sp.Close()
defer close(spTerminate)
n, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer n.Close()
sc, err := NewServiceClient(ServiceClientConf{
Node: n,
Name: "test_srv",
Srv: &TestService{},
})
require.NoError(t, err)
defer sc.Close()
ctx, ctxCancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
err := sc.CallContext(ctx, &TestServiceReq{
A: 123,
B: "456",
}, &TestServiceRes{})
require.Error(t, err)
close(done)
}()
<-clientCreated
ctxCancel()
<-done
}
func TestServiceClientErrors(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
n, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib_sp",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer n.Close()
t.Run("missing node", func(t *testing.T) {
_, err := NewServiceClient(ServiceClientConf{})
require.Error(t, err)
})
t.Run("missing name", func(t *testing.T) {
_, err := NewServiceClient(ServiceClientConf{
Node: n,
})
require.Error(t, err)
})
t.Run("missing srv", func(t *testing.T) {
_, err := NewServiceClient(ServiceClientConf{
Node: n,
Name: "test_srv",
})
require.Error(t, err)
})
}
func TestServiceClientReqResType(t *testing.T) {
m := newContainerMaster(t)
defer m.close()
n, err := NewNode(NodeConf{
Namespace: "/myns",
Name: "goroslib_sp",
MasterAddress: m.IP() + ":11311",
})
require.NoError(t, err)
defer n.Close()
sp, err := NewServiceProvider(ServiceProviderConf{
Node: n, Name: "test_srv", Srv: &TestService{},
Callback: func(_ *TestServiceReq) (*TestServiceRes, bool) {
return &TestServiceRes{}, true
},
})
require.NoError(t, err)
defer sp.Close()
sc, err := NewServiceClient(ServiceClientConf{Node: n, Name: "test_srv", Srv: &TestService{}})
require.NoError(t, err)
defer sc.Close()
t.Run("req with same structure", func(t *testing.T) {
res := &TestServiceRes{}
err := sc.Call(&struct {
A float64
B string
}{A: 123, B: "456"}, res)
require.NoError(t, err)
})
t.Run("req with different structure", func(t *testing.T) {
res := &TestServiceRes{}
assert.PanicsWithValue(t, "wrong req", func() {
sc.Call(&struct { //nolint:errcheck
A float32
B string
}{A: 123, B: "456"}, res)
})
})
t.Run("res with same structure", func(t *testing.T) {
res := &struct {
C float64
}{}
err := sc.Call(&TestServiceReq{A: 123, B: "456"}, res)
require.NoError(t, err)
})
t.Run("res with different structure", func(t *testing.T) {
res := &struct {
C float32
}{}
assert.PanicsWithValue(t, "wrong res", func() {
sc.Call(&TestServiceReq{A: 123, B: "456"}, res) //nolint:errcheck
})
})
}