-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
105 lines (100 loc) · 1.86 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
package localcache
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestGet(t *testing.T) {
c := NewLocalCache()
defer c.Stop()
_, has := c.Get("123")
if has {
t.Error("TestGet1 not exists")
}
c.Set("123", 1)
time.Sleep(1 * time.Millisecond)
_, has = c.Get("123")
if !has {
t.Error("TestGet2 not exists")
}
c.Set("123", 3)
time.Sleep(1 * time.Millisecond)
obj, _ := c.Get("123")
if obj.(int) != 3 {
t.Errorf("TestGet3 get <> 3 %+v", obj)
}
c.SetWithExpire("123", 2, 0)
time.Sleep(1 * time.Second)
obj, has = c.Get("123")
if has {
t.Errorf("TestGet4 not exists %+v", obj)
}
}
func TestGetOrLoad(t *testing.T) {
c := NewLocalCache()
defer c.Stop()
var wg sync.WaitGroup
var callCnt int32
ch := make(chan struct{})
for i := 1; i <= 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
res, err := c.GetOrLoad("k", func() (interface{}, error) {
<-ch
return atomic.AddInt32(&callCnt, 1), nil
})
if err != nil || res.(int32) != 1 {
t.Errorf("TestGetOrLoad err != nil: res=%d", res)
}
}()
}
time.AfterFunc(100*time.Millisecond, func() {
close(ch)
})
wg.Wait()
if atomic.LoadInt32(&callCnt) != 1 {
t.Error("TestGetOrLoad callcnt != 1")
}
}
func TestDel(t *testing.T) {
c := NewLocalCache()
defer c.Stop()
c.Set("123", 1)
c.Del("123")
time.Sleep(1 * time.Millisecond)
_, has := c.Get("123")
if has {
t.Error("TestDel2 not exists")
}
}
func TestLen(t *testing.T) {
c := NewLocalCache()
defer c.Stop()
l := c.Len()
if l != 0 {
t.Error("TestLen1 <> 0")
}
c.Set("123", 1)
time.Sleep(1 * time.Millisecond)
l = c.Len()
if l != 1 {
t.Error("TestLen2 <> 1")
}
}
func TestFlush(t *testing.T) {
c := NewLocalCache()
defer c.Stop()
c.Set("123", 1)
time.Sleep(1 * time.Millisecond)
l := c.Len()
if l != 1 {
t.Error("TestFlush1 <> 1")
}
c.Flush()
l = c.Len()
if l != 0 {
t.Error("TestFlush2 <> 0")
}
}