-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not increment metrics on CaffeineCache#getIfPresent call
- Loading branch information
Showing
3 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 54 additions & 5 deletions
59
integration-tests/cache/src/test/java/io/quarkus/it/cache/CacheTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,82 @@ | ||
package io.quarkus.it.cache; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static java.util.concurrent.CompletableFuture.completedFuture; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.cache.Cache; | ||
import io.quarkus.cache.CacheName; | ||
import io.quarkus.cache.CaffeineCache; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
@DisplayName("Tests the cache extension") | ||
public class CacheTestCase { | ||
|
||
private static final String EXPENSIVE_RESOURCE_CACHE_NAME = "expensiveResourceCache"; | ||
private static final String GET_IF_PRESENT_CACHE_NAME = "getIfPresentCache"; | ||
private static final String CACHE_KEY = "foo"; | ||
|
||
@CacheName(GET_IF_PRESENT_CACHE_NAME) | ||
Cache cache; | ||
|
||
@Test | ||
public void testCache() { | ||
void testCache() { | ||
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 0, 0, 0); | ||
|
||
runExpensiveRequest(); | ||
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 1, 1, 0); | ||
|
||
runExpensiveRequest(); | ||
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 1, 1, 1); | ||
|
||
runExpensiveRequest(); | ||
assertMetrics(EXPENSIVE_RESOURCE_CACHE_NAME, 1, 1, 2); | ||
|
||
when().get("/expensive-resource/invocations").then().statusCode(200).body(is("1")); | ||
} | ||
|
||
String metricsResponse = when().get("/q/metrics").then().extract().asString(); | ||
assertTrue(metricsResponse.contains("cache_puts_total{cache=\"expensiveResourceCache\"} 1.0")); | ||
assertTrue(metricsResponse.contains("cache_gets_total{cache=\"expensiveResourceCache\",result=\"miss\"} 1.0")); | ||
assertTrue(metricsResponse.contains("cache_gets_total{cache=\"expensiveResourceCache\",result=\"hit\"} 2.0")); | ||
@Test | ||
void testGetIfPresentMetrics() throws ExecutionException, InterruptedException { | ||
|
||
CaffeineCache caffeineCache = cache.as(CaffeineCache.class); | ||
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 0, 0, 0); | ||
|
||
assertNull(caffeineCache.getIfPresent(CACHE_KEY)); | ||
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 0, 1, 0); | ||
|
||
assertNull(caffeineCache.getIfPresent(CACHE_KEY)); | ||
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 0, 2, 0); | ||
|
||
caffeineCache.put(CACHE_KEY, completedFuture("bar")); | ||
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 1, 2, 0); | ||
|
||
assertEquals("bar", caffeineCache.getIfPresent(CACHE_KEY).get()); | ||
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 1, 2, 1); | ||
|
||
assertEquals("bar", caffeineCache.getIfPresent(CACHE_KEY).get()); | ||
assertMetrics(GET_IF_PRESENT_CACHE_NAME, 1, 2, 2); | ||
} | ||
|
||
private void runExpensiveRequest() { | ||
when().get("/expensive-resource/I/love/Quarkus?foo=bar").then().statusCode(200).body("result", | ||
is("I love Quarkus too!")); | ||
} | ||
|
||
private void assertMetrics(String cacheName, double expectedPuts, double expectedMisses, double expectedHits) { | ||
String metricsResponse = when().get("/q/metrics").then().extract().asString(); | ||
assertTrue(metricsResponse.contains(String.format("cache_puts_total{cache=\"%s\"} %.1f", cacheName, expectedPuts))); | ||
assertTrue(metricsResponse | ||
.contains(String.format("cache_gets_total{cache=\"%s\",result=\"miss\"} %.1f", cacheName, expectedMisses))); | ||
assertTrue(metricsResponse | ||
.contains(String.format("cache_gets_total{cache=\"%s\",result=\"hit\"} %.1f", cacheName, expectedHits))); | ||
} | ||
} |