text block

This commit is contained in:
@hangga 2023-10-17 18:24:02 +07:00
parent 2efe2c7987
commit 38d4d8f621

View File

@ -1,9 +1,7 @@
package com.baeldung.concurrent.completablefuture; package com.baeldung.concurrent.completablefuture;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.client.WireMock;
import net.minidev.json.writer.JsonReader;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -15,7 +13,6 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.function.Supplier;
import static com.github.tomakehurst.wiremock.client.WireMock.*; import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -26,100 +23,102 @@ class CompletableFutureTimeoutUnitTest {
private WireMockServer wireMockServer; private WireMockServer wireMockServer;
private ScheduledExecutorService executorService; private ScheduledExecutorService executorService;
private static final int DEFAULT_TIMEOUT = 1000; //1 seconds private static final int DEFAULT_TIMEOUT = 1000; //1 seconds
private static final String DEFAULT_PRODUCT = "{\n" + private static final String DEFAULT_PRODUCT = """
" \"products\": [\n" + {
" {\n" + "products": [
" \"id\": 2,\n" + {
" \"title\": \"iPhone X\",\n" + "id": 2,
" \"description\": \"SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...\",\n" + "title": "iPhone X",
" \"price\": 899,\n" + "description": "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...",
" \"discountPercentage\": 0.0,\n" + "price": 899,
" \"rating\": 4.44,\n" + "discountPercentage": 0.0,
" \"stock\": 34,\n" + "rating": 4.44,
" \"brand\": \"Apple\",\n" + "stock": 34,
" \"category\": \"smartphones\"\n" + "brand": "Apple",
" },\n" + "category": "smartphones"
" {\n" + },
" \"id\": 3,\n" + {
" \"title\": \"Samsung Universe 9\",\n" + "id": 3,
" \"description\": \"Samsung's new variant which goes beyond Galaxy to the Universe\",\n" + "title": "Samsung Universe 9",
" \"price\": 1249,\n" + "description": "Samsung's new variant which goes beyond Galaxy to the Universe",
" \"discountPercentage\": 0.0,\n" + "price": 1249,
" \"rating\": 4.09,\n" + "discountPercentage": 0.0,
" \"stock\": 36,\n" + "rating": 4.09,
" \"brand\": \"Samsung\",\n" + "stock": 36,
" \"category\": \"smartphones\"\n" + "brand": "Samsung",
" }\n" + "category": "smartphones"
" ],\n" + }
" \"total\": 2\n" + ],
"}"; "total": 2
private static final String PRODUCT_OFFERS = "{\n" + }""";
" \"products\": [\n" + private static final String PRODUCT_OFFERS = """
" {\n" + {
" \"id\": 1,\n" + "products": [
" \"title\": \"iPhone 9\",\n" + {
" \"description\": \"An apple mobile which is nothing like apple\",\n" + "id": 1,
" \"price\": 549,\n" + "title": "iPhone 9",
" \"discountPercentage\": 12.96,\n" + "description": "An apple mobile which is nothing like apple",
" \"rating\": 4.69,\n" + "price": 549,
" \"stock\": 94,\n" + "discountPercentage": 12.96,
" \"brand\": \"Apple\",\n" + "rating": 4.69,
" \"category\": \"smartphones\"\n" + "stock": 94,
" },\n" + "brand": "Apple",
" {\n" + "category": "smartphones"
" \"id\": 2,\n" + },
" \"title\": \"iPhone X\",\n" + {
" \"description\": \"SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...\",\n" + "id": 2,
" \"price\": 899,\n" + "title": "iPhone X",
" \"discountPercentage\": 17.94,\n" + "description": "SIM-Free, Model A19211 6.5-inch Super Retina HD display with OLED technology A12 Bionic chip with ...",
" \"rating\": 4.44,\n" + "price": 899,
" \"stock\": 34,\n" + "discountPercentage": 17.94,
" \"brand\": \"Apple\",\n" + "rating": 4.44,
" \"category\": \"smartphones\"\n" + "stock": 34,
" },\n" + "brand": "Apple",
" {\n" + "category": "smartphones"
" \"id\": 3,\n" + },
" \"title\": \"Samsung Universe 9\",\n" + {
" \"description\": \"Samsung's new variant which goes beyond Galaxy to the Universe\",\n" + "id": 3,
" \"price\": 1249,\n" + "title": "Samsung Universe 9",
" \"discountPercentage\": 15.46,\n" + "description": "Samsung's new variant which goes beyond Galaxy to the Universe",
" \"rating\": 4.09,\n" + "price": 1249,
" \"stock\": 36,\n" + "discountPercentage": 15.46,
" \"brand\": \"Samsung\",\n" + "rating": 4.09,
" \"category\": \"smartphones\"\n" + "stock": 36,
" },\n" + "brand": "Samsung",
" {\n" + "category": "smartphones"
" \"id\": 4,\n" + },
" \"title\": \"OPPOF19\",\n" + {
" \"description\": \"OPPO F19 is officially announced on April 2021.\",\n" + "id": 4,
" \"price\": 280,\n" + "title": "OPPOF19",
" \"discountPercentage\": 17.91,\n" + "description": "OPPO F19 is officially announced on April 2021.",
" \"rating\": 4.3,\n" + "price": 280,
" \"stock\": 123,\n" + "discountPercentage": 17.91,
" \"brand\": \"OPPO\",\n" + "rating": 4.3,
" \"category\": \"smartphones\"\n" + "stock": 123,
" },\n" + "brand": "OPPO",
" {\n" + "category": "smartphones"
" \"id\": 5,\n" + },
" \"title\": \"Huawei P30\",\n" + {
" \"description\": \"Huaweis re-badged P30 Pro New Edition was officially unveiled yesterday in Germany and now the device has made its way to the UK.\",\n" + "id": 5,
" \"price\": 499,\n" + "title": "Huawei P30",
" \"discountPercentage\": 10.58,\n" + "description": "Huaweis re-badged P30 Pro New Edition was officially unveiled yesterday in Germany and now the device has made its way to the UK.",
" \"rating\": 4.09,\n" + "price": 499,
" \"stock\": 32,\n" + "discountPercentage": 10.58,
" \"brand\": \"Huawei\",\n" + "rating": 4.09,
" \"category\": \"smartphones\"\n" + "stock": 32,
" }\n" + "brand": "Huawei",
" ],\n" + "category": "smartphones"
" \"total\": 5\n" + }
"}"; ],
"total": 5
}""";
@BeforeAll @BeforeAll
void setUp() { void setUp() {
wireMockServer = new WireMockServer(8080); wireMockServer = new WireMockServer(8080);
wireMockServer.start(); wireMockServer.start();
WireMock.configureFor("localhost", 8080); WireMock.configureFor("localhost", 8080);
System.out.println(PRODUCT_OFFERS);
stubFor(get(urlEqualTo("/api/dummy")) stubFor(get(urlEqualTo("/api/dummy"))
.willReturn(aResponse() .willReturn(aResponse()
.withFixedDelay(5000) // must be > DEFAULT_TIMEOUT for a timeout to occur. .withFixedDelay(5000) // must be > DEFAULT_TIMEOUT for a timeout to occur.