-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaggregator_test.go
123 lines (102 loc) · 3.83 KB
/
aggregator_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
package errplane
import (
"fmt"
. "launchpad.net/gocheck"
"net"
"time"
)
type ErrplaneAggregatorApiSuite struct{}
var _ = Suite(&ErrplaneAggregatorApiSuite{})
var (
udpListener *net.UDPConn
udpRecorder *UdpRequestRecorder
)
type UdpRequestRecorder struct {
requests []string
}
func (self *UdpRequestRecorder) recordRequest(conn net.Conn) {
}
func (s *ErrplaneAggregatorApiSuite) SetUpTest(c *C) {
udpRecorder.requests = nil
}
func (s *ErrplaneAggregatorApiSuite) SetUpSuite(c *C) {
var err error
addr, err := net.ResolveUDPAddr("udp4", "")
c.Assert(err, IsNil)
udpListener, err = net.ListenUDP("udp4", addr)
c.Assert(err, IsNil)
udpRecorder = new(UdpRequestRecorder)
go func() {
for {
buffer := make([]byte, 1024)
n, _, _, _, err := udpListener.ReadMsgUDP(buffer, nil)
if err != nil || n <= 0 {
break
}
udpRecorder.requests = append(udpRecorder.requests, string(buffer[:n]))
}
}()
currentTime = time.Now()
}
func (s *ErrplaneAggregatorApiSuite) TearDownSuite(c *C) {
udpListener.Close()
}
func (s *ErrplaneAggregatorApiSuite) TestDoesNotOverrideUdpAddr(c *C) {
ep := newTestClient("app4you2love", "staging", "some_key")
ep.SetUdpAddr(udpListener.LocalAddr().(*net.UDPAddr).String())
ep.SetHttpHost("localhost") // there was a bug where this call will override the udp addr that the user sets
c.Assert(ep, NotNil)
err := ep.ReportUDP("some_metric", 123.4, "doesn't send empty points", Dimensions{
"foo": "bar",
})
c.Assert(err, IsNil)
ep.Close()
time.Sleep(200 * time.Millisecond)
c.Assert(udpRecorder.requests, HasLen, 1)
expected := fmt.Sprintf(`{"d":"app4you2lovestaging","a":"some_key","o":"r","w":[{"n":"some_metric","p":[{"v":123.4,"c":"doesn't send empty points","d":{"foo":"bar"}}]}]}`)
c.Assert(udpRecorder.requests, Contains, expected)
}
// the purpose of this test is to make sure that we don't send empty arrays
// to the backend which we were doing.
func (s *ErrplaneAggregatorApiSuite) TestDoesNotSendEmptyPoints(c *C) {
ep := newTestClient("app4you2love", "staging", "some_key")
ep.SetUdpAddr(udpListener.LocalAddr().(*net.UDPAddr).String())
c.Assert(ep, NotNil)
err := ep.ReportUDP("some_metric", 123.4, "doesn't send empty points", Dimensions{
"foo": "bar",
})
c.Assert(err, IsNil)
ep.Close()
time.Sleep(200 * time.Millisecond)
c.Assert(udpRecorder.requests, HasLen, 1)
expected := fmt.Sprintf(`{"d":"app4you2lovestaging","a":"some_key","o":"r","w":[{"n":"some_metric","p":[{"v":123.4,"c":"doesn't send empty points","d":{"foo":"bar"}}]}]}`)
c.Assert(udpRecorder.requests, Contains, expected)
}
func (s *ErrplaneAggregatorApiSuite) TestApi(c *C) {
ep := newTestClient("app4you2love", "staging", "some_key")
ep.SetUdpAddr(udpListener.LocalAddr().(*net.UDPAddr).String())
c.Assert(ep, NotNil)
err := ep.ReportUDP("some_metric", 123.4, "some_context", Dimensions{
"foo": "bar",
})
c.Assert(err, IsNil)
time.Sleep(200 * time.Millisecond)
err = ep.Sum("some_metric", 10, "some_context", Dimensions{
"foo": "bar",
})
c.Assert(err, IsNil)
time.Sleep(200 * time.Millisecond)
err = ep.Aggregate("some_metric", 234.5, "some_context", Dimensions{
"foo": "bar",
})
c.Assert(err, IsNil)
ep.Close()
time.Sleep(200 * time.Millisecond)
c.Assert(udpRecorder.requests, HasLen, 3)
expected := fmt.Sprintf(`{"d":"app4you2lovestaging","a":"some_key","o":"r","w":[{"n":"some_metric","p":[{"v":123.4,"c":"some_context","d":{"foo":"bar"}}]}]}`)
c.Assert(udpRecorder.requests, Contains, expected)
expected = fmt.Sprintf(`{"d":"app4you2lovestaging","a":"some_key","o":"t","w":[{"n":"some_metric","p":[{"v":234.5,"c":"some_context","d":{"foo":"bar"}}]}]}`)
c.Assert(udpRecorder.requests, Contains, expected)
expected = fmt.Sprintf(`{"d":"app4you2lovestaging","a":"some_key","o":"c","w":[{"n":"some_metric","p":[{"v":10,"c":"some_context","d":{"foo":"bar"}}]}]}`)
c.Assert(udpRecorder.requests, Contains, expected)
}