|
8 | 8 |
|
9 | 9 | package org.opensearch.cache.store.disk;
|
10 | 10 |
|
| 11 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; |
| 12 | + |
11 | 13 | import org.opensearch.cache.EhcacheDiskCacheSettings;
|
12 | 14 | import org.opensearch.common.Randomness;
|
13 | 15 | import org.opensearch.common.cache.CacheType;
|
|
47 | 49 | import static org.opensearch.cache.EhcacheDiskCacheSettings.DISK_STORAGE_PATH_KEY;
|
48 | 50 | import static org.hamcrest.CoreMatchers.instanceOf;
|
49 | 51 |
|
| 52 | +@ThreadLeakFilters(filters = { EhcacheThreadLeakFilter.class }) |
50 | 53 | public class EhCacheDiskCacheTests extends OpenSearchSingleNodeTestCase {
|
51 | 54 |
|
52 | 55 | private static final int CACHE_SIZE_IN_BYTES = 1024 * 101;
|
@@ -633,6 +636,47 @@ public void testBasicGetAndPutBytesReference() throws Exception {
|
633 | 636 | }
|
634 | 637 | }
|
635 | 638 |
|
| 639 | + public void testInvalidate() throws Exception { |
| 640 | + Settings settings = Settings.builder().build(); |
| 641 | + MockRemovalListener<String, String> removalListener = new MockRemovalListener<>(); |
| 642 | + try (NodeEnvironment env = newNodeEnvironment(settings)) { |
| 643 | + ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setThreadPoolAlias("ehcacheTest") |
| 644 | + .setStoragePath(env.nodePaths()[0].indicesPath.toString() + "/request_cache") |
| 645 | + .setIsEventListenerModeSync(true) |
| 646 | + .setKeyType(String.class) |
| 647 | + .setKeySerializer(new StringSerializer()) |
| 648 | + .setValueSerializer(new StringSerializer()) |
| 649 | + .setValueType(String.class) |
| 650 | + .setCacheType(CacheType.INDICES_REQUEST_CACHE) |
| 651 | + .setSettings(settings) |
| 652 | + .setExpireAfterAccess(TimeValue.MAX_VALUE) |
| 653 | + .setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES) |
| 654 | + .setRemovalListener(removalListener) |
| 655 | + .build(); |
| 656 | + int randomKeys = randomIntBetween(10, 100); |
| 657 | + Map<String, String> keyValueMap = new HashMap<>(); |
| 658 | + for (int i = 0; i < randomKeys; i++) { |
| 659 | + keyValueMap.put(UUID.randomUUID().toString(), UUID.randomUUID().toString()); |
| 660 | + } |
| 661 | + for (Map.Entry<String, String> entry : keyValueMap.entrySet()) { |
| 662 | + ehcacheTest.put(entry.getKey(), entry.getValue()); |
| 663 | + } |
| 664 | + assertEquals(keyValueMap.size(), ehcacheTest.count()); |
| 665 | + List<String> removedKeyList = new ArrayList<>(); |
| 666 | + for (Map.Entry<String, String> entry : keyValueMap.entrySet()) { |
| 667 | + if (randomBoolean()) { |
| 668 | + removedKeyList.add(entry.getKey()); |
| 669 | + ehcacheTest.invalidate(entry.getKey()); |
| 670 | + } |
| 671 | + } |
| 672 | + for (String removedKey : removedKeyList) { |
| 673 | + assertNull(ehcacheTest.get(removedKey)); |
| 674 | + } |
| 675 | + assertEquals(keyValueMap.size() - removedKeyList.size(), ehcacheTest.count()); |
| 676 | + ehcacheTest.close(); |
| 677 | + } |
| 678 | + } |
| 679 | + |
636 | 680 | private static String generateRandomString(int length) {
|
637 | 681 | String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
638 | 682 | StringBuilder randomString = new StringBuilder(length);
|
|
0 commit comments