-
Notifications
You must be signed in to change notification settings - Fork 6
/
xport_udp_test.go
267 lines (225 loc) · 5.7 KB
/
xport_udp_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
package main
import (
"bytes"
"encoding/binary"
"fmt"
samples "github.com/cisco-ie/pipeline-gnmi/mdt_msg_samples"
"github.com/dlintw/goconf"
log "github.com/sirupsen/logrus"
"net"
"sync"
"testing"
"time"
)
// Workaround for uninit lock assignment go vet error:
// https://github.com/golang/go/issues/13675
type udpTestContextLock struct {
sync.Mutex
sync.WaitGroup
}
type udpTestContext struct {
name string
maxlag int
max int
encap encapSTHdrMsgEncap
send int
handled int
dataChan chan dataMsg
tDone chan struct{}
lock *udpTestContextLock
conn *net.UDPConn
}
func udpTestSendOneMessage(
sample *samples.SampleTelemetryTableEntry,
context samples.MDTContext) (abort bool) {
var err error
c := context.(*udpTestContext)
hdr := encapSTHdr{
MsgType: ENC_ST_HDR_MSG_TYPE_TELEMETRY_DATA,
MsgEncap: ENC_ST_HDR_MSG_ENCAP_GPB,
MsgHdrVersion: ENC_ST_HDR_VERSION,
Msgflag: ENC_ST_HDR_MSG_FLAGS_NONE,
}
hdr.MsgEncap = c.encap
if c.encap == ENC_ST_HDR_MSG_ENCAP_GPB {
hdr.Msglen = uint32(len(sample.SampleStreamGPB))
} else if c.encap == ENC_ST_HDR_MSG_ENCAP_JSON {
hdr.Msglen = uint32(len(sample.SampleStreamJSON))
} else {
return true
}
hdrBuf := new(bytes.Buffer)
err = binary.Write(hdrBuf, binary.BigEndian, hdr)
if err != nil {
panic(err)
}
if c.encap == ENC_ST_HDR_MSG_ENCAP_GPB {
_, err = c.conn.Write(append(hdrBuf.Bytes(), sample.SampleStreamGPB...))
} else if c.encap == ENC_ST_HDR_MSG_ENCAP_JSON {
_, err = c.conn.Write(append(hdrBuf.Bytes(), sample.SampleStreamJSON...))
}
if err == nil {
c.lock.Lock()
c.send++
c.lock.Unlock()
}
return false
}
func udpTestSendMessages(c *udpTestContext) {
defer c.lock.Done()
for {
select {
case <-c.tDone:
return
default:
takeabreak := false
done := false
c.lock.Lock()
if c.handled >= c.max {
done = true
}
if c.send-c.handled >= c.maxlag {
takeabreak = true
}
c.lock.Unlock()
if done {
return
}
if takeabreak {
// fmt.Printf("send: waiting 1s at t %d r %d\n", c.send, c.handled)
time.Sleep(time.Millisecond * 2)
continue
}
samples.MDTSampleTelemetryTableIterate(
samples.SAMPLE_TELEMETRY_DATABASE_BASIC,
udpTestSendOneMessage, c)
}
}
}
func udpTestHandleMessages(c *udpTestContext) {
for {
select {
case <-c.dataChan:
c.lock.Lock()
c.handled++
c.lock.Unlock()
case <-c.tDone:
// fmt.Printf("receive: stop at t %d r %d\n", c.send, c.handled)
c.lock.Done()
return
}
}
}
func TestUDPServerNegative(tb *testing.T) {
var dataChans = make([]chan<- dataMsg, 0)
var nc nodeConfig
var err error
logfile := startup()
logger = theLogger.WithFields(log.Fields{"tag": "test"})
if logfile != nil {
defer logfile.Close()
}
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
dataChan := make(chan dataMsg, DATACHANNELDEPTH)
dataChans = append(dataChans, dataChan)
i := udpInputModuleNew()
err, _ = i.configure("udpinnolisten", nc, dataChans)
if err == nil {
tb.Fatal("Configured input module without listen")
}
err, _ = i.configure("udpinbadlisten", nc, dataChans)
if err == nil {
tb.Fatal("Configured input module with bad listen")
}
}
func TestUDPServer(tb *testing.T) {
var dataChans = make([]chan<- dataMsg, 0)
var nc nodeConfig
var err error
logfile := startup()
logger = theLogger.WithFields(log.Fields{"tag": "test"})
if logfile != nil {
defer logfile.Close()
}
nc.config, err = goconf.ReadConfigFile("pipeline_test.conf")
go metamonitoring_init(nc)
server, err := nc.config.GetString("udpin", "listen")
if err != nil {
tb.Fatal("Failed to pick up 'listen'", err)
}
dataChan := make(chan dataMsg, DATACHANNELDEPTH)
dataChans = append(dataChans, dataChan)
i := udpInputModuleNew()
err, ctrlChan := i.configure("udpin", nc, dataChans)
if err != nil {
tb.Fatal("Failed to config input module", err)
}
udpAddr, err := net.ResolveUDPAddr("udp", server)
if err != nil {
tb.Fatal("Failed to get server address", err)
}
outConn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
tb.Fatal("Failed to dial UDP", err)
}
//
// Read, as opposed to write buffer is the limitation for bursts.
// No need to setup outConn.SetWriteBuffer(46388608)
//
subtests := []udpTestContext{
{name: "BurstSparseGPB", maxlag: 5, max: 10000, encap: ENC_ST_HDR_MSG_ENCAP_GPB},
{name: "BurstMediumGPB", maxlag: 50, max: 10000, encap: ENC_ST_HDR_MSG_ENCAP_GPB},
{name: "BurstDenseGPB", maxlag: 500, max: 10000, encap: ENC_ST_HDR_MSG_ENCAP_GPB},
}
//
// Structure test as subtest in case I need to add more.
for _, subt := range subtests {
subt.lock = &udpTestContextLock{}
tb.Run(
fmt.Sprint(subt.name),
func(tb *testing.T) {
subt.tDone = make(chan struct{})
subt.conn = outConn
subt.dataChan = dataChan
subt.lock.Add(2)
go udpTestHandleMessages(&subt)
time.Sleep(time.Millisecond * 500)
go udpTestSendMessages(&subt)
ticker := time.NewTicker(time.Second * 2)
old_handled := 0
for range ticker.C {
subt.lock.Lock()
if subt.handled >= subt.max {
ticker.Stop()
break
}
if subt.handled == old_handled {
//
// stopped making progress
ticker.Stop()
tb.Fatalf("Progress stalled: handled %d, sent %d, max %d",
subt.handled, subt.send, subt.max)
}
old_handled = subt.handled
subt.lock.Unlock()
}
close(subt.tDone)
subt.lock.Wait()
})
}
//
// Test shutdown
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
//
// Send shutdown message
ctrlChan <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
tb.Error("failed to recieve acknowledgement indicating shutdown complete")
}
}