diff --git a/metadata/com.github.ben-manes.caffeine/caffeine/2.9.3/index.json b/metadata/com.github.ben-manes.caffeine/caffeine/2.9.3/index.json new file mode 100644 index 000000000..768b5502a --- /dev/null +++ b/metadata/com.github.ben-manes.caffeine/caffeine/2.9.3/index.json @@ -0,0 +1,3 @@ +[ + "reflect-config.json" +] diff --git a/metadata/com.github.ben-manes.caffeine/caffeine/2.9.3/reflect-config.json b/metadata/com.github.ben-manes.caffeine/caffeine/2.9.3/reflect-config.json new file mode 100644 index 000000000..3348c7f2d --- /dev/null +++ b/metadata/com.github.ben-manes.caffeine/caffeine/2.9.3/reflect-config.json @@ -0,0 +1,189 @@ +[ +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BBHeader$ReadAndWriteCounterRef"}, + "name":"com.github.benmanes.caffeine.cache.BBHeader$ReadAndWriteCounterRef", + "fields":[{"name":"writeCounter"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BBHeader$ReadCounterRef"}, + "name":"com.github.benmanes.caffeine.cache.BBHeader$ReadCounterRef", + "fields":[{"name":"readCounter"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BLCHeader$DrainStatusRef"}, + "name":"com.github.benmanes.caffeine.cache.BLCHeader$DrainStatusRef", + "fields":[{"name":"drainStatus"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BaseMpscLinkedArrayQueue"}, + "name":"com.github.benmanes.caffeine.cache.BaseMpscLinkedArrayQueueColdProducerFields", + "fields":[{"name":"producerLimit"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BaseMpscLinkedArrayQueue"}, + "name":"com.github.benmanes.caffeine.cache.BaseMpscLinkedArrayQueueConsumerFields", + "fields":[{"name":"consumerIndex"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BaseMpscLinkedArrayQueue"}, + "name":"com.github.benmanes.caffeine.cache.BaseMpscLinkedArrayQueueProducerFields", + "fields":[{"name":"producerIndex"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.Caffeine"}, + "name":"com.github.benmanes.caffeine.cache.CacheLoader", + "methods":[{"name":"asyncLoadAll","parameterTypes":["java.lang.Iterable","java.util.concurrent.Executor"] }, {"name":"loadAll","parameterTypes":["java.lang.Iterable"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.LocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.CacheLoader", + "methods":[{"name":"loadAll","parameterTypes":["java.lang.Iterable"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.FW"}, + "name":"com.github.benmanes.caffeine.cache.FW", + "fields":[{"name":"value"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.WI"}, + "name":"com.github.benmanes.caffeine.cache.FW", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.NodeFactory"}, + "name":"com.github.benmanes.caffeine.cache.PD", + "fields":[{"name":"value"}], + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.PS"}, + "name":"com.github.benmanes.caffeine.cache.PS", + "fields":[{"name":"key"}, {"name":"value"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.PSA"}, + "name":"com.github.benmanes.caffeine.cache.PSA", + "fields":[{"name":"accessTime"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSA"}, + "name":"com.github.benmanes.caffeine.cache.PSA", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSSMS"}, + "name":"com.github.benmanes.caffeine.cache.PSMS", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSMW"}, + "name":"com.github.benmanes.caffeine.cache.PSMW", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.PSR"}, + "name":"com.github.benmanes.caffeine.cache.PSR", + "fields":[{"name":"writeTime"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSR"}, + "name":"com.github.benmanes.caffeine.cache.PSR", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.PSW"}, + "name":"com.github.benmanes.caffeine.cache.PSW", + "fields":[{"name":"writeTime"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSA"}, + "name":"com.github.benmanes.caffeine.cache.PSW", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSW"}, + "name":"com.github.benmanes.caffeine.cache.PSW", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSSMW"}, + "name":"com.github.benmanes.caffeine.cache.PSWMW", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.NodeFactory"}, + "name":"com.github.benmanes.caffeine.cache.PW", + "fields":[{"name":"value"}], + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalManualCache"}, + "name":"com.github.benmanes.caffeine.cache.SI", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.SSA", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.SSMS", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.LocalCacheFactory"}, + "name":"com.github.benmanes.caffeine.cache.SSMSW", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.SSMW", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.SSR", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalManualCache"}, + "name":"com.github.benmanes.caffeine.cache.SSSMS", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.SSSMWW", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.SSW", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.StripedBuffer"}, + "name":"com.github.benmanes.caffeine.cache.StripedBuffer", + "fields":[{"name":"tableBusy"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.BoundedLocalCache$BoundedLocalLoadingCache"}, + "name":"com.github.benmanes.caffeine.cache.WI", + "methods":[{"name":"","parameterTypes":["com.github.benmanes.caffeine.cache.Caffeine","com.github.benmanes.caffeine.cache.CacheLoader","boolean"] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.StripedBuffer"}, + "name":"java.lang.Thread", + "fields":[{"name":"threadLocalRandomProbe"}] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.SSMSR"}, + "name":"com.github.benmanes.caffeine.cache.PSRMS", + "methods":[{"name":"","parameterTypes":[] }] +}, +{ + "condition":{"typeReachable":"com.github.benmanes.caffeine.cache.NodeFactory"}, + "name":"com.github.benmanes.caffeine.cache.PSWMS", + "methods":[{"name":"","parameterTypes":[] }] +} +] diff --git a/metadata/com.github.ben-manes.caffeine/caffeine/index.json b/metadata/com.github.ben-manes.caffeine/caffeine/index.json index d18c4708f..36073d36b 100644 --- a/metadata/com.github.ben-manes.caffeine/caffeine/index.json +++ b/metadata/com.github.ben-manes.caffeine/caffeine/index.json @@ -6,5 +6,12 @@ "tested-versions": [ "3.1.2" ] + }, + { + "metadata-version": "2.9.3", + "module": "com.github.ben-manes.caffeine:caffeine", + "tested-versions": [ + "2.9.3" + ] } ] diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/.gitignore b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/.gitignore new file mode 100644 index 000000000..c98c7875b --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/.gitignore @@ -0,0 +1,4 @@ +gradlew.bat +gradlew +gradle/ +build/ diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/build.gradle b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/build.gradle new file mode 100644 index 000000000..09614f064 --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/build.gradle @@ -0,0 +1,35 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ + +plugins { + id "org.graalvm.internal.tck" +} + +String libraryVersion = tck.testedLibraryVersion.get() + +dependencies { + testImplementation "com.github.ben-manes.caffeine:caffeine:$libraryVersion" + testImplementation 'org.assertj:assertj-core:3.22.0' + testImplementation 'org.awaitility:awaitility:4.2.0' + testImplementation 'com.google.guava:guava-testlib:32.1.2-jre' +} + +graalvmNative { + agent { + defaultMode = "conditional" + modes { + conditional { + userCodeFilterPath = "user-code-filter.json" + } + } + metadataCopy { + mergeWithExisting = true + inputTaskNames.add("test") + outputDirectories.add("src/test/resources/META-INF/native-image/com.github.ben-manes.caffeine/caffeine") + } + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/gradle.properties b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/gradle.properties new file mode 100644 index 000000000..185d7f50a --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/gradle.properties @@ -0,0 +1,2 @@ +library.version = 2.9.3 +metadata.dir = com.github.ben-manes.caffeine/caffeine/2.9.3/ diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/settings.gradle b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/settings.gradle new file mode 100644 index 000000000..d58cd0cda --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/settings.gradle @@ -0,0 +1,13 @@ +pluginManagement { + def tckPath = Objects.requireNonNullElse( + System.getenv("GVM_TCK_TCKDIR"), + "../../../../tck-build-logic" + ) + includeBuild(tckPath) +} + +plugins { + id "org.graalvm.internal.tck-settings" version "1.0.0-SNAPSHOT" +} + +rootProject.name = 'com.github.ben-manes.caffeine.caffeine_tests' diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/CaffeineTest.java b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/CaffeineTest.java new file mode 100644 index 000000000..e520fa041 --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/CaffeineTest.java @@ -0,0 +1,101 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ +package com_github_ben_manes_caffeine.caffeine; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.CaffeineSpec; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.github.benmanes.caffeine.cache.Scheduler; +import com.github.benmanes.caffeine.cache.stats.CacheStats; +import com.google.common.testing.FakeTicker; +import org.junit.jupiter.api.Test; + +import java.lang.ref.Cleaner; +import java.time.Duration; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class CaffeineTest { + @Test + void testRefresh() { + TestRefreshLoader testRefreshLoader = new TestRefreshLoader(); + LoadingCache> cache = Caffeine.newBuilder() + .refreshAfterWrite(Duration.ofMillis(100)) + .build(testRefreshLoader::load); + cache.get("Hello"); + CompletableFuture firstValue = cache.getIfPresent("Hello"); + assertThat(firstValue).isNotNull(); + assertEquals("World", firstValue.join()); + testRefreshLoader.setFlag(true); + cache.refresh("Hello"); + await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> { + CompletableFuture secondValue = cache.getIfPresent("Hello"); + assertThat(secondValue).isNotNull(); + assertEquals("Universe", secondValue.join()); + }); + } + + @Test + void testStatistics() { + Cache graphs = Caffeine.newBuilder().maximumSize(10_000).recordStats().build(); + graphs.put("Hello", "World"); + assertThat(graphs.getIfPresent("Hello")).isEqualTo("World"); + CacheStats cacheStats = graphs.stats(); + assertThat(cacheStats.hitCount()).isEqualTo(1); + assertThat(cacheStats.missCount()).isEqualTo(0); + assertThat(cacheStats.evictionCount()).isEqualTo(0); + } + + @Test + void testSpecification() { + CaffeineSpec spec = CaffeineSpec.parse("maximumWeight=1000, expireAfterWrite=10m, recordStats"); + LoadingCache graphs = Caffeine.from(spec).weigher((String key, String graph) -> graph.length()) + .build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(graphs.get("Hello")).isEqualTo("World"); + } + + @Test + void testCleanup() { + FakeTicker ticker = new FakeTicker(); + LoadingCache firstGraphs = Caffeine.newBuilder() + .scheduler(Scheduler.systemScheduler()) + .expireAfterWrite(10, TimeUnit.MINUTES) + .ticker(ticker::read) + .build(key -> key.equals("Hello") ? "World" : "Universe"); + firstGraphs.put("Hello", "World"); + assertNotEquals(0, firstGraphs.estimatedSize()); + assertThat(firstGraphs.getIfPresent("Hello")).isEqualTo("World"); + ticker.advance(10, TimeUnit.MINUTES); + firstGraphs.cleanUp(); + assertEquals(0, firstGraphs.estimatedSize()); + Cache secondGraphs = Caffeine.newBuilder().weakValues().build(); + Cleaner cleaner = Cleaner.create(); + cleaner.register("World", secondGraphs::cleanUp); + secondGraphs.put("Hello", "World"); + assertThat(secondGraphs.getIfPresent("Hello")).isEqualTo("World"); + } + + @Test + void testTesting() { + FakeTicker ticker = new FakeTicker(); + Cache cache = Caffeine.newBuilder() + .expireAfterWrite(10, TimeUnit.MINUTES) + .executor(Runnable::run) + .ticker(ticker::read) + .maximumSize(10) + .build(); + cache.put("Hello", "World"); + ticker.advance(30, TimeUnit.MINUTES); + assertThat(cache.getIfPresent("Hello")).isNull(); + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/EvictionTest.java b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/EvictionTest.java new file mode 100644 index 000000000..bbe1d5e0a --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/EvictionTest.java @@ -0,0 +1,62 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ +package com_github_ben_manes_caffeine.caffeine; + +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.Expiry; +import com.github.benmanes.caffeine.cache.LoadingCache; +import org.junit.jupiter.api.Test; + +import java.time.ZonedDateTime; +import java.util.concurrent.TimeUnit; + +import static java.time.temporal.ChronoUnit.MILLIS; +import static org.assertj.core.api.Assertions.assertThat; + +public class EvictionTest { + @Test + void testSizeBased() { + LoadingCache firstGraphs = Caffeine.newBuilder().maximumSize(10_000).build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(firstGraphs.get("Hello")).isEqualTo("World"); + LoadingCache secondGraphs = Caffeine.newBuilder().maximumWeight(10_000).weigher((String key, String graph) -> graph.length()) + .build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(secondGraphs.get("Hello")).isEqualTo("World"); + } + + @Test + void testTimeBased() { + LoadingCache firstGraphs = Caffeine.newBuilder().expireAfterAccess(5, TimeUnit.MINUTES) + .build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(firstGraphs.get("Hello")).isEqualTo("World"); + LoadingCache secondGraphs = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES) + .build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(secondGraphs.get("Hello")).isEqualTo("World"); + LoadingCache thirdGraphs = Caffeine.newBuilder().expireAfter(new Expiry() { + public long expireAfterCreate(String key, String graph, long currentTime) { + long seconds = ZonedDateTime.now().plusHours(5).minus(System.currentTimeMillis(), MILLIS).toEpochSecond(); + return TimeUnit.SECONDS.toNanos(seconds); + } + + public long expireAfterUpdate(String key, String graph, long currentTime, long currentDuration) { + return currentDuration; + } + + public long expireAfterRead(String key, String graph, long currentTime, long currentDuration) { + return currentDuration; + } + }).build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(thirdGraphs.get("Hello")).isEqualTo("World"); + } + + @Test + void testReferenceBased() { + LoadingCache firstGraphs = Caffeine.newBuilder().weakKeys().weakValues().build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(firstGraphs.get("Hello")).isEqualTo("World"); + LoadingCache secondGraphs = Caffeine.newBuilder().softValues().build(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(secondGraphs.get("Hello")).isEqualTo("World"); + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/PolicyTest.java b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/PolicyTest.java new file mode 100644 index 000000000..c0c063412 --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/PolicyTest.java @@ -0,0 +1,37 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ +package com_github_ben_manes_caffeine.caffeine; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PolicyTest { + @Test + void testSizeBased() { + Cache cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(10_000).build(); + cache.policy().eviction().ifPresent(eviction -> eviction.setMaximum(2 * eviction.getMaximum())); + cache.put("Hello", "World"); + assertThat(cache.getIfPresent("Hello")).isEqualTo("World"); + } + + @Test + void testTimeBased() { + Cache cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(10_000).build(); + cache.policy().expireAfterAccess().ifPresent(eviction -> eviction.setExpiresAfter(Duration.ofMinutes(5))); + cache.policy().expireAfterWrite().ifPresent(eviction -> eviction.setExpiresAfter(Duration.ofMinutes(5))); + cache.policy().expireVariably().ifPresent(eviction -> eviction.setExpiresAfter("Hello", Duration.ofMinutes(5))); + cache.policy().refreshAfterWrite().ifPresent(refresh -> refresh.setExpiresAfter(Duration.ofMinutes(5))); + cache.put("Hello", "World"); + assertThat(cache.getIfPresent("Hello")).isEqualTo("World"); + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/PopulationTest.java b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/PopulationTest.java new file mode 100644 index 000000000..64eee10a7 --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/PopulationTest.java @@ -0,0 +1,73 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ +package com_github_ben_manes_caffeine.caffeine; + +import com.github.benmanes.caffeine.cache.AsyncCache; +import com.github.benmanes.caffeine.cache.AsyncLoadingCache; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.google.common.testing.FakeTicker; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class PopulationTest { + @Test + void testManual() { + Cache cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(10_000).build(); + assertThat(cache.getIfPresent("Hello")).isNull(); + assertThat(cache.get("Hello", k -> "World")).isEqualTo("World"); + cache.put("Hello", "World"); + assertThat(cache.getIfPresent("Hello")).isEqualTo("World"); + cache.invalidate("Hello"); + assertThat(cache.getIfPresent("Hello")).isNull(); + } + + @Test + void testExpireAfterWrite() { + FakeTicker ticker = new FakeTicker(); + Cache cache = Caffeine.newBuilder().ticker(ticker::read).expireAfterWrite(100, TimeUnit.MINUTES).build(); + cache.put("Aloha", "Universe"); + assertEquals("Universe", cache.getIfPresent("Aloha")); + ticker.advance(Duration.ofMinutes(200)); + assertNull(cache.getIfPresent("Aloha")); + } + + @SuppressWarnings("DataFlowIssue") + @Test + void testAsynchronousManual() throws ExecutionException, InterruptedException { + AsyncCache cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(10_000).buildAsync(); + assertThat(cache.getIfPresent("Hello")).isNull(); + assertThat(cache.get("Hello", k -> "World").get()).isEqualTo("World"); + cache.put("Hello", CompletableFuture.supplyAsync(() -> "World")); + assertThat(cache.getIfPresent("Hello").get()).isEqualTo("World"); + cache.synchronous().invalidate("Hello"); + assertThat(cache.getIfPresent("Hello")).isNull(); + } + + @Test + void testAsynchronouslyLoading() throws ExecutionException, InterruptedException { + AsyncLoadingCache firstCache = Caffeine.newBuilder().maximumSize(10_000).expireAfterWrite(10, TimeUnit.MINUTES) + .buildAsync((key, executor) -> + key.equals("Hello") ? CompletableFuture.supplyAsync(() -> "World") : CompletableFuture.supplyAsync(() -> "Universe")); + assertThat(firstCache.get("Hello").get()).isEqualTo("World"); + assertThat(firstCache.getAll(List.of("Hi", "Aloha")).get()).isEqualTo(Map.of("Hi", "Universe", "Aloha", "Universe")); + AsyncLoadingCache secondCache = Caffeine.newBuilder().maximumSize(10_000).expireAfterWrite(10, TimeUnit.MINUTES) + .buildAsync(key -> key.equals("Hello") ? "World" : "Universe"); + assertThat(secondCache.get("Hello").get()).isEqualTo("World"); + assertThat(secondCache.getAll(List.of("Hi", "Aloha")).get()).isEqualTo(Map.of("Hi", "Universe", "Aloha", "Universe")); + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/RemovalTest.java b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/RemovalTest.java new file mode 100644 index 000000000..469afe9ac --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/RemovalTest.java @@ -0,0 +1,48 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ +package com_github_ben_manes_caffeine.caffeine; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.RemovalCause; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RemovalTest { + @Test + void testExplicitRemovals() { + Map map = Map.of("Hello", "World", "Hi", "Universe", "Aloha", "Universe"); + Cache cache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(10_000).build(); + cache.putAll(map); + assertThat(cache.getAll(List.of("Hello", "Hi", "Aloha"), key -> null)).isEqualTo(map); + cache.invalidate("Hello"); + assertThat(cache.getIfPresent("Hello")).isNull(); + cache.invalidateAll(List.of("Hi", "Aloha")); + Stream.of("Hi", "Aloha").forEach(s -> assertThat(cache.getIfPresent(s)).isNull()); + cache.put("Ciao", "Earth"); + cache.invalidateAll(); + Stream.of("Hello", "Hi", "Aloha", "Ciao").forEach(s -> assertThat(cache.getIfPresent(s)).isNull()); + } + + @Test + void testRemovalListeners() { + Cache graphs = Caffeine.newBuilder() + .evictionListener((String key, String graph, RemovalCause cause) -> System.out.printf("Key %s was evicted (%s)%n", key, cause)) + .removalListener((String key, String graph, RemovalCause cause) -> System.out.printf("Key %s was removed (%s)%n", key, cause)) + .build(); + graphs.put("Hello", "World"); + assertThat(graphs.getIfPresent("Hello")).isEqualTo("World"); + graphs.invalidate("Hello"); + assertThat(graphs.getIfPresent("Hello")).isNull(); + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/TestRefreshLoader.java b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/TestRefreshLoader.java new file mode 100644 index 000000000..8c00eb5aa --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/src/test/java/com_github_ben_manes_caffeine/caffeine/TestRefreshLoader.java @@ -0,0 +1,28 @@ +/* + * Copyright and related rights waived via CC0 + * + * You should have received a copy of the CC0 legalcode along with this + * work. If not, see . + */ +package com_github_ben_manes_caffeine.caffeine; + +import java.util.concurrent.CompletableFuture; + +public class TestRefreshLoader { + private Boolean flag = false; + + public CompletableFuture load(String ignoredKey) { + if (getFlag()) { + return CompletableFuture.completedFuture("Universe"); + } + return CompletableFuture.completedFuture("World"); + } + + public Boolean getFlag() { + return flag; + } + + public void setFlag(Boolean flag) { + this.flag = flag; + } +} diff --git a/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/user-code-filter.json b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/user-code-filter.json new file mode 100644 index 000000000..f6a552f04 --- /dev/null +++ b/tests/src/com.github.ben-manes.caffeine/caffeine/2.9.3/user-code-filter.json @@ -0,0 +1,10 @@ +{ + "rules": [ + { + "excludeClasses": "**" + }, + { + "includeClasses": "com.github.benmanes.caffeine.cache.**" + } + ] +} diff --git a/tests/src/index.json b/tests/src/index.json index b0e687e5e..ba4578878 100644 --- a/tests/src/index.json +++ b/tests/src/index.json @@ -34,6 +34,12 @@ "name" : "com.fasterxml.jackson.core:jackson-databind", "versions" : [ "2.15.2" ] } ] +}, { + "test-project-path" : "com.github.ben-manes.caffeine/caffeine/2.9.3", + "libraries" : [ { + "name" : "com.github.ben-manes.caffeine:caffeine", + "versions" : [ "2.9.3" ] + } ] }, { "test-project-path" : "com.github.ben-manes.caffeine/caffeine/3.1.2", "libraries" : [ {