forked from traefik/plugin-simplecache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
158 lines (119 loc) · 2.79 KB
/
file_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
package plugin_simplecache
import (
"bytes"
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
)
const testCacheKey = "GETlocalhost:8080/test/path"
func TestFileCache(t *testing.T) {
dir := createTempDir(t)
fc, err := newFileCache(dir, time.Second)
if err != nil {
t.Errorf("unexpected newFileCache error: %v", err)
}
_, err = fc.Get(testCacheKey)
if err == nil {
t.Error("unexpected cache content")
}
cacheContent := []byte("some random cache content that should be exact")
err = fc.Set(testCacheKey, cacheContent, time.Second)
if err != nil {
t.Errorf("unexpected cache set error: %v", err)
}
got, err := fc.Get(testCacheKey)
if err != nil {
t.Errorf("unexpected cache get error: %v", err)
}
if !bytes.Equal(got, cacheContent) {
t.Errorf("unexpected cache content: want %s, got %s", cacheContent, got)
}
}
func TestFileCache_ConcurrentAccess(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
defer func() {
if r := recover(); r != nil {
t.Fatal(r)
}
}()
dir := createTempDir(t)
fc, err := newFileCache(dir, time.Second)
if err != nil {
t.Errorf("unexpected newFileCache error: %v", err)
}
cacheContent := []byte("some random cache content that should be exact")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for {
got, _ := fc.Get(testCacheKey)
if got != nil && !bytes.Equal(got, cacheContent) {
panic(fmt.Errorf("unexpected cache content: want %s, got %s", cacheContent, got))
}
select {
case <-ctx.Done():
return
default:
}
}
}()
go func() {
defer wg.Done()
for {
err = fc.Set(testCacheKey, cacheContent, time.Second)
if err != nil {
panic(fmt.Errorf("unexpected cache set error: %w", err))
}
select {
case <-ctx.Done():
return
default:
}
}
}()
wg.Wait()
}
func TestPathMutex(t *testing.T) {
pm := &pathMutex{lock: map[string]*fileLock{}}
mu := pm.MutexAt("sometestpath")
mu.Lock()
var (
wg sync.WaitGroup
locked uint32
)
wg.Add(1)
go func() {
defer wg.Done()
mu := pm.MutexAt("sometestpath")
mu.Lock()
defer mu.Unlock()
atomic.AddUint32(&locked, 1)
}()
// locked should be 0 as we already have a lock on the path.
if atomic.LoadUint32(&locked) != 0 {
t.Error("unexpected second lock")
}
mu.Unlock()
wg.Wait()
if l := len(pm.lock); l > 0 {
t.Errorf("unexpected lock length: want 0, got %d", l)
}
}
func BenchmarkFileCache_Get(b *testing.B) {
dir := createTempDir(b)
fc, err := newFileCache(dir, time.Minute)
if err != nil {
b.Errorf("unexpected newFileCache error: %v", err)
}
_ = fc.Set(testCacheKey, []byte("some random cache content that should be exact"), time.Minute)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = fc.Get(testCacheKey)
}
}