This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
cache_wrapper_test.go
102 lines (83 loc) · 2.76 KB
/
cache_wrapper_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
package storage_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/Shopify/go-storage"
"github.com/Shopify/go-storage/internal/testutils"
)
func withCache(options *storage.CacheOptions, cb func(fs storage.FS, src storage.FS, cache storage.FS)) {
withLocal(func(local storage.FS) {
withMem(func(mem storage.FS) {
fs := storage.NewCacheWrapper(local, mem, options)
cb(fs, local, mem)
})
})
}
func withFileCache(options *storage.CacheOptions, cb func(fs storage.FS, src storage.FS, cache storage.FS)) {
withLocal(func(local storage.FS) {
withLocal(func(localCache storage.FS) {
fs := storage.NewCacheWrapper(local, localCache, options)
cb(fs, local, localCache)
})
})
}
func TestCacheWrapper_Open(t *testing.T) {
withCache(nil, func(fs storage.FS, _ storage.FS, _ storage.FS) {
testutils.OpenNotExists(t, fs, "foo")
})
}
func TestCacheWrapper_Create(t *testing.T) {
withCache(nil, func(fs storage.FS, src storage.FS, cache storage.FS) {
testutils.Create(t, fs, "foo", "")
testutils.OpenExists(t, src, "foo", "")
testutils.OpenExists(t, cache, "foo", "")
testutils.Create(t, fs, "foo", "bar")
testutils.OpenExists(t, src, "foo", "bar")
testutils.OpenExists(t, cache, "foo", "bar")
})
}
func TestCacheWrapper_Create_fileCache(t *testing.T) {
withFileCache(nil, func(fs storage.FS, src storage.FS, cache storage.FS) {
testutils.Create(t, fs, "foo", "")
testutils.OpenExists(t, src, "foo", "")
testutils.OpenExists(t, cache, "foo", "")
testutils.Create(t, fs, "foo", "bar")
testutils.OpenExists(t, src, "foo", "bar")
testutils.OpenExists(t, cache, "foo", "bar")
})
}
func TestCacheWrapper_Delete(t *testing.T) {
withCache(nil, func(fs storage.FS, _ storage.FS, _ storage.FS) {
testutils.Delete(t, fs, "foo")
})
}
func TestCacheWrapper_CacheOptions_MaxAge(t *testing.T) {
options := &storage.CacheOptions{
MaxAge: 500 * time.Millisecond,
}
withCache(options, func(fs storage.FS, _ storage.FS, _ storage.FS) {
testutils.Create(t, fs, "foo", "")
ctx := context.Background()
f, err := fs.Open(ctx, "foo", nil)
assert.NoError(t, err)
assert.NotZero(t, f)
assert.NotZero(t, f.CreationTime)
assert.True(t, time.Since(f.CreationTime) < options.MaxAge, "file should not be expired")
<-time.After(options.MaxAge)
f2, err := fs.Open(ctx, "foo", nil)
assert.NoError(t, err)
assert.NotZero(t, f2.CreationTime)
assert.True(t, f2.CreationTime.After(f.CreationTime)) // New cache
})
}
func TestCacheWrapper_CacheOptions_NoData(t *testing.T) {
options := &storage.CacheOptions{
NoData: true,
}
withCache(options, func(fs storage.FS, _ storage.FS, cache storage.FS) {
testutils.Create(t, fs, "foo", "bar")
testutils.OpenExists(t, cache, "foo", "") // No content actually stored
})
}