Skip to content

Commit 016c20b

Browse files
authored
Merge pull request #656 from ChristinaDsl/WFLY-17740
[WFLY-17740] Add missing @fallback test in Microprofile Fault Tolerance quickstart
2 parents 41b56bf + 0866ae7 commit 016c20b

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

microprofile-fault-tolerance/src/main/java/org/wildfly/quickstarts/microprofile/faulttolerance/Coffee.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public Coffee(Integer id, String name, String countryOfOrigin, Integer price) {
3737
this.countryOfOrigin = countryOfOrigin;
3838
this.price = price;
3939
}
40+
41+
public int getId() {
42+
return id;
43+
}
44+
4045
}

microprofile-fault-tolerance/src/main/java/org/wildfly/quickstarts/microprofile/faulttolerance/CoffeeRepositoryService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public List<Coffee> getRecommendations(Integer id) {
6565
if (id == null) {
6666
return Collections.emptyList();
6767
}
68+
6869
return coffeeList.values().stream()
6970
.filter(coffee -> !id.equals(coffee.id))
7071
.limit(2)

microprofile-fault-tolerance/src/main/java/org/wildfly/quickstarts/microprofile/faulttolerance/CoffeeResource.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public class CoffeeResource {
5555

5656
private Float failRatio = 0.5f;
5757

58+
private int minDelay = 0;
59+
60+
private int maxDelay = 500;
61+
5862
/**
5963
* Provides list of all our coffees.
6064
* <p>
@@ -139,6 +143,7 @@ public List<Coffee> recommendations(@PathParam("id") int id) {
139143

140144
try {
141145
randomDelay();
146+
142147
LOGGER.infof("CoffeeResource#recommendations() invocation #%d returning successfully", invocationNumber);
143148
return coffeeRepository.getRecommendations(id);
144149
} catch (InterruptedException e) {
@@ -157,7 +162,6 @@ public List<Coffee> fallbackRecommendations(int id) {
157162
return Collections.singletonList(coffeeRepository.getCoffeeById(1));
158163
}
159164

160-
161165
private void maybeFail(String failureLogMessage) {
162166
// introduce some artificial failures
163167
if (new Random().nextFloat() < failRatio) {
@@ -168,7 +172,7 @@ private void maybeFail(String failureLogMessage) {
168172

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

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

186+
@GET
187+
@Path("/setMaxDelay/{maxDelay}")
188+
public void setMaxDelay(@PathParam("maxDelay") int maxDelay) {
189+
this.maxDelay = maxDelay;
190+
}
191+
192+
@GET
193+
@Path("/setMinDelay/{minDelay}")
194+
public void setMinDelay(@PathParam("minDelay") int minDelay) {
195+
this.minDelay = minDelay;
196+
}
197+
182198
@GET
183199
@Path("/getCounter")
184200
public Long getCounter() {

microprofile-fault-tolerance/src/test/java/org/wildfly/quickstarts/microprofile/faulttolerance/CoffeeResourceIT.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.wildfly.quickstarts.microprofile.faulttolerance;
1818

1919
import java.util.List;
20+
import java.util.ArrayList;
21+
2022
import jakarta.ws.rs.client.Client;
2123
import jakarta.ws.rs.client.ClientBuilder;
2224
import jakarta.ws.rs.core.GenericType;
@@ -37,6 +39,7 @@
3739
* @author Radoslav Husar
3840
* @author Eduardo Martins
3941
*/
42+
4043
public class CoffeeResourceIT {
4144

4245
private Client client;
@@ -124,6 +127,58 @@ private void resetCounter() {
124127
}
125128
}
126129

130+
/**
131+
* Testing whether customer has at his disposal all the suggestions for coffee.
132+
* <p>
133+
* In cases of small-time delay, the total number of recommendations will appear and fallbackMethod won't be called.
134+
*/
135+
@Test
136+
public void testCoffeeRecommendations() {
137+
setMinDelay(0);
138+
setMaxDelay(250);
139+
140+
try (Response response = this.getResponse("/coffee/1/recommendations")) {
141+
Assert.assertEquals(200, response.getStatus());
142+
ArrayList<Coffee> ordersList = response.readEntity(new GenericType<ArrayList<Coffee>>() {});
143+
Assert.assertNotNull(ordersList);
144+
Assert.assertEquals(2, ordersList.size());
145+
Assert.assertNotEquals(1, ordersList.get(0).getId());
146+
Assert.assertNotEquals(1, ordersList.get(1).getId());
147+
}
148+
}
149+
150+
/**
151+
* Testing if fallbackMethod will offer to the customer one recommendation.
152+
* <p>
153+
* In cases of big-time delay fallbackMethod will propose a safe recommendation choice.
154+
*/
155+
@Test
156+
public void testCoffeeRecommendationsSafeChoice() {
157+
setMinDelay(251);
158+
setMaxDelay(500);
159+
160+
try (Response response = this.getResponse("/coffee/1/recommendations")) {
161+
Assert.assertEquals(200, response.getStatus());
162+
163+
ArrayList<Coffee> ordersList = response.readEntity(new GenericType<ArrayList<Coffee>>() {});
164+
Assert.assertNotNull(ordersList);
165+
Assert.assertEquals(1, ordersList.size());
166+
Assert.assertEquals(1, ordersList.get(0).getId());
167+
}
168+
}
169+
170+
private void setMaxDelay(int maxDelay) {
171+
try (Response response = this.getResponse("/coffee/setMaxDelay/" + maxDelay)) {
172+
Assert.assertEquals(204, response.getStatus());
173+
}
174+
}
175+
176+
private void setMinDelay(int minDelay) {
177+
try (Response response = this.getResponse("/coffee/setMinDelay/" + minDelay)) {
178+
Assert.assertEquals(204, response.getStatus());
179+
}
180+
}
181+
127182
private Response getResponse(String path) {
128183
return client.target(TestUtils.getServerHost())
129184
.path(path)

0 commit comments

Comments
 (0)