-
Notifications
You must be signed in to change notification settings - Fork 0
/
expired_test.go
92 lines (69 loc) · 3.38 KB
/
expired_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 ezcache
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestCacheSync_Expire(t *testing.T) {
Convey("输入了正确的参数,成功返回", t, func() {
cache := NewCacheSyncNoExpire()
cache.Set("testkey-1", 0)
cache.Set("testkey-2", 0)
cache.Set("testkey-3", 0)
cache.Expire("testkey-1", time.Millisecond*100)
cache.Expire("testkey-2", time.Millisecond*500)
cache.Expire("testkey-3", time.Millisecond*300)
So(len(cache.expiredSet), ShouldEqual, 3)
So(cache.expiredRoot.key == "testkey-1", ShouldBeTrue)
So(cache.expiredSet["testkey-1"].prev == nil, ShouldBeTrue)
So(cache.expiredSet["testkey-1"].next != nil && cache.expiredSet["testkey-1"].next.key == "testkey-3", ShouldBeTrue)
So(cache.expiredSet["testkey-3"].prev != nil && cache.expiredSet["testkey-3"].prev.key == "testkey-1", ShouldBeTrue)
So(cache.expiredSet["testkey-3"].next != nil && cache.expiredSet["testkey-3"].next.key == "testkey-2", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].prev != nil && cache.expiredSet["testkey-2"].prev.key == "testkey-3", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
//
time.Sleep(time.Millisecond * 200)
cache.cleanExpireList()
So(len(cache.expiredSet), ShouldEqual, 2)
So(cache.expiredRoot.key == "testkey-3", ShouldBeTrue)
So(cache.expiredSet["testkey-3"].prev == nil, ShouldBeTrue)
So(cache.expiredSet["testkey-3"].next != nil && cache.expiredSet["testkey-3"].next.key == "testkey-2", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].prev != nil && cache.expiredSet["testkey-2"].prev.key == "testkey-3", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
//
time.Sleep(time.Millisecond * 200)
cache.cleanExpireList()
So(len(cache.expiredSet), ShouldEqual, 1)
So(cache.expiredRoot.key == "testkey-2", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].prev == nil, ShouldBeTrue)
So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
//
time.Sleep(time.Millisecond * 200)
cache.cleanExpireList()
So(len(cache.expiredSet), ShouldEqual, 0)
So(cache.expiredRoot == nil, ShouldBeTrue)
})
}
func TestCacheSync_Expire2(t *testing.T) {
Convey("输入了正确的参数,成功返回", t, func() {
cache := NewCacheSyncNoExpire()
cache.Set("testkey-1", 0)
cache.Set("testkey-2", 0)
cache.Expire("testkey-1", time.Millisecond*300)
cache.Expire("testkey-2", time.Millisecond*100)
So(len(cache.expiredSet), ShouldEqual, 2)
So(cache.expiredRoot.key == "testkey-2", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].prev == nil, ShouldBeTrue)
So(cache.expiredSet["testkey-2"].next != nil && cache.expiredSet["testkey-2"].next.key == "testkey-1", ShouldBeTrue)
So(cache.expiredSet["testkey-1"].prev != nil && cache.expiredSet["testkey-1"].prev.key == "testkey-2", ShouldBeTrue)
So(cache.expiredSet["testkey-1"].next == nil, ShouldBeTrue)
//
cache.Expire("testkey-2", time.Millisecond*500)
So(len(cache.expiredSet), ShouldEqual, 2)
So(cache.expiredRoot.key == "testkey-1", ShouldBeTrue)
So(cache.expiredSet["testkey-1"].prev == nil, ShouldBeTrue)
So(cache.expiredSet["testkey-1"].next != nil && cache.expiredSet["testkey-1"].next.key == "testkey-2", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].prev != nil && cache.expiredSet["testkey-2"].prev.key == "testkey-1", ShouldBeTrue)
So(cache.expiredSet["testkey-2"].next == nil, ShouldBeTrue)
})
}