forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
78 lines (65 loc) · 1.67 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
package server
import (
"strconv"
"testing"
)
func TestCacheBase(t *testing.T) {
c := newCache(5)
foo := struct{}{}
c.Insert("foo", foo)
ensureCacheKey(t, c, "foo", foo)
}
func TestCacheLimit(t *testing.T) {
max := 10
c := newCache(max)
// Fill the cache with values
var i int
for i = 0; i < max; i++ {
c.Insert(strconv.Itoa(i), i)
}
// Ensure its at the max size
ensureCacheSize(t, c, max)
// Ensure they are all stored..
for j := 0; j < max; j++ {
ensureCacheKey(t, c, strconv.Itoa(j), j)
}
// Continue filling the cache, expect old keys to be dropped
c.Insert(strconv.Itoa(i), i)
ensureCacheKey(t, c, strconv.Itoa(i), i)
// Should still be at max size
ensureCacheSize(t, c, max)
// Expect that "0" got dropped
_, ok := c.Get("0")
if ok {
t.Fatal("Expected key '0' to not be found")
}
// Load the cache with many more than max
for ; i < max*20; i++ {
c.Insert(strconv.Itoa(i), i)
}
ensureCacheSize(t, c, max)
// Ensure the last set of "max" number are available, and everything else is not
for j := 0; j < i; j++ {
k := strconv.Itoa(j)
if j >= (i - max) {
ensureCacheKey(t, c, k, j)
} else {
_, ok := c.Get(k)
if ok {
t.Fatalf("Expected key %s to not be found", k)
}
}
}
}
func ensureCacheKey(t *testing.T, c *cache, k string, v interface{}) {
t.Helper()
actual, ok := c.Get(k)
if !ok || v != actual {
t.Fatalf("expected to retrieve value %v for key %s, got %v ok==%t", v, k, actual, ok)
}
}
func ensureCacheSize(t *testing.T, c *cache, size int) {
if len(c.data) != size && len(c.keylist) != size {
t.Fatalf("Unexpected cache size len(data)=%d len(keylist)=%d, expected %d", size, len(c.data), len(c.keylist))
}
}