Skip to content

Commit

Permalink
Test EvictableCache hits
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jan 18, 2022
1 parent 6984e9e commit f616b4e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lib/trino-plugin-toolkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.gaul</groupId>
<artifactId>modernizer-maven-annotations</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@
*/
package io.trino.plugin.base.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheStats;

import java.util.concurrent.Callable;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

public class CacheStatsAssertions
{
public static CacheStatsAssertions assertCacheStats(Cache<?, ?> cache)
{
requireNonNull(cache, "cache is null");
return assertCacheStats(cache::stats);
}

public static CacheStatsAssertions assertCacheStats(Supplier<CacheStats> statsSupplier)
{
return new CacheStatsAssertions(statsSupplier);
Expand Down Expand Up @@ -57,9 +65,23 @@ public CacheStatsAssertions misses(long value)
}

public void afterRunning(Runnable runnable)
{
try {
calling(() -> {
runnable.run();
return null;
});
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

public <T> T calling(Callable<T> callable)
throws Exception
{
CacheStats beforeStats = stats.get();
runnable.run();
T value = callable.call();
CacheStats afterStats = stats.get();

long expectedLoad = beforeStats.loadCount() + loads;
Expand All @@ -75,5 +97,7 @@ public void afterRunning(Runnable runnable)
assertThat(afterStats.missCount())
.withFailMessage("Expected miss count is %d but actual is %d", expectedMisses, afterStats.missCount())
.isEqualTo(expectedMisses);

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
import io.airlift.concurrent.MoreFutures;
import org.gaul.modernizer_maven_annotations.SuppressModernizer;
import org.testng.annotations.Test;

import java.util.List;
Expand All @@ -30,10 +31,12 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.plugin.base.cache.CacheStatsAssertions.assertCacheStats;
import static java.lang.String.format;
import static java.util.concurrent.Executors.newFixedThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

Expand All @@ -44,14 +47,47 @@ public class TestEvictableCache
@Test(timeOut = TEST_TIMEOUT_MILLIS)
public void testLoad()
throws Exception
{
Cache<Integer, String> cache = EvictableCache.buildWith(
CacheBuilder.newBuilder());
assertEquals(cache.get(42, () -> "abc"), "abc");
}

@Test(timeOut = TEST_TIMEOUT_MILLIS)
public void testLoadStats()
throws Exception
{
Cache<Integer, String> cache = EvictableCache.buildWith(
CacheBuilder.newBuilder());

assertEquals(cache.stats(), new CacheStats(0, 0, 0, 0, 0, 0));
String value = cache.get(42, () -> "abc");
assertEquals(cache.stats(), new CacheStats(0, 1, 1, 0, cache.stats().totalLoadTime(), 0));

String value = assertCacheStats(cache)
.misses(1)
.loads(1)
.calling(() -> cache.get(42, () -> "abc"));
assertEquals(value, "abc");

value = assertCacheStats(cache)
.hits(1)
.calling(() -> cache.get(42, () -> "xyz"));
assertEquals(value, "abc");

// with equal, but not the same key
value = assertCacheStats(cache)
.hits(1)
.calling(() -> cache.get(newInteger(42), () -> "xyz"));
assertEquals(value, "abc");
}

@SuppressModernizer
private static Integer newInteger(int value)
{
Integer integer = value;
@SuppressWarnings({"UnnecessaryBoxing", "deprecation", "BoxedPrimitiveConstructor"})
Integer newInteger = new Integer(value);
assertNotSame(integer, newInteger);
return newInteger;
}

/**
Expand Down

0 comments on commit f616b4e

Please sign in to comment.