Example usage for org.apache.http.concurrent FutureCallback failed

List of usage examples for org.apache.http.concurrent FutureCallback failed

Introduction

In this page you can find the example usage for org.apache.http.concurrent FutureCallback failed.

Prototype

void failed(Exception exception);

Source Link

Usage

From source file:org.elasticsearch.client.RestClientSingleHostTests.java

@Before
@SuppressWarnings("unchecked")
public void createRestClient() throws IOException {
    httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class)))
                    .thenAnswer(new Answer<Future<HttpResponse>>() {
                        @Override
                        public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
                            HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock
                                    .getArguments()[0];
                            HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2];
                            assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class));
                            FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock
                                    .getArguments()[3];
                            HttpUriRequest request = (HttpUriRequest) requestProducer.generateRequest();
                            //return the desired status code or exception depending on the path
                            if (request.getURI().getPath().equals("/soe")) {
                                futureCallback.failed(new SocketTimeoutException());
                            } else if (request.getURI().getPath().equals("/coe")) {
                                futureCallback.failed(new ConnectTimeoutException());
                            } else {
                                int statusCode = Integer.parseInt(request.getURI().getPath().substring(1));
                                StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1),
                                        statusCode, "");

                                HttpResponse httpResponse = new BasicHttpResponse(statusLine);
                                //return the same body that was sent
                                if (request instanceof HttpEntityEnclosingRequest) {
                                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                                    if (entity != null) {
                                        assertTrue(
                                                "the entity is not repeatable, cannot set it to the response directly",
                                                entity.isRepeatable());
                                        httpResponse.setEntity(entity);
                                    }/*from w  w w. j  a  v  a 2s.  c om*/
                                }
                                //return the same headers that were sent
                                httpResponse.setHeaders(request.getAllHeaders());
                                futureCallback.completed(httpResponse);
                            }
                            return null;
                        }
                    });

    defaultHeaders = RestClientTestUtil.randomHeaders(getRandom(), "Header-default");
    httpHost = new HttpHost("localhost", 9200);
    failureListener = new HostsTrackingFailureListener();
    restClient = new RestClient(httpClient, 10000, defaultHeaders, new HttpHost[] { httpHost }, null,
            failureListener);
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test(expected = ExecutionException.class)
public void testRequestConnectionFailed() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, null);
    Assert.assertNotNull(future);//from w  w  w. ja  v a  2s .c  o m

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    callaback.failed(new Exception());

    Assert.assertTrue(future.isDone());
    future.get();
}