| 1 | package gocache |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestCache_StartJanitor(t *testing.T) { |
| 10 | cache := NewCache() |
| 11 | cache.SetWithTTL("1", "1", time.Nanosecond) |
| 12 | if cacheSize := cache.Count(); cacheSize != 1 { |
| 13 | t.Errorf("expected cacheSize to be 1, but was %d", cacheSize) |
| 14 | } |
| 15 | err := cache.StartJanitor() |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | defer cache.StopJanitor() |
| 20 | time.Sleep(JanitorMinShiftBackOff * 2) |
| 21 | if cacheSize := cache.Count(); cacheSize != 0 { |
| 22 | t.Errorf("expected cacheSize to be 0, but was %d", cacheSize) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestCache_StartJanitorWhenAlreadyStarted(t *testing.T) { |
| 27 | cache := NewCache() |
| 28 | if err := cache.StartJanitor(); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if err := cache.StartJanitor(); err == nil { |
| 32 | t.Fatal("expected StartJanitor to return an error, because the janitor is already started") |
| 33 | } |
| 34 | cache.StopJanitor() |
| 35 | } |
| 36 | |
| 37 | func TestCache_StopJanitor(t *testing.T) { |
| 38 | cache := NewCache() |
| 39 | _ = cache.StartJanitor() |
| 40 | if cache.stopJanitor == nil { |
| 41 | t.Error("starting the janitor should've initialized cache.stopJanitor") |
| 42 | } |
| 43 | cache.StopJanitor() |
| 44 | if cache.stopJanitor != nil { |
| 45 | t.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.StopJanitor() |
| 49 | } |
| 50 | |
| 51 | func TestJanitor(t *testing.T) { |
| 52 | cache := NewCache().WithMaxSize(3 * JanitorMaxIterationsPerShift) |
| 53 | defer cache.Clear() |
| 54 | for i := 0; i < 3*JanitorMaxIterationsPerShift; i++ { |
| 55 | if i < JanitorMaxIterationsPerShift && i%2 == 0 { |
| 56 | cache.SetWithTTL(fmt.Sprintf("%d", i), "value", time.Millisecond) |
| 57 | } else { |
| 58 | cache.SetWithTTL(fmt.Sprintf("%d", i), "value", time.Hour) |
| 59 | } |
| 60 | } |
| 61 | cacheSize := cache.Count() |
| 62 | err := cache.StartJanitor() |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | defer cache.StopJanitor() |
| 67 | time.Sleep(JanitorMinShiftBackOff * 4) |
| 68 | if cacheSize <= cache.Count() { |
| 69 | t.Error("The janitor should be deleting expired cache entries") |
| 70 | } |
| 71 | cacheSize = cache.Count() |
| 72 | time.Sleep(JanitorMinShiftBackOff * 4) |
| 73 | if cacheSize <= cache.Count() { |
| 74 | t.Error("The janitor should be deleting expired cache entries") |
| 75 | } |
| 76 | cacheSize = cache.Count() |
| 77 | time.Sleep(JanitorMinShiftBackOff * 4) |
| 78 | if cacheSize <= cache.Count() { |
| 79 | t.Error("The janitor should be deleting expired cache entries") |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestJanitorIsLoopingProperly(t *testing.T) { |
| 84 | cache := NewCache().WithMaxSize(JanitorMaxIterationsPerShift + 3) |
| 85 | defer cache.Clear() |
| 86 | for i := 0; i < JanitorMaxIterationsPerShift; i++ { |
| 87 | cache.SetWithTTL(fmt.Sprintf("%d", i), "value", time.Hour) |
| 88 | } |
| 89 | cache.SetWithTTL("key-to-expire-1", "value", JanitorMinShiftBackOff*2) |
| 90 | cache.SetWithTTL("key-to-expire-2", "value", JanitorMinShiftBackOff*2) |
| 91 | cache.SetWithTTL("key-to-expire-3", "value", JanitorMinShiftBackOff*2) |
| 92 | err := cache.StartJanitor() |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | defer cache.StopJanitor() |
| 97 | if cache.Count() != JanitorMaxIterationsPerShift+3 { |
| 98 | t.Error("The janitor shouldn't have had enough time to remove anything from the cache yet", cache.Count()) |
| 99 | } |
| 100 | const timeout = JanitorMinShiftBackOff * 20 |
| 101 | threeKeysExpiredWithinOneSecond := false |
| 102 | for start := time.Now(); time.Since(start) < timeout; { |
| 103 | if cache.Stats().ExpiredKeys == 3 { |
| 104 | threeKeysExpiredWithinOneSecond = true |
| 105 | break |
| 106 | } |
| 107 | time.Sleep(JanitorMinShiftBackOff) |
| 108 | } |
| 109 | if !threeKeysExpiredWithinOneSecond { |
| 110 | t.Error("expected 3 keys to expire within 1 second") |
| 111 | } |
| 112 | if cache.Count() != JanitorMaxIterationsPerShift { |
| 113 | t.Error("The janitor should've deleted 3 entries") |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func TestJanitorDoesNotThrowATantrumWhenThereIsNothingToClean(t *testing.T) { |
| 118 | cache := NewCache() |
| 119 | start := time.Now() |
| 120 | _ = cache.StartJanitor() |
| 121 | defer cache.StopJanitor() |
| 122 | time.Sleep(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 time.Since(start) > JanitorMaxShiftBackOff*4 { |
| 127 | t.Error("The janitor should've backed off and prevented CPU usage from throttling the application") |
| 128 | } |
| 129 | } |