Async refactor (#3570)
This commit is contained in:
parent
55660e7702
commit
2169be4301
@ -1,15 +1,6 @@
|
|||||||
package com.baeldung.asynchttpclient;
|
package com.baeldung.asynchttpclient;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import io.netty.handler.codec.http.HttpHeaders;
|
||||||
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 org.asynchttpclient.AsyncCompletionHandler;
|
import org.asynchttpclient.AsyncCompletionHandler;
|
||||||
import org.asynchttpclient.AsyncHandler;
|
import org.asynchttpclient.AsyncHandler;
|
||||||
import org.asynchttpclient.AsyncHttpClient;
|
import org.asynchttpclient.AsyncHttpClient;
|
||||||
@ -27,7 +18,15 @@ import org.asynchttpclient.ws.WebSocketUpgradeHandler;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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 {
|
public class AsyncHttpClientTestCase {
|
||||||
|
|
||||||
@ -35,7 +34,6 @@ public class AsyncHttpClientTestCase {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
|
|
||||||
AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build();
|
AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build();
|
||||||
HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig);
|
HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig);
|
||||||
}
|
}
|
||||||
@ -50,12 +48,7 @@ public class AsyncHttpClientTestCase {
|
|||||||
Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS);
|
Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS);
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
assertEquals(200, response.getStatusCode());
|
assertEquals(200, response.getStatusCode());
|
||||||
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (ExecutionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (TimeoutException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +67,7 @@ public class AsyncHttpClientTestCase {
|
|||||||
|
|
||||||
HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler<Integer>() {
|
HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler<Integer>() {
|
||||||
@Override
|
@Override
|
||||||
public Integer onCompleted(Response response) throws Exception {
|
public Integer onCompleted(Response response) {
|
||||||
|
|
||||||
int resposeStatusCode = response.getStatusCode();
|
int resposeStatusCode = response.getStatusCode();
|
||||||
assertEquals(200, resposeStatusCode);
|
assertEquals(200, resposeStatusCode);
|
||||||
@ -87,8 +80,7 @@ public class AsyncHttpClientTestCase {
|
|||||||
|
|
||||||
boundGetRequest.execute(new AsyncCompletionHandler<Integer>() {
|
boundGetRequest.execute(new AsyncCompletionHandler<Integer>() {
|
||||||
@Override
|
@Override
|
||||||
public Integer onCompleted(Response response) throws Exception {
|
public Integer onCompleted(Response response) {
|
||||||
|
|
||||||
int resposeStatusCode = response.getStatusCode();
|
int resposeStatusCode = response.getStatusCode();
|
||||||
assertEquals(200, resposeStatusCode);
|
assertEquals(200, resposeStatusCode);
|
||||||
return resposeStatusCode;
|
return resposeStatusCode;
|
||||||
@ -113,18 +105,18 @@ public class AsyncHttpClientTestCase {
|
|||||||
int responseStatusCode = -1;
|
int responseStatusCode = -1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
|
public State onStatusReceived(HttpResponseStatus responseStatus) {
|
||||||
responseStatusCode = responseStatus.getStatusCode();
|
responseStatusCode = responseStatus.getStatusCode();
|
||||||
return State.CONTINUE;
|
return State.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public State onHeadersReceived(HttpHeaders headers) throws Exception {
|
public State onHeadersReceived(HttpHeaders headers) {
|
||||||
return State.CONTINUE;
|
return State.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
|
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) {
|
||||||
return State.CONTINUE;
|
return State.CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +126,7 @@ public class AsyncHttpClientTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer onCompleted() throws Exception {
|
public Integer onCompleted() {
|
||||||
assertEquals(200, responseStatusCode);
|
assertEquals(200, responseStatusCode);
|
||||||
return responseStatusCode;
|
return responseStatusCode;
|
||||||
}
|
}
|
||||||
@ -161,7 +153,6 @@ public class AsyncHttpClientTestCase {
|
|||||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}, Executors.newCachedThreadPool());
|
}, Executors.newCachedThreadPool());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -207,7 +198,6 @@ public class AsyncHttpClientTestCase {
|
|||||||
WEBSOCKET_CLIENT.sendTextFrame("test message");
|
WEBSOCKET_CLIENT.sendTextFrame("test message");
|
||||||
WEBSOCKET_CLIENT.sendBinaryFrame(new byte[]{'t', 'e', 's', 't'});
|
WEBSOCKET_CLIENT.sendBinaryFrame(new byte[]{'t', 'e', 's', 't'});
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user