[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.TestInstance;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.*;
|
||||
|
@ -21,18 +23,19 @@ class CompletableFutureTimeoutUnitTest {
|
|||
private WireMockServer wireMockServer;
|
||||
private ScheduledExecutorService executorService;
|
||||
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
|
||||
void setUp() {
|
||||
wireMockServer = new WireMockServer(8080);
|
||||
wireMockServer.start();
|
||||
WireMock.configureFor("localhost", 8080);
|
||||
|
||||
System.out.println("stubing");
|
||||
stubFor(get(urlEqualTo("/api/dummy"))
|
||||
.willReturn(aResponse()
|
||||
.withFixedDelay(5000) // must be > DEFAULT_TIMEOUT for a timeout to occur.
|
||||
.withStatus(408)));
|
||||
.withBody(PRODUCT_OFFERS)));
|
||||
|
||||
executorService = Executors.newScheduledThreadPool(1);
|
||||
}
|
||||
|
@ -44,39 +47,47 @@ class CompletableFutureTimeoutUnitTest {
|
|||
wireMockServer.stop();
|
||||
}
|
||||
|
||||
private CompletableFuture<Integer> createDummyRequest() {
|
||||
private CompletableFuture<String> fetchProductData() {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
URL url = new URL("http://localhost:8080/api/dummy");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
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 {
|
||||
connection.disconnect();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return TIMEOUT_STATUS_CODE;
|
||||
} catch (IOException e) {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenorTimeout_thenGetThrow() {
|
||||
CompletableFuture<Integer> completableFuture = createDummyRequest();
|
||||
CompletableFuture<String> completableFuture = fetchProductData();
|
||||
completableFuture.orTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||
assertThrows(ExecutionException.class, completableFuture::get);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whencompleteOnTimeout_thenReturnValue() throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<Integer> completableFuture = createDummyRequest();
|
||||
completableFuture.completeOnTimeout(TIMEOUT_STATUS_CODE, DEFAULT_TIMEOUT,TimeUnit.MILLISECONDS);
|
||||
assertEquals(TIMEOUT_STATUS_CODE, completableFuture.get());
|
||||
CompletableFuture<String> completableFuture = fetchProductData();
|
||||
completableFuture.completeOnTimeout(DEFAULT_PRODUCT, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||
assertEquals(DEFAULT_PRODUCT, completableFuture.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whencompleteExceptionally_thenGetThrow() {
|
||||
CompletableFuture<Integer> completableFuture = createDummyRequest();
|
||||
CompletableFuture<String> completableFuture = fetchProductData();
|
||||
executorService.schedule(() -> completableFuture
|
||||
.completeExceptionally(new TimeoutException("Timeout occurred")), DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||
assertThrows(ExecutionException.class, completableFuture::get);
|
||||
|
|
Loading…
Reference in New Issue