-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_counter_test.go
168 lines (131 loc) · 3.11 KB
/
label_counter_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
package gounter
import (
"sync"
"testing"
)
func testGenerateLabels() []string {
labels := make([]string, 26*2)
for i := 0; i < 26; i++ {
labels[i] = string(rune('a' + i))
}
for i := 0; i < 26; i++ {
labels[26+i] = string(rune('A' + i))
}
return labels
}
func TestLabelCounterWithNormal(t *testing.T) {
t.Parallel()
// generate random labels, use a-z A-z
labels := testGenerateLabels()
c := NewLabelCounterNormal()
testLabelCounterIncDec(labels, 10000, c, t)
}
func TestLabelCounterWithMax(t *testing.T) {
t.Parallel()
labels := testGenerateLabels()
c := NewLabelCounterWithMax(10000)
testLabelCounterIncDec(labels, 10000, c, t)
c1 := NewLabelCounterWithMax(1000)
testLabelCounterIncDec(labels, 1000, c1, t)
c2 := NewLabelCounterWithMax(100)
testLabelCounterIncDec(labels, 100, c2, t)
}
var c = NewLabelCounterNormal()
func TestLabelCounterChange(t *testing.T) {
t.Parallel()
c.Set("set", 10)
v, _ := c.Get("set")
if v != 10 {
t.Errorf("wrong result, expect %d, got %f", 10, v)
}
c.Add("set", 10)
v, _ = c.Get("set")
if v != 20 {
t.Errorf("wrong result, expect %d, got %f", 20, v)
}
c.Sub("set", 20)
v, _ = c.Get("set")
if v != 0 {
t.Errorf("wrong result, expect %d, got %f", 0, v)
}
}
func TestLabelCounter_ChangeLabel(t *testing.T) {
t.Parallel()
label := "remove_label"
c.Set(label, 10)
if v, _ := c.Get(label); v != 10 {
t.Errorf("wrong result, expect %d, got %f", 10, v)
}
c.ResetLabel(label)
if v, _ := c.Get(label); v != 0 {
t.Errorf("wrong result, expect %d, got %f", 0, v)
}
c.Add(label, 20)
if v, _ := c.Get(label); v != 20 {
t.Errorf("wrong result, expect %d, got %f", 20, v)
}
c.RemoveLabel(label)
if v, _ := c.Get(label); v != 0 {
t.Errorf("wrong result, expect %d, got %f", 0, v)
}
// multi label
label1 := "remove_label1"
label2 := "remove_label2"
c.Set(label1, 10)
c.Set(label2, 20)
c.RemoveLabel(label1)
c.RemoveLabel(label2)
}
func TestLabelCounter_Reset(t *testing.T) {
t.Parallel()
c := NewLabelCounterWithMax(20)
c.Set("reset", 10)
c.Set("test", 10)
if len(c.value) != 2 {
t.Errorf("wrong result, expect %d, got %d", 2, len(c.value))
}
c.Reset()
if len(c.value) != 0 {
t.Errorf("wrong result, expect %d, got %d", 0, len(c.value))
}
}
// testLabelCounterIncDec tests LabelCounter.
func testLabelCounterIncDec[T Gounter](labels []string, count int, c *LabelCounter[T], t *testing.T) {
wg := sync.WaitGroup{}
// each label add
for _, label := range labels {
wg.Add(count)
for i := 0; i < count; i++ {
go func(ll string) {
c.Inc(ll)
wg.Done()
}(label)
}
}
wg.Wait()
// assert each label add
for _, label := range labels {
v, _ := c.Get(label)
if v != float64(count) {
t.Errorf("label %s, wrong result, expect %d, got %f", label, 10, v)
}
}
// decrement each label
for _, label := range labels {
wg.Add(count)
for i := 0; i < count; i++ {
go func(ll string) {
c.Dec(ll)
wg.Done()
}(label)
}
}
wg.Wait()
// assert each label decrement
for _, label := range labels {
v, _ := c.Get(label)
if v != 0 {
t.Errorf("label %s, wrong result, expect %d, got %f", label, 0, v)
}
}
}