This repository is currently being migrated. It's locked while the migration is in progress.
forked from quipo/statsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
204 lines (168 loc) · 4.06 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
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
package statsd
import (
"bytes"
"fmt"
"net"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
)
func newLocalListenerUDP(t *testing.T) (*net.UDPConn, *net.UDPAddr) {
udpAddr, err := net.ResolveUDPAddr("udp", ":1200")
if err != nil {
t.Fatal(err)
}
ln, err := net.ListenUDP("udp", udpAddr)
if err != nil {
t.Fatal(err)
}
return ln, udpAddr
}
func TestTotal(t *testing.T) {
ln, udpAddr := newLocalListenerUDP(t)
defer ln.Close()
prefix := "myproject."
client := NewStatsdClient(udpAddr.String(), prefix)
ch := make(chan string, 0)
s := map[string]int64{
"a:b:c": 5,
"d:e:f": 2,
"x:b:c": 5,
"g.h.i": 1,
}
expected := make(map[string]int64)
for k, v := range s {
expected[k] = v
}
// also test %HOST% replacement
s["zz.%HOST%"] = 1
hostname, err := os.Hostname()
expected["zz."+hostname] = 1
go doListenUDP(ln, ch, len(s))
err = client.CreateSocket()
if nil != err {
t.Fatal(err)
}
defer client.Close()
for k, v := range s {
client.Total(k, v)
}
actual := make(map[string]int64)
re := regexp.MustCompile(`^(.*)\:(\d+)\|(\w).*$`)
for i := len(s); i > 0; i-- {
x := <-ch
x = strings.TrimSpace(x)
//fmt.Println(x)
if !strings.HasPrefix(x, prefix) {
t.Errorf("Metric without expected prefix: expected '%s', actual '%s'", prefix, x)
}
vv := re.FindStringSubmatch(x)
if vv[3] != "t" {
t.Errorf("Metric without expected suffix: expected 't', actual '%s'", vv[3])
}
v, err := strconv.ParseInt(vv[2], 10, 64)
if err != nil {
t.Error(err)
}
actual[vv[1][len(prefix):]] = v
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("did not receive all metrics: Expected: %T %v, Actual: %T %v ", expected, expected, actual, actual)
}
}
func doListenUDP(conn *net.UDPConn, ch chan string, n int) {
for n > 0 {
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c *net.UDPConn, ch chan string) {
buffer := make([]byte, 1024)
size, err := c.Read(buffer)
// size, address, err := sock.ReadFrom(buffer) <- This starts printing empty and nil values below immediatly
if err != nil {
fmt.Println(string(buffer), size, err)
panic(err)
}
ch <- string(buffer)
}(conn, ch)
n--
}
}
func doListenTCP(t *testing.T, conn net.Listener, ch chan string, n int) {
for {
client, err := conn.Accept()
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 1024)
c, err := client.Read(buf)
if err != nil {
t.Fatal(err)
}
for _, s := range bytes.Split(buf[:c], []byte{'\n'}) {
ch <- string(s)
}
}
}
func newLocalListenerTCP(t *testing.T) net.Listener {
ln, err := net.Listen("tcp", "127.0.0.1:1200")
if err != nil {
t.Fatal(err)
}
return ln
}
func TestTCP(t *testing.T) {
ln := newLocalListenerTCP(t)
defer ln.Close()
prefix := "myproject."
client := NewStatsdClient("127.0.0.1:1200", prefix)
ch := make(chan string, 0)
s := map[string]int64{
"a:b:c": 5,
"d:e:f": 2,
"x:b:c": 5,
"g.h.i": 1,
}
expected := make(map[string]int64)
for k, v := range s {
expected[k] = v
}
// also test %HOST% replacement
s["zz.%HOST%"] = 1
hostname, err := os.Hostname()
expected["zz."+hostname] = 1
go doListenTCP(t, ln, ch, len(s))
err = client.CreateTCPSocket()
if nil != err {
t.Fatal(err)
}
defer client.Close()
for k, v := range s {
client.Total(k, v)
}
actual := make(map[string]int64)
re := regexp.MustCompile(`^(.*)\:(\d+)\|(\w).*$`)
for i := len(s); i > 0; i-- {
x := <-ch
x = strings.TrimSpace(x)
//fmt.Println(x)
if !strings.HasPrefix(x, prefix) {
t.Errorf("Metric without expected prefix: expected '%s', actual '%s'", prefix, x)
}
vv := re.FindStringSubmatch(x)
if vv[3] != "t" {
t.Errorf("Metric without expected suffix: expected 't', actual '%s'", vv[3])
}
v, err := strconv.ParseInt(vv[2], 10, 64)
if err != nil {
t.Error(err)
}
actual[vv[1][len(prefix):]] = v
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("did not receive all metrics: Expected: %T %v, Actual: %T %v ", expected, expected, actual, actual)
}
}