Debian Codegraph @

keys_test.go

raw
1 package sturdyc_test
2
3 import (
4 "strconv"
5 "testing"
6 "time"
7
8 "github.com/viccon/sturdyc"
9 )
10
11 func TestTimeBasedKeys(t *testing.T) {
12 t.Parallel()
13
14 timeValue := time.Now()
15 regularTimeClient := sturdyc.New[any](100, 1, time.Hour, 5)
16
17 clientSecondClock := sturdyc.NewTestClock(time.Now().Truncate(time.Second))
18 relativeTimeClientSecond := sturdyc.New[any](100, 1, time.Hour, 5,
19 sturdyc.WithNoContinuousEvictions(),
20 sturdyc.WithRelativeTimeKeyFormat(time.Second),
21 sturdyc.WithClock(clientSecondClock),
22 )
23
24 clientMinuteClock := sturdyc.NewTestClock(time.Now().Truncate(time.Minute))
25 relativeTimeClientMinute := sturdyc.New[any](100, 1, time.Hour, 5,
26 sturdyc.WithNoContinuousEvictions(),
27 sturdyc.WithRelativeTimeKeyFormat(time.Minute),
28 sturdyc.WithClock(clientMinuteClock),
29 )
30
31 clientHourClock := sturdyc.NewTestClock(time.Now().Truncate(time.Hour))
32 relativeTimeClientHour := sturdyc.New[any](100, 1, time.Hour, 5,
33 sturdyc.WithNoContinuousEvictions(),
34 sturdyc.WithRelativeTimeKeyFormat(time.Hour),
35 sturdyc.WithClock(clientHourClock),
36 )
37
38 type opts struct {
39 Time time.Time
40 }
41
42 epochString := strconv.FormatInt(timeValue.Unix(), 10)
43 keyOne := regularTimeClient.PermutatedKey("keyPrefix", opts{timeValue})
44 if keyOne != "keyPrefix-"+epochString {
45 t.Errorf("got: %s wanted: %s", keyOne, "keyPrefix-"+epochString)
46 }
47
48 // Adding 1 minute and 10 seconds to the time. No truncation should occur.
49 secondTimeValue := clientSecondClock.Now()
50 clientSecondClock.Add(time.Second * 70)
51 secondKey := relativeTimeClientSecond.PermutatedKey("keyPrefix", opts{secondTimeValue})
52 wantSecondKey := "keyPrefix-(-)0h01m10s"
53 if secondKey != wantSecondKey {
54 t.Errorf("got: %s wanted: %s", secondKey, wantSecondKey)
55 }
56
57 // Adding 1h, 1 minute and 10 seconds to the time. The seconds should be truncated.
58 minuteTimeValue := clientMinuteClock.Now()
59 clientMinuteClock.Add(time.Hour + time.Minute + (time.Second * 10))
60 minuteKey := relativeTimeClientMinute.PermutatedKey("keyPrefix", opts{minuteTimeValue})
61 wantMinuteKey := "keyPrefix-(-)1h01m00s"
62 if minuteKey != wantMinuteKey {
63 t.Errorf("got: %s wanted: %s", minuteKey, wantMinuteKey)
64 }
65
66 // Adding 72h to the time. The minutes and seconds should be truncated.
67 hourTimeValue := clientHourClock.Now()
68 clientHourClock.Add((72 * time.Hour) + time.Minute + time.Second)
69 hourKey := relativeTimeClientHour.PermutatedKey("keyPrefix", opts{hourTimeValue})
70 wantHourKey := "keyPrefix-(-)72h00m00s"
71 if hourKey != wantHourKey {
72 t.Errorf("got: %s wanted: %s", hourKey, wantHourKey)
73 }
74 }
75
76 func TestPermutatedRelativeTimeKeys(t *testing.T) {
77 t.Parallel()
78
79 c := sturdyc.New[any](100, 1, time.Hour, 5,
80 sturdyc.WithNoContinuousEvictions(),
81 sturdyc.WithRelativeTimeKeyFormat(time.Minute),
82 )
83 prefix := "cache-key"
84 stringValue := "string"
85 intValue := 1
86 stringSliceValue := []string{"string1", "string2"}
87 intSliceValue := []int{1, 2}
88 boolValue := true
89 negativeTimeValue := time.Now().Add(-time.Hour)
90 positiveTimeValue := time.Now().Add(90 * time.Minute)
91
92 type queryParams struct {
93 String string
94 StringPointer *string
95 StringNilPointer *string
96 Int int
97 IntPointer *int
98 IntNilPointer *int
99 StringSlice []string
100 StringNilSlice []string
101 StringSlicePointer *[]string
102 StringNilSlicePointer *[]string
103 IntSlice []int
104 IntNilSlice []int
105 IntSlicePointer *[]int
106 IntNilSlicePointer *[]int
107 Bool bool
108 BoolPointer *bool
109 BoolNilPointer *bool
110 Time time.Time
111 TimePointer *time.Time
112 TimeNilPointer *time.Time
113 }
114
115 // We'll specify the fields in a different order than they are defined
116 // in the struct. This should have no impact on the cache key.
117 queryOne := queryParams{
118 Time: negativeTimeValue,
119 TimePointer: &positiveTimeValue,
120 TimeNilPointer: nil,
121 String: stringValue,
122 StringPointer: &stringValue,
123 StringNilPointer: nil,
124 Int: intValue,
125 IntPointer: &intValue,
126 IntNilPointer: nil,
127 StringSlice: stringSliceValue,
128 StringNilSlice: nil,
129 StringSlicePointer: &stringSliceValue,
130 StringNilSlicePointer: nil,
131 IntSlice: intSliceValue,
132 IntNilSlice: nil,
133 IntSlicePointer: &intSliceValue,
134 IntNilSlicePointer: nil,
135 Bool: boolValue,
136 BoolPointer: &boolValue,
137 BoolNilPointer: nil,
138 }
139
140 //nolint:lll // This is a test case, we want to keep the line length.
141 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"
142 got := c.PermutatedKey(prefix, queryOne)
143
144 if got != want {
145 t.Errorf("got: %s wanted: %s", got, want)
146 }
147 }
148
149 func TestPermutatedKeyHandlesEmptySlices(t *testing.T) {
150 t.Parallel()
151
152 c := sturdyc.New[any](100, 1, time.Hour, 5,
153 sturdyc.WithNoContinuousEvictions(),
154 )
155 type queryParams struct {
156 StringValues []string
157 IntValues []int
158 }
159 params := queryParams{
160 StringValues: []string{},
161 IntValues: []int{},
162 }
163
164 want := "cache-key-empty-empty"
165 got := c.PermutatedKey("cache-key", params)
166
167 if got != want {
168 t.Errorf("got: %s wanted: %s", got, want)
169 }
170 }
171
172 func TestPermutatedBatchKeyFn(t *testing.T) {
173 t.Parallel()
174
175 c := sturdyc.New[any](100, 1, time.Hour, 5,
176 sturdyc.WithNoContinuousEvictions(),
177 )
178 type queryParams struct {
179 IncludeUpcoming bool
180 // Note that limit isn't exported (lowercase), hence it should be omitted from the key.
181 limit int
182 }
183
184 cacheKeyFunc := c.PermutatedBatchKeyFn("cache-key", queryParams{
185 IncludeUpcoming: true,
186 limit: 2,
187 })
188 want := "cache-key-true-ID-1"
189 got := cacheKeyFunc("1")
190
191 if got != want {
192 t.Errorf("got: %s wanted: %s", got, want)
193 }
194 }
195
196 func TestTimePointers(t *testing.T) {
197 t.Parallel()
198
199 type opts struct{ Time *time.Time }
200 cache := sturdyc.New[any](100, 1, time.Hour, 5,
201 sturdyc.WithNoContinuousEvictions(),
202 )
203
204 now := time.Now().Truncate(time.Hour)
205 got := cache.PermutatedKey("key", opts{Time: &now})
206 want := "key-" + strconv.Itoa(int(now.Unix()))
207 if got != want {
208 t.Errorf("got: %s wanted: %s", got, want)
209 }
210
211 got = cache.PermutatedKey("key", opts{Time: nil})
212 want = "key-nil"
213 if got != want {
214 t.Errorf("got: %s wanted: %s", got, want)
215 }
216
217 empty := time.Time{}
218 got = cache.PermutatedKey("key", opts{Time: &empty})
219 want = "key-empty-time"
220 if got != want {
221 t.Errorf("got: %s wanted: %s", got, want)
222 }
223 }