-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscope_info_test.go
92 lines (73 loc) · 1.54 KB
/
scope_info_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
package fixenv
import (
"sort"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestScopeInfo_AddKey(t *testing.T) {
at := assert.New(t)
t.Run("simple", func(t *testing.T) {
si := newScopeInfo(t)
at.Equal(t, si.t)
at.Len(si.cacheKeys, 0)
si.AddKey("asd")
si.AddKey("ddd")
at.Equal([]cacheKey{"asd", "ddd"}, si.cacheKeys)
})
t.Run("race", func(t *testing.T) {
si := newScopeInfo(t)
count := 10000
source := make([]cacheKey, count)
for i := 0; i < count; i++ {
source[i] = cacheKey(strconv.Itoa(i))
}
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
go func(key cacheKey) {
si.AddKey(key)
wg.Done()
}(source[i])
}
wg.Wait()
sort.Slice(si.cacheKeys, func(i, j int) bool {
iInt, _ := strconv.Atoi(string(si.cacheKeys[i]))
jInt, _ := strconv.Atoi(string(si.cacheKeys[j]))
return iInt < jInt
})
at := assert.New(t)
at.Equal(source, si.cacheKeys)
})
}
func TestScopeInfo_Keys(t *testing.T) {
t.Run("simple", func(t *testing.T) {
at := assert.New(t)
si := newScopeInfo(t)
si.AddKey("asd")
si.AddKey("kkk")
keys := si.Keys()
at.Equal([]cacheKey{"asd", "kkk"}, keys)
})
t.Run("mutex", func(t *testing.T) {
at := assert.New(t)
si := newScopeInfo(t)
si.AddKey("asd")
si.AddKey("kkk")
si.m.Lock()
var keys []cacheKey
var wg sync.WaitGroup
wg.Add(1)
go func() {
keys = si.Keys()
wg.Done()
}()
time.Sleep(waitTime)
at.Len(keys, 0)
si.m.Unlock()
wg.Wait()
at.Equal([]cacheKey{"asd", "kkk"}, keys)
})
}