forked from peterbourgon/g2g
/
g2g_test.go
165 lines (148 loc) · 2.83 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
160
161
162
163
164
165
package g2g
import (
"expvar"
"fmt"
"net"
"strings"
"sync"
"testing"
"time"
)
func TestPublish(t *testing.T) {
// setup
port := 2003
mock := NewMockGraphite(t, port)
d := 25 * time.Millisecond
attempts, maxAttempts := 0, 3
var g *Graphite
for {
attempts++
var err error
g, err = NewGraphite(fmt.Sprintf("localhost:%d", port), d, d)
if err == nil || attempts > maxAttempts {
break
}
t.Logf("(%d/%d) %s", attempts, maxAttempts, err)
time.Sleep(d)
}
if g == nil {
t.Fatalf("Mock Graphite server never came up")
}
// register, wait, check
i := expvar.NewInt("i")
i.Set(34)
g.Register("test.foo.i", i)
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
port int
count int
mtx sync.Mutex
ln net.Listener
done chan bool
}
func NewMockGraphite(t *testing.T, port int) *MockGraphite {
m := &MockGraphite{
t: t,
port: port,
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() {
m.ln.Close()
<-m.done
}
func (m *MockGraphite) loop() {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", m.port))
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()
}
}