-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
109 lines (94 loc) · 1.91 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
103
104
105
106
107
108
109
package wcache
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCache_Get(t *testing.T) {
tests := map[string]struct {
setKey string
getKey string
value interface{}
wantValue interface{}
wantOk bool
}{
"found": {
setKey: "exists",
getKey: "exists",
value: "test",
wantValue: "test",
wantOk: true,
},
"not_found": {
setKey: "exists",
getKey: "not exists",
value: "test",
wantValue: nil,
wantOk: false,
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
c := New(context.Background(), time.Minute, NoopExpire)
c.Set(tt.setKey, tt.value)
v, ok := c.Get(tt.getKey)
require.Equal(t, tt.wantOk, ok)
assert.Equal(t, tt.wantValue, v)
})
}
}
func TestCache_Set(t *testing.T) {
t.Run("overwrites_value", func(t *testing.T) {
const key = "1"
c := New(context.Background(), time.Minute, NoopExpire)
c.Set(key, "value1")
v, ok := c.Get(key)
require.True(t, ok)
assert.Equal(t, "value1", v)
c.Set(key, "value2")
v2, ok := c.Get(key)
require.True(t, ok)
assert.Equal(t, "value2", v2)
})
}
func TestCache_Delete(t *testing.T) {
const (
setKey = "exists"
value = "test"
)
tests := map[string]struct {
key string
wantOk bool
}{
"found": {
key: setKey,
wantOk: false,
},
"not_found": {
key: "not exists",
wantOk: false,
},
}
ctx, cancel := context.WithCancel(context.Background())
c := New(ctx, time.Minute, NoopExpire)
t.Run("prepare", func(t *testing.T) {
c.Set(setKey, value)
_, ok := c.Get(setKey)
require.True(t, ok)
})
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
c.Delete(tt.key)
_, ok := c.Get(tt.key)
require.Equal(t, tt.wantOk, ok)
})
}
t.Run("vaults_are_closed", func(t *testing.T) {
cancel()
<-c.Done()
})
}