Skip to content

Commit

Permalink
Adding UT to fix line coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Sagar Upadhyaya <sagar.upadhyaya.121@gmail.com>
  • Loading branch information
sgup432 committed Jul 12, 2024
1 parent 80ef056 commit 5422064
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@
import java.util.function.ToLongBiFunction;

import org.ehcache.Cache;
import org.ehcache.CachePersistenceException;
import org.ehcache.PersistentCacheManager;
import org.ehcache.StateTransitionException;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheEventListenerConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
Expand Down Expand Up @@ -467,11 +465,7 @@ public void close() {
cacheManager.removeCache(this.diskCacheAlias);
cacheManager.close();
cacheManager.destroyCache(this.diskCacheAlias);
} catch (CachePersistenceException e) {
// When something goes wrong while destroying the persistence data
logger.error(() -> new ParameterizedMessage("Exception occurred while destroying ehcache and associated " + "data"), e);
throw new OpenSearchException("Exception occurred while destroying ehcache and associated data", e);
} catch (StateTransitionException e) {
} catch (Exception e) {
logger.error(() -> new ParameterizedMessage("Exception occurred while trying to close/remove ehcache"), e);
} finally {
// Delete all the disk cache related files/data in case it is present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,43 @@ public void testDiskCacheFilesAreClearedUpDuringCloseAndInitialization() throws
}
}

public void testDiskCacheCloseCalledTwiceAndVerifyDiskDataIsCleanedUp() throws Exception {
Settings settings = Settings.builder().build();
MockRemovalListener<String, String> removalListener = new MockRemovalListener<>();
ToLongBiFunction<ICacheKey<String>, String> weigher = getWeigher();
try (NodeEnvironment env = newNodeEnvironment(settings)) {
String path = env.nodePaths()[0].path.toString() + "/request_cache";
ICache<String, String> ehcacheTest = new EhcacheDiskCache.Builder<String, String>().setThreadPoolAlias("ehcacheTest")
.setStoragePath(path)
.setIsEventListenerModeSync(true)
.setKeyType(String.class)
.setValueType(String.class)
.setKeySerializer(new StringSerializer())
.setDiskCacheAlias("test1")
.setValueSerializer(new StringSerializer())
.setDimensionNames(List.of(dimensionName))
.setCacheType(CacheType.INDICES_REQUEST_CACHE)
.setSettings(settings)
.setExpireAfterAccess(TimeValue.MAX_VALUE)
.setMaximumWeightInBytes(CACHE_SIZE_IN_BYTES)
.setRemovalListener(removalListener)
.setWeigher(weigher)
.setStatsTrackingEnabled(false)
.build();
int randomKeys = randomIntBetween(10, 100);
for (int i = 0; i < randomKeys; i++) {
ICacheKey<String> iCacheKey = getICacheKey(UUID.randomUUID().toString());
ehcacheTest.put(iCacheKey, UUID.randomUUID().toString());
assertEquals(0, ehcacheTest.count()); // Expect count storagePath 0 if NoopCacheStatsHolder is used
assertEquals(new ImmutableCacheStats(0, 0, 0, 0, 0), ehcacheTest.stats().getTotalStats());
}
ehcacheTest.close();
// Call it again. This will throw an exception.
ehcacheTest.close();
assertFalse(Files.exists(Path.of(path))); // Verify everything is cleared up now after close()
}
}

private List<String> getRandomDimensions(List<String> dimensionNames) {
Random rand = Randomness.get();
int bound = 3;
Expand Down

0 comments on commit 5422064

Please sign in to comment.