-
Notifications
You must be signed in to change notification settings - Fork 0
/
filecache_test.go
125 lines (94 loc) · 2.52 KB
/
filecache_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package filecache_test
import (
"errors"
"testing"
"time"
"github.com/0xc000022070/filecache"
)
func TestBasic(t *testing.T) {
cache := filecache.New("basic-test",
filecache.WithMaxItems(10),
filecache.WithTTL(time.Second*3),
filecache.WithCheckInterval(time.Second),
)
defer cache.Destroy()
type BasicTestData struct {
Why string
}
data := BasicTestData{
Why: "I'm a life",
}
const key = "internal/basic-test.gob"
if err := filecache.SetEncoded(cache, key, data); err != nil {
t.Fatal(err)
}
ticker := time.NewTicker(time.Second)
ticks := 0
t.Log("The cache data should be removed after 3 seconds")
for range ticker.C {
if ticks >= 5 {
t.Log("The cache data wasn't removed")
t.Fail()
break
}
ticks++
t.Logf("tick n° %d", ticks)
data, err := filecache.GetDecoded[BasicTestData](cache, key)
if err != nil {
if errors.Is(err, filecache.ErrNotFound) {
if ticks < 3 {
t.Log("the cache data was removed but that was not expected")
t.Fail()
} else {
t.Log("as expected, the cache data was removed")
}
break
}
}
t.Log("stored data is", data)
}
}
func TestDestroy(t *testing.T) {
t.Parallel()
cache := filecache.New("destroy-test")
defer cache.Destroy()
if err := cache.Set("a-misty-value", []byte("I'm a misty value")); err != nil {
t.Errorf("failed to set a value in file-based cache: %v", err)
t.FailNow()
}
if err := cache.Destroy(); err != nil {
t.Errorf("failed to destroy file-based cache: %v", err)
t.FailNow()
}
cache = filecache.New("destroy-test")
data, err := cache.Get("a-misty-value")
if err != nil {
if errors.Is(err, filecache.ErrNotFound) {
t.Log("as expected, the value was removed from the cache")
return
}
t.Errorf("failed to retrieve the value from the cache: %v", err)
t.FailNow()
}
t.Errorf("failed to remove the value from the cache: %v", err)
t.Logf("a-misty-value: %s", data)
t.Fail()
}
func TestShutdown(t *testing.T) {
t.Parallel()
cache := filecache.New("shutdown-test")
if err := cache.Set("extraordinary", []byte("the extraordinary value")); err != nil {
t.Errorf("failed to set a value in file-based cache: %v", err)
t.FailNow()
}
cache.Shutdown()
cache = filecache.New("shutdown-test")
defer cache.Destroy()
data, err := cache.Get("extraordinary")
if err != nil {
t.Errorf("failed to get the value from the cache when it was suposed to be in file-system: %v", err)
t.FailNow()
}
t.Log("as expected, the value was removed from the cache")
t.Logf("extraordinary: %s", data)
}