Skip to content

Commit

Permalink
Add 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 c22f59f commit 8d4e46f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;

import io.quarkus.cache.CacheInvalidate;
import io.quarkus.cache.CacheInvalidateAll;
Expand Down Expand Up @@ -53,4 +54,23 @@ public Uni<Void> invalidateWithPrefix(@PathParam("prefix") @CacheKey String pref
public Uni<Void> invalidateAll() {
return Uni.createFrom().nullItem();
}

@GET
@Path("/failure/{key}")
@CacheResult(cacheName = CACHE_NAME)
public Uni<Response> getValueWithFailure(@PathParam("key") @CacheKey String key) {

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

private synchronized int incrementCounter() {
return counter++;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
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 @@ -72,6 +75,32 @@ public void shouldGetTheSameValueForSamePrefixesWhenGettingValueFromPath(String
"Value was equal which means @CacheKey didn't work");
}

/**
* Ensure that failed unis are not cached
*/
@Tag("QUARKUS-4541")
@Test
public void shouldNotCacheFailures() {
String path = RESOURCE_REACTIVE_API_PATH + "/failure/key-failure";

// First call to register the failure
given()
.when().get(path)
.then()
.statusCode(500);

// second call should be success
Response response = given()
.when().get(path)
.then()
.extract()
.response();

assertNotEquals(500, response.statusCode(), "The failure has been cached and should not be");
assertEquals("Success for key: key-failure", response.asString());

}

/**
* Check whether the `@CacheInvalidate` annotation does not invalidate all the caches
*/
Expand Down

0 comments on commit 8d4e46f

Please sign in to comment.