-
Notifications
You must be signed in to change notification settings - Fork 5
/
metricsmanager_test.go
84 lines (63 loc) · 2.05 KB
/
metricsmanager_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
package gamq
import (
"bufio"
"bytes"
"fmt"
"net"
"testing"
"github.com/onsi/gomega"
)
func TestMetricsManager_ReceivesBasicMetric_PublishesDownstreamAndSendsToStatsD(t *testing.T) {
gomega.RegisterTestingT(t)
// Listen on UDP
var statsDBuffer [2048]byte
var udpPacketsReceived int
udpAddr, _ := net.ResolveUDPAddr("udp", ":0")
udpConn, _ := net.ListenUDP("udp", udpAddr)
// Don't care about the contents of the received messages - just the fact
// that we received them. We trust the StatsD library
go func() {
for i := 0; i < 3; i++ {
_, _, _ = udpConn.ReadFromUDP(statsDBuffer[0:])
udpPacketsReceived++
}
}()
config := Config{StatsDEndpoint: fmt.Sprintf("localhost:%d", udpConn.LocalAddr().(*net.UDPAddr).Port)}
SetConfig(&config)
qm := newQueueManager()
// Listen to metrics queue
writerBuffer := new(bytes.Buffer)
dummyWriter := bufio.NewWriter(writerBuffer)
closedChannel := make(chan bool)
dummyClient := Client{Name: "Test", Writer: dummyWriter, Closed: &closedChannel}
qm.Subscribe("metrics", &dummyClient)
// Log one of each metric
// Check we've received metrics both via UDP - and on the metrics channel
testMetric := NewMetric("test", "guage", 123)
qm.metricsManager.metricsChannel <- testMetric
gomega.Eventually(func() int {
return udpPacketsReceived
}, "2s").Should(gomega.Equal(1))
gomega.Eventually(func() []byte {
return writerBuffer.Bytes()
}).ShouldNot(gomega.BeNil())
writerBuffer.Reset()
testMetric2 := NewMetric("test", "counter", 123)
qm.metricsManager.metricsChannel <- testMetric2
gomega.Eventually(func() int {
return udpPacketsReceived
}, "2s").Should(gomega.Equal(2))
gomega.Eventually(func() []byte {
return writerBuffer.Bytes()
}).ShouldNot(gomega.BeNil())
writerBuffer.Reset()
testMetric3 := NewMetric("test", "timing", 123)
qm.metricsManager.metricsChannel <- testMetric3
gomega.Eventually(func() int {
return udpPacketsReceived
}, "2s").Should(gomega.Equal(3))
gomega.Eventually(func() []byte {
return writerBuffer.Bytes()
}).ShouldNot(gomega.BeNil())
writerBuffer.Reset()
}