Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.8] [QUARKUS-4541] Fix test to verify that failed unis are not cached #1874

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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