|
22 | 22 | import org.opensearch.test.OpenSearchSingleNodeTestCase;
|
23 | 23 |
|
24 | 24 | import java.io.IOException;
|
| 25 | +import java.util.ArrayList; |
25 | 26 | import java.util.HashMap;
|
26 | 27 | import java.util.Iterator;
|
27 | 28 | import java.util.List;
|
@@ -481,6 +482,49 @@ public String load(String key) throws Exception {
|
481 | 482 | }
|
482 | 483 | }
|
483 | 484 |
|
| 485 | + public void testEhcacheKeyIteratorWithRemove() throws IOException { |
| 486 | + Settings settings = Settings.builder().build(); |
| 487 | + try (NodeEnvironment env = newNodeEnvironment(settings)) { |
| 488 | + ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setDiskCacheAlias("test1") |
| 489 | + .setThreadPoolAlias("ehcacheTest") |
| 490 | + .setStoragePath(env.nodePaths()[0].indicesPath.toString() + "/request_cache") |
| 491 | + .setIsEventListenerModeSync(true) |
| 492 | + .setKeyType(String.class) |
| 493 | + .setValueType(String.class) |
| 494 | + .setCacheType(CacheType.INDICES_REQUEST_CACHE) |
| 495 | + .setSettings(settings) |
| 496 | + .setExpireAfterAccess(TimeValue.MAX_VALUE) |
| 497 | + .setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES) |
| 498 | + .setRemovalListener(new MockRemovalListener<>()) |
| 499 | + .build(); |
| 500 | + |
| 501 | + int randomKeys = randomIntBetween(2, 100); |
| 502 | + for (int i = 0; i < randomKeys; i++) { |
| 503 | + ehcacheTest.put(UUID.randomUUID().toString(), UUID.randomUUID().toString()); |
| 504 | + } |
| 505 | + long originalSize = ehcacheTest.count(); |
| 506 | + assertEquals(randomKeys, originalSize); |
| 507 | + |
| 508 | + // Now try removing subset of keys and verify |
| 509 | + List<String> removedKeyList = new ArrayList<>(); |
| 510 | + for (Iterator<String> iterator = ehcacheTest.keys().iterator(); iterator.hasNext();) { |
| 511 | + String key = iterator.next(); |
| 512 | + if (randomBoolean()) { |
| 513 | + removedKeyList.add(key); |
| 514 | + iterator.remove(); |
| 515 | + } |
| 516 | + } |
| 517 | + // Verify the removed key doesn't exist anymore. |
| 518 | + for (String ehcacheKey : removedKeyList) { |
| 519 | + assertNull(ehcacheTest.get(ehcacheKey)); |
| 520 | + } |
| 521 | + // Verify ehcache entry size again. |
| 522 | + assertEquals(originalSize - removedKeyList.size(), ehcacheTest.count()); |
| 523 | + ehcacheTest.close(); |
| 524 | + } |
| 525 | + |
| 526 | + } |
| 527 | + |
484 | 528 | private static String generateRandomString(int length) {
|
485 | 529 | String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
486 | 530 | StringBuilder randomString = new StringBuilder(length);
|
|
0 commit comments