forked from golang/groupcache
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_expiration_test.go
158 lines (128 loc) · 4.91 KB
/
cache_expiration_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
// Tests for groupcache expiration
package groupcache
import (
"fmt"
"sync"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
var (
expireGroup *Group
failedGroup *Group
initOnce_expire sync.Once
)
func expireCacheSetup() {
setTimeProvider(defaultTimeProvider{}) // reset TimeProvider
chanSequence := make(chan int, 10)
for i := 0; i < 10; i++ {
chanSequence <- i
}
expireGroup = NewGroup("expire-group", 1<<20, GetterFunc(func(_ Context, key string, dest Sink) error {
time.Sleep(200 * time.Millisecond)
return dest.SetTimestampBytes([]byte(fmt.Sprintf("%s=%d", key, <-chanSequence)), GetTime())
}))
expireGroup.SetExpiration(time.Duration(2) * time.Second)
expireGroup.SetStalePeriod(time.Duration(2) * time.Second)
expireGroup.SetStaleDeadline(100 * time.Millisecond) // Get() may return old value if not set
chanSequence2 := make(chan int, 10)
for i := 0; i < 10; i++ {
chanSequence2 <- i
}
failedGroup = NewGroup("failed-group", 1<<20, GetterFunc(func(_ Context, key string, dest Sink) error {
time.Sleep(10 * time.Millisecond)
value := <-chanSequence2
if value%2 == 0 {
return dest.SetTimestampBytes([]byte(fmt.Sprintf("F%s=%d", key, value)), GetTime())
} else {
time.Sleep(40 * time.Millisecond)
return fmt.Errorf("error=%d", value)
}
}))
failedGroup.SetExpiration(time.Duration(5) * time.Millisecond)
failedGroup.SetStalePeriod(time.Duration(2) * time.Second)
failedGroup.SetStaleDeadline(100 * time.Millisecond) // Get() may return old value if not set
}
func callGetCache(gcache *Group, key string) (string, error, int64) {
start_ts := time.Now()
var packedContent []byte
err := gcache.Get(nil, key, AllocatingByteSliceSink(&packedContent))
if err != nil {
return "", err, int64(time.Since(start_ts) / time.Millisecond)
}
content, _, err := UnpackTimestamp(packedContent)
return string(content), err, int64(time.Since(start_ts) / time.Millisecond)
}
func TestGroupExpiration(t *testing.T) {
initOnce_expire.Do(expireCacheSetup)
Convey("It should return cached value when hit", t, func() {
content, err, cost_ms := callGetCache(expireGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "test=0")
So(cost_ms, ShouldBeBetweenOrEqual, 195, 205) // load() cost
time.Sleep(1000 * time.Millisecond)
content, err, cost_ms = callGetCache(expireGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "test=0")
So(cost_ms, ShouldBeBetweenOrEqual, 0, 5) // read from cache
})
Convey("It should return old value when expire, but return new value in next fetch", t, func() {
time.Sleep(2000 * time.Millisecond)
content, err, cost_ms := callGetCache(expireGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "test=0")
So(cost_ms, ShouldBeBetweenOrEqual, 95, 105) // only wait StaleDeadline(100ms)
time.Sleep(220 * time.Millisecond) // wait load() complete, refetch
content, err, cost_ms = callGetCache(expireGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "test=1")
So(cost_ms, ShouldBeBetweenOrEqual, 0, 5) // read from cache
})
Convey("It should return new value when reach stalePeriod", t, func() {
time.Sleep(4200 * time.Millisecond) // wait Expiration + StalePeriod
content, err, cost_ms := callGetCache(expireGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "test=2")
So(cost_ms, ShouldBeBetweenOrEqual, 195, 205) // force load()
})
}
func TestCache_nbytes(t *testing.T) {
initOnce_expire.Do(expireCacheSetup)
// If key exist in Cache, it return wrong nbytes here
Convey("Cache's nbytes should be zero", t, func() {
So(expireGroup.mainCache.bytes(), ShouldNotEqual, 0)
if expireGroup.mainCache.lru != nil {
expireGroup.mainCache.lru.Clear()
So(expireGroup.mainCache.bytes(), ShouldEqual, 0)
}
if expireGroup.hotCache.lru != nil {
expireGroup.hotCache.lru.Clear()
So(expireGroup.hotCache.bytes(), ShouldEqual, 0)
}
})
}
func TestCache_load_errors(t *testing.T) {
initOnce_expire.Do(expireCacheSetup)
Convey("load() error will return cached value when not totally timeout", t, func() {
content, err, cost_ms := callGetCache(failedGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "Ftest=0")
So(cost_ms, ShouldBeBetweenOrEqual, 5, 15) // load() success
time.Sleep(10 * time.Millisecond)
// load() error when second Get()
content, err, cost_ms = callGetCache(failedGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "Ftest=0")
So(cost_ms, ShouldBeBetweenOrEqual, 45, 55) // load() return error
})
Convey("load() error will raise when stale period reached", t, func() {
content, err, cost_ms := callGetCache(failedGroup, "test")
So(err, ShouldBeNil)
So(content, ShouldEqual, "Ftest=2")
So(cost_ms, ShouldBeBetweenOrEqual, 5, 15) // load() success
time.Sleep(2000 * time.Millisecond)
_, err, cost_ms = callGetCache(failedGroup, "test")
So(err, ShouldNotBeNil)
So(cost_ms, ShouldBeBetweenOrEqual, 45, 55) // load() return error
})
}