[update] method name, result, flow
This commit is contained in:
parent
e3ef6b6275
commit
7e924cefe4
@ -7,7 +7,9 @@ import org.junit.jupiter.api.BeforeAll;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
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.*;
|
||||||
@ -21,18 +23,19 @@ 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 int TIMEOUT_STATUS_CODE = 408; //0.5 seconds
|
private static final String DEFAULT_PRODUCT = "default_product";
|
||||||
|
private static final String PRODUCT_OFFERS = "product_offers";
|
||||||
|
|
||||||
@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("stubing");
|
||||||
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.
|
||||||
.withStatus(408)));
|
.withBody(PRODUCT_OFFERS)));
|
||||||
|
|
||||||
executorService = Executors.newScheduledThreadPool(1);
|
executorService = Executors.newScheduledThreadPool(1);
|
||||||
}
|
}
|
||||||
@ -44,39 +47,47 @@ class CompletableFutureTimeoutUnitTest {
|
|||||||
wireMockServer.stop();
|
wireMockServer.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<Integer> createDummyRequest() {
|
private CompletableFuture<String> fetchProductData() {
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
try {
|
try {
|
||||||
URL url = new URL("http://localhost:8080/api/dummy");
|
URL url = new URL("http://localhost:8080/api/dummy");
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
try {
|
try {
|
||||||
return connection.getResponseCode();
|
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
String inputLine;
|
||||||
|
StringBuffer response = new StringBuffer();
|
||||||
|
|
||||||
|
while ((inputLine = in.readLine()) != null) {
|
||||||
|
response.append(inputLine);
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
return response.toString();
|
||||||
} finally {
|
} finally {
|
||||||
connection.disconnect();
|
connection.disconnect();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (IOException e) {
|
||||||
return TIMEOUT_STATUS_CODE;
|
return "";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenorTimeout_thenGetThrow() {
|
void whenorTimeout_thenGetThrow() {
|
||||||
CompletableFuture<Integer> completableFuture = createDummyRequest();
|
CompletableFuture<String> completableFuture = fetchProductData();
|
||||||
completableFuture.orTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
completableFuture.orTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||||
assertThrows(ExecutionException.class, completableFuture::get);
|
assertThrows(ExecutionException.class, completableFuture::get);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whencompleteOnTimeout_thenReturnValue() throws ExecutionException, InterruptedException {
|
void whencompleteOnTimeout_thenReturnValue() throws ExecutionException, InterruptedException {
|
||||||
CompletableFuture<Integer> completableFuture = createDummyRequest();
|
CompletableFuture<String> completableFuture = fetchProductData();
|
||||||
completableFuture.completeOnTimeout(TIMEOUT_STATUS_CODE, DEFAULT_TIMEOUT,TimeUnit.MILLISECONDS);
|
completableFuture.completeOnTimeout(DEFAULT_PRODUCT, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||||
assertEquals(TIMEOUT_STATUS_CODE, completableFuture.get());
|
assertEquals(DEFAULT_PRODUCT, completableFuture.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whencompleteExceptionally_thenGetThrow() {
|
void whencompleteExceptionally_thenGetThrow() {
|
||||||
CompletableFuture<Integer> completableFuture = createDummyRequest();
|
CompletableFuture<String> completableFuture = fetchProductData();
|
||||||
executorService.schedule(() -> completableFuture
|
executorService.schedule(() -> completableFuture
|
||||||
.completeExceptionally(new TimeoutException("Timeout occurred")), DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
.completeExceptionally(new TimeoutException("Timeout occurred")), DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||||
assertThrows(ExecutionException.class, completableFuture::get);
|
assertThrows(ExecutionException.class, completableFuture::get);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user