-
Notifications
You must be signed in to change notification settings - Fork 37
/
cache_test.go
180 lines (142 loc) · 5.28 KB
/
cache_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (c) 2012 The Goon Authors
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package goon
import (
"bytes"
"reflect"
"testing"
"unsafe"
)
func TestCacheBasics(t *testing.T) {
items := []*cacheItem{}
items = append(items, &cacheItem{key: "foo", value: []byte{1, 2, 3}})
items = append(items, &cacheItem{key: "bar", value: []byte{4, 5, 6}})
keys := make([]string, 0, len(items))
for _, item := range items {
keys = append(keys, item.key)
}
c := newCache(defaultCacheLimit)
for i := range items {
if v := c.Get(items[i].key); v != nil {
t.Fatalf("Expected nil for items[%d] but got %v", i, v)
}
c.Set(items[i])
if v := c.Get(items[i].key); !bytes.Equal(v, items[i].value) {
t.Fatalf("Invalid bytes for items[%d]! %x vs %x", i, v, items[i].value)
}
c.Delete(items[i].key)
if v := c.Get(items[i].key); v != nil {
t.Fatalf("Expected nil for items[%d] but got %v", i, v)
}
if c.size != 0 {
t.Fatalf("Expected size to be zero, but got %v", c.size)
}
}
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{nil, nil}) {
t.Fatalf("Expected nils but got %+v", vs)
}
c.SetMulti(items)
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{items[0].value, items[1].value}) {
t.Fatalf("Invalid bytes for items! %+v", vs)
}
c.DeleteMulti(keys)
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{nil, nil}) {
t.Fatalf("Expected nils but got %+v", vs)
}
if c.size != 0 {
t.Fatalf("Expected size to be zero, but got %v", c.size)
}
c.Set(items[0])
c.Flush()
if v := c.Get(items[0].key); v != nil {
t.Fatalf("Expected nil after flush but got %v", v)
}
c.Set(items[0])
c.Set(&cacheItem{key: items[0].key, value: []byte{7, 7, 7}})
if v := c.Get(items[0].key); !bytes.Equal(v, []byte{7, 7, 7}) {
t.Fatalf("Invalid bytes for value change! Got %x", v)
}
}
func TestCacheKeyLeak(t *testing.T) {
ak, bk := string([]byte{'f', 'o', 'o'}), string([]byte{'f', 'o', 'o'})
av, bv := []byte{1, 2, 3}, []byte{4, 5, 6}
c := newCache(defaultCacheLimit)
// Set the original value
c.Set(&cacheItem{key: ak, value: av})
if v := c.Get(ak); !bytes.Equal(v, av) {
t.Fatalf("Invalid bytes! %v", v)
}
// Rewrite it with a different value, and also a different key but same key contents
c.Set(&cacheItem{key: bk, value: bv})
if v := c.Get(bk); !bytes.Equal(v, bv) {
t.Fatalf("Invalid bytes! %v", v)
}
// Modify the new key contents without changing the pointer
*(*byte)(unsafe.Pointer(*(*uintptr)(unsafe.Pointer(&bk)))) = 'g'
if bk != "goo" {
t.Fatalf("Expected key to be 'goo' but it's %v", bk)
}
// Make sure that we can no longer retrieve the value with the new key,
// as that will only be possible via pointer equality, which means that
// the cache is still holding on to the new key, which doubles key storage
if v := c.Get(bk); v != nil {
t.Fatalf("Cache is leaking memory by keeping around the new key pointer! %v", v)
}
// Also make sure that we can retrieve the correct new value
// by using an unrelated key pointer that just matches the key contents
if v := c.Get("foo"); !bytes.Equal(v, bv) {
t.Fatalf("Invalid bytes! %v", v)
}
// Inspect the internals of the cache too, which contains only a single entry with ak as key
keyAddr := *(*uintptr)(unsafe.Pointer(&ak))
for key, elem := range c.elements {
if ka := *(*uintptr)(unsafe.Pointer(&key)); ka != keyAddr {
t.Fatalf("map key has wrong pointer! %x vs %x", ka, keyAddr)
}
if ka := *(*uintptr)(unsafe.Pointer(&(elem.Value.(*cacheItem)).key)); ka != keyAddr {
t.Fatalf("element key has wrong pointer! %x vs %x", ka, keyAddr)
}
}
}
func TestCacheLimit(t *testing.T) {
c := newCache(defaultCacheLimit)
items := []*cacheItem{}
items = append(items, &cacheItem{key: "foo", value: []byte{1, 2, 3}})
items = append(items, &cacheItem{key: "bar", value: []byte{4, 5, 6}})
keys := make([]string, 0, len(items))
for _, item := range items {
keys = append(keys, item.key)
}
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{nil, nil}) {
t.Fatalf("Expected nils but got %+v", vs)
}
c.SetMulti(items)
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{items[0].value, items[1].value}) {
t.Fatalf("Invalid bytes for items! %+v", vs)
}
c.setLimit(0)
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{nil, nil}) {
t.Fatalf("Expected nils but got %+v", vs)
}
if c.size != 0 {
t.Fatalf("Expected size to be zero, but got %v", c.size)
}
c.setLimit(cachedValueOverhead + len(items[1].key) + cap(items[1].value))
c.SetMulti(items)
if vs := c.GetMulti(keys); !reflect.DeepEqual(vs, [][]byte{nil, items[1].value}) {
t.Fatalf("Invalid bytes for items! %+v", vs)
}
}