-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileCache_test.go
180 lines (147 loc) · 3.87 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package simplegocache
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"gotest.tools/assert"
)
const test = "test"
func Test_AddToCache(t *testing.T) {
//create tmp string to cache
content := "This is a cache add test"
testKey := test
prefix := test
//Create new cache
cache, err := NewFileCache("", 60, prefix)
//Check for errors when creating cache
assert.NilError(t, err)
defer cache.Close()
// Add content to cache
err = cache.AddToCache([]byte(content), testKey)
assert.NilError(t, err)
//Test content of file
tmpfnpath := cache.Files[testKey].Path
readContent, err := ioutil.ReadFile(tmpfnpath)
assert.NilError(t, err)
assert.Equal(t, string(readContent), content)
}
func Test_ReadFromCache(t *testing.T) {
//setup cache
//create tmp string to cache
content := "This is a cache read test"
testKey := test
prefix := test
//Create new cache
cache, err := NewFileCache("", 60, prefix)
//Check for errors when creating cach
assert.NilError(t, err)
defer cache.Close()
//Setup cache file
tmpfnpath := filepath.Join(cache.CacheFolder, "testfile.cache")
err = ioutil.WriteFile(tmpfnpath, []byte(content), 0666)
assert.NilError(t, err)
// Manually add file to cache
cache.Files[testKey] = File{
Path: tmpfnpath,
TTL: 60,
Created: time.Now().Unix(),
}
//Attempt to read from cache
readContent, err := cache.ReadFromCache(testKey)
assert.NilError(t, err)
assert.Equal(t, string(readContent), content)
}
func Test_DeleteFromCache(t *testing.T) {
testFiles := []File{{
Path: "/tmp/test",
TTL: 10,
Created: time.Now().Unix(),
},
{
Path: "/tmp/test2",
TTL: 20,
Created: time.Now().Unix(),
},
}
testKeys := []string{"a", "b"}
cache, err := NewFileCache("", 20, "test")
assert.NilError(t, err)
defer cache.Close()
for i, v := range testFiles {
cache.Files[testKeys[i]] = v
err := ioutil.WriteFile(cache.Files[testKeys[i]].Path, []byte("This is a test file"), 0666)
assert.NilError(t, err)
}
//Delete first entry
err = cache.DeleteFromCache(testKeys[0])
assert.NilError(t, err)
//Check that first entry is gone
_, ok := cache.Files[testKeys[0]]
assert.Assert(t, !ok)
//Verify second entry still exists
_, ok = cache.Files[testKeys[1]]
assert.Assert(t, ok)
//Cleanup
for _, v := range cache.Files {
os.Remove(v.Path)
}
}
func Test_UpdateCache(t *testing.T) {
//setup cache
//create tmp string to cache
content := "This is a cache read test"
testKey := test
prefix := test
//Create new cache
cache, err := NewFileCache("", 60, prefix)
//Check for errors when creating cache
assert.NilError(t, err)
defer cache.Close()
//Setup cache file
tmpfnpath := filepath.Join(cache.CacheFolder, "testfile.cache")
err = ioutil.WriteFile(tmpfnpath, []byte(content), 0666)
assert.NilError(t, err)
// Manually add file to cache
cache.Files[testKey] = File{
Path: tmpfnpath,
TTL: 60,
Created: time.Now().Unix(),
}
updatedContent := "This file has updated content"
err = cache.UpdateCache([]byte(updatedContent), testKey)
assert.NilError(t, err)
readContent, err := ioutil.ReadFile(cache.Files[testKey].Path)
assert.NilError(t, err)
assert.Equal(t, string(readContent), updatedContent)
}
func Test_InCache(t *testing.T) {
testFiles := []File{{
Path: "/tmp/test",
TTL: 10,
Created: time.Now().Unix(),
},
{
Path: "/tmp/test2",
TTL: 0,
Created: time.Now().Unix(),
},
}
testKeys := []string{"a", "b"}
prefix := test
cache, err := NewFileCache("", 20, prefix)
assert.NilError(t, err)
defer cache.Close()
for i, v := range testFiles {
cache.Files[testKeys[i]] = v
}
//Sleep to allow for 2nd entry to expire
time.Sleep(2000000000)
//Should exist
assert.Assert(t, cache.InCache(testKeys[0]))
//Should expire, be removed and not exist
assert.Assert(t, !cache.InCache(testKeys[1]))
//Should not exist
assert.Assert(t, !cache.InCache("IdontExist"))
}