From 2169be43014347905e0ff6a35e25020953c3bfc8 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 2 Feb 2018 15:40:03 +0100 Subject: [PATCH] Async refactor (#3570) --- .../AsyncHttpClientTestCase.java | 318 +++++++++--------- 1 file changed, 154 insertions(+), 164 deletions(-) diff --git a/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java index 7f9c2699fe..1398c2ba41 100644 --- a/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java +++ b/libraries/src/test/java/com/baeldung/asynchttpclient/AsyncHttpClientTestCase.java @@ -1,15 +1,6 @@ package com.baeldung.asynchttpclient; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - +import io.netty.handler.codec.http.HttpHeaders; import org.asynchttpclient.AsyncCompletionHandler; import org.asynchttpclient.AsyncHandler; import org.asynchttpclient.AsyncHttpClient; @@ -27,193 +18,192 @@ import org.asynchttpclient.ws.WebSocketUpgradeHandler; import org.junit.Before; import org.junit.Test; -import io.netty.handler.codec.http.HttpHeaders; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class AsyncHttpClientTestCase { - private static AsyncHttpClient HTTP_CLIENT; + private static AsyncHttpClient HTTP_CLIENT; - @Before - public void setup() { + @Before + public void setup() { + AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); + HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); + } - AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build(); - HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig); - } - - @Test - public void givenHttpClient_executeSyncGetRequest() { + @Test + public void givenHttpClient_executeSyncGetRequest() { - BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); - Future responseFuture = boundGetRequest.execute(); - try { - Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); - assertNotNull(response); - assertEquals(200, response.getStatusCode()); + Future responseFuture = boundGetRequest.execute(); + try { + Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS); + assertNotNull(response); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } catch (TimeoutException e) { - e.printStackTrace(); - } - - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - @Test - public void givenHttpClient_executeAsyncGetRequest() { + @Test + public void givenHttpClient_executeAsyncGetRequest() { - // execute an unbound GET request - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); - HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler() { - @Override - public Integer onCompleted(Response response) throws Exception { + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) { - int resposeStatusCode = response.getStatusCode(); - assertEquals(200, resposeStatusCode); - return resposeStatusCode; - } - }); + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); - // execute a bound GET request - BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); + // execute a bound GET request + BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com"); - boundGetRequest.execute(new AsyncCompletionHandler() { - @Override - public Integer onCompleted(Response response) throws Exception { + boundGetRequest.execute(new AsyncCompletionHandler() { + @Override + public Integer onCompleted(Response response) { + int resposeStatusCode = response.getStatusCode(); + assertEquals(200, resposeStatusCode); + return resposeStatusCode; + } + }); - int resposeStatusCode = response.getStatusCode(); - assertEquals(200, resposeStatusCode); - return resposeStatusCode; - } - }); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + @Test + public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { - @Test - public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() { + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); - // execute an unbound GET request - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler() { - HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler() { + int responseStatusCode = -1; - int responseStatusCode = -1; + @Override + public State onStatusReceived(HttpResponseStatus responseStatus) { + responseStatusCode = responseStatus.getStatusCode(); + return State.CONTINUE; + } - @Override - public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception { - responseStatusCode = responseStatus.getStatusCode(); - return State.CONTINUE; - } + @Override + public State onHeadersReceived(HttpHeaders headers) { + return State.CONTINUE; + } - @Override - public State onHeadersReceived(HttpHeaders headers) throws Exception { - return State.CONTINUE; - } + @Override + public State onBodyPartReceived(HttpResponseBodyPart bodyPart) { + return State.CONTINUE; + } - @Override - public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception { - return State.CONTINUE; - } + @Override + public void onThrowable(Throwable t) { - @Override - public void onThrowable(Throwable t) { + } - } + @Override + public Integer onCompleted() { + assertEquals(200, responseStatusCode); + return responseStatusCode; + } + }); - @Override - public Integer onCompleted() throws Exception { - assertEquals(200, responseStatusCode); - return responseStatusCode; - } - }); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + @Test + public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { + // execute an unbound GET request + Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); - @Test - public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() { - // execute an unbound GET request - Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build(); + ListenableFuture listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); + listenableFuture.addListener(() -> { + Response response; + try { + response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); + assertEquals(200, response.getStatusCode()); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + }, Executors.newCachedThreadPool()); - ListenableFuture listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest); - listenableFuture.addListener(() -> { - Response response; - try { - response = listenableFuture.get(5000, TimeUnit.MILLISECONDS); - assertEquals(200, response.getStatusCode()); - } catch (InterruptedException | ExecutionException | TimeoutException e) { - e.printStackTrace(); - } + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } - }, Executors.newCachedThreadPool()); + @Test + public void givenWebSocketClient_tryToConnect() { - try { - Thread.sleep(5000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } + WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); + WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { + @Override + public void onOpen(WebSocket websocket) { + // WebSocket connection opened + } - @Test - public void givenWebSocketClient_tryToConnect() { + @Override + public void onClose(WebSocket websocket, int code, String reason) { + // WebSocket connection closed + } - WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder(); - WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() { - @Override - public void onOpen(WebSocket websocket) { - // WebSocket connection opened - } + @Override + public void onError(Throwable t) { + // WebSocket connection error + assertTrue(t.getMessage().contains("Request timeout")); + } + }).build(); - @Override - public void onClose(WebSocket websocket, int code, String reason) { - // WebSocket connection closed - } + WebSocket WEBSOCKET_CLIENT = null; + try { + WEBSOCKET_CLIENT = Dsl.asyncHttpClient() + .prepareGet("ws://localhost:5590/websocket") + .addHeader("header_name", "header_value") + .addQueryParam("key", "value") + .setRequestTimeout(5000) + .execute(wsHandler).get(); - @Override - public void onError(Throwable t) { - // WebSocket connection error - assertTrue(t.getMessage().contains("Request timeout")); - } - }).build(); - - WebSocket WEBSOCKET_CLIENT = null; - try { - WEBSOCKET_CLIENT = Dsl.asyncHttpClient() - .prepareGet("ws://localhost:5590/websocket") - .addHeader("header_name", "header_value") - .addQueryParam("key", "value") - .setRequestTimeout(5000) - .execute(wsHandler).get(); - - if (WEBSOCKET_CLIENT.isOpen()) { - WEBSOCKET_CLIENT.sendPingFrame(); - WEBSOCKET_CLIENT.sendTextFrame("test message"); - WEBSOCKET_CLIENT.sendBinaryFrame(new byte[] { 't', 'e', 's', 't' }); - } - - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); - } finally { - if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { - WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); - } - } - } + if (WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendPingFrame(); + WEBSOCKET_CLIENT.sendTextFrame("test message"); + WEBSOCKET_CLIENT.sendBinaryFrame(new byte[]{'t', 'e', 's', 't'}); + } + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } finally { + if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) { + WEBSOCKET_CLIENT.sendCloseFrame(200, "OK"); + } + } + } }