forked from leslie-fei/fastcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
175 lines (150 loc) · 3.61 KB
/
cache_test.go
File metadata and controls
175 lines (150 loc) · 3.61 KB
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
package fastcache
import (
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"testing"
)
func TestCache(t *testing.T) {
c, err := NewCache(64*MB, &Config{
MemoryType: GO,
})
if err != nil {
panic(err)
}
var wg sync.WaitGroup
n := 1024
wg.Add(n)
for i := 0; i < n; i++ {
i := i
go func() {
defer wg.Done()
key := []byte(fmt.Sprintf("key_%d", i))
value := key
err = c.Set(key, value)
if err != nil {
panic(err)
}
exists := c.Has(key)
if !exists {
panic("key must exists")
}
v, err := c.Get(key)
if err != nil {
panic(err)
}
if string(v) != string(key) {
panic(fmt.Errorf("get key: %s value: %s not equals", key, v))
}
err = c.Delete(key)
if err != nil {
panic(err)
}
_, err = c.Get(key)
if err == nil || !errors.Is(err, ErrNotFound) {
panic("expect ErrNotFound")
}
}()
}
wg.Wait()
}
// TestMmapPersistence verifies that data written to an MMAP cache
// survives close/reopen cycles. This tests the Sync() functionality
// added to ensure durability.
func TestMmapPersistence(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "fastcache_test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
dbPath := filepath.Join(tmpDir, "test_mmap_db")
// Create cache and write data
cache1, err := NewCache(64*MB, &Config{
MemoryType: MMAP,
MemoryKey: dbPath,
})
if err != nil {
t.Fatalf("failed to create cache: %v", err)
}
// Write test data
testData := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "a longer value with more data",
}
for k, v := range testData {
if err := cache1.Set([]byte(k), []byte(v)); err != nil {
t.Fatalf("failed to set key %s: %v", k, err)
}
}
// Close the cache - this should sync to disk
if err := cache1.Close(); err != nil {
t.Fatalf("failed to close cache: %v", err)
}
// Reopen the cache
cache2, err := NewCache(64*MB, &Config{
MemoryType: MMAP,
MemoryKey: dbPath,
})
if err != nil {
t.Fatalf("failed to reopen cache: %v", err)
}
defer cache2.Close()
// Verify all data survived
for k, expectedV := range testData {
gotV, err := cache2.Get([]byte(k))
if err != nil {
t.Errorf("failed to get key %s after reopen: %v", k, err)
continue
}
if string(gotV) != expectedV {
t.Errorf("key %s: got %q, want %q", k, string(gotV), expectedV)
}
}
}
// TestSyncExplicit verifies that Sync() can be called explicitly
// and data is persisted even without calling Close().
func TestSyncExplicit(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "fastcache_sync_test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
dbPath := filepath.Join(tmpDir, "test_sync_db")
cache1, err := NewCache(64*MB, &Config{
MemoryType: MMAP,
MemoryKey: dbPath,
})
if err != nil {
t.Fatalf("failed to create cache: %v", err)
}
// Write and sync without close
key := []byte("sync_test_key")
value := []byte("sync_test_value")
if err := cache1.Set(key, value); err != nil {
t.Fatalf("failed to set: %v", err)
}
// Explicit sync
if err := cache1.Sync(); err != nil {
t.Fatalf("failed to sync: %v", err)
}
// Reopen without calling Close() on first cache (simulates crash)
cache2, err := NewCache(64*MB, &Config{
MemoryType: MMAP,
MemoryKey: dbPath,
})
if err != nil {
t.Fatalf("failed to reopen cache: %v", err)
}
defer cache2.Close()
// Verify data survived
gotV, err := cache2.Get(key)
if err != nil {
t.Fatalf("failed to get key after sync+reopen: %v", err)
}
if string(gotV) != string(value) {
t.Errorf("got %q, want %q", string(gotV), string(value))
}
}