-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
102 lines (76 loc) · 1.93 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
package yetacache
import (
"testing"
"time"
)
func TestHasMethod(t *testing.T) {
c := New[int, int](time.Millisecond, time.Second)
c.Set(1, 1, DefaultTTL)
if c.Has(2) {
t.Error("Found value that shouldn't exist")
}
if !c.Has(1) {
t.Error("Not found value that should exist")
}
time.Sleep(1010 * time.Microsecond)
if c.Has(1) {
t.Error("Found value that should have expired")
}
c.StopCleanup()
}
func TestGetMethod(t *testing.T) {
c := New[string, string](time.Millisecond, time.Second)
c.Set("123", "abc", DefaultTTL)
if _, found := c.Get("321"); found {
t.Error("Found value that shouldn't exist")
}
if val, found := c.Get("123"); !found {
t.Error("Not found value that should exist")
} else if val != "abc" {
t.Error("Found incorrect value: ", val)
}
time.Sleep(1010 * time.Microsecond)
if _, found := c.Get("123"); found {
t.Error("Found value that should have expired")
}
c.StopCleanup()
}
func TestDeleteMethod(t *testing.T) {
c := New[string, int](time.Millisecond, time.Second)
c.Set("abc", 36, DefaultTTL)
c.Set("def", 72, DefaultTTL)
c.Delete("abc")
if c.Has("abc") {
t.Error("Found value that shouldn't exist")
}
if !c.Has("def") {
t.Error("Not found value that should exist")
}
c.StopCleanup()
}
func TestClearMethod(t *testing.T) {
c := New[string, int](time.Millisecond, time.Second)
c.Set("abc", 36, DefaultTTL)
c.Set("def", 72, DefaultTTL)
c.Clear()
if c.Has("abc") || c.Has("def") {
t.Error("Found value that shouldn't exist")
}
c.StopCleanup()
}
func TestCustomTTL(t *testing.T) {
c := New[int, int](time.Millisecond, time.Second)
c.Set(27, 1, DefaultTTL)
c.Set(48, 1, 2*time.Millisecond)
if !c.Has(27) || !c.Has(48) {
t.Error("Not found value that should exist")
}
time.Sleep(1010 * time.Microsecond)
if c.Has(27) {
t.Error("Found value that should have expired")
}
if !c.Has(48) {
t.Error("Not found value that shouldn't expired")
}
c.StopCleanup()
}