@@ -33,9 +33,10 @@ func TestBatchIsRefreshedWhenTheTimeoutExpires(t *testing.T) {
33
33
// 1. The number of scheduled refreshes exceeds the specified 'batchSize'.
34
34
// 2. The 'batchBufferTimeout' threshold is exceeded.
35
35
client := sturdyc .New [string ](capacity , numShards , ttl , evictionPercentage ,
36
- sturdyc .WithBackgroundRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
36
+ sturdyc .WithNoContinuousEvictions (),
37
+ sturdyc .WithEarlyRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
37
38
sturdyc .WithMissingRecordStorage (),
38
- sturdyc .WithRefreshBuffering (batchSize , batchBufferTimeout ),
39
+ sturdyc .WithRefreshCoalescing (batchSize , batchBufferTimeout ),
39
40
sturdyc .WithClock (clock ),
40
41
)
41
42
@@ -47,7 +48,7 @@ func TestBatchIsRefreshedWhenTheTimeoutExpires(t *testing.T) {
47
48
48
49
fetchObserver := NewFetchObserver (1 )
49
50
fetchObserver .BatchResponse (ids )
50
- sturdyc .GetFetchBatch (ctx , client , ids , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
51
+ sturdyc .GetOrFetchBatch (ctx , client , ids , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
51
52
52
53
<- fetchObserver .FetchCompleted
53
54
fetchObserver .AssertFetchCount (t , 1 )
@@ -59,11 +60,11 @@ func TestBatchIsRefreshedWhenTheTimeoutExpires(t *testing.T) {
59
60
clock .Add (maxRefreshDelay + time .Second )
60
61
61
62
// We'll create a batch function that stores the ids it was called with, and
62
- // then invoke "GetFetchBatch ". We are going to request 3 ids, which is less
63
+ // then invoke "GetOrFetchBatch ". We are going to request 3 ids, which is less
63
64
// than our wanted batch size. This should lead to a batch being scheduled.
64
65
recordsToRequest := []string {"1" , "2" , "3" }
65
66
fetchObserver .BatchResponse (recordsToRequest )
66
- sturdyc .GetFetchBatch (ctx , client , recordsToRequest , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
67
+ sturdyc .GetOrFetchBatch (ctx , client , recordsToRequest , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
67
68
time .Sleep (10 * time .Millisecond )
68
69
fetchObserver .AssertFetchCount (t , 1 )
69
70
@@ -98,9 +99,10 @@ func TestBatchIsRefreshedWhenTheBufferSizeIsReached(t *testing.T) {
98
99
// 1. The number of scheduled refreshes exceeds the specified 'batchSize'.
99
100
// 2. The 'batchBufferTimeout' threshold is exceeded.
100
101
client := sturdyc .New [string ](capacity , numShards , ttl , evictionPercentage ,
101
- sturdyc .WithBackgroundRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
102
+ sturdyc .WithNoContinuousEvictions (),
103
+ sturdyc .WithEarlyRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
102
104
sturdyc .WithMissingRecordStorage (),
103
- sturdyc .WithRefreshBuffering (batchSize , batchBufferTimeout ),
105
+ sturdyc .WithRefreshCoalescing (batchSize , batchBufferTimeout ),
104
106
sturdyc .WithClock (clock ),
105
107
)
106
108
@@ -111,7 +113,7 @@ func TestBatchIsRefreshedWhenTheBufferSizeIsReached(t *testing.T) {
111
113
112
114
fetchObserver := NewFetchObserver (1 )
113
115
fetchObserver .BatchResponse (ids )
114
- sturdyc .GetFetchBatch (ctx , client , ids , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
116
+ sturdyc .GetOrFetchBatch (ctx , client , ids , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
115
117
116
118
<- fetchObserver .FetchCompleted
117
119
fetchObserver .AssertFetchCount (t , 1 )
@@ -123,17 +125,17 @@ func TestBatchIsRefreshedWhenTheBufferSizeIsReached(t *testing.T) {
123
125
clock .Add (maxRefreshDelay + time .Second )
124
126
125
127
// We'll create a batch function that stores the ids it was called with, and
126
- // then invoke "GetFetchBatch ". We are going to request 3 ids, which is less
128
+ // then invoke "GetOrFetchBatch ". We are going to request 3 ids, which is less
127
129
// than our ideal batch size. This should lead to a batch being scheduled.
128
130
firstBatchOfRequestedRecords := []string {"1" , "2" , "3" }
129
131
fetchObserver .BatchResponse ([]string {"1" , "2" , "3" })
130
- sturdyc .GetFetchBatch (ctx , client , firstBatchOfRequestedRecords , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
132
+ sturdyc .GetOrFetchBatch (ctx , client , firstBatchOfRequestedRecords , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
131
133
132
134
// Now, we'll move the clock forward 5 seconds before requesting another 3 records.
133
135
// Our wanted batch size is 10, hence this should NOT be enough to trigger a refresh.
134
136
clock .Add (5 * time .Second )
135
137
secondBatchOfRecords := []string {"4" , "5" , "6" }
136
- sturdyc .GetFetchBatch (ctx , client , secondBatchOfRecords , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
138
+ sturdyc .GetOrFetchBatch (ctx , client , secondBatchOfRecords , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
137
139
138
140
// Move the clock another 10 seconds. Again, this should not trigger a refresh. We'll
139
141
// perform a sleep here too just to ensure that the buffer is not refreshed prematurely.
@@ -144,7 +146,7 @@ func TestBatchIsRefreshedWhenTheBufferSizeIsReached(t *testing.T) {
144
146
// In the the third batch I'm going to request 6 records. With that, we've
145
147
// requested 12 record in total, which is greater than our buffer size of 10.
146
148
thirdBatchOfRecords := []string {"7" , "8" , "9" , "10" , "11" , "12" }
147
- sturdyc .GetFetchBatch (ctx , client , thirdBatchOfRecords , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
149
+ sturdyc .GetOrFetchBatch (ctx , client , thirdBatchOfRecords , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
148
150
149
151
// An actual refresh should happen for the first 10 ids, while the 2 that
150
152
// overflows should get scheduled for a refresh. Block until the request has
@@ -191,9 +193,10 @@ func TestBatchIsNotRefreshedByDuplicates(t *testing.T) {
191
193
// 1. The number of scheduled refreshes exceeds the specified 'batchSize'.
192
194
// 2. The 'batchBufferTimeout' threshold is exceeded.
193
195
client := sturdyc .New [string ](capacity , numShards , ttl , evictionPercentage ,
194
- sturdyc .WithBackgroundRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
196
+ sturdyc .WithNoContinuousEvictions (),
197
+ sturdyc .WithEarlyRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
195
198
sturdyc .WithMissingRecordStorage (),
196
- sturdyc .WithRefreshBuffering (batchSize , batchBufferTimeout ),
199
+ sturdyc .WithRefreshCoalescing (batchSize , batchBufferTimeout ),
197
200
sturdyc .WithClock (clock ),
198
201
)
199
202
@@ -205,7 +208,7 @@ func TestBatchIsNotRefreshedByDuplicates(t *testing.T) {
205
208
206
209
fetchObserver := NewFetchObserver (1 )
207
210
fetchObserver .BatchResponse (ids )
208
- sturdyc .GetFetchBatch (ctx , client , ids , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
211
+ sturdyc .GetOrFetchBatch (ctx , client , ids , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
209
212
<- fetchObserver .FetchCompleted
210
213
fetchObserver .AssertFetchCount (t , 1 )
211
214
fetchObserver .AssertRequestedRecords (t , ids )
@@ -221,7 +224,7 @@ func TestBatchIsNotRefreshedByDuplicates(t *testing.T) {
221
224
for i := 0 ; i < numRequests ; i ++ {
222
225
go func (i int ) {
223
226
id := []string {strconv .Itoa ((i % 3 ) + 1 )}
224
- sturdyc .GetFetchBatch (ctx , client , id , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
227
+ sturdyc .GetOrFetchBatch (ctx , client , id , client .BatchKeyFn ("item" ), fetchObserver .FetchBatch )
225
228
wg .Done ()
226
229
}(i )
227
230
}
@@ -260,9 +263,10 @@ func TestBatchesAreGroupedByPermutations(t *testing.T) {
260
263
// 1. The number of scheduled refreshes exceeds the specified 'batchSize'.
261
264
// 2. The 'batchBufferTimeout' threshold is exceeded.
262
265
c := sturdyc .New [any ](capacity , numShards , ttl , evictionPercentage ,
263
- sturdyc .WithBackgroundRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
266
+ sturdyc .WithNoContinuousEvictions (),
267
+ sturdyc .WithEarlyRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
264
268
sturdyc .WithMissingRecordStorage (),
265
- sturdyc .WithRefreshBuffering (batchSize , batchBufferTimeout ),
269
+ sturdyc .WithRefreshCoalescing (batchSize , batchBufferTimeout ),
266
270
sturdyc .WithClock (clock ),
267
271
)
268
272
@@ -283,9 +287,9 @@ func TestBatchesAreGroupedByPermutations(t *testing.T) {
283
287
284
288
fetchObserver := NewFetchObserver (1 )
285
289
fetchObserver .BatchResponse (seedIDs )
286
- sturdyc .GetFetchBatch (ctx , c , seedIDs , c .PermutatedBatchKeyFn (prefix , optsOne ), fetchObserver .FetchBatch )
290
+ sturdyc .GetOrFetchBatch (ctx , c , seedIDs , c .PermutatedBatchKeyFn (prefix , optsOne ), fetchObserver .FetchBatch )
287
291
<- fetchObserver .FetchCompleted
288
- sturdyc .GetFetchBatch (ctx , c , seedIDs , c .PermutatedBatchKeyFn (prefix , optsTwo ), fetchObserver .FetchBatch )
292
+ sturdyc .GetOrFetchBatch (ctx , c , seedIDs , c .PermutatedBatchKeyFn (prefix , optsTwo ), fetchObserver .FetchBatch )
289
293
<- fetchObserver .FetchCompleted
290
294
fetchObserver .AssertFetchCount (t , 2 )
291
295
fetchObserver .Clear ()
@@ -304,12 +308,12 @@ func TestBatchesAreGroupedByPermutations(t *testing.T) {
304
308
optsTwoBatch2 := []string {"6" , "7" , "8" }
305
309
306
310
// Request the first batch of records. This should wait for additional IDs.
307
- sturdyc .GetFetchBatch (ctx , c , optsOneIDs , c .PermutatedBatchKeyFn (prefix , optsOne ), fetchObserver .FetchBatch )
311
+ sturdyc .GetOrFetchBatch (ctx , c , optsOneIDs , c .PermutatedBatchKeyFn (prefix , optsOne ), fetchObserver .FetchBatch )
308
312
309
313
// Next, we're requesting ids 4-8 with the second options which should exceed the buffer size for that permutation.
310
314
fetchObserver .BatchResponse ([]string {"4" , "5" , "6" , "7" , "8" })
311
- sturdyc .GetFetchBatch (ctx , c , optsTwoBatch1 , c .PermutatedBatchKeyFn (prefix , optsTwo ), fetchObserver .FetchBatch )
312
- sturdyc .GetFetchBatch (ctx , c , optsTwoBatch2 , c .PermutatedBatchKeyFn (prefix , optsTwo ), fetchObserver .FetchBatch )
315
+ sturdyc .GetOrFetchBatch (ctx , c , optsTwoBatch1 , c .PermutatedBatchKeyFn (prefix , optsTwo ), fetchObserver .FetchBatch )
316
+ sturdyc .GetOrFetchBatch (ctx , c , optsTwoBatch2 , c .PermutatedBatchKeyFn (prefix , optsTwo ), fetchObserver .FetchBatch )
313
317
314
318
<- fetchObserver .FetchCompleted
315
319
fetchObserver .AssertFetchCount (t , 3 )
@@ -348,9 +352,10 @@ func TestLargeBatchesAreChunkedCorrectly(t *testing.T) {
348
352
// 1. The number of scheduled refreshes exceeds the specified 'batchSize'.
349
353
// 2. The 'batchBufferTimeout' threshold is exceeded.
350
354
client := sturdyc .New [string ](capacity , numShards , ttl , evictionPercentage ,
351
- sturdyc .WithBackgroundRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
355
+ sturdyc .WithNoContinuousEvictions (),
356
+ sturdyc .WithEarlyRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
352
357
sturdyc .WithMissingRecordStorage (),
353
- sturdyc .WithRefreshBuffering (batchSize , batchBufferTimeout ),
358
+ sturdyc .WithRefreshCoalescing (batchSize , batchBufferTimeout ),
354
359
sturdyc .WithClock (clock ),
355
360
)
356
361
@@ -363,7 +368,7 @@ func TestLargeBatchesAreChunkedCorrectly(t *testing.T) {
363
368
364
369
fetchObserver := NewFetchObserver (5 )
365
370
fetchObserver .BatchResponse (seedIDs )
366
- sturdyc .GetFetchBatch (ctx , client , seedIDs , client .BatchKeyFn (cacheKeyPrefix ), fetchObserver .FetchBatch )
371
+ sturdyc .GetOrFetchBatch (ctx , client , seedIDs , client .BatchKeyFn (cacheKeyPrefix ), fetchObserver .FetchBatch )
367
372
<- fetchObserver .FetchCompleted
368
373
fetchObserver .AssertFetchCount (t , 1 )
369
374
fetchObserver .AssertRequestedRecords (t , seedIDs )
@@ -379,7 +384,7 @@ func TestLargeBatchesAreChunkedCorrectly(t *testing.T) {
379
384
for i := 1 ; i <= 50 ; i ++ {
380
385
largeBatch = append (largeBatch , strconv .Itoa (i ))
381
386
}
382
- sturdyc .GetFetchBatch (ctx , client , largeBatch , client .BatchKeyFn (cacheKeyPrefix ), fetchObserver .FetchBatch )
387
+ sturdyc .GetOrFetchBatch (ctx , client , largeBatch , client .BatchKeyFn (cacheKeyPrefix ), fetchObserver .FetchBatch )
383
388
for i := 0 ; i < 10 ; i ++ {
384
389
<- fetchObserver .FetchCompleted
385
390
}
@@ -409,9 +414,10 @@ func TestValuesAreUpdatedCorrectly(t *testing.T) {
409
414
// 1. The number of scheduled refreshes exceeds the specified 'batchSize'.
410
415
// 2. The 'batchBufferTimeout' threshold is exceeded.
411
416
client := sturdyc .New [any ](capacity , numShards , ttl , evictionPercentage ,
412
- sturdyc .WithBackgroundRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
417
+ sturdyc .WithNoContinuousEvictions (),
418
+ sturdyc .WithEarlyRefreshes (minRefreshDelay , maxRefreshDelay , refreshRetryInterval ),
413
419
sturdyc .WithMissingRecordStorage (),
414
- sturdyc .WithRefreshBuffering (batchSize , batchBufferTimeout ),
420
+ sturdyc .WithRefreshCoalescing (batchSize , batchBufferTimeout ),
415
421
sturdyc .WithClock (clock ),
416
422
)
417
423
@@ -420,7 +426,7 @@ func TestValuesAreUpdatedCorrectly(t *testing.T) {
420
426
}
421
427
422
428
records := []string {"1" , "2" , "3" }
423
- res , _ := sturdyc .GetFetchBatch [Foo ](ctx , client , records , client .BatchKeyFn ("item" ), func (_ context.Context , ids []string ) (map [string ]Foo , error ) {
429
+ res , _ := sturdyc .GetOrFetchBatch [Foo ](ctx , client , records , client .BatchKeyFn ("item" ), func (_ context.Context , ids []string ) (map [string ]Foo , error ) {
424
430
values := make (map [string ]Foo , len (ids ))
425
431
for _ , id := range ids {
426
432
values [id ] = Foo {Value : "foo-" + id }
@@ -433,7 +439,7 @@ func TestValuesAreUpdatedCorrectly(t *testing.T) {
433
439
}
434
440
435
441
clock .Add (time .Minute * 45 )
436
- sturdyc .GetFetchBatch [Foo ](ctx , client , records , client .BatchKeyFn ("item" ), func (_ context.Context , ids []string ) (map [string ]Foo , error ) {
442
+ sturdyc .GetOrFetchBatch [Foo ](ctx , client , records , client .BatchKeyFn ("item" ), func (_ context.Context , ids []string ) (map [string ]Foo , error ) {
437
443
values := make (map [string ]Foo , len (ids ))
438
444
for _ , id := range ids {
439
445
values [id ] = Foo {Value : "foo2-" + id }
@@ -450,7 +456,7 @@ func TestValuesAreUpdatedCorrectly(t *testing.T) {
450
456
clock .Add (time .Minute * 5 )
451
457
time .Sleep (50 * time .Millisecond )
452
458
453
- resTwo , _ := sturdyc .GetFetchBatch [Foo ](ctx , client , records , client .BatchKeyFn ("item" ), func (_ context.Context , ids []string ) (map [string ]Foo , error ) {
459
+ resTwo , _ := sturdyc .GetOrFetchBatch [Foo ](ctx , client , records , client .BatchKeyFn ("item" ), func (_ context.Context , ids []string ) (map [string ]Foo , error ) {
454
460
values := make (map [string ]Foo , len (ids ))
455
461
for _ , id := range ids {
456
462
values [id ] = Foo {Value : "foo3-" + id }
0 commit comments