File: stat_test.go

package info (click to toggle)
golang-github-cactus-go-statsd-client 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 204 kB
  • sloc: makefile: 5
file content (154 lines) | stat: -rw-r--r-- 3,436 bytes parent folder | download | duplicates (2)
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
package statsdtest

import (
	"bytes"
	"reflect"
	"testing"
)

type parsingTestCase struct {
	name     string
	sent     [][]byte
	expected Stats
}

var (
	badStatNameOnly = []byte("foo.bar.baz:")
	bsnoStat        = Stat{
		Raw:    badStatNameOnly,
		Stat:   "foo.bar.baz",
		Parsed: false,
	}

	gaugeWithoutRate = []byte("foo.bar.baz:1.000|g")
	gworStat         = Stat{
		Raw:    gaugeWithoutRate,
		Stat:   "foo.bar.baz",
		Value:  "1.000",
		Tag:    "g",
		Parsed: true,
	}

	counterWithRate = []byte("foo.bar.baz:1.000|c|@0.75")
	cwrStat         = Stat{
		Raw:    counterWithRate,
		Stat:   "foo.bar.baz",
		Value:  "1.000",
		Tag:    "c",
		Rate:   "0.75",
		Parsed: true,
	}

	stringStat = []byte(":some string value|s")
	sStat      = Stat{
		Raw:    stringStat,
		Stat:   "",
		Value:  "some string value",
		Tag:    "s",
		Parsed: true,
	}

	badValue = []byte("asoentuh")
	bvStat   = Stat{Raw: badValue}

	testCases = []parsingTestCase{
		{name: "no stat data",
			sent:     [][]byte{badStatNameOnly},
			expected: Stats{bsnoStat}},
		{name: "trivial case",
			sent:     [][]byte{gaugeWithoutRate},
			expected: Stats{gworStat}},
		{name: "multiple simple",
			sent:     [][]byte{gaugeWithoutRate, counterWithRate},
			expected: Stats{gworStat, cwrStat}},
		{name: "mixed good and bad",
			sent:     [][]byte{badValue, badValue, stringStat, badValue, counterWithRate, badValue},
			expected: Stats{bvStat, bvStat, sStat, bvStat, cwrStat, bvStat}},
	}
)

func TestParseBytes(t *testing.T) {
	for _, tc := range testCases {
		got := ParseStats(bytes.Join(tc.sent, []byte("\n")))
		want := tc.expected
		if !reflect.DeepEqual(got, want) {
			t.Errorf("%s: got: %+v, want: %+v", tc.name, got, want)
		}
	}
}

func TestStatsUnparsed(t *testing.T) {
	start := Stats{bsnoStat, gworStat, bsnoStat, bsnoStat, cwrStat}
	got := start.Unparsed()
	want := Stats{bsnoStat, bsnoStat, bsnoStat}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("got: %+v, want: %+v", got, want)
	}
}

func TestStatsCollectNamed(t *testing.T) {
	type test struct {
		name    string
		start   Stats
		want    Stats
		matchOn string
	}

	cases := []test{
		{"No matches",
			Stats{bsnoStat, cwrStat},
			nil,
			"foo"},
		{"One match",
			Stats{bsnoStat, Stat{Stat: "foo"}, cwrStat},
			Stats{Stat{Stat: "foo"}},
			"foo"},
		{"Two matches",
			Stats{bsnoStat, Stat{Stat: "foo"}, cwrStat},
			Stats{bsnoStat, cwrStat},
			"foo.bar.baz"},
	}

	for _, c := range cases {
		got := c.start.CollectNamed(c.matchOn)
		if !reflect.DeepEqual(got, c.want) {
			t.Errorf("%s: got: %+v, want: %+v", c.name, got, c.want)
		}
	}
}

func TestStatsCollect(t *testing.T) {
	type test struct {
		name  string
		start Stats
		want  Stats
		pred  func(Stat) bool
	}

	cases := []test{
		{"Not called",
			Stats{},
			nil,
			func(_ Stat) bool { t.Errorf("should not be called"); return true }},
		{"Matches value = 1.000",
			Stats{bsnoStat, gworStat, cwrStat, sStat, bsnoStat},
			Stats{gworStat, cwrStat},
			func(s Stat) bool { return s.Value == "1.000" }},
	}

	for _, c := range cases {
		got := c.start.Collect(c.pred)
		if !reflect.DeepEqual(got, c.want) {
			t.Errorf("%s: got: %+v, want: %+v", c.name, got, c.want)
		}
	}
}

func TestStatsValues(t *testing.T) {
	start := Stats{bsnoStat, sStat, gworStat}
	got := start.Values()
	want := []string{bsnoStat.Value, sStat.Value, gworStat.Value}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("got: %+v, want: %+v", got, want)
	}
}