forked from peterbourgon/g2g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg2g_test.go
159 lines (141 loc) · 2.85 KB
/
g2g_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
package g2g
import (
"expvar"
"net"
"strings"
"sync"
"testing"
"time"
)
var testExpvar = expvar.NewInt("i")
func TestPublish(t *testing.T) {
testPub(t, "localhost:2003", NewMockGraphite(t, "tcp://:2003"))
testPub(t, "unix:///tmp/test.sock", NewMockGraphite(t, "unix:///tmp/test.sock"))
}
func testPub(t *testing.T, address string, mock *MockGraphite) {
// setup
d := 25 * time.Millisecond
var g *Graphite
g = NewGraphite(address, d, d)
// register, wait, check
testExpvar.Set(34)
g.Register("test.foo.i", testExpvar)
time.Sleep(2 * d)
count := mock.Count()
if !(0 < count && count <= 2) {
t.Errorf("expected 0 < publishes <= 2, got %d", count)
}
t.Logf("after %s, count=%d", 2*d, count)
time.Sleep(2 * d)
count = mock.Count()
if !(1 < count && count <= 4) {
t.Errorf("expected 1 < publishes <= 4, got %d", count)
}
t.Logf("after second %s, count=%d", 2*d, count)
// teardown
ok := make(chan bool)
go func() {
g.Shutdown()
mock.Shutdown()
ok <- true
}()
select {
case <-ok:
t.Logf("shutdown OK")
case <-time.After(d):
t.Errorf("timeout during shutdown")
}
}
func TestRoundFloat(t *testing.T) {
m := map[string]string{
"abc": "abc",
"0.00.": "0.00.",
"123": "123",
"1.2.3": "1.2.3",
"1.00": "1.00",
"1.001": "1.00",
"1.00000001": "1.00",
"0.00001": "0.00",
"0.01000": "0.01",
"0.01999": "0.02",
"-1.234": "-1.23",
"123.456": "123.46",
"99999.09123": "99999.09",
}
for s, expected := range m {
if got := roundFloat(s, 2); got != expected {
t.Errorf("%s: got %s, expected %s", s, got, expected)
}
}
}
//
//
//
type MockGraphite struct {
t *testing.T
network string
address string
count int
mtx sync.Mutex
ln net.Listener
done chan bool
}
func NewMockGraphite(t *testing.T, address string) *MockGraphite {
network, address := splitEndpoint(address)
m := &MockGraphite{
t: t,
network: network,
address: address,
count: 0,
mtx: sync.Mutex{},
ln: nil,
done: make(chan bool, 1),
}
go m.loop()
return m
}
func (m *MockGraphite) Count() int {
m.mtx.Lock()
defer m.mtx.Unlock()
return m.count
}
func (m *MockGraphite) Shutdown() {
if m.ln != nil {
m.ln.Close()
<-m.done
}
}
func (m *MockGraphite) loop() {
ln, err := net.Listen(m.network, m.address)
if err != nil {
panic(err)
}
m.ln = ln
for {
conn, err := m.ln.Accept()
if err != nil {
m.done <- true
return
}
go m.handle(conn)
}
}
func (m *MockGraphite) handle(conn net.Conn) {
b := make([]byte, 1024)
for {
n, err := conn.Read(b)
if err != nil {
m.t.Logf("Mock Graphite: read error: %s", err)
return
}
if n > 256 {
m.t.Errorf("Mock Graphite: read %dB: too much data", n)
return
}
s := strings.TrimSpace(string(b[:n]))
m.t.Logf("Mock Graphite: read %dB: %s", n, s)
m.mtx.Lock()
m.count++
m.mtx.Unlock()
}
}