forked from creativecreature/sturdyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys_test.go
223 lines (192 loc) · 6.45 KB
/
keys_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package sturdyc_test
import (
"strconv"
"testing"
"time"
"github.com/creativecreature/sturdyc"
)
func TestTimeBasedKeys(t *testing.T) {
t.Parallel()
timeValue := time.Now()
regularTimeClient := sturdyc.New[any](100, 1, time.Hour, 5)
clientSecondClock := sturdyc.NewTestClock(time.Now().Truncate(time.Second))
relativeTimeClientSecond := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
sturdyc.WithRelativeTimeKeyFormat(time.Second),
sturdyc.WithClock(clientSecondClock),
)
clientMinuteClock := sturdyc.NewTestClock(time.Now().Truncate(time.Minute))
relativeTimeClientMinute := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
sturdyc.WithRelativeTimeKeyFormat(time.Minute),
sturdyc.WithClock(clientMinuteClock),
)
clientHourClock := sturdyc.NewTestClock(time.Now().Truncate(time.Hour))
relativeTimeClientHour := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
sturdyc.WithRelativeTimeKeyFormat(time.Hour),
sturdyc.WithClock(clientHourClock),
)
type opts struct {
Time time.Time
}
epochString := strconv.FormatInt(timeValue.Unix(), 10)
keyOne := regularTimeClient.PermutatedKey("keyPrefix", opts{timeValue})
if keyOne != "keyPrefix-"+epochString {
t.Errorf("got: %s wanted: %s", keyOne, "keyPrefix-"+epochString)
}
// Adding 1 minute and 10 seconds to the time. No truncation should occur.
secondTimeValue := clientSecondClock.Now()
clientSecondClock.Add(time.Second * 70)
secondKey := relativeTimeClientSecond.PermutatedKey("keyPrefix", opts{secondTimeValue})
wantSecondKey := "keyPrefix-(-)0h01m10s"
if secondKey != wantSecondKey {
t.Errorf("got: %s wanted: %s", secondKey, wantSecondKey)
}
// Adding 1h, 1 minute and 10 seconds to the time. The seconds should be truncated.
minuteTimeValue := clientMinuteClock.Now()
clientMinuteClock.Add(time.Hour + time.Minute + (time.Second * 10))
minuteKey := relativeTimeClientMinute.PermutatedKey("keyPrefix", opts{minuteTimeValue})
wantMinuteKey := "keyPrefix-(-)1h01m00s"
if minuteKey != wantMinuteKey {
t.Errorf("got: %s wanted: %s", minuteKey, wantMinuteKey)
}
// Adding 72h to the time. The minutes and seconds should be truncated.
hourTimeValue := clientHourClock.Now()
clientHourClock.Add((72 * time.Hour) + time.Minute + time.Second)
hourKey := relativeTimeClientHour.PermutatedKey("keyPrefix", opts{hourTimeValue})
wantHourKey := "keyPrefix-(-)72h00m00s"
if hourKey != wantHourKey {
t.Errorf("got: %s wanted: %s", hourKey, wantHourKey)
}
}
func TestPermutatedRelativeTimeKeys(t *testing.T) {
t.Parallel()
c := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
sturdyc.WithRelativeTimeKeyFormat(time.Minute),
)
prefix := "cache-key"
stringValue := "string"
intValue := 1
stringSliceValue := []string{"string1", "string2"}
intSliceValue := []int{1, 2}
boolValue := true
negativeTimeValue := time.Now().Add(-time.Hour)
positiveTimeValue := time.Now().Add(90 * time.Minute)
type queryParams struct {
String string
StringPointer *string
StringNilPointer *string
Int int
IntPointer *int
IntNilPointer *int
StringSlice []string
StringNilSlice []string
StringSlicePointer *[]string
StringNilSlicePointer *[]string
IntSlice []int
IntNilSlice []int
IntSlicePointer *[]int
IntNilSlicePointer *[]int
Bool bool
BoolPointer *bool
BoolNilPointer *bool
Time time.Time
TimePointer *time.Time
TimeNilPointer *time.Time
}
// We'll specify the fields in a different order than they are defined
// in the struct. This should have no impact on the cache key.
queryOne := queryParams{
Time: negativeTimeValue,
TimePointer: &positiveTimeValue,
TimeNilPointer: nil,
String: stringValue,
StringPointer: &stringValue,
StringNilPointer: nil,
Int: intValue,
IntPointer: &intValue,
IntNilPointer: nil,
StringSlice: stringSliceValue,
StringNilSlice: nil,
StringSlicePointer: &stringSliceValue,
StringNilSlicePointer: nil,
IntSlice: intSliceValue,
IntNilSlice: nil,
IntSlicePointer: &intSliceValue,
IntNilSlicePointer: nil,
Bool: boolValue,
BoolPointer: &boolValue,
BoolNilPointer: nil,
}
//nolint:lll // This is a test case, we want to keep the line length.
want := "cache-key-string-string-nil-1-1-nil-string1,string2-nil-string1,string2-nil-1,2-nil-1,2-nil-true-true-nil-(-)1h00m00s-(+)1h30m00s-nil"
got := c.PermutatedKey(prefix, queryOne)
if got != want {
t.Errorf("got: %s wanted: %s", got, want)
}
}
func TestPermutatedKeyHandlesEmptySlices(t *testing.T) {
t.Parallel()
c := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
)
type queryParams struct {
StringValues []string
IntValues []int
}
params := queryParams{
StringValues: []string{},
IntValues: []int{},
}
want := "cache-key-empty-empty"
got := c.PermutatedKey("cache-key", params)
if got != want {
t.Errorf("got: %s wanted: %s", got, want)
}
}
func TestPermutatedBatchKeyFn(t *testing.T) {
t.Parallel()
c := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
)
type queryParams struct {
IncludeUpcoming bool
// Note that limit isn't exported (lowercase), hence it should be omitted from the key.
limit int
}
cacheKeyFunc := c.PermutatedBatchKeyFn("cache-key", queryParams{
IncludeUpcoming: true,
limit: 2,
})
want := "cache-key-true-ID-1"
got := cacheKeyFunc("1")
if got != want {
t.Errorf("got: %s wanted: %s", got, want)
}
}
func TestTimePointers(t *testing.T) {
t.Parallel()
type opts struct{ Time *time.Time }
cache := sturdyc.New[any](100, 1, time.Hour, 5,
sturdyc.WithNoContinuousEvictions(),
)
now := time.Now().Truncate(time.Hour)
got := cache.PermutatedKey("key", opts{Time: &now})
want := "key-" + strconv.Itoa(int(now.Unix()))
if got != want {
t.Errorf("got: %s wanted: %s", got, want)
}
got = cache.PermutatedKey("key", opts{Time: nil})
want = "key-nil"
if got != want {
t.Errorf("got: %s wanted: %s", got, want)
}
empty := time.Time{}
got = cache.PermutatedKey("key", opts{Time: &empty})
want = "key-empty-time"
if got != want {
t.Errorf("got: %s wanted: %s", got, want)
}
}