void onStartResponse(Consumer<? super Response> onResponse, Response response) {
   if (Strings.hasLength(response.getScrollId()) && response.getHits().isEmpty()) {
     logger.debug(
         "First response looks like a scan response. Jumping right to the second. scroll=[{}]",
         response.getScrollId());
     doStartNextScroll(response.getScrollId(), timeValueMillis(0), onResponse);
   } else {
     onResponse.accept(response);
   }
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void testTooLargeResponse() throws Exception {
    ContentTooLongException tooLong = new ContentTooLongException("too long!");
    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(
            any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class),
            any(FutureCallback.class)))
        .then(
            new Answer<Future<HttpResponse>>() {
              @Override
              public Future<HttpResponse> answer(InvocationOnMock invocationOnMock)
                  throws Throwable {
                HeapBufferedAsyncResponseConsumer consumer =
                    (HeapBufferedAsyncResponseConsumer) invocationOnMock.getArguments()[1];
                FutureCallback callback = (FutureCallback) invocationOnMock.getArguments()[2];

                assertEquals(
                    new ByteSizeValue(200, ByteSizeUnit.MB).bytesAsInt(),
                    consumer.getBufferLimit());
                callback.failed(tooLong);
                return null;
              }
            });
    RemoteScrollableHitSource source = sourceWithMockedClient(true, httpClient);

    AtomicBoolean called = new AtomicBoolean();
    Consumer<Response> checkResponse = r -> called.set(true);
    Throwable e =
        expectThrows(
            RuntimeException.class,
            () -> source.doStartNextScroll(FAKE_SCROLL_ID, timeValueMillis(0), checkResponse));
    // Unwrap the some artifacts from the test
    while (e.getMessage().equals("failed")) {
      e = e.getCause();
    }
    // This next exception is what the user sees
    assertEquals(
        "Remote responded with a chunk that was too large. Use a smaller batch size.",
        e.getMessage());
    // And that exception is reported as being caused by the underlying exception returned by the
    // client
    assertSame(tooLong, e.getCause());
    assertFalse(called.get());
  }