Debian Codegraph @

file_test.go

raw
1 package fastcache
2
3 import (
4 "errors"
5 "fmt"
6 "io/ioutil"
7 "os"
8 "path/filepath"
9 "sync"
10 "testing"
11 )
12
13 func TestSaveLoadSmall(t *testing.T) {
14 tmpDir, err := ioutil.TempDir("", "test")
15 if err != nil {
16 t.Fatal(err)
17 }
18 filePath := filepath.Join(tmpDir, "TestSaveLoadSmall.fastcache")
19 defer os.RemoveAll(filePath)
20
21 c := New(1)
22 defer c.Reset()
23
24 key := []byte("foobar")
25 value := []byte("abcdef")
26 c.Set(key, value)
27 if err := c.SaveToFile(filePath); err != nil {
28 t.Fatalf("SaveToFile error: %s", err)
29 }
30
31 c1, err := LoadFromFile(filePath)
32 if err != nil {
33 t.Fatalf("LoadFromFile error: %s", err)
34 }
35 vv := c1.Get(nil, key)
36 if string(vv) != string(value) {
37 t.Fatalf("unexpected value obtained from cache; got %q; want %q", vv, value)
38 }
39
40 // Verify that key can be overwritten.
41 newValue := []byte("234fdfd")
42 c1.Set(key, newValue)
43 vv = c1.Get(nil, key)
44 if string(vv) != string(newValue) {
45 t.Fatalf("unexpected new value obtained from cache; got %q; want %q", vv, newValue)
46 }
47 }
48
49 func TestLoadFileNotExist(t *testing.T) {
50 c, err := LoadFromFile(`non-existing-file`)
51 if err == nil {
52 t.Fatalf("LoadFromFile must return error; got nil")
53 }
54 if !errors.Is(err, os.ErrNotExist) {
55 t.Fatalf("LoadFromFile must return os.ErrNotExist; got: %s", err)
56 }
57 if c != nil {
58 t.Fatalf("LoadFromFile must return nil cache")
59 }
60 }
61
62 func TestSaveLoadFile(t *testing.T) {
63 for _, concurrency := range []int{0, 1, 2, 4, 10} {
64 t.Run(fmt.Sprintf("concurrency_%d", concurrency), func(t *testing.T) {
65 testSaveLoadFile(t, concurrency)
66 })
67 }
68 }
69
70 func testSaveLoadFile(t *testing.T, concurrency int) {
71 var s Stats
72 tmpDir, err := ioutil.TempDir("", "test")
73 if err != nil {
74 t.Fatal(err)
75 }
76 filePath := filepath.Join(tmpDir, fmt.Sprintf("TestSaveLoadFile.%d.fastcache", concurrency))
77 defer os.RemoveAll(filePath)
78
79 const itemsCount = 10000
80 const maxBytes = bucketsCount * chunkSize * 2
81 c := New(maxBytes)
82 for i := range itemsCount {
83 k := []byte(fmt.Sprintf("key %d", i))
84 v := []byte(fmt.Sprintf("value %d", i))
85 c.Set(k, v)
86 vv := c.Get(nil, k)
87 if string(v) != string(vv) {
88 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
89 }
90 }
91 if concurrency == 1 {
92 if err := c.SaveToFile(filePath); err != nil {
93 t.Fatalf("SaveToFile error: %s", err)
94 }
95 } else {
96 if err := c.SaveToFileConcurrent(filePath, concurrency); err != nil {
97 t.Fatalf("SaveToFileConcurrent(%d) error: %s", concurrency, err)
98 }
99 }
100 s.Reset()
101 c.UpdateStats(&s)
102 if s.EntriesCount != itemsCount {
103 t.Fatalf("unexpected entriesCount; got %d; want %d", s.EntriesCount, itemsCount)
104 }
105 c.Reset()
106
107 // Verify LoadFromFile
108 c, err = LoadFromFile(filePath)
109 if err != nil {
110 t.Fatalf("unexpected error: %s", err)
111 }
112 s.Reset()
113 c.UpdateStats(&s)
114 if s.EntriesCount != itemsCount {
115 t.Fatalf("unexpected entriesCount; got %d; want %d", s.EntriesCount, itemsCount)
116 }
117 for i := range itemsCount {
118 k := []byte(fmt.Sprintf("key %d", i))
119 v := []byte(fmt.Sprintf("value %d", i))
120 vv := c.Get(nil, k)
121 if string(v) != string(vv) {
122 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
123 }
124 }
125 c.Reset()
126
127 // Verify LoadFromFileOrNew
128 c = LoadFromFileOrNew(filePath, maxBytes)
129 s.Reset()
130 c.UpdateStats(&s)
131 if s.EntriesCount != itemsCount {
132 t.Fatalf("unexpected entriesCount; got %d; want %d", s.EntriesCount, itemsCount)
133 }
134 for i := range itemsCount {
135 k := []byte(fmt.Sprintf("key %d", i))
136 v := []byte(fmt.Sprintf("value %d", i))
137 vv := c.Get(nil, k)
138 if string(v) != string(vv) {
139 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
140 }
141 }
142 c.Reset()
143
144 // Overwrite existing keys
145 for i := range itemsCount {
146 k := []byte(fmt.Sprintf("key %d", i))
147 v := []byte(fmt.Sprintf("value %d", i))
148 c.Set(k, v)
149 vv := c.Get(nil, k)
150 if string(v) != string(vv) {
151 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
152 }
153 }
154
155 // Add new keys
156 for i := range itemsCount {
157 k := []byte(fmt.Sprintf("new key %d", i))
158 v := []byte(fmt.Sprintf("new value %d", i))
159 c.Set(k, v)
160 vv := c.Get(nil, k)
161 if string(v) != string(vv) {
162 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
163 }
164 }
165
166 // Verify all the keys exist
167 for i := range itemsCount {
168 k := []byte(fmt.Sprintf("key %d", i))
169 v := []byte(fmt.Sprintf("value %d", i))
170 vv := c.Get(nil, k)
171 if string(v) != string(vv) {
172 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
173 }
174 k = []byte(fmt.Sprintf("new key %d", i))
175 v = []byte(fmt.Sprintf("new value %d", i))
176 vv = c.Get(nil, k)
177 if string(v) != string(vv) {
178 t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
179 }
180 }
181
182 // Verify incorrect maxBytes passed to LoadFromFileOrNew
183 c = LoadFromFileOrNew(filePath, maxBytes*10)
184 s.Reset()
185 c.UpdateStats(&s)
186 if s.EntriesCount != 0 {
187 t.Fatalf("unexpected non-zero entriesCount; got %d", s.EntriesCount)
188 }
189 c.Reset()
190 }
191
192 func TestSaveLoadConcurrent(t *testing.T) {
193 c := New(1024)
194 defer c.Reset()
195 c.Set([]byte("foo"), []byte("bar"))
196
197 stopCh := make(chan struct{})
198
199 // Start concurrent workers that run Get and Set on c.
200 var wgWorkers sync.WaitGroup
201 for range 5 {
202 wgWorkers.Add(1)
203 go func() {
204 defer wgWorkers.Done()
205 var buf []byte
206 j := 0
207 for {
208 k := []byte(fmt.Sprintf("key %d", j))
209 v := []byte(fmt.Sprintf("value %d", j))
210 c.Set(k, v)
211 buf = c.Get(buf[:0], k)
212 if string(buf) != string(v) {
213 panic(fmt.Errorf("unexpected value for key %q; got %q; want %q", k, buf, v))
214 }
215 j++
216 select {
217 case <-stopCh:
218 return
219 default:
220 }
221 }
222 }()
223 }
224
225 // Start concurrent SaveToFile and LoadFromFile calls.
226 tmpDir, err := ioutil.TempDir("", "test")
227 if err != nil {
228 t.Fatal(err)
229 }
230 defer os.RemoveAll(tmpDir)
231
232 var wgSavers sync.WaitGroup
233 for i := range 4 {
234 wgSavers.Add(1)
235 filePath := filepath.Join(tmpDir, fmt.Sprintf("TestSaveLoadFile.%d.fastcache", i))
236 go func() {
237 defer wgSavers.Done()
238 defer os.RemoveAll(filePath)
239 for range 3 {
240 if err := c.SaveToFileConcurrent(filePath, 3); err != nil {
241 panic(fmt.Errorf("cannot save cache to %q: %s", filePath, err))
242 }
243 cc, err := LoadFromFile(filePath)
244 if err != nil {
245 panic(fmt.Errorf("cannot load cache from %q: %s", filePath, err))
246 }
247 var s Stats
248 cc.UpdateStats(&s)
249 if s.EntriesCount == 0 {
250 panic(fmt.Errorf("unexpected empty cache loaded from %q", filePath))
251 }
252 cc.Reset()
253 }
254 }()
255 }
256
257 wgSavers.Wait()
258
259 close(stopCh)
260 wgWorkers.Wait()
261 }