@@ -2,6 +2,7 @@ package pocache
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"sync/atomic"
7
8
"testing"
@@ -153,66 +154,51 @@ func TestCache(tt *testing.T) {
153
154
asserter .True (found )
154
155
})
155
156
156
- tt .Run ("err watcher" , func (t * testing.T ) {
157
- forcedErr := fmt .Errorf ("forced error" )
158
- ranUpdater := atomic.Bool {}
159
- ranErrWatcher := atomic.Bool {}
160
-
157
+ tt .Run ("disabled" , func (t * testing.T ) {
161
158
cache , err := New (Config [string , any ]{
162
159
LRUCacheSize : 10000 ,
163
160
CacheAge : time .Minute ,
164
161
Threshold : time .Second * 59 ,
165
- DisableCache : false ,
162
+ DisableCache : true ,
166
163
Updater : func (ctx context.Context , key string ) (any , error ) {
167
- ranUpdater .Store (true )
168
- return nil , forcedErr
169
- },
170
- ErrWatcher : func (watcherErr error ) {
171
- ranErrWatcher .Store (true )
172
- asserter .ErrorIs (watcherErr , forcedErr )
164
+ return key , nil
173
165
},
174
166
})
175
167
requirer .NoError (err )
176
168
177
169
_ = cache .BulkAdd ([]Tuple [string , any ]{{Key : prefix , Value : value }})
178
170
// wait for threshold window
179
- time .Sleep (time .Second )
171
+ time .Sleep (time .Second * 2 )
172
+
180
173
// trigger auto update within threshold window
181
174
_ = cache .Get (prefix )
182
175
183
- // wait for the updater callback to be executed
184
- time .Sleep (time .Second * 2 )
185
- asserter . True ( ranUpdater . Load () )
186
- asserter .True ( ranErrWatcher . Load () )
176
+ // wait for updater to be executed
177
+ time .Sleep (time .Second * 1 )
178
+ v := cache . Get ( prefix )
179
+ asserter .False ( v . Found )
187
180
})
188
181
189
- tt .Run ("no err watcher" , func (t * testing.T ) {
190
- forcedErr := fmt .Errorf ("forced error" )
191
- ranUpdater := atomic.Bool {}
192
- ranErrWatcher := atomic.Bool {}
193
-
182
+ tt .Run ("no updater" , func (t * testing.T ) {
194
183
cache , err := New (Config [string , any ]{
195
184
LRUCacheSize : 10000 ,
196
185
CacheAge : time .Minute ,
197
186
Threshold : time .Second * 59 ,
198
187
DisableCache : false ,
199
- Updater : func (ctx context.Context , key string ) (any , error ) {
200
- ranUpdater .Store (true )
201
- return nil , forcedErr
202
- },
188
+ Updater : nil ,
203
189
})
204
190
requirer .NoError (err )
205
191
206
- _ = cache .BulkAdd ([] Tuple [ string , any ]{{ Key : prefix , Value : value }} )
192
+ _ = cache .Add ( prefix , value )
207
193
// wait for threshold window
208
- time .Sleep (time .Second )
194
+ time .Sleep (time .Second * 2 )
209
195
// trigger auto update within threshold window
210
196
_ = cache .Get (prefix )
211
-
212
- // wait for the updater callback to be executed
197
+ // wait for updater to run
213
198
time .Sleep (time .Second * 2 )
214
- asserter .True (ranUpdater .Load ())
215
- asserter .False (ranErrWatcher .Load ())
199
+
200
+ v := cache .Get (prefix )
201
+ asserter .EqualValues (value , v .V )
216
202
})
217
203
218
204
}
@@ -369,3 +355,142 @@ func TestPayload(tt *testing.T) {
369
355
asserter .EqualValues (time.Time {}, pyl .Expiry ())
370
356
})
371
357
}
358
+
359
+ func TestErrWatcher (tt * testing.T ) {
360
+ var (
361
+ prefix = "prefix"
362
+ value = "value"
363
+ requirer = require .New (tt )
364
+ asserter = require .New (tt )
365
+ )
366
+
367
+ tt .Run ("err watcher" , func (t * testing.T ) {
368
+ forcedErr := fmt .Errorf ("forced error" )
369
+ ranUpdater := atomic.Bool {}
370
+ ranErrWatcher := atomic.Bool {}
371
+
372
+ cache , err := New (Config [string , any ]{
373
+ LRUCacheSize : 10000 ,
374
+ CacheAge : time .Minute ,
375
+ Threshold : time .Second * 59 ,
376
+ DisableCache : false ,
377
+ Updater : func (ctx context.Context , key string ) (any , error ) {
378
+ ranUpdater .Store (true )
379
+ return nil , forcedErr
380
+ },
381
+ ErrWatcher : func (watcherErr error ) {
382
+ ranErrWatcher .Store (true )
383
+ asserter .ErrorIs (watcherErr , forcedErr )
384
+ },
385
+ })
386
+ requirer .NoError (err )
387
+
388
+ _ = cache .BulkAdd ([]Tuple [string , any ]{{Key : prefix , Value : value }})
389
+ // wait for threshold window
390
+ time .Sleep (time .Second )
391
+ // trigger auto update within threshold window
392
+ _ = cache .Get (prefix )
393
+
394
+ // wait for the updater callback to be executed
395
+ time .Sleep (time .Second * 2 )
396
+ asserter .True (ranUpdater .Load ())
397
+ asserter .True (ranErrWatcher .Load ())
398
+ })
399
+
400
+ tt .Run ("no err watcher" , func (t * testing.T ) {
401
+ forcedErr := fmt .Errorf ("forced error" )
402
+ ranUpdater := atomic.Bool {}
403
+ ranErrWatcher := atomic.Bool {}
404
+
405
+ cache , err := New (Config [string , any ]{
406
+ LRUCacheSize : 10000 ,
407
+ CacheAge : time .Minute ,
408
+ Threshold : time .Second * 59 ,
409
+ DisableCache : false ,
410
+ Updater : func (ctx context.Context , key string ) (any , error ) {
411
+ ranUpdater .Store (true )
412
+ return nil , forcedErr
413
+ },
414
+ })
415
+ requirer .NoError (err )
416
+
417
+ _ = cache .BulkAdd ([]Tuple [string , any ]{{Key : prefix , Value : value }})
418
+ // wait for threshold window
419
+ time .Sleep (time .Second )
420
+ // trigger auto update within threshold window
421
+ _ = cache .Get (prefix )
422
+
423
+ // wait for the updater callback to be executed
424
+ time .Sleep (time .Second * 2 )
425
+ asserter .True (ranUpdater .Load ())
426
+ asserter .False (ranErrWatcher .Load ())
427
+ })
428
+
429
+ tt .Run ("err watcher: catch panic text" , func (t * testing.T ) {
430
+ ranUpdater := atomic.Bool {}
431
+ ranErrWatcher := atomic.Bool {}
432
+
433
+ cache , err := New (Config [string , any ]{
434
+ LRUCacheSize : 10000 ,
435
+ CacheAge : time .Minute ,
436
+ Threshold : time .Second * 59 ,
437
+ DisableCache : false ,
438
+ Updater : func (ctx context.Context , key string ) (any , error ) {
439
+ ranUpdater .Store (true )
440
+ panic ("force panicked" )
441
+ },
442
+ ErrWatcher : func (watcherErr error ) {
443
+ ranErrWatcher .Store (true )
444
+ asserter .ErrorContains (watcherErr , "force panicked" )
445
+ },
446
+ })
447
+ requirer .NoError (err )
448
+ cache .Add (prefix , value )
449
+
450
+ // wait for threshold window
451
+ time .Sleep (time .Second )
452
+ // trigger auto update within threshold window
453
+ _ = cache .Get (prefix )
454
+
455
+ // wait for the updater callback to be executed
456
+ time .Sleep (time .Second * 2 )
457
+ asserter .True (ranUpdater .Load ())
458
+ asserter .True (ranErrWatcher .Load ())
459
+
460
+ })
461
+
462
+ tt .Run ("err watcher: catch panic err" , func (t * testing.T ) {
463
+ ranUpdater := atomic.Bool {}
464
+ ranErrWatcher := atomic.Bool {}
465
+ ErrPanic := errors .New ("panic err" )
466
+
467
+ cache , err := New (Config [string , any ]{
468
+ LRUCacheSize : 10000 ,
469
+ CacheAge : time .Minute ,
470
+ Threshold : time .Second * 59 ,
471
+ DisableCache : false ,
472
+ Updater : func (ctx context.Context , key string ) (any , error ) {
473
+ ranUpdater .Store (true )
474
+ panic (ErrPanic )
475
+ },
476
+ ErrWatcher : func (watcherErr error ) {
477
+ ranErrWatcher .Store (true )
478
+ asserter .ErrorIs (watcherErr , ErrPanic )
479
+ },
480
+ })
481
+ requirer .NoError (err )
482
+ cache .Add (prefix , value )
483
+
484
+ // wait for threshold window
485
+ time .Sleep (time .Second )
486
+ // trigger auto update within threshold window
487
+ _ = cache .Get (prefix )
488
+
489
+ // wait for the updater callback to be executed
490
+ time .Sleep (time .Second * 2 )
491
+ asserter .True (ranUpdater .Load ())
492
+ asserter .True (ranErrWatcher .Load ())
493
+
494
+ })
495
+
496
+ }
0 commit comments