This test exercises the basic insertion and lookup paths by inserting a known set of offsets which fit in the cache entirely. Also tries reading entries which are never inserted into the cache.
| 342 | // offsets which fit in the cache entirely. Also tries reading entries which are never |
| 343 | // inserted into the cache. |
| 344 | TEST_P(DataCacheTest, TestBasics) { |
| 345 | const int64_t cache_size = DEFAULT_CACHE_SIZE; |
| 346 | DataCache cache(Substitute("$0:$1", data_cache_dirs()[0], std::to_string(cache_size)), |
| 347 | FLAGS_data_cache_num_async_write_threads); |
| 348 | ASSERT_OK(cache.Init()); |
| 349 | |
| 350 | // Temporary buffer for holding results read from the cache. |
| 351 | uint8_t buffer[TEMP_BUFFER_SIZE]; |
| 352 | // Read and then insert a range of offsets. Expected all misses in the first iteration |
| 353 | // and all hits in the second iteration. |
| 354 | for (int i = 0; i < 2; ++i) { |
| 355 | for (int64_t offset = 0; offset < NUM_CACHE_ENTRIES_NO_EVICT; ++offset) { |
| 356 | int expected_bytes = i * TEMP_BUFFER_SIZE; |
| 357 | memset(buffer, 0, TEMP_BUFFER_SIZE); |
| 358 | ASSERT_EQ(expected_bytes, |
| 359 | cache.Lookup(FNAME, MTIME, offset, TEMP_BUFFER_SIZE, buffer)) << offset; |
| 360 | if (i == 0) { |
| 361 | ASSERT_TRUE(cache.Store(FNAME, MTIME, offset, test_buffer() + offset, |
| 362 | TEMP_BUFFER_SIZE)); |
| 363 | } else { |
| 364 | ASSERT_EQ(0, memcmp(test_buffer() + offset, buffer, TEMP_BUFFER_SIZE)); |
| 365 | } |
| 366 | } |
| 367 | // Before continue, we should wait for all asynchronous store tasks (if there are) |
| 368 | // finished. |
| 369 | WaitForAsyncWrite(cache); |
| 370 | } |
| 371 | |
| 372 | // Read the same range inserted previously but with a different filename. |
| 373 | for (int64_t offset = NUM_CACHE_ENTRIES_NO_EVICT; offset < TEST_BUFFER_SIZE; |
| 374 | ++offset) { |
| 375 | const string& alt_fname = "random"; |
| 376 | ASSERT_EQ(0, cache.Lookup(alt_fname, MTIME, offset, TEMP_BUFFER_SIZE, buffer)); |
| 377 | } |
| 378 | |
| 379 | // Read the same range inserted previously but with a different mtime. |
| 380 | for (int64_t offset = NUM_CACHE_ENTRIES_NO_EVICT; offset < TEST_BUFFER_SIZE; |
| 381 | ++offset) { |
| 382 | int64_t alt_mtime = 67890; |
| 383 | ASSERT_EQ(0, cache.Lookup(FNAME, alt_mtime, offset, TEMP_BUFFER_SIZE, buffer)); |
| 384 | } |
| 385 | |
| 386 | // Read a range of offsets which should miss in the cache. |
| 387 | for (int64_t offset = NUM_CACHE_ENTRIES_NO_EVICT; offset < TEST_BUFFER_SIZE; |
| 388 | ++offset) { |
| 389 | ASSERT_EQ(0, cache.Lookup(FNAME, MTIME, offset, TEMP_BUFFER_SIZE, buffer)); |
| 390 | } |
| 391 | |
| 392 | // Read the same same range inserted previously. They should still all be in the cache. |
| 393 | for (int64_t offset = 0; offset < NUM_CACHE_ENTRIES_NO_EVICT; ++offset) { |
| 394 | memset(buffer, 0, TEMP_BUFFER_SIZE); |
| 395 | ASSERT_EQ(TEMP_BUFFER_SIZE, |
| 396 | cache.Lookup(FNAME, MTIME, offset, TEMP_BUFFER_SIZE + 10, buffer)); |
| 397 | ASSERT_EQ(0, memcmp(test_buffer() + offset, buffer, TEMP_BUFFER_SIZE)); |
| 398 | ASSERT_EQ(TEMP_BUFFER_SIZE - 10, |
| 399 | cache.Lookup(FNAME, MTIME, offset, TEMP_BUFFER_SIZE - 10, buffer)); |
| 400 | ASSERT_EQ(0, memcmp(test_buffer() + offset, buffer, TEMP_BUFFER_SIZE - 10)); |
| 401 | } |
nothing calls this directly
no test coverage detected