forked from antchfx/xpath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
166 lines (153 loc) · 3.81 KB
/
cache_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
package xpath
import (
"errors"
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
)
func TestLoadingCache(t *testing.T) {
c := NewLoadingCache(
func(key interface{}) (interface{}, error) {
switch v := key.(type) {
case int:
return strconv.Itoa(v), nil
default:
return nil, errors.New("invalid type")
}
},
2) // cap = 2
assertEqual(t, 0, len(c.m))
v, err := c.get(1)
assertNoErr(t, err)
assertEqual(t, "1", v)
assertEqual(t, 1, len(c.m))
v, err = c.get(1)
assertNoErr(t, err)
assertEqual(t, "1", v)
assertEqual(t, 1, len(c.m))
v, err = c.get(2)
assertNoErr(t, err)
assertEqual(t, "2", v)
assertEqual(t, 2, len(c.m))
// over capacity, m is reset
v, err = c.get(3)
assertNoErr(t, err)
assertEqual(t, "3", v)
assertEqual(t, 1, len(c.m))
// Invalid capacity
assertPanic(t, func() {
NewLoadingCache(func(key interface{}) (interface{}, error) { return key, nil }, -1)
})
// Loading failure
c = NewLoadingCache(
func(key interface{}) (interface{}, error) {
if key.(int)%2 == 0 {
return key, nil
} else {
return nil, fmt.Errorf("artificial error: %d", key.(int))
}
}, 0)
v, err = c.get(12)
assertNoErr(t, err)
assertEqual(t, 12, v)
_, err = c.get(21)
assertErr(t, err)
assertEqual(t, "artificial error: 21", err.Error())
}
const (
benchLoadingCacheRandSeed = 12345
benchLoadingCacheConcurrency = 5
benchLoadingCacheKeyRange = 2000
benchLoadingCacheCap = 1000
)
func BenchmarkLoadingCacheCapped_SingleThread(b *testing.B) {
rand.Seed(benchLoadingCacheRandSeed)
c := NewLoadingCache(
func(key interface{}) (interface{}, error) {
return key, nil
}, benchLoadingCacheCap)
for i := 0; i < b.N; i++ {
k := rand.Intn(benchLoadingCacheKeyRange)
v, _ := c.get(k)
if k != v {
b.FailNow()
}
}
b.Logf("N=%d, reset=%d", b.N, c.reset)
}
func BenchmarkLoadingCacheCapped_MultiThread(b *testing.B) {
rand.Seed(benchLoadingCacheRandSeed)
c := NewLoadingCache(
func(key interface{}) (interface{}, error) {
return key, nil
}, benchLoadingCacheCap)
wg := sync.WaitGroup{}
wg.Add(benchLoadingCacheConcurrency)
for i := 0; i < benchLoadingCacheConcurrency; i++ {
go func() {
for j := 0; j < b.N; j++ {
k := rand.Intn(benchLoadingCacheKeyRange)
v, _ := c.get(k)
if k != v {
b.FailNow()
}
}
defer wg.Done()
}()
}
wg.Wait()
b.Logf("N=%d, concurrency=%d, reset=%d", b.N, benchLoadingCacheConcurrency, c.reset)
}
func BenchmarkLoadingCacheNoCap_SingleThread(b *testing.B) {
rand.Seed(benchLoadingCacheRandSeed)
c := NewLoadingCache(
func(key interface{}) (interface{}, error) {
return key, nil
}, 0) // 0 => no cap
for i := 0; i < b.N; i++ {
k := rand.Intn(benchLoadingCacheKeyRange)
v, _ := c.get(k)
if k != v {
b.FailNow()
}
}
b.Logf("N=%d, reset=%d", b.N, c.reset)
}
func BenchmarkLoadingCacheNoCap_MultiThread(b *testing.B) {
rand.Seed(benchLoadingCacheRandSeed)
c := NewLoadingCache(
func(key interface{}) (interface{}, error) {
return key, nil
}, 0) // 0 => no cap
wg := sync.WaitGroup{}
wg.Add(benchLoadingCacheConcurrency)
for i := 0; i < benchLoadingCacheConcurrency; i++ {
go func() {
for j := 0; j < b.N; j++ {
k := rand.Intn(benchLoadingCacheKeyRange)
v, _ := c.get(k)
if k != v {
b.FailNow()
}
}
defer wg.Done()
}()
}
wg.Wait()
b.Logf("N=%d, concurrency=%d, reset=%d", b.N, benchLoadingCacheConcurrency, c.reset)
}
func TestGetRegexp(t *testing.T) {
RegexpCache = defaultRegexpCache()
assertEqual(t, 0, len(RegexpCache.m))
assertEqual(t, defaultCap, RegexpCache.cap)
exp, err := getRegexp("^[0-9]{3,5}$")
assertNoErr(t, err)
assertTrue(t, exp.MatchString("3141"))
assertFalse(t, exp.MatchString("3"))
exp, err = getRegexp("[invalid")
assertErr(t, err)
assertEqual(t, "error parsing regexp: missing closing ]: `[invalid`", err.Error())
assertNil(t, exp)
}