-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequence_test.go
60 lines (56 loc) · 1.17 KB
/
sequence_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
package flyline
import (
"runtime"
"sync"
"testing"
)
func TestNewSequence(t *testing.T) {
seq := NewSequence()
if seq.Get() == -1 {
t.Logf("seq %v", seq)
} else {
t.Errorf("seq %v", seq)
}
}
func TestSequence_Incr(t *testing.T) {
seq := NewSequence()
v := seq.Get()
n := seq.Incr()
if n == v+1 {
t.Logf("seq incr: %v -> %v", v, n)
} else {
t.Errorf("seq incr: %v -> %v", v, n)
}
t.Log("thread safe testing....")
runtime.GOMAXPROCS(4)
wg := new(sync.WaitGroup)
for i := 0; i < 20; i++ {
wg.Add(1)
go func(seq *Sequence, wg *sync.WaitGroup) {
defer wg.Done()
t.Logf("seq now: %v incr : %v, get : %v", seq.Get(), seq.Incr(), seq.Get())
}(seq, wg)
}
wg.Wait()
}
func TestSequence_Decr(t *testing.T) {
seq := NewSequence()
v := seq.Get()
n := seq.Decr()
if n == v-1 {
t.Logf("seq incr: %v -> %v", v, n)
} else {
t.Errorf("seq incr: %v -> %v", v, n)
}
t.Log("thread safe testing....")
runtime.GOMAXPROCS(4)
wg := new(sync.WaitGroup)
for i := 0; i < 20; i++ {
wg.Add(1)
go func(seq *Sequence, wg *sync.WaitGroup) {
defer wg.Done()
t.Logf("seq now: %v incr : %v, get : %v", seq.Get(), seq.Decr(), seq.Get())
}(seq, wg)
}
wg.Wait()
}