-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
63 lines (48 loc) · 1.28 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
package ttl
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGet(t *testing.T) {
cache := NewCache[string, string](time.Second)
data := cache.Get("hello")
assert.Equal(t, "", data)
cache.Set("hello", "world")
data = cache.Get("hello")
assert.Equal(t, "world", data)
}
func TestGetOrSet(t *testing.T) {
cache := NewCache[string, string](time.Second)
data, _ := cache.GetOrSet("hello", "earth")
assert.Equal(t, "earth", data)
cache.GetOrSet("hello", "world")
data = cache.Get("hello")
assert.Equal(t, "earth", data)
}
func TestGetAndDelete(t *testing.T) {
cache := NewCache[string, string](time.Second)
data, _ := cache.GetOrSet("hello", "earth")
assert.Equal(t, "earth", data)
data, _ = cache.GetAndDelete("hello")
assert.Equal(t, "earth", data)
data = cache.Get("hello")
assert.Equal(t, "", data)
}
func TestExpiration(t *testing.T) {
cache := NewCache[string, string](time.Second)
cache.Set("x", "1")
cache.Set("y", "z")
cache.Set("z", "3")
<-time.After(500 * time.Millisecond)
val := cache.Get("x")
assert.Equal(t, "1", val)
<-time.After(time.Second)
val = cache.Get("x")
assert.Equal(t, "", val)
val = cache.Get("y")
assert.Equal(t, "", val)
val = cache.Get("z")
assert.Equal(t, "", val)
assert.Equal(t, 0, len(cache.items))
}