-
Notifications
You must be signed in to change notification settings - Fork 0
/
inmem_test.go
201 lines (157 loc) · 5.14 KB
/
inmem_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package cache_test
import (
"context"
"errors"
"sync"
"testing"
"time"
. "github.com/damianopetrungaro/go-cache"
)
func TestInMem(t *testing.T) {
t.Run("not found", func(t *testing.T) {
inmem := newInMemHelper(t)
val, err := inmem.Get(context.Background(), "one")
if !errors.Is(err, ErrNotFound) {
t.Errorf("could not match not found error. got: %s", err)
}
if val != "" {
t.Errorf("could not match default value, got: %s", val)
}
})
t.Run("find set value", func(t *testing.T) {
inmem := newInMemHelper(t)
const k = "key"
want := "value"
if err := inmem.Set(context.Background(), k, want, NoExpiration); err != nil {
t.Fatalf("could not set item: %s", err)
}
got, err := inmem.Get(context.Background(), k)
if err != nil {
t.Fatalf("could not get item: %s", err)
}
if got != want {
t.Errorf("could not match value, got: %s. want:%s", got, want)
}
})
t.Run("delete set value", func(t *testing.T) {
inmem := newInMemHelper(t)
const k = "key"
want := "value"
if err := inmem.Set(context.Background(), k, want, NoExpiration); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Delete(context.Background(), k); err != nil {
t.Fatalf("could not delete item: %s", err)
}
val, err := inmem.Get(context.Background(), k)
if !errors.Is(err, ErrNotFound) {
t.Errorf("could not match not found error. got: %s", err)
}
if val != "" {
t.Errorf("could not match default value, got: %s", val)
}
})
t.Run("concurrent set, get, and delete", func(t *testing.T) {
inmem := newInMemHelper(t)
const c = 100
wg := sync.WaitGroup{}
wg.Add(c)
for i := 0; i < 100; i++ {
go func() {
defer wg.Done()
_, _ = inmem.Get(context.Background(), "one")
_ = inmem.Set(context.Background(), "two", "two", time.Second)
_ = inmem.Delete(context.Background(), "two")
}()
}
wg.Wait()
})
t.Run("get expired value", func(t *testing.T) {
inmem := newInMemHelper(t)
const k = "key"
want := "value"
if err := inmem.Set(context.Background(), k, want, time.Millisecond); err != nil {
t.Fatalf("could not set item: %s", err)
}
time.Sleep(time.Millisecond)
val, err := inmem.Get(context.Background(), k)
if !errors.Is(err, ErrNotGet) {
t.Errorf("could not match not found error. got: %s", err)
}
if val != "" {
t.Errorf("could not match default value, got: %s", val)
}
})
t.Run("ensure cleanup behavior when expired item", func(t *testing.T) {
inmem := newInMemHelper(t)
const longK, longV = "long key", "long value"
const midK, midV = "mid key", "mid value"
const shortK, shortV = "short key", "short value"
const k, v = "key", "value"
if err := inmem.Set(context.Background(), longK, longV, -5*time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Set(context.Background(), midK, midV, -3*time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Set(context.Background(), shortK, shortV, -1*time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Set(context.Background(), k, v, 1*time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if _, err := inmem.Get(context.Background(), longK); err == nil {
t.Fatalf("could not find item: %s", longK)
}
if _, err := inmem.Get(context.Background(), midK); err == nil {
t.Fatalf("could not find item: %s", midK)
}
if _, err := inmem.Get(context.Background(), shortK); err == nil {
t.Fatalf("could find item: %s", shortK)
}
if found, _ := inmem.Get(context.Background(), k); found != v {
t.Fatalf("could not find item: %s", k)
}
})
t.Run("ensure cleanup behavior when no expired item", func(t *testing.T) {
inmem := newInMemHelper(t)
const longK, longV = "long key", "long value"
const midK, midV = "mid key", "mid value"
const shortK, shortV = "short key", "short value"
const k, v = "key", "value"
if err := inmem.Set(context.Background(), longK, longV, 5*time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Set(context.Background(), midK, midV, 3*time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Set(context.Background(), shortK, shortV, time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if err := inmem.Set(context.Background(), k, v, time.Minute); err != nil {
t.Fatalf("could not set item: %s", err)
}
if found, _ := inmem.Get(context.Background(), longK); found != longV {
t.Fatalf("could not find item: %s", longK)
}
if found, _ := inmem.Get(context.Background(), midK); found != midV {
t.Fatalf("could not find item: %s", midK)
}
if _, err := inmem.Get(context.Background(), shortK); err == nil {
t.Fatalf("could find item: %s", shortK)
}
if found, _ := inmem.Get(context.Background(), k); found != v {
t.Fatalf("could not find item: %s", k)
}
})
}
func newInMemHelper(t *testing.T) *InMem[string, string] {
t.Helper()
inmem := NewInMemory[string, string](time.Minute, 3)
t.Cleanup(func() {
if err := inmem.Close(); err != nil {
t.Errorf("could not close inmem: %s", err)
}
})
return inmem
}