Skip to content

Commit

Permalink
Fix test to verify failed unis are not cached
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Jul 8, 2024
1 parent 7994087 commit 98257f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.quarkus.ts.cache.caffeine;

import java.util.concurrent.atomic.AtomicInteger;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Response;

import io.quarkus.cache.CacheInvalidate;
import io.quarkus.cache.CacheInvalidateAll;
Expand All @@ -20,6 +22,7 @@ public class ReactiveWithCacheResource {
private static final String CACHE_NAME = "api-reactive-cache";

private static int counter = 0;
private AtomicInteger atomicCounter = new AtomicInteger(0);

@GET
@CacheResult(cacheName = CACHE_NAME)
Expand Down Expand Up @@ -56,14 +59,22 @@ public Uni<Void> invalidateAll() {
}

@GET
@Path("/failing-value")
@Path("/failure/{key}")
@CacheResult(cacheName = CACHE_NAME)
public Uni<String> getFailingValue(@QueryParam("fail") boolean fail) {
if (fail) {
return Uni.createFrom().failure(new RuntimeException("Simulated error for cache"));
public Uni<Response> getValueWithFailure(@PathParam("key") @CacheKey String key) {

int currentCounter = incrementCounter();

// Simulate a failure based on the key
if (currentCounter == 0) {
return Uni.createFrom().failure(new RuntimeException("Simulated failure for key: " + key));
} else {
return Uni.createFrom().item("Value " + counter++);
return Uni.createFrom().item(Response.ok("Success for key: " + key).build());
}
}

private int incrementCounter() {
return atomicCounter.getAndIncrement();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.params.provider.ValueSource;

import io.quarkus.test.scenarios.QuarkusScenario;
import io.restassured.response.Response;

@QuarkusScenario
public class CaffeineCacheIT {
Expand Down Expand Up @@ -79,24 +80,24 @@ public void shouldGetTheSameValueForSamePrefixesWhenGettingValueFromPath(String
*/
@Tag("QUARKUS-4541")
@Test
public void shouldNotbeTheFailureCached() {
String path = RESOURCE_REACTIVE_API_PATH + "/failing-value";
public void shouldNotCacheFailures() {
String path = RESOURCE_REACTIVE_API_PATH + "/failure/key-failure";

// First call to register the failure
given()
.queryParam("fail", true)
.when().get(path)
.then()
.statusCode(500);

// second call to be success
String value = given()
.queryParam("fail", false)
// second call should be success
Response response = given()
.when().get(path)
.then()
.statusCode(200)
.extract().asString();
.extract()
.response();

assertEquals("Value 0", value, "Value should be 'Value 0' if not the failures is being cached");
assertNotEquals(500, response.statusCode(), "The failure has been cached and should not be");
assertEquals("Success for key: key-failure", response.asString());

}

Expand Down

0 comments on commit 98257f8

Please sign in to comment.