Debian Codegraph @

helpers_test.go

raw symbols SCIP
1 package gcache
2
3 import (
4 "fmt"
5 "testing"
6 "time"
7 )
8
9 func loader(key interface{}) (interface{}, error) {
10 return fmt.Sprintf("valueFor%s", key), nil
11 }
12
13 func testSetCache(t *testing.T, gc Cache, numbers int) {
14 for i := 0; i < numbers; i++ {
15 key := fmt.Sprintf("Key-%d", i)
16 value, err := loader(key)
17 if err != nil {
18 t.Error(err)
19 return
20 }
21 gc.Set(key, value)
22 }
23 }
24
25 func testGetCache(t *testing.T, gc Cache, numbers int) {
26 for i := 0; i < numbers; i++ {
27 key := fmt.Sprintf("Key-%d", i)
28 v, err := gc.Get(key)
29 if err != nil {
30 t.Errorf("Unexpected error: %v", err)
31 }
32 expectedV, _ := loader(key)
33 if v != expectedV {
34 t.Errorf("Expected value is %v, not %v", expectedV, v)
35 }
36 }
37 }
38
39 func testGetIFPresent(t *testing.T, evT string) {
40 cache :=
41 New(8).
42 EvictType(evT).
43 LoaderFunc(
44 func(key interface{}) (interface{}, error) {
45 return "value", nil
46 }).
47 Build()
48
49 v, err := cache.GetIFPresent("key")
50 if err != KeyNotFoundError {
51 t.Errorf("err should not be %v", err)
52 }
53
54 time.Sleep(2 * time.Millisecond)
55
56 v, err = cache.GetIFPresent("key")
57 if err != nil {
58 t.Errorf("err should not be %v", err)
59 }
60 if v != "value" {
61 t.Errorf("v should not be %v", v)
62 }
63 }
64
65 func setItemsByRange(t *testing.T, c Cache, start, end int) {
66 for i := start; i < end; i++ {
67 if err := c.Set(i, i); err != nil {
68 t.Error(err)
69 }
70 }
71 }
72
73 func keysToMap(keys []interface{}) map[interface{}]struct{} {
74 m := make(map[interface{}]struct{}, len(keys))
75 for _, k := range keys {
76 m[k] = struct{}{}
77 }
78 return m
79 }
80
81 func checkItemsByRange(t *testing.T, keys []interface{}, m map[interface{}]interface{}, size, start, end int) {
82 if len(keys) != size {
83 t.Fatalf("%v != %v", len(keys), size)
84 } else if len(m) != size {
85 t.Fatalf("%v != %v", len(m), size)
86 }
87 km := keysToMap(keys)
88 for i := start; i < end; i++ {
89 if _, ok := km[i]; !ok {
90 t.Errorf("keys should contain %v", i)
91 }
92 v, ok := m[i]
93 if !ok {
94 t.Errorf("m should contain %v", i)
95 continue
96 }
97 if v.(int) != i {
98 t.Errorf("%v != %v", v, i)
99 continue
100 }
101 }
102 }
103
104 func testExpiredItems(t *testing.T, evT string) {
105 size := 8
106 cache :=
107 New(size).
108 Expiration(time.Millisecond).
109 EvictType(evT).
110 Build()
111
112 setItemsByRange(t, cache, 0, size)
113 checkItemsByRange(t, cache.Keys(true), cache.GetALL(true), cache.Len(true), 0, size)
114
115 time.Sleep(time.Millisecond)
116
117 checkItemsByRange(t, cache.Keys(false), cache.GetALL(false), cache.Len(false), 0, size)
118
119 if l := cache.Len(true); l != 0 {
120 t.Fatalf("GetALL should returns no items, but got length %v", l)
121 }
122
123 cache.Set(1, 1)
124 m := cache.GetALL(true)
125 if len(m) != 1 {
126 t.Fatalf("%v != %v", len(m), 1)
127 } else if l := cache.Len(true); l != 1 {
128 t.Fatalf("%v != %v", l, 1)
129 }
130 if m[1] != 1 {
131 t.Fatalf("%v != %v", m[1], 1)
132 }
133 }
134
135 func getSimpleEvictedFunc(t *testing.T) func(interface{}, interface{}) {
136 return func(key, value interface{}) {
137 t.Logf("Key=%v Value=%v will be evicted.\n", key, value)
138 }
139 }
140
141 func buildTestCache(t *testing.T, tp string, size int) Cache {
142 return New(size).
143 EvictType(tp).
144 EvictedFunc(getSimpleEvictedFunc(t)).
145 Build()
146 }
147
148 func buildTestLoadingCache(t *testing.T, tp string, size int, loader LoaderFunc) Cache {
149 return New(size).
150 EvictType(tp).
151 LoaderFunc(loader).
152 EvictedFunc(getSimpleEvictedFunc(t)).
153 Build()
154 }
155
156 func buildTestLoadingCacheWithExpiration(t *testing.T, tp string, size int, ep time.Duration) Cache {
157 return New(size).
158 EvictType(tp).
159 Expiration(ep).
160 LoaderFunc(loader).
161 EvictedFunc(getSimpleEvictedFunc(t)).
162 Build()
163 }