forked from CiscoDevNet/bigmuddy-network-telemetry-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxport_grpc_out_test.go
313 lines (269 loc) · 6.43 KB
/
xport_grpc_out_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
package main
import (
_ "fmt"
log "github.com/Sirupsen/logrus"
samples "github.com/cisco/bigmuddy-network-telemetry-pipeline/mdt_msg_samples"
"github.com/dlintw/goconf"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io"
"math/rand"
"sync"
"testing"
"time"
)
type grpcOutTestClient struct {
t *testing.T
}
func (c *grpcOutTestClient) GetRequestMetadata(
ctx context.Context, uri ...string) (
map[string]string, error) {
mapping := map[string]string{}
return mapping, nil
}
func (c *grpcOutTestClient) RequireTransportSecurity() bool {
return false
}
func (c *grpcOutTestClient) grpcOutTestClientRun(
server string,
timeout int,
target int,
result chan bool) {
var opts []grpc.DialOption
var ctx context.Context
opts = append(opts, grpc.WithInsecure())
opts = append(opts, grpc.WithPerRPCCredentials(c))
opts = append(opts,
grpc.WithTimeout(time.Second*time.Duration(timeout)))
conn, err := grpc.Dial(server, opts...)
if err != nil {
c.t.Log("client dial failed", server, err, timeout)
result <- false
return
}
defer conn.Close()
req := &SubJSONReqMsg{
ReqId: rand.Int63(),
}
ctx, _ = context.WithCancel(context.Background())
client := NewGRPCOutClient(conn)
stream, err := client.Pull(ctx, req)
if err != nil {
c.t.Log("client pull failed", server, err)
result <- false
return
}
i := 0
for {
reply, err := stream.Recv()
if err == nil {
i++
if req.ReqId != reply.ReqId {
c.t.Log("reqId mismatch", server, req.ReqId, reply.ReqId)
}
if i == target {
result <- true
stream.CloseSend()
if _, err := stream.Recv(); err != nil {
break
}
return
}
} else if err == io.EOF {
// c.t.Log("client recv EOF", server, err)
break
} else {
// c.t.Error("client recv failed", server, err)
result <- false
return
}
}
}
type grpcOutProducer struct {
t *testing.T
dMch chan<- dataMsg
codec codec
}
func (g *grpcOutProducer) String() string {
return "grpc_out_test"
}
func grpcOutProduceOneIteration(
sample *samples.SampleTelemetryTableEntry,
context samples.MDTContext) (abort bool) {
c := context.(*grpcOutProducer)
err, msgs := c.codec.blockToDataMsgs(c, sample.SampleStreamGPB)
if err != nil {
c.t.Fatal("producing one iteration blockToDataMsgs failed", err)
}
for _, msg := range msgs {
// fmt.Print("Dispatching msg\n")
c.dMch <- msg
}
return false
}
func grpcOutProduceContent(
t *testing.T,
ctrl chan struct{},
dMch chan<- dataMsg,
iterations int,
periodSeconds int) {
err, codec := getNewCodecGPB("test", ENCODING_GPB)
if err != nil {
t.Fatal("Failed to get codec", err)
}
prod := &grpcOutProducer{
t: t,
dMch: dMch,
codec: codec,
}
for {
// fmt.Print("Iteration\n")
for i := 0; i < iterations; i++ {
samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
grpcOutProduceOneIteration, prod)
select {
case <-ctrl:
return
default:
}
}
timeout := make(chan bool, 1)
go func() {
// fmt.Printf("Wait %vs \n", periodSeconds)
time.Sleep(time.Duration(periodSeconds) * time.Second)
timeout <- true
}()
select {
case <-ctrl:
// We're done here
return
case <-timeout:
// Wait for timeout specified
// fmt.Print("Period\n")
}
}
}
func TestGrpcOutConfigNegative(t *testing.T) {
var nc nodeConfig
var err error
startup()
logger = theLogger.WithFields(log.Fields{"tag": "test"})
//
// Read test config
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
t.Error("Config failed to open config pipeline_test.conf")
}
g := grpcOutputModuleNew()
err, _, _ = g.configure("mygrpcoutnolisten", nc)
if err == nil {
t.Error("Config expected to fail without 'listen'")
}
err, _, _ = g.configure("mygrpcoutbadencoding", nc)
if err == nil {
t.Error("Config expected to fail unsupported 'encoding'")
}
}
func coreTestGrpcOutClient(
t *testing.T,
clients int,
collect int,
produce_iterations int,
period int,
earlyTerminationAfter int,
produceOnly bool) {
var nc nodeConfig
var err error
startup()
//
// Read test config
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
log.Fatalf("Pipeline_test.conf could not be opened [%v]", err)
}
go metamonitoring_init(nc)
o := grpcOutputModuleNew()
err, dM, ctrl := o.configure("mygrpcout", nc)
if err != nil {
t.Fatal("Failed to configure module", err)
}
// Wait for configure to complete before we start test
time.Sleep(time.Second)
//
// Kick off client
c := &grpcOutTestClient{t: t}
server, err := nc.config.GetString("mygrpcout", "listen")
if err != nil {
t.Fatal("Failed to pick up 'listen'", err)
}
produceCtrl := make(chan struct{})
results := make(chan bool, clients)
if !produceOnly {
for i := 0; i < clients; i++ {
//
// Given this uses t we should synchronise and make sure
// it does not outlast the test.
go c.grpcOutTestClientRun(server, 3, collect, results)
}
}
go grpcOutProduceContent(t, produceCtrl, dM, produce_iterations, period)
if produceOnly {
//
// block forever
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}
success := 0
if earlyTerminationAfter == 0 {
for result := range results {
if result {
success++
if success == clients {
break
}
} else {
t.Fatal("Client returned failure")
}
}
} else {
time.Sleep(time.Second * time.Duration(period*earlyTerminationAfter))
}
//
// Stop producing
close(produceCtrl)
//
// Close module from top
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
ctrl <- request
// Wait for ACK
<-respChan
close(ctrl)
}
func TestGrpcOutClient(t *testing.T) {
clients := 10
// collect so many messages before returning ok at client
collect := 14
// number of interations to produce - 2xmsgs per iteration
produce_iterations := 5 // 5x2 = 10, i.e. at least two iterations
// period between iterations
period := 2
coreTestGrpcOutClient(t, clients, collect, produce_iterations, period, 0, false)
}
func TestGrpcOutClientCloseServerSide(t *testing.T) {
// Wait for port to be available again
time.Sleep(time.Second * 3)
clients := 10
// collect so many messages before returning ok at client
collect := 9999999
// number of iterations to produce - 2xmsgs per iteration
produce_iterations := 5 // 5x2 = 10, i.e. 999999/10 - very many iterations
// period between iterations
period := 1
coreTestGrpcOutClient(t, clients, collect, produce_iterations, period, 3, false)
}