-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
51 lines (46 loc) · 1.17 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
package gocache
import (
"fmt"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func TestLen(t *testing.T) {
expectVal, size := 0, 3
Convey("验证len是否正确:", t, func() {
cache := NewGoCacheBuilder(WithExpire(time.Second), WithKeyCnt(size))
So(expectVal == cache.cache.Len(), ShouldBeTrue)
})
}
func TestGet(t *testing.T) {
key := "1"
expectErr := ErrKeyNotFound
size := 3
Convey("验证不存在key:", t, func() {
cache := NewGoCacheBuilder(WithExpire(time.Second), WithKeyCnt(size))
val, err := cache.cache.Get(key)
So(val == nil, ShouldBeTrue)
So(expectErr == err, ShouldBeTrue)
})
}
func TestSet(t *testing.T) {
keys := []string{"1", "2", "3", "1", "4"}
expectVals := []interface{}{"4", "1", "3"}
size := 3
Convey("验证LRU:", t, func() {
cache := NewGoCacheBuilder(WithExpire(time.Second), WithKeyCnt(size))
for i := range keys {
cache.cache.Set(keys[i], keys[i])
}
ok, i := true, 0
for ptr := cache.cache.lru.Front(); ptr != nil; ptr = ptr.Next() {
if ptr.Value.(*elem).value != expectVals[i] {
fmt.Println(ptr.Value.(*elem).value, expectVals[i], i)
ok = false
break
}
i++
}
So(ok, ShouldBeTrue)
})
}