-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_test.go
75 lines (65 loc) · 1.13 KB
/
stat_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
package connpool
import (
"sync"
"testing"
)
func TestCounterInc(t *testing.T) {
c := newCounter()
wg := sync.WaitGroup{}
concur := 1000
loop := 1000
want := concur * loop
wg.Add(concur)
for i := 0; i < concur; i++ {
go func() {
defer wg.Done()
for i := 0; i < loop; i++ {
c.inc()
}
}()
}
wg.Wait()
if c.val() != want {
t.Fatalf("inc error, -want:%d, +got:%d", want, c.val())
}
}
func TestCounterDec(t *testing.T) {
c := newCounter()
wg := sync.WaitGroup{}
concur := 1000
loop := 1000
c.(*count).v = int64(concur * loop)
wg.Add(concur)
for i := 0; i < concur; i++ {
go func() {
defer wg.Done()
for i := 0; i < loop; i++ {
c.dec()
}
}()
}
wg.Wait()
if c.val() != 0 {
t.Fatalf("dec error, -want:%d, +got:%d", 0, c.val())
}
}
func TestCounterReset(t *testing.T) {
c := newCounter()
wg := sync.WaitGroup{}
conCount := 1000
loop := 1000
wg.Add(conCount)
for i := 0; i < conCount; i++ {
go func() {
defer wg.Done()
for i := 0; i < loop; i++ {
c.inc()
}
}()
}
wg.Wait()
c.reset()
if c.val() != 0 {
t.Fatalf("reset error, -want:%d, +got:%d", 0, c.val())
}
}