-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
65 lines (52 loc) · 1.18 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
package cache
import (
"testing"
"time"
)
func TestInMemoryCache(t *testing.T) {
t.Run("test put/get/del", func(t *testing.T) {
mem := NewInMemoryCache[string, int]()
err := mem.Put("a", 1, time.Second)
if err != nil {
t.Error("Put operation should not end up with error")
}
v, ok := mem.Get("a")
if !ok {
t.Error("key 'a' does not exists in cache")
}
if v != 1 {
t.Errorf("wrong value: 1 != %d", v)
}
v, ok = mem.Get("b")
if ok {
t.Error("key 'b' is not supposed to be in cache")
}
err = mem.Del("a")
if err != nil {
t.Error("Del operation should not end up with error")
}
v, ok = mem.Get("a")
if ok {
t.Error("key 'a' should not exists in cache")
}
})
t.Run("test key ttl", func(t *testing.T) {
mem := NewInMemoryCache[string, int]()
err := mem.Put("a", 1, 500*time.Millisecond)
if err != nil {
t.Error("Put operation should not end up with error")
}
v, ok := mem.Get("a")
if !ok {
t.Error("key 'a' does not exists in cache")
}
if v != 1 {
t.Errorf("wrong value: 1 != %d", v)
}
time.Sleep(501 * time.Millisecond)
v, ok = mem.Get("a")
if ok {
t.Error("key 'a' supposed to be expired")
}
})
}