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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package fastcache
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"testing"
)
func TestSaveLoadSmall(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
}
filePath := filepath.Join(tmpDir, "TestSaveLoadSmall.fastcache")
defer os.RemoveAll(filePath)
c := New(1)
defer c.Reset()
key := []byte("foobar")
value := []byte("abcdef")
c.Set(key, value)
if err := c.SaveToFile(filePath); err != nil {
t.Fatalf("SaveToFile error: %s", err)
}
c1, err := LoadFromFile(filePath)
if err != nil {
t.Fatalf("LoadFromFile error: %s", err)
}
vv := c1.Get(nil, key)
if string(vv) != string(value) {
t.Fatalf("unexpected value obtained from cache; got %q; want %q", vv, value)
}
// Verify that key can be overwritten.
newValue := []byte("234fdfd")
c1.Set(key, newValue)
vv = c1.Get(nil, key)
if string(vv) != string(newValue) {
t.Fatalf("unexpected new value obtained from cache; got %q; want %q", vv, newValue)
}
}
func TestSaveLoadFile(t *testing.T) {
for _, concurrency := range []int{0, 1, 2, 4, 10} {
t.Run(fmt.Sprintf("concurrency_%d", concurrency), func(t *testing.T) {
testSaveLoadFile(t, concurrency)
})
}
}
func testSaveLoadFile(t *testing.T, concurrency int) {
var s Stats
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
}
filePath := filepath.Join(tmpDir, fmt.Sprintf("TestSaveLoadFile.%d.fastcache", concurrency))
defer os.RemoveAll(filePath)
const itemsCount = 10000
const maxBytes = bucketsCount * chunkSize * 2
c := New(maxBytes)
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
c.Set(k, v)
vv := c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
}
if concurrency == 1 {
if err := c.SaveToFile(filePath); err != nil {
t.Fatalf("SaveToFile error: %s", err)
}
} else {
if err := c.SaveToFileConcurrent(filePath, concurrency); err != nil {
t.Fatalf("SaveToFileConcurrent(%d) error: %s", concurrency, err)
}
}
s.Reset()
c.UpdateStats(&s)
if s.EntriesCount != itemsCount {
t.Fatalf("unexpected entriesCount; got %d; want %d", s.EntriesCount, itemsCount)
}
c.Reset()
// Verify LoadFromFile
c, err = LoadFromFile(filePath)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
s.Reset()
c.UpdateStats(&s)
if s.EntriesCount != itemsCount {
t.Fatalf("unexpected entriesCount; got %d; want %d", s.EntriesCount, itemsCount)
}
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
vv := c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
}
c.Reset()
// Verify LoadFromFileOrNew
c = LoadFromFileOrNew(filePath, maxBytes)
s.Reset()
c.UpdateStats(&s)
if s.EntriesCount != itemsCount {
t.Fatalf("unexpected entriesCount; got %d; want %d", s.EntriesCount, itemsCount)
}
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
vv := c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
}
c.Reset()
// Overwrite existing keys
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
c.Set(k, v)
vv := c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
}
// Add new keys
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("new key %d", i))
v := []byte(fmt.Sprintf("new value %d", i))
c.Set(k, v)
vv := c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
}
// Verify all the keys exist
for i := 0; i < itemsCount; i++ {
k := []byte(fmt.Sprintf("key %d", i))
v := []byte(fmt.Sprintf("value %d", i))
vv := c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
k = []byte(fmt.Sprintf("new key %d", i))
v = []byte(fmt.Sprintf("new value %d", i))
vv = c.Get(nil, k)
if string(v) != string(vv) {
t.Fatalf("unexpected cache value for k=%q; got %q; want %q; bucket[0]=%#v", k, vv, v, &c.buckets[0])
}
}
// Verify incorrect maxBytes passed to LoadFromFileOrNew
c = LoadFromFileOrNew(filePath, maxBytes*10)
s.Reset()
c.UpdateStats(&s)
if s.EntriesCount != 0 {
t.Fatalf("unexpected non-zero entriesCount; got %d", s.EntriesCount)
}
c.Reset()
}
func TestSaveLoadConcurrent(t *testing.T) {
c := New(1024)
defer c.Reset()
c.Set([]byte("foo"), []byte("bar"))
stopCh := make(chan struct{})
// Start concurrent workers that run Get and Set on c.
var wgWorkers sync.WaitGroup
for i := 0; i < 5; i++ {
wgWorkers.Add(1)
go func() {
defer wgWorkers.Done()
var buf []byte
j := 0
for {
k := []byte(fmt.Sprintf("key %d", j))
v := []byte(fmt.Sprintf("value %d", j))
c.Set(k, v)
buf = c.Get(buf[:0], k)
if string(buf) != string(v) {
panic(fmt.Errorf("unexpected value for key %q; got %q; want %q", k, buf, v))
}
j++
select {
case <-stopCh:
return
default:
}
}
}()
}
// Start concurrent SaveToFile and LoadFromFile calls.
tmpDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
var wgSavers sync.WaitGroup
for i := 0; i < 4; i++ {
wgSavers.Add(1)
filePath := filepath.Join(tmpDir, fmt.Sprintf("TestSaveLoadFile.%d.fastcache", i))
go func() {
defer wgSavers.Done()
defer os.RemoveAll(filePath)
for j := 0; j < 3; j++ {
if err := c.SaveToFileConcurrent(filePath, 3); err != nil {
panic(fmt.Errorf("cannot save cache to %q: %s", filePath, err))
}
cc, err := LoadFromFile(filePath)
if err != nil {
panic(fmt.Errorf("cannot load cache from %q: %s", filePath, err))
}
var s Stats
cc.UpdateStats(&s)
if s.EntriesCount == 0 {
panic(fmt.Errorf("unexpected empty cache loaded from %q", filePath))
}
cc.Reset()
}
}()
}
wgSavers.Wait()
close(stopCh)
wgWorkers.Wait()
}
|