Merge pull request #1 from hangga/non-static

Non static
This commit is contained in:
Hangga Aji Sayekti 2023-10-16 17:33:54 +07:00 committed by GitHub
commit eb4a8eb49d
1 changed files with 29 additions and 17 deletions

View File

@ -5,8 +5,11 @@ import com.github.tomakehurst.wiremock.client.WireMock;
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;
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.*;
@ -15,67 +18,76 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CompletableFutureTimeoutUnitTest { class CompletableFutureTimeoutUnitTest {
private static WireMockServer wireMockServer; private WireMockServer wireMockServer;
private static 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
static 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);
} }
@AfterAll @AfterAll
static void tearDown() { void tearDown() {
executorService.shutdown(); executorService.shutdown();
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 (IOException 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();
.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();
.completeOnTimeout(TIMEOUT_STATUS_CODE, DEFAULT_TIMEOUT, completableFuture.completeOnTimeout(DEFAULT_PRODUCT, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS); assertEquals(DEFAULT_PRODUCT, completableFuture.get());
assertEquals(TIMEOUT_STATUS_CODE, 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);