diff --git a/src/test/java/io/confluent/examples/streams/microservices/EndToEndTest.java b/src/test/java/io/confluent/examples/streams/microservices/EndToEndTest.java index 3422e2c41e..f1688e0dfa 100644 --- a/src/test/java/io/confluent/examples/streams/microservices/EndToEndTest.java +++ b/src/test/java/io/confluent/examples/streams/microservices/EndToEndTest.java @@ -55,7 +55,7 @@ public void shouldCreateNewOrderAndGetBackValidatedOrder() { sendInventory(inventory, Topics.WAREHOUSE_INVENTORY); //When we POST order and immediately GET on the returned location - client.target(path.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(inputOrder)); + postWithRetries(client.target(path.urlPost()).request(APPLICATION_JSON_TYPE), Entity.json(inputOrder), 5); Invocation.Builder builder = client .target(path.urlGetValidated(1)) .queryParam("timeout", MIN) @@ -84,7 +84,7 @@ public void shouldProcessManyValidOrdersEndToEnd() { startTimer(); //POST & GET order - client.target(path.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(inputOrder)); + postWithRetries(client.target(path.urlPost()).request(APPLICATION_JSON_TYPE), Entity.json(inputOrder), 5); Invocation.Builder builder = client .target(path.urlGetValidated(i)) .queryParam("timeout", MIN) @@ -122,7 +122,7 @@ public void shouldProcessManyInvalidOrdersEndToEnd() { startTimer(); //POST & GET order - client.target(path.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(inputOrder)); + postWithRetries(client.target(path.urlPost()).request(APPLICATION_JSON_TYPE), Entity.json(inputOrder), 5); Invocation.Builder builder = client .target(path.urlGetValidated(i)) .queryParam("timeout", MIN) diff --git a/src/test/java/io/confluent/examples/streams/microservices/OrdersServiceTest.java b/src/test/java/io/confluent/examples/streams/microservices/OrdersServiceTest.java index 6bde3db7f6..6fdbc3b4af 100644 --- a/src/test/java/io/confluent/examples/streams/microservices/OrdersServiceTest.java +++ b/src/test/java/io/confluent/examples/streams/microservices/OrdersServiceTest.java @@ -71,9 +71,10 @@ public void shouldPostOrderAndGetItBack() { Paths paths = new Paths("localhost", rest.port()); //When we POST an order - Response response = client.target(paths.urlPost()) - .request(APPLICATION_JSON_TYPE) - .post(Entity.json(bean)); + Response response = postWithRetries( + client.target(paths.urlPost()).request(APPLICATION_JSON_TYPE), + Entity.json(bean), + 5); //Then assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_CREATED); @@ -113,9 +114,7 @@ public void shouldGetValidatedOrderOnRequest() { Paths paths = new Paths("localhost", rest.port()); //When we post an order - client.target(paths.urlPost()) - .request(APPLICATION_JSON_TYPE) - .post(Entity.json(beanV1)); + postWithRetries(client.target(paths.urlPost()).request(APPLICATION_JSON_TYPE), Entity.json(beanV1), 5); //Simulate the order being validated MicroserviceTestUtils.sendOrders(Collections.singletonList( @@ -172,9 +171,7 @@ public void shouldGetOrderByIdWhenOnDifferentHost() { Paths paths2 = new Paths("localhost", rest2.port()); //And one order - client.target(paths1.urlPost()) - .request(APPLICATION_JSON_TYPE) - .post(Entity.json(order)); + postWithRetries(client.target(paths1.urlPost()).request(APPLICATION_JSON_TYPE), Entity.json(order), 5); //When GET to rest1 Invocation.Builder builder = client.target(paths1.urlGet(order.getId())) diff --git a/src/test/java/io/confluent/examples/streams/microservices/util/MicroserviceTestUtils.java b/src/test/java/io/confluent/examples/streams/microservices/util/MicroserviceTestUtils.java index e14e1bd48b..bea67998a4 100644 --- a/src/test/java/io/confluent/examples/streams/microservices/util/MicroserviceTestUtils.java +++ b/src/test/java/io/confluent/examples/streams/microservices/util/MicroserviceTestUtils.java @@ -24,8 +24,10 @@ import org.slf4j.LoggerFactory; import javax.ws.rs.ServerErrorException; +import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.core.GenericType; +import javax.ws.rs.core.Response; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; @@ -259,4 +261,18 @@ public static T getWithRetries(final Invocation.Builder builder, } } + public static Response postWithRetries(final Invocation.Builder builder, + final Entity entity, + int numberOfRetries) { + while (true) { + try { + return builder.post(entity); + } catch (ServerErrorException exception) { + if (exception.getMessage().contains("504") && numberOfRetries-- > 0) { + continue; + } + throw exception; + } + } + } }