-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
108 lines (86 loc) · 2.21 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
package cache
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack"
)
type cacheTest struct {
n string
}
func (ct *cacheTest) Get(ctx context.Context, key string) string {
return ct.n
}
func (ct *cacheTest) Set(ctx context.Context, key string, value interface{}, ex time.Duration) error {
ct.n = "123"
return nil
}
func (ct *cacheTest) Del(ctx context.Context, key string) error {
ct.n = ""
return nil
}
func getData() (data int64, err error) {
time.Sleep(time.Second)
return time.Now().UnixNano(), nil
}
func getZeroData() (data string, err error) {
return "", nil
}
func TestFetch(t *testing.T) {
client := &cacheTest{}
ctx := context.TODO()
cache := NewCache(client, "test")
// 模拟 10 个并发
var results []int64
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
time.Sleep(time.Microsecond)
go func() {
defer wg.Done()
var result int64
ok, err := cache.Fetch(ctx, "test", &result, time.Minute, func() (rawResult interface{}, err error) {
return getData()
})
assert.NoError(t, err)
assert.Equal(t, false, ok)
results = append(results, result)
}()
}
wg.Wait()
assert.Equal(t, results[0], results[9])
result := cache.cacheClient.Get(ctx, "test")
assert.Equal(t, "123", result)
}
type cacheIgnoreZero struct {
n []byte
}
func (ct *cacheIgnoreZero) Get(ctx context.Context, key string) string {
return string(ct.n)
}
func (ct *cacheIgnoreZero) Set(ctx context.Context, key string, value interface{}, ex time.Duration) error {
ct.n, _ = msgpack.Marshal("")
return nil
}
func (ct *cacheIgnoreZero) Del(ctx context.Context, key string) error {
ct.n = []byte("")
return nil
}
func TestFetchIgnoreZero(t *testing.T) {
client := &cacheIgnoreZero{}
ctx := context.TODO()
cache := NewCache(client, "test")
var result string
ok, err := cache.FetchIgnoreZero(ctx, "test", &result, time.Minute, func() (rawResult interface{}, err error) {
return getZeroData()
})
assert.NoError(t, err)
assert.Equal(t, false, ok)
ok, err = cache.FetchIgnoreZero(ctx, "test", &result, time.Minute, func() (rawResult interface{}, err error) {
return getZeroData()
})
assert.NoError(t, err)
assert.Equal(t, false, ok)
}