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

[WFLY-17740] Add missing @fallback test in Microprofile Fault Tolerance quickstart #656

Merged
merged 1 commit into from
Apr 3, 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
Expand Up @@ -37,4 +37,9 @@ public Coffee(Integer id, String name, String countryOfOrigin, Integer price) {
this.countryOfOrigin = countryOfOrigin;
this.price = price;
}

public int getId() {
return id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public List<Coffee> getRecommendations(Integer id) {
if (id == null) {
return Collections.emptyList();
}

return coffeeList.values().stream()
.filter(coffee -> !id.equals(coffee.id))
.limit(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class CoffeeResource {

private Float failRatio = 0.5f;

private int minDelay = 0;

private int maxDelay = 500;

/**
* Provides list of all our coffees.
* <p>
Expand Down Expand Up @@ -139,6 +143,7 @@ public List<Coffee> recommendations(@PathParam("id") int id) {

try {
randomDelay();

LOGGER.infof("CoffeeResource#recommendations() invocation #%d returning successfully", invocationNumber);
return coffeeRepository.getRecommendations(id);
} catch (InterruptedException e) {
Expand All @@ -157,7 +162,6 @@ public List<Coffee> fallbackRecommendations(int id) {
return Collections.singletonList(coffeeRepository.getCoffeeById(1));
}


private void maybeFail(String failureLogMessage) {
// introduce some artificial failures
if (new Random().nextFloat() < failRatio) {
Expand All @@ -168,7 +172,7 @@ private void maybeFail(String failureLogMessage) {

private void randomDelay() throws InterruptedException {
// introduce some artificial delay
Thread.sleep(new Random().nextInt(500));
Thread.sleep(new Random().nextInt(maxDelay - minDelay) + minDelay);
}

// The following methods are only used for automated integration testing
Expand All @@ -179,6 +183,18 @@ public void setFailRatio(@PathParam("failRatio") Float failRatio) {
this.failRatio = failRatio;
}

@GET
@Path("/setMaxDelay/{maxDelay}")
public void setMaxDelay(@PathParam("maxDelay") int maxDelay) {
this.maxDelay = maxDelay;
}

@GET
@Path("/setMinDelay/{minDelay}")
public void setMinDelay(@PathParam("minDelay") int minDelay) {
this.minDelay = minDelay;
}

@GET
@Path("/getCounter")
public Long getCounter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.wildfly.quickstarts.microprofile.faulttolerance;

import java.util.List;
import java.util.ArrayList;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.GenericType;
Expand All @@ -37,6 +39,7 @@
* @author Radoslav Husar
* @author Eduardo Martins
*/

public class CoffeeResourceIT {

private Client client;
Expand Down Expand Up @@ -124,6 +127,58 @@ private void resetCounter() {
}
}

/**
* Testing whether customer has at his disposal all the suggestions for coffee.
* <p>
* In cases of small-time delay, the total number of recommendations will appear and fallbackMethod won't be called.
*/
@Test
public void testCoffeeRecommendations() {
setMinDelay(0);
setMaxDelay(250);

try (Response response = this.getResponse("/coffee/1/recommendations")) {
Assert.assertEquals(200, response.getStatus());
ArrayList<Coffee> ordersList = response.readEntity(new GenericType<ArrayList<Coffee>>() {});
Assert.assertNotNull(ordersList);
Assert.assertEquals(2, ordersList.size());
Assert.assertNotEquals(1, ordersList.get(0).getId());
Assert.assertNotEquals(1, ordersList.get(1).getId());
}
}

/**
* Testing if fallbackMethod will offer to the customer one recommendation.
* <p>
* In cases of big-time delay fallbackMethod will propose a safe recommendation choice.
*/
@Test
public void testCoffeeRecommendationsSafeChoice() {
setMinDelay(251);
setMaxDelay(500);

try (Response response = this.getResponse("/coffee/1/recommendations")) {
Assert.assertEquals(200, response.getStatus());

ArrayList<Coffee> ordersList = response.readEntity(new GenericType<ArrayList<Coffee>>() {});
Assert.assertNotNull(ordersList);
Assert.assertEquals(1, ordersList.size());
Assert.assertEquals(1, ordersList.get(0).getId());
}
}

private void setMaxDelay(int maxDelay) {
try (Response response = this.getResponse("/coffee/setMaxDelay/" + maxDelay)) {
Assert.assertEquals(204, response.getStatus());
}
}

private void setMinDelay(int minDelay) {
try (Response response = this.getResponse("/coffee/setMinDelay/" + minDelay)) {
Assert.assertEquals(204, response.getStatus());
}
}

private Response getResponse(String path) {
return client.target(TestUtils.getServerHost())
.path(path)
Expand Down