forked from fgrosse/graphigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
92 lines (78 loc) · 2.93 KB
/
client_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
package graphigo_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"time"
"gopkg.in/fgrosse/graphigo.v2"
)
var _ = Describe("Graphigo", func() {
var (
c *graphigo.Client
conn *connMock
)
BeforeEach(func() {
conn = newConnectionMock()
c = &graphigo.Client{}
c.Connection = conn
})
Describe("Close", func() {
It("should close the connection", func() {
Expect(conn.IsClosed).NotTo(BeTrue())
c.Close()
Expect(conn.IsClosed).To(BeTrue())
})
})
Describe("Send", func() {
It("should send string values to graphite", func() {
Expect(c.Send(graphigo.Metric{Name: "test_metric", Value: "42"})).To(Succeed())
Expect(conn.SentMetrics).To(HaveLen(1))
Expect(conn.SentMetrics[0].Name).To(Equal("test_metric"))
Expect(conn.SentMetrics[0].Value).To(Equal("42"))
Expect(conn.SentMetrics[0].Timestamp).To(BeTemporally("~", time.Now().UTC(), 1*time.Second))
})
It("should send integer values to graphite", func() {
Expect(c.Send(graphigo.Metric{Name: "test_metric", Value: 7})).To(Succeed())
Expect(conn.SentMetrics).To(HaveLen(1))
Expect(conn.SentMetrics[0].Name).To(Equal("test_metric"))
Expect(conn.SentMetrics[0].Value).To(Equal("7"))
Expect(conn.SentMetrics[0].Timestamp).To(BeTemporally("~", time.Now().UTC(), 1*time.Second))
})
It("should send float values to graphite", func() {
Expect(c.Send(graphigo.Metric{Name: "test_metric", Value: 3.14159265359})).To(Succeed())
Expect(conn.SentMetrics).To(HaveLen(1))
Expect(conn.SentMetrics[0].Name).To(Equal("test_metric"))
Expect(conn.SentMetrics[0].Value).To(Equal("3.14159265359"))
Expect(conn.SentMetrics[0].Timestamp).To(BeTemporally("~", time.Now().UTC(), 1*time.Second))
})
Context("with prefix", func() {
BeforeEach(func() {
c.Prefix = "foo_bar.baz"
})
It("should set the correct metric name", func() {
Expect(c.Send(graphigo.Metric{Name: "test_metric", Value: 7})).To(Succeed())
Expect(conn.SentMetrics).To(HaveLen(1))
Expect(conn.SentMetrics[0].Name).To(Equal("foo_bar.baz.test_metric"))
})
It("should not leak changes to the given metric", func() {
metric := graphigo.Metric{Name: "test_metric", Value: 7}
Expect(c.Send(metric)).To(Succeed())
Expect(metric.Name).To(Equal("test_metric"))
})
})
})
Describe("SendAll", func() {
It("should send multiple values in one write command", func() {
metrics := []graphigo.Metric{
{Name: "test_metric_a", Value: "1", Timestamp: time.Now().Add(-1 * time.Hour)},
{Name: "test_metric_b", Value: "2", Timestamp: time.Now().Add(-30 * time.Minute)},
{Name: "test_metric_c", Value: "3", Timestamp: time.Now()},
}
Expect(c.SendAll(metrics)).To(Succeed())
for i, sentMetric := range conn.SentMetrics {
Expect(sentMetric.Name).To(Equal(metrics[i].Name))
Expect(sentMetric.Value).To(Equal(metrics[i].Value))
Expect(sentMetric.Timestamp).To(BeTemporally("~", metrics[i].Timestamp, 1*time.Second))
}
})
})
})