-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope_info_test.go
83 lines (68 loc) · 1.48 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
package fixenv
import (
"sort"
"strconv"
"sync"
"testing"
"time"
)
func TestScopeInfo_AddKey(t *testing.T) {
t.Run("simple", func(t *testing.T) {
si := newScopeInfo(t)
requireEquals(t, t, si.t)
requireEquals(t, len(si.cacheKeys), 0)
si.AddKey("asd")
si.AddKey("ddd")
requireEquals(t, []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
})
requireEquals(t, source, si.cacheKeys)
})
}
func TestScopeInfo_Keys(t *testing.T) {
t.Run("simple", func(t *testing.T) {
si := newScopeInfo(t)
si.AddKey("asd")
si.AddKey("kkk")
keys := si.Keys()
requireEquals(t, []cacheKey{"asd", "kkk"}, keys)
})
t.Run("mutex", func(t *testing.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)
requireEquals(t, len(keys), 0)
si.m.Unlock()
wg.Wait()
requireEquals(t, []cacheKey{"asd", "kkk"}, keys)
})
}