-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
127 lines (101 loc) · 3.08 KB
/
cache_test.go
File metadata and controls
127 lines (101 loc) · 3.08 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ttlmap_test
import (
"github.com/packaged/ttlmap"
"testing"
"time"
)
func TestGet(t *testing.T) {
cache := ttlmap.New(ttlmap.WithDefaultTTL(300*time.Millisecond), ttlmap.WithCleanupDuration(time.Millisecond*100))
data, exists := cache.Get("hello")
if exists || data != nil {
t.Errorf("Expected empty cache to return no data")
}
cache.Set("hello", "world", nil)
data, exists = cache.Get("hello")
if !exists {
t.Errorf("Expected cache to return data for `hello`")
}
if data.(string) != "world" {
t.Errorf("Expected cache to return `world` for `hello`")
}
// Check to see if cleanup is clearing unexpired items
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if !exists || data == nil {
t.Errorf("Expected cache to return data")
}
// Check Cache is re-touching after a get
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if !exists || data == nil {
t.Errorf("Expected cache to return data")
}
// Check Cache is optionally re-touching after a get
time.Sleep(time.Millisecond * 200)
data, exists = cache.TouchGet("hello", false)
if !exists || data == nil {
t.Errorf("Expected cache to return data")
}
// Make sure cache clears after expiry
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if exists || data != nil {
t.Errorf("Expected empty cache to return no data")
}
}
func TestMaxLifetime(t *testing.T) {
cache := ttlmap.New(ttlmap.WithMaxLifetime(time.Millisecond * 100))
data, exists := cache.Get("hello")
if exists || data != nil {
t.Errorf("Expected empty cache to return no data")
}
cache.Set("hello", "world", nil)
data, exists = cache.Get("hello")
if !exists {
t.Errorf("Expected cache to return data for `hello`")
}
if data.(string) != "world" {
t.Errorf("Expected cache to return `world` for `hello`")
}
// Check to see if max lifetime has killed the item
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if exists || data != nil {
t.Errorf("Expected empty cache to return no data")
}
}
func TestItems(t *testing.T) {
dur := time.Millisecond * 100
cache := ttlmap.New()
cache.Set("item1", "one", nil)
cache.Set("item2", "two", nil)
cache.Set("item3", "three", &dur)
cache.Set("item4", "four", nil)
if len(cache.Items()) != 4 {
t.Errorf("Expected cache to return 4 items")
}
time.Sleep(dur)
if len(cache.Items()) != 3 {
t.Errorf("Expected cache to return 3 items after cache expiry")
}
}
func TestCleanup(t *testing.T) {
dur := time.Millisecond * 100
cleanup := 0
cache := ttlmap.New(ttlmap.WithCleanupDuration(time.Millisecond * 5))
cache.SetWithCleanup("item1", "one", nil, func(item *ttlmap.Item) { cleanup++ })
if cleanup != 0 {
t.Errorf("Cache item cleaned up too early")
}
cache.Remove("item1")
if cleanup != 1 {
t.Errorf("Cache item cleanup not called on Remove")
}
cache.SetWithCleanup("item2", "two", &dur, func(item *ttlmap.Item) { cleanup++ })
time.Sleep(dur)
// Wait a few more milliseconds for cleanup to run
time.Sleep(time.Millisecond * 20)
if cleanup != 2 {
t.Errorf("Cache item cleanup not called on expiry")
}
}