-
Notifications
You must be signed in to change notification settings - Fork 12
/
parse_test.go
64 lines (59 loc) · 1.66 KB
/
parse_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
package main
import (
"testing"
)
var parsePacketTests = []struct {
msg string
length int
name string
bucket string
value float64
}{
{"invalid", 0, "", "", 0},
{"wrong.type:1|z", 0, "", "", 0},
{"prefix.type:2|cg", 1, "prefix.type", "c", 2},
{"my.name:1|c", 1, "my.name", "c", 1},
{"first.name:6|c\nlast.name:7|c", 2, "first.name", "c", 6},
{"some.gauge:234.6|g", 1, "some.gauge", "g", 234.6},
{"sampled.counter:4|c|@0.5", 1, "sampled.counter", "c", 8},
{"sampled.counter:4|c|@0.33", 1, "sampled.counter", "c", 12},
{"first.timer:123.4567|ms\nsecond.timer:456.7890|ms", 2, "first.timer", "ms", 123.4567},
}
func TestParsePacket(t *testing.T) {
for _, s := range parsePacketTests {
ps := parsePacket(s.msg)
if len(ps) != s.length {
t.Errorf("%s: got %d packets, expected %d", s.msg, len(ps), s.length)
}
if len(ps) > 0 {
if ps[0].name != s.name {
t.Errorf("%s: got name '%s', expected '%s'", s.msg, ps[0].name, s.name)
}
if ps[0].bucket != s.bucket {
t.Errorf("%s: got bucket '%s', expected '%s'", s.msg, ps[0].bucket, s.bucket)
}
if ps[0].value != s.value {
t.Errorf("%s: got value '%f', expected '%f'", s.msg, ps[0].value, s.value)
}
}
}
}
var parseSourceTests = []struct {
in string
name string
source string
}{
{"some_metric", "some_metric", ""},
{"some_source,some_metric", "some_metric", "some_source"},
}
func TestParseSource(t *testing.T) {
for _, s := range parseSourceTests {
name, source := parseSource(s.in)
if name != s.name {
t.Errorf("%s: got '%s', expected '%s'", s.in, name, s.name)
}
if source != s.source {
t.Errorf("%s: got '%s', expected '%s'", s.in, source, s.source)
}
}
}