(t *testing.T)
| 221 | } |
| 222 | |
| 223 | func TestSmallTTLOnBigPayloadAreCacheWithFile(t *testing.T) { |
| 224 | cache, redis := getRedisCacheAndServer(t) |
| 225 | defer cache.Close() |
| 226 | key := &Key{ |
| 227 | Query: []byte(fmt.Sprintf("SELECT test")), |
| 228 | } |
| 229 | payloadSize := 4 * 1024 * 1024 |
| 230 | expectedValue := strings.Repeat("a", payloadSize) |
| 231 | reader := strings.NewReader(expectedValue) |
| 232 | |
| 233 | if _, err := cache.Put(reader, ContentMetadata{Encoding: "ce", Type: "ct", Length: int64(payloadSize)}, key); err != nil { |
| 234 | t.Fatalf("failed to put it to cache: %s", err) |
| 235 | } |
| 236 | |
| 237 | //simulate a value almost expired |
| 238 | redis.SetTTL(key.String(), 2*time.Second) |
| 239 | nbFileCacheBeforeGet, err := countFilesWithPrefix(os.TempDir(), redisTmpFilePrefix) |
| 240 | if err != nil { |
| 241 | t.Fatalf("could not read directory %s", err) |
| 242 | } |
| 243 | |
| 244 | cachedData, err := cache.Get(key) |
| 245 | if err != nil { |
| 246 | t.Fatalf("expected cached to have the value") |
| 247 | } |
| 248 | nbFileCacheAfterGet, err := countFilesWithPrefix(os.TempDir(), redisTmpFilePrefix) |
| 249 | if err != nil { |
| 250 | t.Fatalf("could not read directory %s", err) |
| 251 | } |
| 252 | if nbFileCacheBeforeGet+1 != nbFileCacheAfterGet { |
| 253 | t.Fatalf("expected a file to be stored by redisFileCache ") |
| 254 | } |
| 255 | |
| 256 | cachedValue, err := io.ReadAll(cachedData.Data) |
| 257 | if err != nil { |
| 258 | t.Fatalf("could not read data from redisFileCache, err=%s", err) |
| 259 | } |
| 260 | if string(cachedValue) != expectedValue { |
| 261 | t.Fatalf("got a value different than the expected one len(value)=%d vs len(expectedValue)=%d", len(string(cachedValue)), len(expectedValue)) |
| 262 | } |
| 263 | cachedData.Data.Close() |
| 264 | nbFileCacheAfterClose, err := countFilesWithPrefix(os.TempDir(), redisTmpFilePrefix) |
| 265 | if err != nil { |
| 266 | t.Fatalf("could not read directory %s", err) |
| 267 | } |
| 268 | |
| 269 | if nbFileCacheBeforeGet != nbFileCacheAfterClose { |
| 270 | t.Fatalf("expected the file stored by redisFileCache to be removed: nbFileCacheBeforeGet=%d | nbFileCacheAfterClose=%d", nbFileCacheBeforeGet, nbFileCacheAfterClose) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func countFilesWithPrefix(dir, prefix string) (int, error) { |
| 275 | count := 0 |
nothing calls this directly
no test coverage detected