/
lrucache_test.go
181 lines (162 loc) · 4.82 KB
/
lrucache_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
181
package lrucache
import (
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/viki-org/gspec"
)
func TestReturnsNilIfPrimaryKeyIsNotInTheCache(t *testing.T) {
c := New(Configure())
if c.Get("leto", "") != nil {
t.Error("expecting nothing to be in the cache")
}
}
func TestReturnsNilIfSecondaryKeyIsNotInTheCache(t *testing.T) {
c := New(Configure())
c.Set("leto", "ghanima", NewItem("SAMPLE BODY FOR TESTING"))
if c.Get("leto", "duncan") != nil {
t.Error("expecting nothing to be in the cache")
}
}
func TestGetReturnsTheItem(t *testing.T) {
spec := gspec.New(t)
c := New(Configure())
item := NewItem("SAMPLE BODY FOR TESTING")
c.Set("the-p", "the-s", item)
spec.Expect(c.Get("the-p", "the-s")).ToEqual(item)
}
func TestGetReturnsTheItemWithEmptySecondaryKey(t *testing.T) {
spec := gspec.New(t)
c := New(Configure())
item := NewItem("SAMPLE BODY FOR TESTING")
c.Set("the-p", "", item)
spec.Expect(c.Get("the-p", "")).ToEqual(item)
}
func TestGetPromotesTheItemToFrontOfTheCache(t *testing.T) {
spec := gspec.New(t)
c := New(Configure())
item1 := NewItem("SAMPLE BODY FOR TESTING")
item2 := NewItem("SAMPLE BODY FOR TESTING")
c.Set("a", "1", item1)
c.Set("b", "1", item2)
c.Get("a", "1")
spec.Expect(c.list.head.item).ToEqual(item1)
}
// ignore, we no longer prune on set, we now prune in a separate timed goroutine
// func TestPrunesWhenAtCapacity(t *testing.T) {
// spec := gspec.New(t)
// c := New(5550 + 5550 * int(ITEM_OVERHEAD), false)
// for i := 0; i < 5550; i++ {
// c.Set("a", strconv.Itoa(i), &CachedResponse{length: 1})
// }
// spec.Expect(len(c.groups)).ToEqual(1)
// count := 0
// for node := c.list.head; node != nil; node = node.next { count++ }
// spec.Expect(count).ToEqual(5550)
// c.Set("x", "xx", &CachedResponse{length: 1})
// time.Sleep(100)
// spec.Expect(len(c.groups)).ToEqual(2)
// count = 0
// for node := c.list.head; node != nil; node = node.next { count++ }
// spec.Expect(count).ToEqual(551)
// spec.Expect(c.list.tail.group.key).ToEqual("a")
// spec.Expect(c.list.tail.key).ToEqual("5000")
// }
func TestRemovesAllSecondaryItemsFromTheCache(t *testing.T) {
spec := gspec.New(t)
c := New(Configure().Size(550))
item1 := NewItem("SAMPLE BODY FOR TESTING")
c.Set("a", "1", item1)
c.Set("b", "2", NewItem("SAMPLE BODY FOR TESTING"))
c.Set("b", "3", NewItem("SAMPLE BODY FOR TESTING"))
c.Remove("b")
spec.Expect(c.Get("a", "1")).ToEqual(item1)
spec.Expect(len(c.groups)).ToEqual(1) // b has to be removed since there'2 only 1 item, and we know, from the above, that it's a
}
func TestHandlesRemovalOfAnInvalidPrimaryKey(t *testing.T) {
spec := gspec.New(t)
c := New(Configure().Size(550))
item1 := NewItem("SAMPLE BODY FOR TESTING")
c.Set("a", "1", item1)
c.Remove("b")
spec.Expect(c.Get("a", "1")).ToEqual(item1)
spec.Expect(len(c.groups)).ToEqual(1) // b has to be removed since there'2 only 1 item, and we know, from the above, that it's a
}
func TestRemovesAnIndividualSecondaryItemFromTheCache(t *testing.T) {
spec := gspec.New(t)
c := New(Configure().Size(550))
item1 := NewItem("SAMPLE BODY FOR TESTING")
item2 := NewItem("SAMPLE BODY FOR TESTING")
c.Set("a", "1", item1)
c.Set("b", "2", item2)
c.Set("b", "3", NewItem("SAMPLE BODY FOR TESTING"))
c.RemoveSecondary("b", "3")
spec.Expect(c.Get("a", "1")).ToEqual(item1)
spec.Expect(c.Get("b", "2")).ToEqual(item2)
spec.Expect(c.Get("b", "3")).ToEqual(nil)
spec.Expect(len(c.groups)).ToEqual(2)
}
func TestHandlesRemovalOfAnInvalidSecondaryKey(t *testing.T) {
spec := gspec.New(t)
c := New(Configure().Size(550))
item1 := NewItem("SAMPLE BODY FOR TESTING")
item2 := NewItem("SAMPLE BODY FOR TESTING")
c.Set("a", "1", item1)
c.Set("b", "2", item2)
c.Set("b", "3", NewItem("SAMPLE BODY FOR TESTING"))
c.RemoveSecondary("b", "c")
spec.Expect(len(c.groups["a"].nodes)).ToEqual(1)
spec.Expect(len(c.groups["b"].nodes)).ToEqual(2)
spec.Expect(len(c.groups)).ToEqual(2)
}
func TestConcurrencyStress(t *testing.T) {
fmt.Printf("%v\n", time.Now())
c := New(Configure().Size(550))
var wg sync.WaitGroup
for i := 1; i < 1000; i++ {
wg.Add(1)
go func() {
for j := 1; j < 2; j++ {
for _, cp := range "helloviki" {
p := string(cp)
for _, cs := range "helloviki" {
item := NewItem("test string")
s := string(cs)
c.Set(p, s, item)
c.Get(p, s)
c.RemoveSecondary(p, s)
// c.Remove(p)
if cs == 'v' {
c.gcFactor = 1
} else {
c.gcFactor = 0
}
c.Set(p, s, item)
}
}
}
wg.Done()
}()
}
wg.Wait()
c.Debug(os.Stdout)
fmt.Printf("%v\n", time.Now())
}
type ItemToCache struct {
body string
length int
}
func (i *ItemToCache) Debug() []byte {
return []byte("")
}
func (i *ItemToCache) Expires() time.Time {
return time.Now()
}
func NewItem(body string) *ItemToCache {
return &ItemToCache{
body: body,
length: len(body),
}
}