io.searchbox.client.JestResultHandler Java Examples

The following examples show how to use io.searchbox.client.JestResultHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PollCachingESRegistry.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Stores a "dataversion" record in the ES store.  There is only a single one of these.  The
 * return value of the add will include the version number of the entity.  This version
 * number is what we use to determine whether our cache is stale.
 */
protected void updateDataVersion() {
    DataVersionBean dv = new DataVersionBean();
    dv.setUpdatedOn(System.currentTimeMillis());
    Index index = new Index.Builder(dv).refresh(false)
            .index(getDefaultIndexName())
            .type("dataVersion").id("instance").build(); //$NON-NLS-1$ //$NON-NLS-2$
    getClient().executeAsync(index, new JestResultHandler<JestResult>() {
        @Override
        public void completed(JestResult result) {
            dataVersion = null;
        }
        @Override
        public void failed(Exception e) {
            dataVersion = null;
        }
    });
}
 
Example #2
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncDelegatesToFailureHandlerOnPrepareRequestIOException() throws IOException {

    // given
    BufferedJestHttpClient client = spy(createDefaultTestHttpClient());
    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    String expectedMesage = UUID.randomUUID().toString();
    BufferedBulk bulk = createDefaultTestBufferedBulk();
    when(client.prepareRequest(bulk)).thenThrow(new IOException(expectedMesage));

    JestResultHandler<JestResult> jestResultHandler = createMockTestResultHandler();

    // when
    client.executeAsync(bulk, jestResultHandler);

    // then
    verify(jestResultHandler).failed(exceptionCaptor.capture());
    assertEquals(expectedMesage, exceptionCaptor.getValue().getMessage());

    verify(client, never()).getAsyncClient();
    verify(asyncClient, never()).execute(any(HttpUriRequest.class), any());

}
 
Example #3
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) {
    return new JestResultHandler<JestResult>() {
        @Override
        public void completed(JestResult result) {

            backoffPolicy.deregister(bulk);

            if (!result.isSucceeded()) {
                LOG.warn(result.getErrorMessage());
                failureHandler.apply(bulk);
            }
        }
        @Override
        public void failed(Exception ex) {
            LOG.warn(ex.getMessage(), ex);
            backoffPolicy.deregister(bulk);
            failureHandler.apply(bulk);
        }
    };
}
 
Example #4
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncCallbackHandlesBufferedJestResult() throws IOException {

    // given
    JestResultHandler<JestResult> jestResultHandler = createMockTestResultHandler();

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(jestResultHandler);

    String reasonPhrase = UUID.randomUUID().toString();
    HttpResponse httpResponse = createDefaultTestHttpResponse(200, reasonPhrase);

    // when
    asyncCallback.completed(httpResponse);

    // then
    verify(jestResultHandler, never()).failed(any());
    verify(jestResultHandler).completed(captor.capture());
    JestResult jestResult = captor.getValue();

    assertTrue(jestResult.isSucceeded());
    assertEquals(200, jestResult.getResponseCode());
    assertEquals(reasonPhrase, jestResult.getErrorMessage());
    assertNull(((BufferedJestResult)jestResult).getItems());

}
 
Example #5
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsExecutedAfterNonSuccessfulRequest() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    JestHttpObjectFactory config = spy(builder.build());

    String payload1 = "test1";
    String payload2 = "test2";
    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(false);

    // when
    resultHandler.completed(result);

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(failoverHandler, times(1)).apply(captor.capture());

    assertEquals(bulk, captor.getValue());
}
 
Example #6
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsNotExecutedAfterSuccessfulRequest() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    JestHttpObjectFactory config = spy(builder.build());

    String payload1 = "test1";
    String payload2 = "test2";
    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(true);

    // when
    resultHandler.completed(result);

    // then
    verify(failoverHandler, never()).apply(Mockito.any(Bulk.class));
}
 
Example #7
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsExecutedAfterFailedRequest() {

    // given
    Builder builder = createTestObjectFactoryBuilder();
    JestHttpObjectFactory config = spy(builder.build());

    String payload1 = "test1";
    String payload2 = "test2";
    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    // when
    resultHandler.failed(new IOException());

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(failoverHandler, times(1)).apply(captor.capture());

    assertEquals(bulk, captor.getValue());
}
 
Example #8
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsExecutedAfterFailedRequest() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2");

    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    // when
    resultHandler.failed(new IOException());

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(failoverHandler, times(1)).apply(captor.capture());
    verify((BufferedBulk)bulk, times(1)).completed();

    assertEquals(bulk, captor.getValue());

}
 
Example #9
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsNotExecutedAfterSuccessfulRequest() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2");
    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(true);

    // when
    resultHandler.completed(result);

    // then
    verify(failoverHandler, never()).apply(Mockito.any(Bulk.class));
}
 
Example #10
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
private BufferedJestHttpClient.BufferedResultCallback mockHttpResponseCallback(JestResultHandler<JestResult> jestResultHandler, BufferedBulkResult bufferedBulkResult) throws IOException {
    BufferedJestHttpClient client = spy(createDefaultTestHttpClient());
    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    BufferedBulk bulk = mock(BufferedBulk.class);
    when(bulk.getURI()).thenReturn(UUID.randomUUID().toString());
    when(bulk.deserializeResponse(any())).thenReturn(bufferedBulkResult);
    when(bulk.serializeRequest()).thenReturn(mock(ByteBuf.class));

    client.executeAsync(bulk, jestResultHandler);
    verify(asyncClient).execute(any(HttpUriRequest.class), bufferedResultCallbackCaptor.capture());
    return bufferedResultCallbackCaptor.getValue();
}
 
Example #11
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void executeAsyncCallbackCallsResultHandlerFailedOnThrowable() throws IOException {

    // given
    JestResultHandler<JestResult> resultHandler = createMockTestResultHandler();

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(resultHandler);

    HttpEntity entity = mock(HttpEntity.class);
    String exceptionMessage = UUID.randomUUID().toString();
    when(entity.getContent()).thenAnswer((Answer<InputStream>) invocation -> {
        throw new Throwable(exceptionMessage);
    });

    HttpResponse httpResponse = createDefaultTestHttpResponse(200, UUID.randomUUID().toString());
    when(httpResponse.getEntity()).thenReturn(entity);

    // when
    asyncCallback.completed(httpResponse);

    // then
    verify(resultHandler).failed(exceptionCaptor.capture());
    assertEquals("Problem during request processing", exceptionCaptor.getValue().getMessage());
    assertEquals(exceptionMessage, exceptionCaptor.getValue().getCause().getMessage());

    verify(resultHandler, never()).completed(any());

}
 
Example #12
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void executeAsyncCallbackHandlesNonSuccessfulBufferedJestResult() throws IOException {

    // given
    JestResultHandler<JestResult> jestResultHandler = createMockTestResultHandler();

    BufferedBulkResult bufferedBulkResult = spy(new BufferedBulkResult(0, true, null, 400, new ArrayList<>()));

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(jestResultHandler, bufferedBulkResult);

    int expectedStatusCode = new Random().nextInt(1000) + 1000;
    String reasonPhrase = UUID.randomUUID().toString();
    HttpResponse httpResponse = createDefaultTestHttpResponse(expectedStatusCode, reasonPhrase);

    // when
    asyncCallback.completed(httpResponse);

    // then
    verify(jestResultHandler, never()).failed(any());
    verify(jestResultHandler).completed(captor.capture());
    JestResult jestResult = captor.getValue();

    assertFalse(jestResult.isSucceeded());
    assertEquals(expectedStatusCode, jestResult.getResponseCode());
    assertNotEquals(httpResponse.getStatusLine().getReasonPhrase(), jestResult.getErrorMessage());
    assertNotNull(((BufferedJestResult)jestResult).getItems());

}
 
Example #13
Source File: BufferedJestHttpClient.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends JestResult> void executeAsync(Action<T> clientRequest, JestResultHandler<? super T> resultHandler) {
    HttpUriRequest request;
    try {
        request = prepareRequest((BufferedBulk)clientRequest);
    } catch (IOException e) {
        resultHandler.failed(e);
        return;
    }

    getAsyncClient().execute(request, responseHandler(clientRequest, resultHandler, request));

}
 
Example #14
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void failoverIsExecutedAfterNonSuccessfulRequest() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2");
    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(false);

    // when
    resultHandler.completed(result);

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(failoverHandler, times(1)).apply(captor.capture());
    verify((BufferedBulk)bulk, times(1)).completed();

    assertEquals(bulk, captor.getValue());
}
 
Example #15
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
private JestResultHandler<JestResult> createMockTestResultHandler() {
    return spy(new JestResultHandler<JestResult>() {
        @Override
        public void completed(JestResult result) {
        }

        @Override
        public void failed(Exception ex) {
        }
    });
}
 
Example #16
Source File: BufferedJestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) {
    return new JestResultHandler<JestResult>() {

        @Override
        public void completed(JestResult result) {

            backoffPolicy.deregister(bulk);

            if (!result.isSucceeded()) {
                LOG.warn(result.getErrorMessage());
                // TODO: filter only failed items when retry is ready.
                // failing whole bulk for now
                failureHandler.apply(bulk);
            }
            ((BufferedBulk)bulk).completed();
        }

        @Override
        public void failed(Exception ex) {
            LOG.warn(ex.getMessage(), ex);
            backoffPolicy.deregister(bulk);
            failureHandler.apply(bulk);
            ((BufferedBulk)bulk).completed();
        }

    };
}
 
Example #17
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Function<Bulk, Boolean> createBatchListener(FailoverPolicy failoverPolicy) {
    return new Function<Bulk, Boolean>() {

        private Function<Bulk, Boolean> failureHandler = createFailureHandler(failoverPolicy);

        @Override
        public Boolean apply(Bulk bulk) {

            while (!operations.isEmpty()) {
                try {
                    operations.remove().execute();
                } catch (Exception e) {
                    // TODO: redirect to failover (?) retry with exp. backoff (?) multiple options here
                    LOG.error("Deferred operation failed: {}", e.getMessage());
                }
            }

            if (backoffPolicy.shouldApply(bulk)) {
                LOG.warn("Backoff applied. Request rejected.");
                failureHandler.apply(bulk);
                return false;
            } else {
                backoffPolicy.register(bulk);
            }

            JestResultHandler<JestResult> jestResultHandler = createResultHandler(bulk, failureHandler);
            createClient().executeAsync(bulk, jestResultHandler);
            return true;
        }

    };
}
 
Example #18
Source File: EsResetter.java    From apiman with Apache License 2.0 5 votes vote down vote up
@Override
public void reset() {
    try {
        CountDownLatch latch = new CountDownLatch(1);

        // Important! Or will get cached client that assumes the DB schema has already been created
        // and subtly horrible things will happen, and you'll waste a whole day debugging it! :-)
        DefaultEsClientFactory.clearClientCache();

        getClient().executeAsync(new Delete.Builder(getDefaultIndexName()).build(),
                new JestResultHandler<JestResult>() {

            @Override
            public void completed(JestResult result) {
                latch.countDown();
                System.out.println("=== Deleted index: " + result.getJsonString());
            }

            @Override
            public void failed(Exception ex) {
                latch.countDown();
                System.err.println("=== Failed to delete index: " + ex.getMessage());
                throw new RuntimeException(ex);
            }
        });

        Flush flush = new Flush.Builder().build();
        getClient().execute(flush);
        Thread.sleep(100);

        latch.await();
    } catch (IOException | InterruptedException e) {
        throw new RuntimeException(e);
    }
}
 
Example #19
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void failureHandlerDoesNotRethrowExceptions() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    BufferedJestHttpObjectFactory objectFactory = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");


    Function<Bulk, Boolean> failoverHandler = objectFactory.createFailureHandler(failedPayload -> {
        throw new ClassCastException("test exception");
    });

    Bulk bulk = createTestBatch(payload1);
    JestResultHandler<JestResult> responseHandler = objectFactory.createResultHandler(bulk, failoverHandler);

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(false);

    // when
    responseHandler.completed(result);

}
 
Example #20
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void executeAsyncCallbackCallsResultHandlerWhenCancelled() throws IOException {

    // given
    JestResultHandler<JestResult> resultHandler = createMockTestResultHandler();

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(resultHandler);

    // when
    asyncCallback.cancelled();

    // then
    verify(resultHandler).failed(exceptionCaptor.capture());
    assertEquals("Request cancelled", exceptionCaptor.getValue().getMessage());

    verify(resultHandler, never()).completed(any());

}
 
Example #21
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void executeAsyncCallbackCallsResultHandlerFailedOnIOException() throws IOException {

    // given
    JestResultHandler<JestResult> resultHandler = createMockTestResultHandler();

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(resultHandler);

    HttpEntity entity = mock(HttpEntity.class);
    String exceptionMessage = UUID.randomUUID().toString();
    when(entity.getContent()).thenThrow(new IOException(exceptionMessage));

    HttpResponse httpResponse = createDefaultTestHttpResponse(200, UUID.randomUUID().toString());
    when(httpResponse.getEntity()).thenReturn(entity);

    // when
    asyncCallback.completed(httpResponse);

    // then
    verify(resultHandler).failed(exceptionCaptor.capture());
    assertEquals(exceptionMessage, exceptionCaptor.getValue().getMessage());

    verify(resultHandler, never()).completed(any());

}
 
Example #22
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
public void executeAsyncCallbackCallsResultHandlerCompleted() throws IOException {

    // given
    JestResultHandler<JestResult> resultHandler = createMockTestResultHandler();

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(resultHandler);

    HttpResponse httpResponse = createDefaultTestHttpResponse(200, UUID.randomUUID().toString());

    // when
    asyncCallback.completed(httpResponse);

    // then
    verify(resultHandler, never()).failed(any());
    verify(resultHandler).completed(any());

}
 
Example #23
Source File: BufferedJestHttpClient.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
public BufferedResultCallback(Action<T> clientRequest, JestResultHandler<T> request) {
    this.clientRequest = clientRequest;
    this.resultHandler = request;
}
 
Example #24
Source File: BufferedJestHttpClient.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private <T extends JestResult> BufferedResultCallback<T> responseHandler(Action<T> clientRequest, JestResultHandler<? super T> resultHandler, HttpUriRequest request) {
    return new BufferedResultCallback(clientRequest, resultHandler);
}
 
Example #25
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void responseHandlerDeregistersRequestFromBackoffPolicyAfterSuccess() {

    // given
    BackoffPolicy<AbstractAction<BulkResult>> backoffPolicy = mock(BackoffPolicy.class);
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    builder.withBackoffPolicy(backoffPolicy);

    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    Bulk bulk = createTestBatch(payload1);

    FailoverPolicy failoverPolicy = mock(FailoverPolicy.class);

    JestResultHandler<JestResult> responseHandler = config.createResultHandler(bulk, config.createFailureHandler(failoverPolicy));

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(true);

    // when
    responseHandler.completed(result);

    // then
    verify(backoffPolicy, times(1)).deregister(eq(bulk));

}
 
Example #26
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void responseHandlerDeregistersRequestFromBackoffPolicyAfterException() {

    // given
    BackoffPolicy<AbstractAction<BulkResult>> backoffPolicy = mock(BackoffPolicy.class);
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    builder.withBackoffPolicy(backoffPolicy);

    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    Bulk bulk = createTestBatch(payload1);

    FailoverPolicy failoverPolicy = mock(FailoverPolicy.class);

    JestResultHandler<JestResult> responseHandler = config.createResultHandler(bulk, config.createFailureHandler(failoverPolicy));

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(false);

    // when
    responseHandler.completed(result);

    // then
    verify(backoffPolicy, times(1)).deregister(eq(bulk));

}
 
Example #27
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
private BufferedJestHttpClient.BufferedResultCallback mockHttpResponseCallback(JestResultHandler<JestResult> jestResultHandler) throws IOException {

        BufferedBulkResult bufferedBulkResult = mock(BufferedBulkResult.class);
        when(bufferedBulkResult.isSucceeded()).thenReturn(true);
        when(bufferedBulkResult.getItems()).thenReturn(null);
        when(bufferedBulkResult.getErrorMessage(any())).thenCallRealMethod();

        return mockHttpResponseCallback(jestResultHandler, bufferedBulkResult);

    }
 
Example #28
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void responseHandlerDeregistersRequestFromBackoffPolicyAfterSuccess() {

    // given
    BackoffPolicy<AbstractAction<BulkResult>> backoffPolicy = mock(BackoffPolicy.class);
    Builder builder = createTestObjectFactoryBuilder();
    builder.withBackoffPolicy(backoffPolicy);

    JestHttpObjectFactory config = spy(builder.build());

    String payload1 = "test1";
    Bulk bulk = createTestBatch(payload1);

    FailoverPolicy failoverPolicy = mock(FailoverPolicy.class);

    JestResultHandler<JestResult> responseHandler = config.createResultHandler(bulk, config.createFailureHandler(failoverPolicy));

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(true);

    // when
    responseHandler.completed(result);

    // then
    verify(backoffPolicy, times(1)).deregister(eq(bulk));

}
 
Example #29
Source File: JestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void responseHandlerDeregistersRequestFromBackoffPolicyAfterException() {

    // given
    BackoffPolicy<AbstractAction<BulkResult>> backoffPolicy = mock(BackoffPolicy.class);
    Builder builder = createTestObjectFactoryBuilder();
    builder.withBackoffPolicy(backoffPolicy);

    JestHttpObjectFactory config = spy(builder.build());

    String payload1 = "test1";
    Bulk bulk = createTestBatch(payload1);

    FailoverPolicy failoverPolicy = mock(FailoverPolicy.class);

    JestResultHandler<JestResult> responseHandler = config.createResultHandler(bulk, config.createFailureHandler(failoverPolicy));

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(false);

    // when
    responseHandler.completed(result);

    // then
    verify(backoffPolicy, times(1)).deregister(eq(bulk));

}