-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphite_test.go
538 lines (473 loc) · 14 KB
/
graphite_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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package graphite
import (
"bytes"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"testing"
"time"
)
var metricTestData = []struct {
name string // Metric.Name
value interface{} // Metric.Value
timestamp int64 // Metric.Timestamp
prefix string // Client.prefix
plain string // expected plain value
}{
// test int value types
{"test.int", int(12), int64(1493712949), "", "test.int 12 1493712949\n"},
{"test.int16", int16(12), int64(1493712949), "", "test.int16 12 1493712949\n"},
{"test.int32", int32(12), int64(1493712949), "", "test.int32 12 1493712949\n"},
{"test.int64", int64(12), int64(1493712949), "", "test.int64 12 1493712949\n"},
{"test.uint", uint(12), int64(1493712949), "", "test.uint 12 1493712949\n"},
// test uint value types
{"test.uint16", uint16(12), int64(1493712949), "", "test.uint16 12 1493712949\n"},
{"test.uint32", uint32(12), int64(1493712949), "", "test.uint32 12 1493712949\n"},
{"test.uint64", uint64(12), int64(1493712949), "", "test.uint64 12 1493712949\n"},
// test float types
{"test.float32", float32(12.3), int64(1493712949), "", "test.float32 12.3 1493712949\n"},
{"test.float64", float64(12.3), int64(1493712949), "", "test.float64 12.3 1493712949\n"},
{"test.float64", float64(12.34567890), int64(1493712949), "", "test.float64 12.3456789 1493712949\n"},
// test prefix
{"test.int", int(34), int64(1493712949), "abc", "abctest.int 34 1493712949\n"},
{"test.int", int(34), int64(1493712949), "def.", "def.test.int 34 1493712949\n"},
{"test.int", int(34), int64(1493712949), "ghi- ", "ghi-test.int 34 1493712949\n"},
{"test.int", int(34), int64(1493712949), " jkl ", "jkltest.int 34 1493712949\n"},
}
var tcpServer = NewGraphiteServer("tcp", "127.0.0.1:62003")
var udpServer = NewGraphiteServer("udp", "127.0.0.1:62003")
type GraphiteServer struct {
network string
address string
messageBuffer chan string
shutdown bool
listener *net.TCPListener
tcpConns []*net.TCPConn
udpConn *net.UDPConn
sync.Mutex
workerStarted chan struct{}
workerStopped chan struct{}
}
func NewGraphiteServer(network string, address string) *GraphiteServer {
server := &GraphiteServer{
network: network,
address: address,
messageBuffer: make(chan string, 100),
shutdown: false,
workerStarted: make(chan struct{}, 1),
workerStopped: make(chan struct{}, 1),
}
go server.receiveWorker()
return server
}
func (s *GraphiteServer) GetMessage(timeout time.Duration) (string, error) {
// if server has already shutdown, and no message in buffer, just return empty imediately
if s.shutdown == true && len(s.messageBuffer) == 0 {
return "", nil
}
select {
case message := <-s.messageBuffer:
return message, nil
case <-time.After(timeout):
return "", errors.New("timeout waiting for data")
}
}
func (s *GraphiteServer) Shutdown() {
// Wait for worker to be run (and listener started)
// otherwise, listener may be started after Shutdown()
<-s.workerStarted
s.Lock()
if s.network == "tcp" {
if s.listener != nil {
s.listener.Close()
s.listener = nil
}
for _, tcpConn := range s.tcpConns {
tcpConn.Close()
}
}
if s.network == "udp" && s.udpConn != nil {
s.udpConn.Close()
s.udpConn = nil
}
s.Unlock()
// wait for worker stop
<-s.workerStopped
s.shutdown = true
}
func (s *GraphiteServer) receiveWorker() {
defer func() {
// close workerStarted in case some error happend before listener started
close(s.workerStarted)
close(s.workerStopped)
}()
if s.network == "tcp" {
tcpAddr, err := net.ResolveTCPAddr(s.network, s.address)
if err != nil {
fmt.Println("net.ResolveTCPAddr() failed with:", err)
return
}
listener, err := net.ListenTCP(s.network, tcpAddr)
if err != nil {
fmt.Println("net.ListenTCP() failed with:", err)
return
}
s.Lock()
s.listener = listener
s.Unlock()
s.workerStarted <- struct{}{}
for {
if s.listener == nil {
// server was shutdown
return
}
tcpConn, err := s.listener.AcceptTCP()
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
fmt.Println("listener.AcceptTCP() failed with:", err)
}
break
}
s.Lock()
s.tcpConns = append(s.tcpConns, tcpConn)
s.Unlock()
go func(conn *net.TCPConn) {
defer conn.Close()
for {
buf := make([]byte, 1500)
n, err := conn.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Println("conn.Read() failed with:", err)
}
break
}
messages := bytes.Split(buf[:n], []byte("\n"))
for _, message := range messages {
if len(message) > 0 {
messageString := string(message)
messageString = messageString + "\n"
s.messageBuffer <- messageString
}
}
}
}(tcpConn)
}
} else {
udpAddr, err := net.ResolveUDPAddr(s.network, s.address)
if err != nil {
fmt.Println("net.ResolveUDPAddr() failed with:", err)
return
}
conn, err := net.ListenUDP(s.network, udpAddr)
if err != nil {
fmt.Println("net.ListenUDP() failed with:", err)
return
}
s.Lock()
s.udpConn = conn
s.Unlock()
s.workerStarted <- struct{}{}
for {
if s.udpConn == nil {
// server was shutdown
return
}
buf := make([]byte, 1500)
if l, _, err := conn.ReadFromUDP(buf); err == nil {
s.messageBuffer <- string(buf[:l])
} else {
if !strings.Contains(err.Error(), "use of closed network connection") {
fmt.Println("conn.ReadFromUDP() failed with:", err)
}
break
}
}
}
}
func TestMetricPlain(t *testing.T) {
for _, data := range metricTestData {
metric := &Metric{Name: data.name, Value: data.value, Timestamp: data.timestamp}
ret := metric.Plain(cleanPrefix(data.prefix))
if ret != data.plain {
t.Errorf(
"Metric:Plain() was incorrect, got: %s, want: %s",
strings.Replace(ret, "\n", "\\n", -1),
strings.Replace(data.plain, "\n", "\\n", -1),
)
}
}
}
func TestMetricPlainB(t *testing.T) {
for _, data := range metricTestData {
metric := &Metric{Name: data.name, Value: data.value, Timestamp: data.timestamp}
ret := metric.PlainB(cleanPrefix(data.prefix))
plain := []byte(data.plain)
if bytes.Compare(ret, plain) != 0 {
t.Errorf(
"Metric:PlainB() was incorrect, got: %s, want: %s",
bytes.Replace(ret, []byte("\n"), []byte("\\n"), -1),
bytes.Replace(plain, []byte("\n"), []byte("\\n"), -1),
)
}
}
}
func TestNopClient(t *testing.T) {
// nop client does nothing, this test is just for code coverage
c, err := NewNopClient()
if err != nil {
t.Errorf("Failed to create Nop Client")
return
}
// send 100000 metrics in total, ensure nop client won't block
size := 100000
for i := 0; i < size; i++ {
c.SendMetric(&Metric{Name: "test.nop", Value: i, Timestamp: 0})
c.SendSimple("test.nop", 0, 0)
}
metrics := make([]*Metric, size)
for i := 0; i < size; i++ {
metrics[i] = &Metric{Name: "test.nop", Value: i, Timestamp: 0}
}
c.SendMetrics(metrics)
}
func TestNewClient(t *testing.T) {
var clients = []struct {
url string
errsig string
}{
{"127.0.0.1", ""},
{"127.0.0.1:2003", ""},
{"tcp://127.0.0.1", ""},
{"tcp://127.0.0.1:2003", ""},
{"udp://127.0.0.1", ""},
{"udp://127.0.0.1:2003", ""},
{"pickle://127.0.0.1", ""},
{"pickle://127.0.0.1:2004", ""},
{"", "Invalid graphite url"},
{"tcp://", "Invalid graphite url"},
{"foo://127.0.0.1", "Invalid graphite url"},
{":2003", "Invalid graphite url"},
}
for _, data := range clients {
client, err := NewClient(data.url, "", time.Second)
switch {
case err == nil && data.errsig != "":
t.Errorf("NewClient didn't fail as expected, url: %s, expected signature: %s", data.url, data.errsig)
case err != nil && data.errsig == "":
t.Errorf("NewClient failed for url: %s with error: %v", data.url, err)
case err != nil && !strings.Contains(err.Error(), data.errsig):
t.Errorf("NewClient failed with unexpected error, url: %s, error: %v, expected error: %s", data.url, err, data.errsig)
default:
// great!
}
if client != nil {
client.Shutdown(time.Second)
}
}
}
func checkServerMessage(server *GraphiteServer, expected string, t *testing.T) {
message, err := server.GetMessage(1 * time.Second)
if err != nil {
t.Errorf("server.GetMessage() failed with: %s", err)
} else if message != message {
t.Errorf(
"server got incorrect message, got: %s, want: %s",
strings.Replace(message, "\n", "\\n", -1),
strings.Replace(expected, "\n", "\\n", -1),
)
}
}
func testClient(client *Client, server *GraphiteServer, t *testing.T) {
var metric *Metric
var err error
// for testing SendChan
metricChan := make(chan *Metric)
go client.SendChan(metricChan)
for _, data := range metricTestData {
if data.prefix != "" {
continue
}
// test SendChan()
metric = &Metric{Name: data.name, Value: data.value, Timestamp: data.timestamp}
metricChan <- metric
checkServerMessage(server, data.plain, t)
// test SendMetric()
metric = &Metric{Name: data.name, Value: data.value, Timestamp: data.timestamp}
client.SendMetric(metric)
checkServerMessage(server, data.plain, t)
// test SendSimple()
client.SendSimple(data.name, data.value, data.timestamp)
checkServerMessage(server, data.plain, t)
}
close(metricChan)
// after Shutdown(), should not receive anything
client.Shutdown(1 * time.Second)
client.SendMetric(&Metric{Name: "test.shutdown", Value: 0, Timestamp: 0})
_, err = server.GetMessage(1 * time.Second)
if err == nil {
t.Errorf("Clien still sends data to server after shutdown")
}
}
func TestTCPClient(t *testing.T) {
client, err := NewClient("tcp://127.0.0.1:62003", "", 1*time.Second)
if err != nil {
t.Errorf("Failed to create TCP Client")
return
}
testClient(client, tcpServer, t)
}
func TestUDPClient(t *testing.T) {
client, err := NewClient("udp://127.0.0.1:62003", "", 1*time.Second)
if err != nil {
t.Errorf("Failed to create UDP Client")
return
}
testClient(client, udpServer, t)
}
func testClientWithPrefix(newClientFunc func(string) (*Client, error), server *GraphiteServer, t *testing.T) {
var metric *Metric
for _, data := range metricTestData {
if data.prefix == "" {
continue
}
client, err := newClientFunc(data.prefix)
if err != nil {
t.Errorf("Failed to create Client")
continue
}
metric = &Metric{Name: data.name, Value: data.value, Timestamp: data.timestamp}
client.SendMetric(metric)
checkServerMessage(server, data.plain, t)
client.SendSimple(data.name, data.value, data.timestamp)
checkServerMessage(server, data.plain, t)
client.Shutdown(1 * time.Second)
}
}
func TestTCPClientWithPrefix(t *testing.T) {
testClientWithPrefix(
func(prefix string) (*Client, error) {
return NewClient("tcp://127.0.0.1:62003", prefix, 1*time.Second)
},
tcpServer,
t,
)
}
func TestUDPClientWithPrefix(t *testing.T) {
testClientWithPrefix(
func(prefix string) (*Client, error) {
return NewClient("udp://127.0.0.1:62003", prefix, 1*time.Second)
},
udpServer,
t,
)
}
func testClientBulkSend(client *Client, server *GraphiteServer, t *testing.T) {
var metrics []*Metric
var plains []string
for _, data := range metricTestData {
if data.prefix != "" {
continue
}
metrics = append(metrics, &Metric{Name: data.name, Value: data.value, Timestamp: data.timestamp})
plains = append(plains, data.plain)
}
client.SendMetrics(metrics)
for _, plain := range plains {
checkServerMessage(server, plain, t)
}
client.Shutdown(1 * time.Second)
}
func TestTCPClientBulkSend(t *testing.T) {
client, err := NewClient("tcp://127.0.0.1:62003", "", 1*time.Second)
if err != nil {
t.Errorf("Failed to create TCP Client")
return
}
testClientBulkSend(client, tcpServer, t)
}
func TestUDPClientBuldSend(t *testing.T) {
client, err := NewClient("udp://127.0.0.1:62003", "", 1*time.Second)
if err != nil {
t.Errorf("Failed to create UDP Client")
return
}
testClientBulkSend(client, udpServer, t)
}
func testClientReconnect(newClient func() *Client, newServer func() *GraphiteServer, t *testing.T) {
// procedure
// * new server and client
// * send some data
// * shutdown the server, new another server
// * send some data
// * check if data after new server created is received
// new server
server := newServer()
client := newClient()
// send some data
for i := 0; i < 10; i++ {
client.SendSimple("test.reconnect", i, 1493712949)
}
// shutdown the server, and new server
server.Shutdown()
server = newServer()
// send some other data
var dataAfterReconnect = make(map[string]struct{})
var foundDataAfterReconnect = 0
for i := 10; i < 20; i++ {
client.SendSimple("test.reconnect", i, 1493712949)
plain := fmt.Sprintf("test.reconnect %v 1493712949\n", i)
dataAfterReconnect[plain] = struct{}{}
// wait for some time, make sure the lower level network stack has sent the data out,
// so can network error (e.g. server closed) be noticed.
// if we don't sleep here, network stack may merge subsequent data into one packet,
// and subsequent net.Conn.Write() would success without notice that server has already closed connection.
time.Sleep(time.Microsecond * 10)
}
for {
ret, err := server.GetMessage(1 * time.Second)
if err != nil {
// timeout
break
}
if _, ok := dataAfterReconnect[ret]; ok {
foundDataAfterReconnect++
}
}
if foundDataAfterReconnect <= 5 {
t.Errorf("Client:reconnect() may not working properly, expected to get at least 5 messages, got %d instead.", foundDataAfterReconnect)
}
}
func TestTCPClientReconnect(t *testing.T) {
testClientReconnect(
func() *Client {
client, err := NewClient("tcp://127.0.0.1:62004", "", 1*time.Microsecond)
if err != nil {
fmt.Println("failed to create tcp client:", err)
}
return client
},
func() *GraphiteServer {
return NewGraphiteServer("tcp", "127.0.0.1:62004")
},
t,
)
}
func TestUDPClientReconnect(t *testing.T) {
testClientReconnect(
func() *Client {
client, err := NewClient("udp://127.0.0.1:62004", "", 1*time.Microsecond)
if err != nil {
fmt.Println("failed to create udp client:", err)
}
return client
},
func() *GraphiteServer {
return NewGraphiteServer("udp", "127.0.0.1:62004")
},
t,
)
}