testCacheTTLExpiration tests TTL expiration behavior.
(t *testing.T, c interfaces.Cache)
| 203 | |
| 204 | // testCacheTTLExpiration tests TTL expiration behavior. |
| 205 | func testCacheTTLExpiration(t *testing.T, c interfaces.Cache) { |
| 206 | t.Run("short_ttl_expiration", func(t *testing.T) { |
| 207 | key := "ttl-test" |
| 208 | value := "expires-soon" |
| 209 | ttl := time.Second |
| 210 | |
| 211 | // Set value with short TTL |
| 212 | err := c.Set(key, value, ttl) |
| 213 | require.NoError(t, err) |
| 214 | |
| 215 | // Get immediately - should exist |
| 216 | var result string |
| 217 | found, err := c.Get(key, &result) |
| 218 | require.NoError(t, err) |
| 219 | assert.True(t, found) |
| 220 | assert.Equal(t, value, result) |
| 221 | |
| 222 | // Wait for expiration |
| 223 | time.Sleep(ttl + time.Second) |
| 224 | |
| 225 | // Get after expiration - should not exist |
| 226 | found, err = c.Get(key, &result) |
| 227 | require.NoError(t, err) |
| 228 | assert.False(t, found) |
| 229 | }) |
| 230 | |
| 231 | t.Run("zero_ttl_no_expiration", func(t *testing.T) { |
| 232 | key := "no-ttl-test" |
| 233 | value := "never-expires" |
| 234 | |
| 235 | // Set value with zero TTL (no expiration) |
| 236 | err := c.Set(key, value, 0) |
| 237 | require.NoError(t, err) |
| 238 | |
| 239 | // Wait a bit |
| 240 | time.Sleep(100 * time.Millisecond) |
| 241 | |
| 242 | // Should still exist |
| 243 | var result string |
| 244 | found, err := c.Get(key, &result) |
| 245 | require.NoError(t, err) |
| 246 | assert.True(t, found) |
| 247 | assert.Equal(t, value, result) |
| 248 | }) |
| 249 | } |
| 250 | |
| 251 | // testCacheComplexDataTypes tests caching of complex data structures. |
| 252 | func testCacheComplexDataTypes(t *testing.T, c interfaces.Cache) { |
no test coverage detected