/
cache_test.go
72 lines (67 loc) · 1.24 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
package dingtalk
import (
"context"
"testing"
"time"
)
func TestCache(t *testing.T) {
now := time.Now()
cases := []struct {
e *KVExpirable
hasErr bool
}{
{
e: &KVExpirable{
Key: "key1",
Value: "token",
ExpiresIn: 100,
Created: now.Unix(),
},
},
{
e: &KVExpirable{
Key: "key2",
Value: "token",
ExpiresIn: 100,
Created: now.Unix(),
},
},
{
e: &KVExpirable{
Key: "key1",
Value: "token",
ExpiresIn: 10,
Created: now.Unix(),
},
hasErr: true,
},
{
e: &KVExpirable{
Key: "key2",
Value: "token",
ExpiresIn: 10,
Created: now.Unix(),
},
hasErr: true,
},
}
ctx := context.Background()
cache := NewFileCache(".mock_cache_file")
for _, c := range cases {
if err := cache.Set(ctx, c.e); err != nil {
t.Fatalf("set cache %v", err)
}
n, err := cache.Get(ctx, c.e.Key)
hasErr := err != nil
if c.hasErr != hasErr {
t.Fatalf("get cache got %v, expected %v", hasErr, c.hasErr)
}
if err != nil && c.e.Value != n.Value {
t.Fatalf("get cache got %v, expected %v", n.Value, c.e.Value)
}
}
_, err := cache.Get(ctx, "nokey")
if err == nil {
t.Fatal("should be get an error")
}
}