| 1 | package gocachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.24.4 fmt/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.24.4 testing/" |
| 6 | "timescip-go gomod github.com/golang/go/src go1.24.4 time/" |
| 7 | ) |
| 8 | |
| 9 | func TestCache_StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/TestCache_StartJanitor().(t *testingscip-go gomod github.com/golang/go/src go1.24.4 testing/.Tscip-go gomod github.com/golang/go/src go1.24.4 testing/T#) { |
| 10 | cache := NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().() |
| 11 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().("1", "1", timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nanosecondscip-go gomod github.com/golang/go/src go1.24.4 time/Nanosecond.) |
| 12 | if cacheSize := cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().(); cacheSize != 1 { |
| 13 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Errorf().("expected cacheSize to be 1, but was %d", cacheSize) |
| 14 | } |
| 15 | err := cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().() |
| 16 | if err != nil { |
| 17 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Fatal().(err) |
| 18 | } |
| 19 | defer cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 20 | timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.4 time/Sleep().(JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. * 2) |
| 21 | if cacheSize := cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().(); cacheSize != 0 { |
| 22 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Errorf().("expected cacheSize to be 0, but was %d", cacheSize) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestCache_StartJanitorWhenAlreadyStartedscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/TestCache_StartJanitorWhenAlreadyStarted().(t *testingscip-go gomod github.com/golang/go/src go1.24.4 testing/.Tscip-go gomod github.com/golang/go/src go1.24.4 testing/T#) { |
| 27 | cache := NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().() |
| 28 | if err := cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().(); err != nil { |
| 29 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Fatal().(err) |
| 30 | } |
| 31 | if err := cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().(); err == nil { |
| 32 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Fatal().("expected StartJanitor to return an error, because the janitor is already started") |
| 33 | } |
| 34 | cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 35 | } |
| 36 | |
| 37 | func TestCache_StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/TestCache_StopJanitor().(t *testingscip-go gomod github.com/golang/go/src go1.24.4 testing/.Tscip-go gomod github.com/golang/go/src go1.24.4 testing/T#) { |
| 38 | cache := NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().() |
| 39 | _ = cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().() |
| 40 | if cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. == nil { |
| 41 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("starting the janitor should've initialized cache.stopJanitor") |
| 42 | } |
| 43 | cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 44 | if cache.stopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#stopJanitor. != nil { |
| 45 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("stopping the janitor should've set cache.stopJanitor to nil") |
| 46 | } |
| 47 | // Check if stopping the janitor even though it's already stopped causes a panic |
| 48 | cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 49 | } |
| 50 | |
| 51 | func TestJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/TestJanitor().(t *testingscip-go gomod github.com/golang/go/src go1.24.4 testing/.Tscip-go gomod github.com/golang/go/src go1.24.4 testing/T#) { |
| 52 | cache := NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().().WithMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithMaxSize().(3 * JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift.) |
| 53 | defer cache.Clearscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Clear().() |
| 54 | for i := 0; i < 3*JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift.; i++ { |
| 55 | if i < JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift. && i%2 == 0 { |
| 56 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().(fmtscip-go gomod github.com/golang/go/src go1.24.4 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.4 fmt/Sprintf().("%d", i), "value", timescip-go gomod github.com/golang/go/src go1.24.4 time/.Millisecondscip-go gomod github.com/golang/go/src go1.24.4 time/Millisecond.) |
| 57 | } else { |
| 58 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().(fmtscip-go gomod github.com/golang/go/src go1.24.4 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.4 fmt/Sprintf().("%d", i), "value", timescip-go gomod github.com/golang/go/src go1.24.4 time/.Hourscip-go gomod github.com/golang/go/src go1.24.4 time/Hour.) |
| 59 | } |
| 60 | } |
| 61 | cacheSize := cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() |
| 62 | err := cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().() |
| 63 | if err != nil { |
| 64 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Fatal().(err) |
| 65 | } |
| 66 | defer cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 67 | timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.4 time/Sleep().(JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. * 4) |
| 68 | if cacheSize <= cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() { |
| 69 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("The janitor should be deleting expired cache entries") |
| 70 | } |
| 71 | cacheSize = cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() |
| 72 | timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.4 time/Sleep().(JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. * 4) |
| 73 | if cacheSize <= cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() { |
| 74 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("The janitor should be deleting expired cache entries") |
| 75 | } |
| 76 | cacheSize = cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() |
| 77 | timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.4 time/Sleep().(JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. * 4) |
| 78 | if cacheSize <= cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() { |
| 79 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("The janitor should be deleting expired cache entries") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestJanitorIsLoopingProperlyscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/TestJanitorIsLoopingProperly().(t *testingscip-go gomod github.com/golang/go/src go1.24.4 testing/.Tscip-go gomod github.com/golang/go/src go1.24.4 testing/T#) { |
| 84 | cache := NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().().WithMaxSizescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#WithMaxSize().(JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift. + 3) |
| 85 | defer cache.Clearscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Clear().() |
| 86 | for i := 0; i < JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift.; i++ { |
| 87 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().(fmtscip-go gomod github.com/golang/go/src go1.24.4 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.4 fmt/Sprintf().("%d", i), "value", timescip-go gomod github.com/golang/go/src go1.24.4 time/.Hourscip-go gomod github.com/golang/go/src go1.24.4 time/Hour.) |
| 88 | } |
| 89 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().("key-to-expire-1", "value", JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff.*2) |
| 90 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().("key-to-expire-2", "value", JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff.*2) |
| 91 | cache.SetWithTTLscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#SetWithTTL().("key-to-expire-3", "value", JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff.*2) |
| 92 | err := cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().() |
| 93 | if err != nil { |
| 94 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Fatal().(err) |
| 95 | } |
| 96 | defer cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 97 | if cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() != JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift.+3 { |
| 98 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("The janitor shouldn't have had enough time to remove anything from the cache yet", cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().()) |
| 99 | } |
| 100 | const timeout = JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff. * 20 |
| 101 | threeKeysExpiredWithinOneSecond := false |
| 102 | for start := timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().(); timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sincescip-go gomod github.com/golang/go/src go1.24.4 time/Since().(start) < timeout; { |
| 103 | if cache.Statsscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Stats().().ExpiredKeysscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Statistics#ExpiredKeys. == 3 { |
| 104 | threeKeysExpiredWithinOneSecond = true |
| 105 | break |
| 106 | } |
| 107 | timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.4 time/Sleep().(JanitorMinShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMinShiftBackOff.) |
| 108 | } |
| 109 | if !threeKeysExpiredWithinOneSecond { |
| 110 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("expected 3 keys to expire within 1 second") |
| 111 | } |
| 112 | if cache.Countscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#Count().() != JanitorMaxIterationsPerShiftscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxIterationsPerShift. { |
| 113 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("The janitor should've deleted 3 entries") |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestJanitorDoesNotThrowATantrumWhenThereIsNothingToCleanscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/TestJanitorDoesNotThrowATantrumWhenThereIsNothingToClean().(t *testingscip-go gomod github.com/golang/go/src go1.24.4 testing/.Tscip-go gomod github.com/golang/go/src go1.24.4 testing/T#) { |
| 118 | cache := NewCachescip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/NewCache().() |
| 119 | start := timescip-go gomod github.com/golang/go/src go1.24.4 time/.Nowscip-go gomod github.com/golang/go/src go1.24.4 time/Now().() |
| 120 | _ = cache.StartJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StartJanitor().() |
| 121 | defer cache.StopJanitorscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/Cache#StopJanitor().() |
| 122 | timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sleepscip-go gomod github.com/golang/go/src go1.24.4 time/Sleep().(JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff. * 3) |
| 123 | // Technically, if the janitor doesn't backoff properly, the sleep above is likely to take more time than the sleep |
| 124 | // below because it would be eating up the CPU. |
| 125 | // This is a far-fetched test, but it's a good sanity check. |
| 126 | if timescip-go gomod github.com/golang/go/src go1.24.4 time/.Sincescip-go gomod github.com/golang/go/src go1.24.4 time/Since().(start) > JanitorMaxShiftBackOffscip-go gomod github.com/TwiN/gocache/v2 . `github.com/TwiN/gocache/v2`/JanitorMaxShiftBackOff.*4 { |
| 127 | t.Errorscip-go gomod github.com/golang/go/src go1.24.4 testing/common#Error().("The janitor should've backed off and prevented CPU usage from throttling the application") |
| 128 | } |
| 129 | } |