| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
func TestDeletedCache(t *testing.T) { |
| 10 |
ctx, cancel := context.WithCancel(context.Background()) |
| 11 |
defer cancel() |
| 12 |
|
| 13 |
nc := NewContext[string, int](ctx) |
| 14 |
key := "key" |
| 15 |
nc.Set(key, 1, WithExpiration(-time.Second)) |
| 16 |
|
| 17 |
_, ok := nc.cache.Get(key) |
| 18 |
if !ok { |
| 19 |
t.Fatal("want true") |
| 20 |
} |
| 21 |
|
| 22 |
nc.DeleteExpired() |
| 23 |
|
| 24 |
_, ok = nc.cache.Get(key) |
| 25 |
if ok { |
| 26 |
t.Fatal("want false") |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestDeleteExpired(t *testing.T) { |
| 31 |
now := time.Now() |
| 32 |
restore := func() { |
| 33 |
nowFunc = time.Now |
| 34 |
} |
| 35 |
|
| 36 |
t.Run("normal", func(t *testing.T) { |
| 37 |
defer restore() |
| 38 |
c := New[string, int]() |
| 39 |
|
| 40 |
c.Set("0", 0) |
| 41 |
c.Set("1", 10, WithExpiration(10*time.Millisecond)) |
| 42 |
c.Set("2", 20, WithExpiration(20*time.Millisecond)) |
| 43 |
c.Set("3", 30, WithExpiration(30*time.Millisecond)) |
| 44 |
c.Set("4", 40, WithExpiration(40*time.Millisecond)) |
| 45 |
c.Set("5", 50) |
| 46 |
|
| 47 |
maxItems := c.Len() |
| 48 |
|
| 49 |
expItems := 2 |
| 50 |
|
| 51 |
for i := 0; i <= maxItems; i++ { |
| 52 |
nowFunc = func() time.Time { |
| 53 |
|
| 54 |
advanced := time.Duration(i * 10) |
| 55 |
return now.Add(advanced * time.Millisecond).Add(time.Millisecond) |
| 56 |
} |
| 57 |
|
| 58 |
c.DeleteExpired() |
| 59 |
|
| 60 |
got := c.Len() |
| 61 |
want := max(maxItems-i, expItems) |
| 62 |
if want != got { |
| 63 |
t.Errorf("want %d items but got %d", want, got) |
| 64 |
} |
| 65 |
} |
| 66 |
}) |
| 67 |
|
| 68 |
t.Run("with remove", func(t *testing.T) { |
| 69 |
defer restore() |
| 70 |
c := New[string, int]() |
| 71 |
|
| 72 |
c.Set("0", 0) |
| 73 |
c.Set("1", 10, WithExpiration(10*time.Millisecond)) |
| 74 |
c.Set("2", 20, WithExpiration(20*time.Millisecond)) |
| 75 |
|
| 76 |
c.Delete("1") |
| 77 |
|
| 78 |
nowFunc = func() time.Time { |
| 79 |
return now.Add(30 * time.Millisecond).Add(time.Millisecond) |
| 80 |
} |
| 81 |
|
| 82 |
c.DeleteExpired() |
| 83 |
|
| 84 |
keys := c.Keys() |
| 85 |
want := 1 |
| 86 |
if want != len(keys) { |
| 87 |
t.Errorf("want %d items but got %d", want, len(keys)) |
| 88 |
} |
| 89 |
}) |
| 90 |
|
| 91 |
t.Run("with update", func(t *testing.T) { |
| 92 |
defer restore() |
| 93 |
c := New[string, int]() |
| 94 |
|
| 95 |
c.Set("0", 0) |
| 96 |
c.Set("1", 10, WithExpiration(10*time.Millisecond)) |
| 97 |
c.Set("2", 20, WithExpiration(20*time.Millisecond)) |
| 98 |
c.Set("1", 30, WithExpiration(30*time.Millisecond)) |
| 99 |
|
| 100 |
maxItems := c.Len() |
| 101 |
|
| 102 |
nowFunc = func() time.Time { |
| 103 |
return now.Add(10 * time.Millisecond).Add(time.Millisecond) |
| 104 |
} |
| 105 |
|
| 106 |
c.DeleteExpired() |
| 107 |
|
| 108 |
got1 := c.Len() |
| 109 |
want1 := maxItems |
| 110 |
if want1 != got1 { |
| 111 |
t.Errorf("want1 %d items but got1 %d", want1, got1) |
| 112 |
} |
| 113 |
|
| 114 |
nowFunc = func() time.Time { |
| 115 |
return now.Add(30 * time.Millisecond).Add(time.Millisecond) |
| 116 |
} |
| 117 |
|
| 118 |
c.DeleteExpired() |
| 119 |
|
| 120 |
got2 := c.Len() |
| 121 |
want2 := 1 |
| 122 |
if want2 != got2 { |
| 123 |
t.Errorf("want2 %d items but got2 %d", want2, got2) |
| 124 |
} |
| 125 |
}) |
| 126 |
|
| 127 |
t.Run("issue #51", func(t *testing.T) { |
| 128 |
defer restore() |
| 129 |
c := New[string, int]() |
| 130 |
|
| 131 |
c.Set("1", 10, WithExpiration(10*time.Millisecond)) |
| 132 |
c.Set("2", 20, WithExpiration(20*time.Millisecond)) |
| 133 |
c.Set("1", 30, WithExpiration(100*time.Millisecond)) |
| 134 |
|
| 135 |
nowFunc = func() time.Time { |
| 136 |
return now.Add(30 * time.Millisecond).Add(time.Millisecond) |
| 137 |
} |
| 138 |
|
| 139 |
c.DeleteExpired() |
| 140 |
|
| 141 |
got := c.Len() |
| 142 |
if want := 1; want != got { |
| 143 |
t.Errorf("want %d items but got %d", want, got) |
| 144 |
} |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
func max(x, y int) int { |
| 149 |
if x < y { |
| 150 |
return y |
| 151 |
} |
| 152 |
return x |
| 153 |
} |