use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.
the class ProxyTest method doHttpConnectionManagerProxyTest.
private void doHttpConnectionManagerProxyTest(HttpClientConnectionManager manager) {
HttpRequest request = new HttpRequest("GET", "/");
CompletableFuture requestCompleteFuture = new CompletableFuture();
manager.acquireConnection().whenComplete((conn, throwable) -> {
if (throwable != null) {
requestCompleteFuture.completeExceptionally(throwable);
}
HttpStream stream = conn.makeRequest(request, new HttpStreamResponseHandler() {
@Override
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
;
}
@Override
public void onResponseComplete(HttpStream stream, int errorCode) {
// When this Request is complete, release the conn back to the pool
manager.releaseConnection(conn);
stream.close();
if (errorCode != CRT.AWS_CRT_SUCCESS) {
requestCompleteFuture.completeExceptionally(new CrtRuntimeException(errorCode));
} else {
requestCompleteFuture.complete(null);
}
}
});
if (stream != null) {
stream.activate();
}
});
requestCompleteFuture.join();
}
use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.
the class HttpRequestResponseTest method testHttpRequestUnActivated.
@Test
public void testHttpRequestUnActivated() throws Exception {
skipIfNetworkUnavailable();
URI uri = new URI("https://httpbin.org");
HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()) };
HttpRequest request = new HttpRequest("GET", "/get", requestHeaders, null);
CompletableFuture<Void> shutdownComplete = null;
try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri)) {
shutdownComplete = connPool.getShutdownCompleteFuture();
try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) {
HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() {
@Override
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
// do nothing
}
@Override
public void onResponseHeadersDone(HttpStream stream, int blockType) {
// do nothing
}
@Override
public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
// do nothing
return bodyBytesIn.length;
}
@Override
public void onResponseComplete(HttpStream stream, int errorCode) {
// do nothing.
}
};
HttpStream stream = conn.makeRequest(request, streamHandler);
stream.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
if (shutdownComplete != null) {
shutdownComplete.get();
}
CrtResource.waitForNoResources();
}
use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.
the class HttpRequestResponseTest method getResponse.
public TestHttpResponse getResponse(URI uri, HttpRequest request, byte[] chunkedData) throws Exception {
boolean actuallyConnected = false;
final CompletableFuture<Void> reqCompleted = new CompletableFuture<>();
final TestHttpResponse response = new TestHttpResponse();
CompletableFuture<Void> shutdownComplete = null;
try (HttpClientConnectionManager connPool = createConnectionPoolManager(uri)) {
shutdownComplete = connPool.getShutdownCompleteFuture();
try (HttpClientConnection conn = connPool.acquireConnection().get(60, TimeUnit.SECONDS)) {
actuallyConnected = true;
HttpStreamResponseHandler streamHandler = new HttpStreamResponseHandler() {
@Override
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
response.statusCode = responseStatusCode;
Assert.assertEquals(responseStatusCode, stream.getResponseStatusCode());
response.headers.addAll(Arrays.asList(nextHeaders));
}
@Override
public void onResponseHeadersDone(HttpStream stream, int blockType) {
response.blockType = blockType;
}
@Override
public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) {
response.bodyBuffer.put(bodyBytesIn);
int amountRead = bodyBytesIn.length;
// Slide the window open by the number of bytes just read
return amountRead;
}
@Override
public void onResponseComplete(HttpStream stream, int errorCode) {
response.onCompleteErrorCode = errorCode;
reqCompleted.complete(null);
stream.close();
}
};
HttpStream stream = conn.makeRequest(request, streamHandler);
stream.activate();
if (chunkedData != null) {
stream.writeChunk(chunkedData, true).get(5, TimeUnit.SECONDS);
}
// Give the request up to 60 seconds to complete, otherwise throw a TimeoutException
reqCompleted.get(60, TimeUnit.SECONDS);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
Assert.assertTrue(actuallyConnected);
shutdownComplete.get();
CrtResource.waitForNoResources();
return response;
}
use of software.amazon.awssdk.crt.http.HttpStreamResponseHandler in project aws-crt-java by awslabs.
the class HttpClientConnectionManagerTest method testParallelConnections.
private void testParallelConnections(HttpClientConnectionManager connPool, HttpRequest request, int numThreads, int numRequests) {
final AtomicInteger numRequestsMade = new AtomicInteger(0);
final AtomicInteger numConnectionFailures = new AtomicInteger(0);
final ConcurrentHashMap<Integer, Integer> reqIdToStatus = new ConcurrentHashMap<>();
final AtomicInteger numErrorCode = new AtomicInteger(0);
final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
List<CompletableFuture> requestCompleteFutures = new ArrayList<>();
for (int i = 0; i < numRequests; i++) {
Log.log(Log.LogLevel.Trace, Log.LogSubject.HttpConnectionManager, String.format("Starting request %d", i));
CompletableFuture requestCompleteFuture = new CompletableFuture();
requestCompleteFutures.add(requestCompleteFuture);
threadPool.execute(() -> {
// Request a connection from the connection pool
connPool.acquireConnection().whenComplete((conn, throwable) -> {
if (throwable != null) {
numConnectionFailures.incrementAndGet();
connPool.releaseConnection(conn);
requestCompleteFuture.completeExceptionally(throwable);
}
int requestId = numRequestsMade.incrementAndGet();
HttpStream stream = conn.makeRequest(request, new HttpStreamResponseHandler() {
@Override
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
reqIdToStatus.put(requestId, responseStatusCode);
}
@Override
public void onResponseComplete(HttpStream stream, int errorCode) {
if (errorCode != CRT.AWS_CRT_SUCCESS) {
numErrorCode.incrementAndGet();
}
// When this Request is complete, release the conn back to the pool
connPool.releaseConnection(conn);
stream.close();
requestCompleteFuture.complete(null);
}
});
if (stream != null) {
stream.activate();
}
});
});
}
// Wait for all Requests to complete
for (CompletableFuture f : requestCompleteFutures) {
f.join();
}
final int requiredSuccesses = (int) Math.floor(numRequests * 0.95);
final int allowedFailures = numRequests - requiredSuccesses;
// Verify we got some Http Status Code for each Request
Assert.assertTrue(reqIdToStatus.size() >= requiredSuccesses);
// Verify that the failure counts aren't too high
Assert.assertTrue(numErrorCode.get() <= allowedFailures);
Assert.assertTrue(numConnectionFailures.get() <= allowedFailures);
}
Aggregations