-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_test.go
243 lines (193 loc) · 3.94 KB
/
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package gounter
import (
"math"
"math/rand"
"sync/atomic"
"testing"
"time"
)
func TestCounterReleaseNil(t *testing.T) {
ReleaseCounter(nil)
}
func TestNewCounter(t *testing.T) {
t.Parallel()
testNewCounter(t)
testGo(t, testNewCounter, 10)
testGo(t, testNewCounter, 100)
testGo(t, testNewCounter, 1000)
testGo(t, testNewCounter, 10000)
}
func TestCounterCopyTo(t *testing.T) {
t.Parallel()
testCopyTo(t)
testGo(t, testCopyTo, 10)
testGo(t, testCopyTo, 100)
testGo(t, testCopyTo, 1000)
testGo(t, testCopyTo, 10000)
}
// race fail
func TestCounterChange(t *testing.T) {
t.Parallel()
testCounterInc(t)
testCounterIncAndDec(t)
testCounterDecZero(t)
testGo(t, testCounterInc, 10)
testGo(t, testCounterIncAndDec, 10)
testGo(t, testCounterDecZero, 10)
}
func TestReset(t *testing.T) {
t.Parallel()
c := AcquireCounter()
for i := 0; i < 1000; i += 1 {
c.Inc()
}
v := c.Get()
if v != 1000 {
t.Fatalf("should be %d, but %f", 1000, v)
}
c.Reset()
v = c.Get()
if v != 0 {
t.Fatalf("should be %d, but %f", 0, v)
}
}
func testNewCounter(t *testing.T) {
for i := 0; i < 10; i++ {
c := AcquireCounter()
if c.Get() != 0 {
t.Fatalf("error: bits=%d", c.bits)
}
// change val1
atomic.AddUint64(&c.bits, 2)
// Test Release
ReleaseCounter(c)
}
}
func testCopyTo(t *testing.T) {
for i := 0; i < 10; i++ {
c1 := AcquireCounter()
c2 := AcquireCounter()
// change val1
v1Change := rand.Uint64()
atomic.AddUint64(&c1.bits, v1Change)
// c1 to c1
ok, err := c1.CopyTo(c1)
if ok {
t.Fatal("same counter should err, but not!")
}
if err != ErrSameCounterPointer {
t.Fatalf("same counter should err, but %s", err.Error())
}
// c2 to c2
ok, err = c2.CopyTo(c2)
if ok {
t.Fatal("same counter should err, but not!")
}
if err != ErrSameCounterPointer {
t.Fatalf("same counter should err, but %s", err.Error())
}
// copy to
ok, err = c1.CopyTo(c2)
if !ok {
t.Fatal("counter should be copied, but not")
}
if err != nil {
t.Fatalf("counter should be copied, but err: %s", err.Error())
}
if c2.bits != v1Change {
t.Fatalf("copy error: val=%d", c2.bits)
}
// copy to other
ok, err = c1.CopyTo(1)
if ok || err == nil {
t.Fatal("should err, but not")
}
ReleaseCounter(c2)
ReleaseCounter(c1)
}
}
func testCounterInc(t *testing.T) {
ch := make(chan struct{}, 100)
c := AcquireCounter()
defer ReleaseCounter(c)
for i := 0; i < 100; i++ {
go func() {
c.Inc()
ch <- struct{}{}
}()
}
for i := 0; i < 100; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
if c.Get() != 100 {
t.Fatalf("inc error: should 100, but %f", c.Get())
}
}
func testCounterIncAndDec(t *testing.T) {
ch := make(chan struct{}, 200)
c := AcquireCounter()
defer ReleaseCounter(c)
for i := 0; i < 100; i++ {
go func() {
c.Inc()
ch <- struct{}{}
}()
go func() {
c.Dec()
ch <- struct{}{}
}()
}
for i := 0; i < 200; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatal("timeout")
}
}
if c.Get() != 0 {
t.Fatalf("inc error: should 0, but %f", c.Get())
}
}
func testCounterDecZero(t *testing.T) {
ch := make(chan struct{}, 200)
c := AcquireCounter()
defer ReleaseCounter(c)
var num int64
for i := 0; i < 100; i++ {
go func() {
// Rounding
sub := math.Round(rand.Float64())
c.Sub(sub)
atomic.AddInt64(&num, int64(sub)*-1)
ch <- struct{}{}
}()
go func() {
c.Dec()
atomic.AddInt64(&num, -1)
ch <- struct{}{}
}()
}
for i := 0; i < 200; i++ {
select {
case <-ch:
case <-time.After(time.Second * 10):
// test -race
t.Fatal("timeout")
}
}
bits := atomic.LoadUint64(&c.bits)
n := math.Float64frombits(bits)
if int64(n) != num {
t.Fatalf("dec error: num should %d, but %0f", num, n)
}
if int64(c.Real()) != num {
t.Fatalf("dec error: num should %d, but %0f", num, c.Real())
}
if c.Get() != 0 {
t.Fatalf("dec error: should 0, but %f", c.Get())
}
}