-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphite_test.go
208 lines (174 loc) · 5.4 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
package graphite_test
import (
"bytes"
"net"
. "github.com/gguridi/graphite-client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("graphite client", func() {
var (
client Graphite
result chan string
resultString string
)
Context("tcp protocol", func() {
var (
listener net.Listener
)
BeforeEach(func() {
listener, result = createTCPServer(":3000")
client = NewGraphiteTCP(&Config{
Host: "localhost",
Port: 3000,
})
})
AfterEach(func() {
listener.Close()
})
It("connects successfully if graphite is listening", func() {
err := client.Connect()
Expect(err).ToNot(HaveOccurred())
})
It("connects returns an error if graphite host is not reacheable", func() {
client = NewGraphiteTCP(&Config{
Host: "unknown",
Port: 3000,
})
err := client.Connect()
Expect(err).To(HaveOccurred())
})
It("connects returns an error if graphite is not listening", func() {
client = NewGraphiteTCP(&Config{
Host: "localhost",
Port: 3010,
})
err := client.Connect()
Expect(err).To(HaveOccurred())
})
It("reconnects successfully if graphite is listening", func() {
err := client.Reconnect()
Expect(err).ToNot(HaveOccurred())
})
It("disconnects with an error if never was connected", func() {
err := client.Disconnect()
Expect(err).To(HaveOccurred())
})
It("disconnects without errors if a connection was previously established", func() {
client.Connect()
err := client.Disconnect()
Expect(err).ToNot(HaveOccurred())
})
It("reconnects automatically when sending metrics if connection hasn't been set", func() {
n, err := client.Send("test", "1")
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(18))
})
It("send a message with metric and value to graphite", func() {
n, err := client.Send("metricA", "10")
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(22))
Eventually(result).Should(Receive(&resultString))
Expect(resultString).To(MatchRegexp(`metricA 10 \d{10}\n`))
})
It("reconnects automatically when sending a buffer if connection hasn't been set", func() {
n, err := client.SendBuffer(bytes.NewBufferString("metric 10 1554992147\n"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(21))
})
It("send a whole buffer to graphite", func() {
client.Connect()
n, err := client.SendBuffer(bytes.NewBufferString("metric 10 1554992147\n"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(21))
Eventually(result).Should(Receive(&resultString))
Expect(resultString).To(ContainSubstring("metric 10 1554992147\n"))
})
It("returns an error if it can't deliver the metric to graphite", func() {
listener.Close()
n, err := client.Send("metricA", "10")
Expect(err).To(HaveOccurred())
Expect(n).To(Equal(0))
})
})
Context("udp protocol", func() {
var (
listener net.PacketConn
)
BeforeEach(func() {
listener, result = createUDPServer(":3001")
client = NewGraphiteUDP(&Config{
Host: "localhost",
Port: 3001,
})
})
AfterEach(func() {
listener.Close()
})
It("connects successfully if graphite is listening", func() {
err := client.Connect()
Expect(err).ToNot(HaveOccurred())
})
It("connects returns an error if graphite host is not reacheable", func() {
client = NewGraphiteUDP(&Config{
Host: "unknown",
Port: 103006,
})
err := client.Connect()
Expect(err).To(HaveOccurred())
})
It("connects returns an error if graphite is not listening", func() {
client = NewGraphiteTCP(&Config{
Host: "localhost",
Port: 3010,
})
err := client.Connect()
Expect(err).To(HaveOccurred())
})
It("reconnects successfully if graphite is listening", func() {
err := client.Reconnect()
Expect(err).ToNot(HaveOccurred())
})
It("disconnects with an error if never was connected", func() {
err := client.Disconnect()
Expect(err).To(HaveOccurred())
})
It("disconnects without errors if a connection was previously established", func() {
client.Connect()
err := client.Disconnect()
Expect(err).ToNot(HaveOccurred())
})
It("reconnects automatically when sending metrics if connection hasn't been set", func() {
n, err := client.Send("test", "1")
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(18))
})
It("send a message with metric and value to graphite", func() {
client.Connect()
n, err := client.Send("metricA", "10")
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(22))
Eventually(result).Should(Receive(&resultString))
Expect(resultString).To(MatchRegexp(`metricA 10 \d{10}\n`))
})
It("reconnects automatically when sending a buffer if connection hasn't been set", func() {
n, err := client.SendBuffer(bytes.NewBufferString("metric 10 1554992147\n"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(21))
})
It("send a whole buffer to graphite", func() {
client.Connect()
n, err := client.SendBuffer(bytes.NewBufferString("metric 10 1554992147\n"))
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(21))
Eventually(result).Should(Receive(&resultString))
Expect(resultString).To(ContainSubstring("metric 10 1554992147\n"))
})
It("doesn't return an error if can't deliver the metric to graphite because it's UDP", func() {
listener.Close()
n, err := client.Send("metricA", "10")
Expect(err).ToNot(HaveOccurred())
Expect(n).To(Equal(22))
})
})
})