| 1 |
// Copyright 2013 Ooyala, Inc. |
| 2 |
|
| 3 |
package statsd |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"io/ioutil" |
| 10 |
"net" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"reflect" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
"testing" |
| 17 |
"time" |
| 18 |
) |
| 19 |
|
| 20 |
var dogstatsdTests = []struct { |
| 21 |
GlobalNamespace string |
| 22 |
GlobalTags []string |
| 23 |
Method string |
| 24 |
Metric string |
| 25 |
Value interface{} |
| 26 |
Tags []string |
| 27 |
Rate float64 |
| 28 |
Expected string |
| 29 |
}{ |
| 30 |
{"", nil, "Gauge", "test.gauge", 1.0, nil, 1.0, "test.gauge:1.000000|g"}, |
| 31 |
{"", nil, "Gauge", "test.gauge", 1.0, nil, 0.999999, "test.gauge:1.000000|g|@0.999999"}, |
| 32 |
{"", nil, "Gauge", "test.gauge", 1.0, []string{"tagA"}, 1.0, "test.gauge:1.000000|g|#tagA"}, |
| 33 |
{"", nil, "Gauge", "test.gauge", 1.0, []string{"tagA", "tagB"}, 1.0, "test.gauge:1.000000|g|#tagA,tagB"}, |
| 34 |
{"", nil, "Gauge", "test.gauge", 1.0, []string{"tagA"}, 0.999999, "test.gauge:1.000000|g|@0.999999|#tagA"}, |
| 35 |
{"", nil, "Count", "test.count", int64(1), []string{"tagA"}, 1.0, "test.count:1|c|#tagA"}, |
| 36 |
{"", nil, "Count", "test.count", int64(-1), []string{"tagA"}, 1.0, "test.count:-1|c|#tagA"}, |
| 37 |
{"", nil, "Histogram", "test.histogram", 2.3, []string{"tagA"}, 1.0, "test.histogram:2.300000|h|#tagA"}, |
| 38 |
{"", nil, "Distribution", "test.distribution", 2.3, []string{"tagA"}, 1.0, "test.distribution:2.300000|d|#tagA"}, |
| 39 |
{"", nil, "Set", "test.set", "uuid", []string{"tagA"}, 1.0, "test.set:uuid|s|#tagA"}, |
| 40 |
{"flubber.", nil, "Set", "test.set", "uuid", []string{"tagA"}, 1.0, "flubber.test.set:uuid|s|#tagA"}, |
| 41 |
{"", []string{"tagC"}, "Set", "test.set", "uuid", []string{"tagA"}, 1.0, "test.set:uuid|s|#tagC,tagA"}, |
| 42 |
{"", nil, "Count", "test.count", int64(1), []string{"hello\nworld"}, 1.0, "test.count:1|c|#helloworld"}, |
| 43 |
} |
| 44 |
|
| 45 |
func assertNotPanics(t *testing.T, f func()) { |
| 46 |
defer func() { |
| 47 |
if r := recover(); r != nil { |
| 48 |
t.Fatal(r) |
| 49 |
} |
| 50 |
}() |
| 51 |
f() |
| 52 |
} |
| 53 |
|
| 54 |
func TestClientUDP(t *testing.T) { |
| 55 |
addr := "localhost:1201" |
| 56 |
udpAddr, err := net.ResolveUDPAddr("udp", addr) |
| 57 |
if err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
|
| 61 |
server, err := net.ListenUDP("udp", udpAddr) |
| 62 |
if err != nil { |
| 63 |
t.Fatal(err) |
| 64 |
} |
| 65 |
defer server.Close() |
| 66 |
|
| 67 |
client, err := New(addr) |
| 68 |
if err != nil { |
| 69 |
t.Fatal(err) |
| 70 |
} |
| 71 |
|
| 72 |
clientTest(t, server, client) |
| 73 |
} |
| 74 |
|
| 75 |
type statsdWriterWrapper struct { |
| 76 |
io.WriteCloser |
| 77 |
} |
| 78 |
|
| 79 |
func (statsdWriterWrapper) SetWriteTimeout(time.Duration) error { |
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
func TestClientWithConn(t *testing.T) { |
| 84 |
server, conn, err := os.Pipe() |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
|
| 89 |
client, err := NewWithWriter(statsdWriterWrapper{conn}) |
| 90 |
if err != nil { |
| 91 |
t.Fatal(err) |
| 92 |
} |
| 93 |
|
| 94 |
clientTest(t, server, client) |
| 95 |
} |
| 96 |
|
| 97 |
func clientTest(t *testing.T, server io.Reader, client *Client) { |
| 98 |
for _, tt := range dogstatsdTests { |
| 99 |
client.Namespace = tt.GlobalNamespace |
| 100 |
client.Tags = tt.GlobalTags |
| 101 |
method := reflect.ValueOf(client).MethodByName(tt.Method) |
| 102 |
e := method.Call([]reflect.Value{ |
| 103 |
reflect.ValueOf(tt.Metric), |
| 104 |
reflect.ValueOf(tt.Value), |
| 105 |
reflect.ValueOf(tt.Tags), |
| 106 |
reflect.ValueOf(tt.Rate)})[0] |
| 107 |
errInter := e.Interface() |
| 108 |
if errInter != nil { |
| 109 |
t.Fatal(errInter.(error)) |
| 110 |
} |
| 111 |
|
| 112 |
bytes := make([]byte, 1024) |
| 113 |
n, err := server.Read(bytes) |
| 114 |
if err != nil { |
| 115 |
t.Fatal(err) |
| 116 |
} |
| 117 |
message := bytes[:n] |
| 118 |
if string(message) != tt.Expected { |
| 119 |
t.Errorf("Expected: %s. Actual: %s", tt.Expected, string(message)) |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func TestClientUDS(t *testing.T) { |
| 125 |
dir, err := ioutil.TempDir("", "socket") |
| 126 |
if err != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
defer os.RemoveAll(dir) // clean up |
| 130 |
|
| 131 |
addr := filepath.Join(dir, "dsd.socket") |
| 132 |
|
| 133 |
udsAddr, err := net.ResolveUnixAddr("unixgram", addr) |
| 134 |
if err != nil { |
| 135 |
t.Fatal(err) |
| 136 |
} |
| 137 |
|
| 138 |
server, err := net.ListenUnixgram("unixgram", udsAddr) |
| 139 |
if err != nil { |
| 140 |
t.Fatal(err) |
| 141 |
} |
| 142 |
defer server.Close() |
| 143 |
|
| 144 |
addrParts := []string{UnixAddressPrefix, addr} |
| 145 |
client, err := New(strings.Join(addrParts, "")) |
| 146 |
if err != nil { |
| 147 |
t.Fatal(err) |
| 148 |
} |
| 149 |
|
| 150 |
for _, tt := range dogstatsdTests { |
| 151 |
client.Namespace = tt.GlobalNamespace |
| 152 |
client.Tags = tt.GlobalTags |
| 153 |
method := reflect.ValueOf(client).MethodByName(tt.Method) |
| 154 |
e := method.Call([]reflect.Value{ |
| 155 |
reflect.ValueOf(tt.Metric), |
| 156 |
reflect.ValueOf(tt.Value), |
| 157 |
reflect.ValueOf(tt.Tags), |
| 158 |
reflect.ValueOf(tt.Rate)})[0] |
| 159 |
errInter := e.Interface() |
| 160 |
if errInter != nil { |
| 161 |
t.Fatal(errInter.(error)) |
| 162 |
} |
| 163 |
|
| 164 |
bytes := make([]byte, 1024) |
| 165 |
n, err := server.Read(bytes) |
| 166 |
if err != nil { |
| 167 |
t.Fatal(err) |
| 168 |
} |
| 169 |
message := bytes[:n] |
| 170 |
if string(message) != tt.Expected { |
| 171 |
t.Errorf("Expected: %s. Actual: %s", tt.Expected, string(message)) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestClientUDSClose(t *testing.T) { |
| 177 |
dir, err := ioutil.TempDir("", "socket") |
| 178 |
if err != nil { |
| 179 |
t.Fatal(err) |
| 180 |
} |
| 181 |
defer os.RemoveAll(dir) // clean up |
| 182 |
|
| 183 |
addr := filepath.Join(dir, "dsd.socket") |
| 184 |
|
| 185 |
addrParts := []string{UnixAddressPrefix, addr} |
| 186 |
client, err := New(strings.Join(addrParts, "")) |
| 187 |
if err != nil { |
| 188 |
t.Fatal(err) |
| 189 |
} |
| 190 |
|
| 191 |
assertNotPanics(t, func() { client.Close() }) |
| 192 |
} |
| 193 |
|
| 194 |
func TestBufferedClient(t *testing.T) { |
| 195 |
addr := "localhost:1201" |
| 196 |
udpAddr, err := net.ResolveUDPAddr("udp", addr) |
| 197 |
if err != nil { |
| 198 |
t.Fatal(err) |
| 199 |
} |
| 200 |
|
| 201 |
server, err := net.ListenUDP("udp", udpAddr) |
| 202 |
if err != nil { |
| 203 |
t.Fatal(err) |
| 204 |
} |
| 205 |
defer server.Close() |
| 206 |
|
| 207 |
bufferLength := 9 |
| 208 |
client, err := NewBuffered(addr, bufferLength) |
| 209 |
if err != nil { |
| 210 |
t.Fatal(err) |
| 211 |
} |
| 212 |
|
| 213 |
client.Namespace = "foo." |
| 214 |
client.Tags = []string{"dd:2"} |
| 215 |
|
| 216 |
dur, _ := time.ParseDuration("123us") |
| 217 |
|
| 218 |
client.Incr("ic", nil, 1) |
| 219 |
client.Decr("dc", nil, 1) |
| 220 |
client.Count("cc", 1, nil, 1) |
| 221 |
client.Gauge("gg", 10, nil, 1) |
| 222 |
client.Histogram("hh", 1, nil, 1) |
| 223 |
client.Distribution("dd", 1, nil, 1) |
| 224 |
client.Timing("tt", dur, nil, 1) |
| 225 |
client.Set("ss", "ss", nil, 1) |
| 226 |
|
| 227 |
if len(client.commands) != (bufferLength - 1) { |
| 228 |
t.Errorf("Expected client to have buffered %d commands, but found %d\n", (bufferLength - 1), len(client.commands)) |
| 229 |
} |
| 230 |
|
| 231 |
client.Set("ss", "xx", nil, 1) |
| 232 |
client.Lock() |
| 233 |
err = client.flushLocked() |
| 234 |
client.Unlock() |
| 235 |
if err != nil { |
| 236 |
t.Errorf("Error sending: %s", err) |
| 237 |
} |
| 238 |
|
| 239 |
if len(client.commands) != 0 { |
| 240 |
t.Errorf("Expecting send to flush commands, but found %d\n", len(client.commands)) |
| 241 |
} |
| 242 |
|
| 243 |
buffer := make([]byte, 4096) |
| 244 |
n, err := io.ReadAtLeast(server, buffer, 1) |
| 245 |
result := string(buffer[:n]) |
| 246 |
|
| 247 |
if err != nil { |
| 248 |
t.Error(err) |
| 249 |
} |
| 250 |
|
| 251 |
expected := []string{ |
| 252 |
`foo.ic:1|c|#dd:2`, |
| 253 |
`foo.dc:-1|c|#dd:2`, |
| 254 |
`foo.cc:1|c|#dd:2`, |
| 255 |
`foo.gg:10.000000|g|#dd:2`, |
| 256 |
`foo.hh:1.000000|h|#dd:2`, |
| 257 |
`foo.dd:1.000000|d|#dd:2`, |
| 258 |
`foo.tt:0.123000|ms|#dd:2`, |
| 259 |
`foo.ss:ss|s|#dd:2`, |
| 260 |
`foo.ss:xx|s|#dd:2`, |
| 261 |
} |
| 262 |
|
| 263 |
for i, res := range strings.Split(result, "\n") { |
| 264 |
if res != expected[i] { |
| 265 |
t.Errorf("Got `%s`, expected `%s`", res, expected[i]) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
client.Event(&Event{Title: "title1", Text: "text1", Priority: Normal, AlertType: Success, Tags: []string{"tagg"}}) |
| 270 |
client.SimpleEvent("event1", "text1") |
| 271 |
|
| 272 |
if len(client.commands) != 2 { |
| 273 |
t.Errorf("Expected to find %d commands, but found %d\n", 2, len(client.commands)) |
| 274 |
} |
| 275 |
|
| 276 |
client.Lock() |
| 277 |
err = client.flushLocked() |
| 278 |
client.Unlock() |
| 279 |
|
| 280 |
if err != nil { |
| 281 |
t.Errorf("Error sending: %s", err) |
| 282 |
} |
| 283 |
|
| 284 |
if len(client.commands) != 0 { |
| 285 |
t.Errorf("Expecting send to flush commands, but found %d\n", len(client.commands)) |
| 286 |
} |
| 287 |
|
| 288 |
buffer = make([]byte, 1024) |
| 289 |
n, err = io.ReadAtLeast(server, buffer, 1) |
| 290 |
result = string(buffer[:n]) |
| 291 |
|
| 292 |
if err != nil { |
| 293 |
t.Error(err) |
| 294 |
} |
| 295 |
|
| 296 |
if n == 0 { |
| 297 |
t.Errorf("Read 0 bytes but expected more.") |
| 298 |
} |
| 299 |
|
| 300 |
expected = []string{ |
| 301 |
`_e{6,5}:title1|text1|p:normal|t:success|#dd:2,tagg`, |
| 302 |
`_e{6,5}:event1|text1|#dd:2`, |
| 303 |
} |
| 304 |
|
| 305 |
for i, res := range strings.Split(result, "\n") { |
| 306 |
if res != expected[i] { |
| 307 |
t.Errorf("Got `%s`, expected `%s`", res, expected[i]) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
func TestBufferedClientBackground(t *testing.T) { |
| 314 |
addr := "localhost:1201" |
| 315 |
udpAddr, err := net.ResolveUDPAddr("udp", addr) |
| 316 |
if err != nil { |
| 317 |
t.Fatal(err) |
| 318 |
} |
| 319 |
|
| 320 |
server, err := net.ListenUDP("udp", udpAddr) |
| 321 |
if err != nil { |
| 322 |
t.Fatal(err) |
| 323 |
} |
| 324 |
defer server.Close() |
| 325 |
|
| 326 |
bufferLength := 5 |
| 327 |
client, err := NewBuffered(addr, bufferLength) |
| 328 |
if err != nil { |
| 329 |
t.Fatal(err) |
| 330 |
} |
| 331 |
defer client.Close() |
| 332 |
|
| 333 |
client.Namespace = "foo." |
| 334 |
client.Tags = []string{"dd:2"} |
| 335 |
|
| 336 |
client.Count("cc", 1, nil, 1) |
| 337 |
client.Gauge("gg", 10, nil, 1) |
| 338 |
client.Histogram("hh", 1, nil, 1) |
| 339 |
client.Distribution("dd", 1, nil, 1) |
| 340 |
client.Set("ss", "ss", nil, 1) |
| 341 |
client.Set("ss", "xx", nil, 1) |
| 342 |
|
| 343 |
time.Sleep(client.flushTime * 2) |
| 344 |
client.Lock() |
| 345 |
if len(client.commands) != 0 { |
| 346 |
t.Errorf("Watch goroutine should have flushed commands, but found %d\n", len(client.commands)) |
| 347 |
} |
| 348 |
client.Unlock() |
| 349 |
} |
| 350 |
|
| 351 |
func TestBufferedClientFlush(t *testing.T) { |
| 352 |
addr := "localhost:1201" |
| 353 |
udpAddr, err := net.ResolveUDPAddr("udp", addr) |
| 354 |
if err != nil { |
| 355 |
t.Fatal(err) |
| 356 |
} |
| 357 |
|
| 358 |
server, err := net.ListenUDP("udp", udpAddr) |
| 359 |
if err != nil { |
| 360 |
t.Fatal(err) |
| 361 |
} |
| 362 |
defer server.Close() |
| 363 |
|
| 364 |
bufferLength := 5 |
| 365 |
client, err := NewBuffered(addr, bufferLength) |
| 366 |
if err != nil { |
| 367 |
t.Fatal(err) |
| 368 |
} |
| 369 |
defer client.Close() |
| 370 |
|
| 371 |
client.Namespace = "foo." |
| 372 |
client.Tags = []string{"dd:2"} |
| 373 |
|
| 374 |
client.Count("cc", 1, nil, 1) |
| 375 |
client.Gauge("gg", 10, nil, 1) |
| 376 |
client.Histogram("hh", 1, nil, 1) |
| 377 |
client.Distribution("dd", 1, nil, 1) |
| 378 |
client.Set("ss", "ss", nil, 1) |
| 379 |
client.Set("ss", "xx", nil, 1) |
| 380 |
|
| 381 |
client.Flush() |
| 382 |
|
| 383 |
client.Lock() |
| 384 |
if len(client.commands) != 0 { |
| 385 |
t.Errorf("Flush should have flushed commands, but found %d\n", len(client.commands)) |
| 386 |
} |
| 387 |
client.Unlock() |
| 388 |
} |
| 389 |
|
| 390 |
func TestJoinMaxSize(t *testing.T) { |
| 391 |
c := Client{} |
| 392 |
elements := []string{"abc", "abcd", "ab", "xyz", "foobaz", "x", "wwxxyyzz"} |
| 393 |
res, n := c.joinMaxSize(elements, " ", 8) |
| 394 |
|
| 395 |
if len(res) != len(n) && len(res) != 4 { |
| 396 |
t.Errorf("Was expecting 4 frames to flush but got: %v - %v", n, res) |
| 397 |
} |
| 398 |
if n[0] != 2 { |
| 399 |
t.Errorf("Was expecting 2 elements in first frame but got: %v", n[0]) |
| 400 |
} |
| 401 |
if string(res[0]) != "abc abcd" { |
| 402 |
t.Errorf("Join should have returned \"abc abcd\" in frame, but found: %s", res[0]) |
| 403 |
} |
| 404 |
if n[1] != 2 { |
| 405 |
t.Errorf("Was expecting 2 elements in second frame but got: %v - %v", n[1], n) |
| 406 |
} |
| 407 |
if string(res[1]) != "ab xyz" { |
| 408 |
t.Errorf("Join should have returned \"ab xyz\" in frame, but found: %s", res[1]) |
| 409 |
} |
| 410 |
if n[2] != 2 { |
| 411 |
t.Errorf("Was expecting 2 elements in third frame but got: %v - %v", n[2], n) |
| 412 |
} |
| 413 |
if string(res[2]) != "foobaz x" { |
| 414 |
t.Errorf("Join should have returned \"foobaz x\" in frame, but found: %s", res[2]) |
| 415 |
} |
| 416 |
if n[3] != 1 { |
| 417 |
t.Errorf("Was expecting 1 element in fourth frame but got: %v - %v", n[3], n) |
| 418 |
} |
| 419 |
if string(res[3]) != "wwxxyyzz" { |
| 420 |
t.Errorf("Join should have returned \"wwxxyyzz\" in frame, but found: %s", res[3]) |
| 421 |
} |
| 422 |
|
| 423 |
res, n = c.joinMaxSize(elements, " ", 11) |
| 424 |
|
| 425 |
if len(res) != len(n) && len(res) != 3 { |
| 426 |
t.Errorf("Was expecting 3 frames to flush but got: %v - %v", n, res) |
| 427 |
} |
| 428 |
if n[0] != 3 { |
| 429 |
t.Errorf("Was expecting 3 elements in first frame but got: %v", n[0]) |
| 430 |
} |
| 431 |
if string(res[0]) != "abc abcd ab" { |
| 432 |
t.Errorf("Join should have returned \"abc abcd ab\" in frame, but got: %s", res[0]) |
| 433 |
} |
| 434 |
if n[1] != 2 { |
| 435 |
t.Errorf("Was expecting 2 elements in second frame but got: %v", n[1]) |
| 436 |
} |
| 437 |
if string(res[1]) != "xyz foobaz" { |
| 438 |
t.Errorf("Join should have returned \"xyz foobaz\" in frame, but got: %s", res[1]) |
| 439 |
} |
| 440 |
if n[2] != 2 { |
| 441 |
t.Errorf("Was expecting 2 elements in third frame but got: %v", n[2]) |
| 442 |
} |
| 443 |
if string(res[2]) != "x wwxxyyzz" { |
| 444 |
t.Errorf("Join should have returned \"x wwxxyyzz\" in frame, but got: %s", res[2]) |
| 445 |
} |
| 446 |
|
| 447 |
res, n = c.joinMaxSize(elements, " ", 8) |
| 448 |
|
| 449 |
if len(res) != len(n) && len(res) != 7 { |
| 450 |
t.Errorf("Was expecting 7 frames to flush but got: %v - %v", n, res) |
| 451 |
} |
| 452 |
if n[0] != 1 { |
| 453 |
t.Errorf("Separator is long, expected a single element in frame but got: %d - %v", n[0], res) |
| 454 |
} |
| 455 |
if string(res[0]) != "abc" { |
| 456 |
t.Errorf("Join should have returned \"abc\" in first frame, but got: %s", res) |
| 457 |
} |
| 458 |
if n[1] != 1 { |
| 459 |
t.Errorf("Separator is long, expected a single element in frame but got: %d - %v", n[1], res) |
| 460 |
} |
| 461 |
if string(res[1]) != "abcd" { |
| 462 |
t.Errorf("Join should have returned \"abcd\" in second frame, but got: %s", res[1]) |
| 463 |
} |
| 464 |
if n[2] != 1 { |
| 465 |
t.Errorf("Separator is long, expected a single element in third frame but got: %d - %v", n[2], res) |
| 466 |
} |
| 467 |
if string(res[2]) != "ab" { |
| 468 |
t.Errorf("Join should have returned \"ab\" in third frame, but got: %s", res[2]) |
| 469 |
} |
| 470 |
if n[3] != 1 { |
| 471 |
t.Errorf("Separator is long, expected a single element in fourth frame but got: %d - %v", n[3], res) |
| 472 |
} |
| 473 |
if string(res[3]) != "xyz" { |
| 474 |
t.Errorf("Join should have returned \"xyz\" in fourth frame, but got: %s", res[3]) |
| 475 |
} |
| 476 |
if n[4] != 1 { |
| 477 |
t.Errorf("Separator is long, expected a single element in fifth frame but got: %d - %v", n[4], res) |
| 478 |
} |
| 479 |
if string(res[4]) != "foobaz" { |
| 480 |
t.Errorf("Join should have returned \"foobaz\" in fifth frame, but got: %s", res[4]) |
| 481 |
} |
| 482 |
if n[5] != 1 { |
| 483 |
t.Errorf("Separator is long, expected a single element in sixth frame but got: %d - %v", n[5], res) |
| 484 |
} |
| 485 |
if string(res[5]) != "x" { |
| 486 |
t.Errorf("Join should have returned \"x\" in sixth frame, but got: %s", res[5]) |
| 487 |
} |
| 488 |
if n[6] != 1 { |
| 489 |
t.Errorf("Separator is long, expected a single element in seventh frame but got: %d - %v", n[6], res) |
| 490 |
} |
| 491 |
if string(res[6]) != "wwxxyyzz" { |
| 492 |
t.Errorf("Join should have returned \"wwxxyyzz\" in seventh frame, but got: %s", res[6]) |
| 493 |
} |
| 494 |
|
| 495 |
res, n = c.joinMaxSize(elements[4:], " ", 6) |
| 496 |
if len(res) != len(n) && len(res) != 3 { |
| 497 |
t.Errorf("Was expecting 3 frames to flush but got: %v - %v", n, res) |
| 498 |
|
| 499 |
} |
| 500 |
if n[0] != 1 { |
| 501 |
t.Errorf("Element should just fit in frame - expected single element in frame: %d - %v", n[0], res) |
| 502 |
} |
| 503 |
if string(res[0]) != "foobaz" { |
| 504 |
t.Errorf("Join should have returned \"foobaz\" in first frame, but got: %s", res[0]) |
| 505 |
} |
| 506 |
if n[1] != 1 { |
| 507 |
t.Errorf("Single element expected in frame, but got. %d - %v", n[1], res) |
| 508 |
} |
| 509 |
if string(res[1]) != "x" { |
| 510 |
t.Errorf("Join should' have returned \"x\" in second frame, but got: %s", res[1]) |
| 511 |
} |
| 512 |
if n[2] != 1 { |
| 513 |
t.Errorf("Even though element is greater then max size we still try to send it. %d - %v", n[2], res) |
| 514 |
} |
| 515 |
if string(res[2]) != "wwxxyyzz" { |
| 516 |
t.Errorf("Join should have returned \"wwxxyyzz\" in third frame, but got: %s", res[2]) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
func TestSendMsgUDP(t *testing.T) { |
| 521 |
addr := "localhost:1201" |
| 522 |
udpAddr, err := net.ResolveUDPAddr("udp", addr) |
| 523 |
if err != nil { |
| 524 |
t.Fatal(err) |
| 525 |
} |
| 526 |
|
| 527 |
server, err := net.ListenUDP("udp", udpAddr) |
| 528 |
if err != nil { |
| 529 |
t.Fatal(err) |
| 530 |
} |
| 531 |
defer server.Close() |
| 532 |
|
| 533 |
client, err := New(addr) |
| 534 |
if err != nil { |
| 535 |
t.Fatal(err) |
| 536 |
} |
| 537 |
|
| 538 |
err = client.sendMsg(strings.Repeat("x", MaxUDPPayloadSize+1)) |
| 539 |
if err == nil { |
| 540 |
t.Error("Expected error to be returned if message size is bigger than MaxUDPPayloadSize") |
| 541 |
} |
| 542 |
|
| 543 |
message := "test message" |
| 544 |
|
| 545 |
err = client.sendMsg(message) |
| 546 |
if err != nil { |
| 547 |
t.Errorf("Expected no error to be returned if message size is smaller or equal to MaxUDPPayloadSize, got: %s", err.Error()) |
| 548 |
} |
| 549 |
|
| 550 |
buffer := make([]byte, MaxUDPPayloadSize+1) |
| 551 |
n, err := io.ReadAtLeast(server, buffer, 1) |
| 552 |
|
| 553 |
if err != nil { |
| 554 |
t.Fatalf("Expected no error to be returned reading the buffer, got: %s", err.Error()) |
| 555 |
} |
| 556 |
|
| 557 |
if n != len(message) { |
| 558 |
t.Fatalf("Failed to read full message from buffer. Got size `%d` expected `%d`", n, MaxUDPPayloadSize) |
| 559 |
} |
| 560 |
|
| 561 |
if string(buffer[:n]) != message { |
| 562 |
t.Fatalf("The received message did not match what we expect.") |
| 563 |
} |
| 564 |
|
| 565 |
client, err = NewBuffered(addr, 1) |
| 566 |
if err != nil { |
| 567 |
t.Fatal(err) |
| 568 |
} |
| 569 |
|
| 570 |
err = client.sendMsg(strings.Repeat("x", MaxUDPPayloadSize+1)) |
| 571 |
if err == nil { |
| 572 |
t.Error("Expected error to be returned if message size is bigger than MaxUDPPayloadSize") |
| 573 |
} |
| 574 |
|
| 575 |
err = client.sendMsg(message) |
| 576 |
if err != nil { |
| 577 |
t.Errorf("Expected no error to be returned if message size is smaller or equal to MaxUDPPayloadSize, got: %s", err.Error()) |
| 578 |
} |
| 579 |
|
| 580 |
client.Lock() |
| 581 |
err = client.flushLocked() |
| 582 |
client.Unlock() |
| 583 |
|
| 584 |
if err != nil { |
| 585 |
t.Fatalf("Expected no error to be returned flushing the client, got: %s", err.Error()) |
| 586 |
} |
| 587 |
|
| 588 |
buffer = make([]byte, MaxUDPPayloadSize+1) |
| 589 |
n, err = io.ReadAtLeast(server, buffer, 1) |
| 590 |
|
| 591 |
if err != nil { |
| 592 |
t.Fatalf("Expected no error to be returned reading the buffer, got: %s", err.Error()) |
| 593 |
} |
| 594 |
|
| 595 |
if n != len(message) { |
| 596 |
t.Fatalf("Failed to read full message from buffer. Got size `%d` expected `%d`", n, MaxUDPPayloadSize) |
| 597 |
} |
| 598 |
|
| 599 |
if string(buffer[:n]) != message { |
| 600 |
t.Fatalf("The received message did not match what we expect.") |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
func TestSendUDSErrors(t *testing.T) { |
| 605 |
dir, err := ioutil.TempDir("", "socket") |
| 606 |
if err != nil { |
| 607 |
t.Fatal(err) |
| 608 |
} |
| 609 |
defer os.RemoveAll(dir) // clean up |
| 610 |
|
| 611 |
message := "test message" |
| 612 |
|
| 613 |
addr := filepath.Join(dir, "dsd.socket") |
| 614 |
udsAddr, err := net.ResolveUnixAddr("unixgram", addr) |
| 615 |
if err != nil { |
| 616 |
t.Fatal(err) |
| 617 |
} |
| 618 |
|
| 619 |
addrParts := []string{UnixAddressPrefix, addr} |
| 620 |
client, err := New(strings.Join(addrParts, "")) |
| 621 |
if err != nil { |
| 622 |
t.Fatal(err) |
| 623 |
} |
| 624 |
|
| 625 |
// Server not listening yet |
| 626 |
err = client.sendMsg(message) |
| 627 |
if err == nil || !strings.HasSuffix(err.Error(), "no such file or directory") { |
| 628 |
t.Errorf("Expected error \"no such file or directory\", got: %s", err.Error()) |
| 629 |
} |
| 630 |
|
| 631 |
// Start server and send packet |
| 632 |
server, err := net.ListenUnixgram("unixgram", udsAddr) |
| 633 |
if err != nil { |
| 634 |
t.Fatal(err) |
| 635 |
} |
| 636 |
err = client.sendMsg(message) |
| 637 |
if err != nil { |
| 638 |
t.Errorf("Expected no error to be returned when server is listening, got: %s", err.Error()) |
| 639 |
} |
| 640 |
bytes := make([]byte, 1024) |
| 641 |
n, err := server.Read(bytes) |
| 642 |
if err != nil { |
| 643 |
t.Fatal(err) |
| 644 |
} |
| 645 |
if string(bytes[:n]) != message { |
| 646 |
t.Errorf("Expected: %s. Actual: %s", string(message), string(bytes)) |
| 647 |
} |
| 648 |
|
| 649 |
// close server and send packet |
| 650 |
server.Close() |
| 651 |
os.Remove(addr) |
| 652 |
err = client.sendMsg(message) |
| 653 |
if err == nil { |
| 654 |
t.Error("Expected an error, got nil") |
| 655 |
} |
| 656 |
|
| 657 |
// Restart server and send packet |
| 658 |
server, err = net.ListenUnixgram("unixgram", udsAddr) |
| 659 |
if err != nil { |
| 660 |
t.Fatal(err) |
| 661 |
} |
| 662 |
time.Sleep(100 * time.Millisecond) |
| 663 |
defer server.Close() |
| 664 |
err = client.sendMsg(message) |
| 665 |
if err != nil { |
| 666 |
t.Errorf("Expected no error to be returned when server is listening, got: %s", err.Error()) |
| 667 |
} |
| 668 |
|
| 669 |
bytes = make([]byte, 1024) |
| 670 |
n, err = server.Read(bytes) |
| 671 |
if err != nil { |
| 672 |
t.Fatal(err) |
| 673 |
} |
| 674 |
if string(bytes[:n]) != message { |
| 675 |
t.Errorf("Expected: %s. Actual: %s", string(message), string(bytes)) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
func TestSendUDSIgnoreErrors(t *testing.T) { |
| 680 |
client, err := New("unix:///invalid") |
| 681 |
if err != nil { |
| 682 |
t.Fatal(err) |
| 683 |
} |
| 684 |
|
| 685 |
// Default mode throws error |
| 686 |
err = client.sendMsg("message") |
| 687 |
if err == nil || !strings.HasSuffix(err.Error(), "no such file or directory") { |
| 688 |
t.Errorf("Expected error \"connect: no such file or directory\", got: %s", err.Error()) |
| 689 |
} |
| 690 |
|
| 691 |
// Skip errors |
| 692 |
client.SkipErrors = true |
| 693 |
err = client.sendMsg("message") |
| 694 |
if err != nil { |
| 695 |
t.Errorf("Expected no error to be returned when in skip errors mode, got: %s", err.Error()) |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
func TestNilSafe(t *testing.T) { |
| 700 |
var c *Client |
| 701 |
assertNotPanics(t, func() { c.SetWriteTimeout(0) }) |
| 702 |
assertNotPanics(t, func() { c.Flush() }) |
| 703 |
assertNotPanics(t, func() { c.Close() }) |
| 704 |
assertNotPanics(t, func() { c.Count("", 0, nil, 1) }) |
| 705 |
assertNotPanics(t, func() { c.Histogram("", 0, nil, 1) }) |
| 706 |
assertNotPanics(t, func() { c.Distribution("", 0, nil, 1) }) |
| 707 |
assertNotPanics(t, func() { c.Gauge("", 0, nil, 1) }) |
| 708 |
assertNotPanics(t, func() { c.Set("", "", nil, 1) }) |
| 709 |
assertNotPanics(t, func() { |
| 710 |
c.send("", "", []byte(""), nil, 1) |
| 711 |
}) |
| 712 |
assertNotPanics(t, func() { c.Event(NewEvent("", "")) }) |
| 713 |
assertNotPanics(t, func() { c.SimpleEvent("", "") }) |
| 714 |
assertNotPanics(t, func() { c.ServiceCheck(NewServiceCheck("", Ok)) }) |
| 715 |
assertNotPanics(t, func() { c.SimpleServiceCheck("", Ok) }) |
| 716 |
} |
| 717 |
|
| 718 |
func TestEvents(t *testing.T) { |
| 719 |
matrix := []struct { |
| 720 |
event *Event |
| 721 |
encoded string |
| 722 |
}{ |
| 723 |
{ |
| 724 |
NewEvent("Hello", "Something happened to my event"), |
| 725 |
`_e{5,30}:Hello|Something happened to my event`, |
| 726 |
}, { |
| 727 |
&Event{Title: "hi", Text: "okay", AggregationKey: "foo"}, |
| 728 |
`_e{2,4}:hi|okay|k:foo`, |
| 729 |
}, { |
| 730 |
&Event{Title: "hi", Text: "okay", AggregationKey: "foo", AlertType: Info}, |
| 731 |
`_e{2,4}:hi|okay|k:foo|t:info`, |
| 732 |
}, { |
| 733 |
&Event{Title: "hi", Text: "w/e", AlertType: Error, Priority: Normal}, |
| 734 |
`_e{2,3}:hi|w/e|p:normal|t:error`, |
| 735 |
}, { |
| 736 |
&Event{Title: "hi", Text: "uh", Tags: []string{"host:foo", "app:bar"}}, |
| 737 |
`_e{2,2}:hi|uh|#host:foo,app:bar`, |
| 738 |
}, { |
| 739 |
&Event{Title: "hi", Text: "line1\nline2", Tags: []string{"hello\nworld"}}, |
| 740 |
`_e{2,12}:hi|line1\nline2|#helloworld`, |
| 741 |
}, |
| 742 |
} |
| 743 |
|
| 744 |
for _, m := range matrix { |
| 745 |
r, err := m.event.Encode() |
| 746 |
if err != nil { |
| 747 |
t.Errorf("Error encoding: %s\n", err) |
| 748 |
continue |
| 749 |
} |
| 750 |
if r != m.encoded { |
| 751 |
t.Errorf("Expected `%s`, got `%s`\n", m.encoded, r) |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
e := NewEvent("", "hi") |
| 756 |
if _, err := e.Encode(); err == nil { |
| 757 |
t.Errorf("Expected error on empty Title.") |
| 758 |
} |
| 759 |
|
| 760 |
e = NewEvent("hi", "") |
| 761 |
if _, err := e.Encode(); err == nil { |
| 762 |
t.Errorf("Expected error on empty Text.") |
| 763 |
} |
| 764 |
|
| 765 |
e = NewEvent("hello", "world") |
| 766 |
s, err := e.Encode("tag1", "tag2") |
| 767 |
if err != nil { |
| 768 |
t.Error(err) |
| 769 |
} |
| 770 |
expected := "_e{5,5}:hello|world|#tag1,tag2" |
| 771 |
if s != expected { |
| 772 |
t.Errorf("Expected %s, got %s", expected, s) |
| 773 |
} |
| 774 |
if len(e.Tags) != 0 { |
| 775 |
t.Errorf("Modified event in place illegally.") |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
func TestServiceChecks(t *testing.T) { |
| 780 |
matrix := []struct { |
| 781 |
serviceCheck *ServiceCheck |
| 782 |
encoded string |
| 783 |
}{ |
| 784 |
{ |
| 785 |
NewServiceCheck("DataCatService", Ok), |
| 786 |
`_sc|DataCatService|0`, |
| 787 |
}, { |
| 788 |
NewServiceCheck("DataCatService", Warn), |
| 789 |
`_sc|DataCatService|1`, |
| 790 |
}, { |
| 791 |
NewServiceCheck("DataCatService", Critical), |
| 792 |
`_sc|DataCatService|2`, |
| 793 |
}, { |
| 794 |
NewServiceCheck("DataCatService", Unknown), |
| 795 |
`_sc|DataCatService|3`, |
| 796 |
}, { |
| 797 |
&ServiceCheck{Name: "DataCatService", Status: Ok, Hostname: "DataStation.Cat"}, |
| 798 |
`_sc|DataCatService|0|h:DataStation.Cat`, |
| 799 |
}, { |
| 800 |
&ServiceCheck{Name: "DataCatService", Status: Ok, Hostname: "DataStation.Cat", Message: "Here goes valuable message"}, |
| 801 |
`_sc|DataCatService|0|h:DataStation.Cat|m:Here goes valuable message`, |
| 802 |
}, { |
| 803 |
&ServiceCheck{Name: "DataCatService", Status: Ok, Hostname: "DataStation.Cat", Message: "Here are some cyrillic chars: к л м н о п р с т у ф х ц ч ш"}, |
| 804 |
`_sc|DataCatService|0|h:DataStation.Cat|m:Here are some cyrillic chars: к л м н о п р с т у ф х ц ч ш`, |
| 805 |
}, { |
| 806 |
&ServiceCheck{Name: "DataCatService", Status: Ok, Hostname: "DataStation.Cat", Message: "Here goes valuable message", Tags: []string{"host:foo", "app:bar"}}, |
| 807 |
`_sc|DataCatService|0|h:DataStation.Cat|#host:foo,app:bar|m:Here goes valuable message`, |
| 808 |
}, { |
| 809 |
&ServiceCheck{Name: "DataCatService", Status: Ok, Hostname: "DataStation.Cat", Message: "Here goes \n that should be escaped", Tags: []string{"host:foo", "app:b\nar"}}, |
| 810 |
`_sc|DataCatService|0|h:DataStation.Cat|#host:foo,app:bar|m:Here goes \n that should be escaped`, |
| 811 |
}, { |
| 812 |
&ServiceCheck{Name: "DataCatService", Status: Ok, Hostname: "DataStation.Cat", Message: "Here goes m: that should be escaped", Tags: []string{"host:foo", "app:bar"}}, |
| 813 |
`_sc|DataCatService|0|h:DataStation.Cat|#host:foo,app:bar|m:Here goes m\: that should be escaped`, |
| 814 |
}, |
| 815 |
} |
| 816 |
|
| 817 |
for _, m := range matrix { |
| 818 |
r, err := m.serviceCheck.Encode() |
| 819 |
if err != nil { |
| 820 |
t.Errorf("Error encoding: %s\n", err) |
| 821 |
continue |
| 822 |
} |
| 823 |
if r != m.encoded { |
| 824 |
t.Errorf("Expected `%s`, got `%s`\n", m.encoded, r) |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
sc := NewServiceCheck("", Ok) |
| 829 |
if _, err := sc.Encode(); err == nil { |
| 830 |
t.Errorf("Expected error on empty Name.") |
| 831 |
} |
| 832 |
|
| 833 |
sc = NewServiceCheck("sc", ServiceCheckStatus(5)) |
| 834 |
if _, err := sc.Encode(); err == nil { |
| 835 |
t.Errorf("Expected error on invalid status value.") |
| 836 |
} |
| 837 |
|
| 838 |
sc = NewServiceCheck("hello", Warn) |
| 839 |
s, err := sc.Encode("tag1", "tag2") |
| 840 |
if err != nil { |
| 841 |
t.Error(err) |
| 842 |
} |
| 843 |
expected := "_sc|hello|1|#tag1,tag2" |
| 844 |
if s != expected { |
| 845 |
t.Errorf("Expected %s, got %s", expected, s) |
| 846 |
} |
| 847 |
if len(sc.Tags) != 0 { |
| 848 |
t.Errorf("Modified serviceCheck in place illegally.") |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
func TestFlushOnClose(t *testing.T) { |
| 853 |
client, err := NewBuffered("localhost:1201", 64) |
| 854 |
if err != nil { |
| 855 |
t.Fatal(err) |
| 856 |
} |
| 857 |
// stop the flushing mechanism so we can test the buffer without interferences |
| 858 |
client.stop <- struct{}{} |
| 859 |
|
| 860 |
message := "test message" |
| 861 |
|
| 862 |
err = client.sendMsg(message) |
| 863 |
if err != nil { |
| 864 |
t.Fatal(err) |
| 865 |
} |
| 866 |
|
| 867 |
if len(client.commands) != 1 { |
| 868 |
t.Errorf("Commands buffer should contain 1 item, got %d", len(client.commands)) |
| 869 |
} |
| 870 |
|
| 871 |
err = client.Close() |
| 872 |
if err != nil { |
| 873 |
t.Fatal(err) |
| 874 |
} |
| 875 |
|
| 876 |
if len(client.commands) != 0 { |
| 877 |
t.Errorf("Commands buffer should be empty, got %d", len(client.commands)) |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
// These benchmarks show that using different format options: |
| 882 |
// v1: sprintf-ing together a bunch of intermediate strings is 4-5x faster |
| 883 |
// v2: some use of buffer |
| 884 |
// v3: removing sprintf from stat generation and pushing stat building into format |
| 885 |
func BenchmarkFormatV3(b *testing.B) { |
| 886 |
b.StopTimer() |
| 887 |
c := &Client{} |
| 888 |
c.Namespace = "foo.bar." |
| 889 |
c.Tags = []string{"app:foo", "host:bar"} |
| 890 |
b.StartTimer() |
| 891 |
for i := 0; i < b.N; i++ { |
| 892 |
c.format("system.cpu.idle", 10, gaugeSuffix, []string{"foo"}, 1) |
| 893 |
c.format("system.cpu.load", 0.1, gaugeSuffix, nil, 0.9) |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
func BenchmarkFormatV1(b *testing.B) { |
| 898 |
b.StopTimer() |
| 899 |
c := &Client{} |
| 900 |
c.Namespace = "foo.bar." |
| 901 |
c.Tags = []string{"app:foo", "host:bar"} |
| 902 |
b.StartTimer() |
| 903 |
for i := 0; i < b.N; i++ { |
| 904 |
c.formatV1("system.cpu.idle", 10, []string{"foo"}, 1) |
| 905 |
c.formatV1("system.cpu.load", 0.1, nil, 0.9) |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
// V1 formatting function, added to client for tests |
| 910 |
func (c *Client) formatV1(name string, value float64, tags []string, rate float64) string { |
| 911 |
valueAsString := fmt.Sprintf("%f|g", value) |
| 912 |
if rate < 1 { |
| 913 |
valueAsString = fmt.Sprintf("%s|@%f", valueAsString, rate) |
| 914 |
} |
| 915 |
if c.Namespace != "" { |
| 916 |
name = fmt.Sprintf("%s%s", c.Namespace, name) |
| 917 |
} |
| 918 |
|
| 919 |
tags = append(c.Tags, tags...) |
| 920 |
if len(tags) > 0 { |
| 921 |
valueAsString = fmt.Sprintf("%s|#%s", valueAsString, strings.Join(tags, ",")) |
| 922 |
} |
| 923 |
|
| 924 |
return fmt.Sprintf("%s:%s", name, valueAsString) |
| 925 |
|
| 926 |
} |
| 927 |
|
| 928 |
func BenchmarkFormatV2(b *testing.B) { |
| 929 |
b.StopTimer() |
| 930 |
c := &Client{} |
| 931 |
c.Namespace = "foo.bar." |
| 932 |
c.Tags = []string{"app:foo", "host:bar"} |
| 933 |
b.StartTimer() |
| 934 |
for i := 0; i < b.N; i++ { |
| 935 |
c.formatV2("system.cpu.idle", 10, []string{"foo"}, 1) |
| 936 |
c.formatV2("system.cpu.load", 0.1, nil, 0.9) |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
// V2 formatting function, added to client for tests |
| 941 |
func (c *Client) formatV2(name string, value float64, tags []string, rate float64) string { |
| 942 |
var buf bytes.Buffer |
| 943 |
if c.Namespace != "" { |
| 944 |
buf.WriteString(c.Namespace) |
| 945 |
} |
| 946 |
buf.WriteString(name) |
| 947 |
buf.WriteString(":") |
| 948 |
buf.WriteString(fmt.Sprintf("%f|g", value)) |
| 949 |
if rate < 1 { |
| 950 |
buf.WriteString(`|@`) |
| 951 |
buf.WriteString(strconv.FormatFloat(rate, 'f', -1, 64)) |
| 952 |
} |
| 953 |
|
| 954 |
writeTagString(&buf, c.Tags, tags) |
| 955 |
|
| 956 |
return buf.String() |
| 957 |
} |