-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
105 lines (94 loc) · 2.44 KB
/
cache_test.go
File metadata and controls
105 lines (94 loc) · 2.44 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
package cbytecache
import (
"bytes"
"testing"
"time"
"github.com/koykov/byteconv"
"github.com/koykov/hash/fnv"
)
func TestIO(t *testing.T) {
testIO := func(t *testing.T, entries int, verbose bool) {
conf := DefaultConfig(time.Minute, &fnv.Hasher{}, 0)
cache, err := New(conf)
if err != nil {
t.Fatal(err)
}
var (
key, dst []byte
w, wf, r, rf, r404 int
)
for i := 0; i < entries; i++ {
w++
key = makeKey(key, i)
if err := cache.Set(byteconv.B2S(key), getEntryBody(i)); err != nil {
wf++
t.Error(err)
}
}
for i := 0; i < entries; i++ {
r++
key = makeKey(key, i)
if dst, err = cache.GetTo(dst[:0], byteconv.B2S(key)); err != nil {
rf++
r404++
if err != ErrNotFound {
r404--
t.Error(err)
}
continue
}
assertBytes(t, getEntryBody(i), dst)
}
if verbose {
t.Logf("write: %d\nwrite fail: %d\nread: %d\nread fail: %d\nread 404: %d", w, wf, r, rf, r404)
}
if err = cache.Close(); err != nil {
t.Error(err)
}
}
verbose := false
t.Run("1", func(t *testing.T) { testIO(t, 1, verbose) })
t.Run("10", func(t *testing.T) { testIO(t, 10, verbose) })
t.Run("100", func(t *testing.T) { testIO(t, 100, verbose) })
t.Run("1K", func(t *testing.T) { testIO(t, 1000, verbose) })
t.Run("10K", func(t *testing.T) { testIO(t, 10000, verbose) })
t.Run("100K", func(t *testing.T) { testIO(t, 100000, verbose) })
t.Run("1M", func(t *testing.T) { testIO(t, 1000000, verbose) })
}
func TestDelete(t *testing.T) {
conf := DefaultConfig(time.Minute, &fnv.Hasher{}, 0)
cache, err := New(conf)
if err != nil {
t.Fatal(err)
}
if err = cache.Set("foobar", getEntryBody(0)); err != nil {
t.Fatal(err)
}
if err = cache.Delete("foobar"); err != nil {
t.Fatal(err)
}
if _, err = cache.Get("foobar"); err != ErrNotFound {
t.Errorf("error mismatch: need '%s', got '%s'", ErrNotFound.Error(), err.Error())
}
}
func TestExtract(t *testing.T) {
conf := DefaultConfig(time.Minute, &fnv.Hasher{}, 0)
cache, err := New(conf)
if err != nil {
t.Fatal(err)
}
body := getEntryBody(0)
if err = cache.Set("foobar", body); err != nil {
t.Fatal(err)
}
var b []byte
if b, err = cache.Extract("foobar"); err != nil {
t.Fatal(err)
}
if !bytes.Equal(b, body) {
t.Errorf("entry mismatch: need '%s', got '%s'", string(body), string(b))
}
if _, err = cache.Get("foobar"); err != ErrNotFound {
t.Errorf("error mismatch: need '%s', got '%s'", ErrNotFound.Error(), err.Error())
}
}