TestInMemoryCache_Close verifies Close stops the sweep goroutine (no leak).
(t *testing.T)
| 141 | |
| 142 | // TestInMemoryCache_Close verifies Close stops the sweep goroutine (no leak). |
| 143 | func TestInMemoryCache_Close(t *testing.T) { |
| 144 | c := NewInMemoryCache[string]( |
| 145 | WithSweepInterval[string](10 * time.Millisecond), |
| 146 | ) |
| 147 | ctx := context.Background() |
| 148 | c.Set(ctx, "k", "v", 0) |
| 149 | |
| 150 | c.Close() |
| 151 | // Close should be idempotent |
| 152 | c.Close() |
| 153 | |
| 154 | // After Close, cache is still readable for lazy access but sweep is stopped |
| 155 | if _, ok := c.Get(ctx, "k"); !ok { |
| 156 | t.Error("Get should still work after Close") |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // TestInMemoryCache_ConcurrentSweepAndSet verifies no race between sweep and Set. |
| 161 | func TestInMemoryCache_ConcurrentSweepAndSet(t *testing.T) { |