use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class AbstractIOSessionPool method getSessionInternal.
private void getSessionInternal(final PoolEntry poolEntry, final boolean requestNew, final T namedEndpoint, final Timeout connectTimeout, final FutureCallback<IOSession> callback) {
synchronized (poolEntry) {
if (poolEntry.session != null && requestNew) {
closeSession(poolEntry.session, CloseMode.GRACEFUL);
poolEntry.session = null;
}
if (poolEntry.session != null && !poolEntry.session.isOpen()) {
poolEntry.session = null;
}
if (poolEntry.session != null) {
callback.completed(poolEntry.session);
} else {
poolEntry.requestQueue.add(callback);
if (poolEntry.sessionFuture != null && poolEntry.sessionFuture.isDone()) {
poolEntry.sessionFuture = null;
}
if (poolEntry.sessionFuture == null) {
poolEntry.sessionFuture = connectSession(namedEndpoint, connectTimeout, new FutureCallback<IOSession>() {
@Override
public void completed(final IOSession result) {
synchronized (poolEntry) {
poolEntry.session = result;
for (; ; ) {
final FutureCallback<IOSession> callback = poolEntry.requestQueue.poll();
if (callback != null) {
callback.completed(result);
} else {
break;
}
}
}
}
@Override
public void failed(final Exception ex) {
synchronized (poolEntry) {
poolEntry.session = null;
for (; ; ) {
final FutureCallback<IOSession> callback = poolEntry.requestQueue.poll();
if (callback != null) {
callback.failed(ex);
} else {
break;
}
}
}
}
@Override
public void cancelled() {
failed(new ConnectionClosedException("Connection request cancelled"));
}
});
}
}
}
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class TestBasicFuture method testCompletedWithTimeout.
@Test
public void testCompletedWithTimeout() throws Exception {
final FutureCallback<Object> callback = Mockito.mock(FutureCallback.class);
final BasicFuture<Object> future = new BasicFuture<>(callback);
Assertions.assertFalse(future.isDone());
final Object result = new Object();
final Exception boom = new Exception();
future.completed(result);
future.failed(boom);
Mockito.verify(callback).completed(result);
Mockito.verify(callback, Mockito.never()).failed(Mockito.any());
Mockito.verify(callback, Mockito.never()).cancelled();
Assertions.assertSame(result, future.get(1, TimeUnit.MILLISECONDS));
Assertions.assertTrue(future.isDone());
Assertions.assertFalse(future.isCancelled());
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class TestBasicFuture method testFailed.
@Test
public void testFailed() throws Exception {
final FutureCallback<Object> callback = Mockito.mock(FutureCallback.class);
final BasicFuture<Object> future = new BasicFuture<>(callback);
final Object result = new Object();
final Exception boom = new Exception();
future.failed(boom);
future.completed(result);
Mockito.verify(callback, Mockito.never()).completed(Mockito.any());
Mockito.verify(callback).failed(boom);
Mockito.verify(callback, Mockito.never()).cancelled();
try {
future.get();
} catch (final ExecutionException ex) {
Assertions.assertSame(boom, ex.getCause());
}
Assertions.assertTrue(future.isDone());
Assertions.assertFalse(future.isCancelled());
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class AbstractBinAsyncEntityConsumer method streamStart.
@Override
public final void streamStart(final EntityDetails entityDetails, final FutureCallback<T> resultCallback) throws IOException, HttpException {
Args.notNull(resultCallback, "Result callback");
this.resultCallback = resultCallback;
try {
final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null;
streamStart(contentType);
} catch (final UnsupportedCharsetException ex) {
throw new UnsupportedEncodingException(ex.getMessage());
}
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class TestHttpRequestExecutor method testExecutionEntityEnclosingRequestWithExpectContinueMultiple1xxResponses.
@Test
public void testExecutionEntityEnclosingRequestWithExpectContinueMultiple1xxResponses() throws Exception {
final HttpProcessor httprocessor = Mockito.mock(HttpProcessor.class);
final HttpClientConnection conn = Mockito.mock(HttpClientConnection.class);
final HttpRequestExecutor executor = new HttpRequestExecutor();
final HttpCoreContext context = HttpCoreContext.create();
final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/");
request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
final HttpEntity entity = Mockito.mock(HttpEntity.class);
request.setEntity(entity);
executor.preProcess(request, httprocessor, context);
Mockito.verify(httprocessor).process(request, request.getEntity(), context);
Mockito.when(conn.receiveResponseHeader()).thenReturn(new BasicClassicHttpResponse(110, "Huh?"), new BasicClassicHttpResponse(100, "Continue"), new BasicClassicHttpResponse(111, "Huh?"), new BasicClassicHttpResponse(200, "OK"));
Mockito.when(conn.isDataAvailable(ArgumentMatchers.any(Timeout.class))).thenReturn(Boolean.TRUE);
final HttpResponseInformationCallback callback = Mockito.mock(HttpResponseInformationCallback.class);
final ClassicHttpResponse response = executor.execute(request, conn, callback, context);
Mockito.verify(conn).sendRequestHeader(request);
Mockito.verify(conn).sendRequestEntity(request);
Mockito.verify(conn, Mockito.times(2)).flush();
Mockito.verify(conn, Mockito.times(2)).isDataAvailable(Timeout.ofMilliseconds(3000));
Mockito.verify(conn, Mockito.times(4)).receiveResponseHeader();
Mockito.verify(conn).receiveResponseEntity(response);
final ArgumentCaptor<HttpResponse> responseCaptor = ArgumentCaptor.forClass(HttpResponse.class);
Mockito.verify(callback, Mockito.times(2)).execute(responseCaptor.capture(), ArgumentMatchers.eq(conn), ArgumentMatchers.eq(context));
final List<HttpResponse> infos = responseCaptor.getAllValues();
Assertions.assertNotNull(infos);
Assertions.assertEquals(2, infos.size());
final HttpResponse info1 = infos.get(0);
Assertions.assertNotNull(info1);
Assertions.assertEquals(110, info1.getCode());
final HttpResponse info2 = infos.get(1);
Assertions.assertNotNull(info2);
Assertions.assertEquals(111, info2.getCode());
executor.postProcess(response, httprocessor, context);
Mockito.verify(httprocessor).process(response, response.getEntity(), context);
}
Aggregations