use of java.util.function.Consumer in project elasticsearch by elastic.
the class RemoteScrollableHitSourceTests method testTooLargeResponse.
@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(HttpClientContext.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()[3];
assertEquals(new ByteSizeValue(100, 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());
}
use of java.util.function.Consumer in project elasticsearch by elastic.
the class RemoteScrollableHitSourceTests method sourceWithMockedClient.
private RemoteScrollableHitSource sourceWithMockedClient(boolean mockRemoteVersion, CloseableHttpAsyncClient httpClient) throws Exception {
HttpAsyncClientBuilder clientBuilder = mock(HttpAsyncClientBuilder.class);
when(clientBuilder.build()).thenReturn(httpClient);
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(httpClientBuilder -> clientBuilder).build();
TestRemoteScrollableHitSource hitSource = new TestRemoteScrollableHitSource(restClient) {
@Override
void lookupRemoteVersion(Consumer<Version> onVersion) {
if (mockRemoteVersion) {
onVersion.accept(Version.CURRENT);
} else {
super.lookupRemoteVersion(onVersion);
}
}
};
if (mockRemoteVersion) {
hitSource.remoteVersion = Version.CURRENT;
}
return hitSource;
}
use of java.util.function.Consumer in project che by eclipse.
the class ProjectTreeTracker method getDeleteOperation.
private Consumer<String> getDeleteOperation(String endpointId) {
return it -> {
timers.add(it);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (timers.contains(it)) {
timers.remove(it);
ProjectTreeStateUpdateDto params = newDto(ProjectTreeStateUpdateDto.class).withPath(it).withType(DELETED);
transmitter.transmitOneToNone(endpointId, OUTGOING_METHOD, params);
}
}
}, 1_000L);
};
}
use of java.util.function.Consumer in project vert.x by eclipse.
the class EventBusTestBase method testSendFromExecuteBlocking.
@Test
public void testSendFromExecuteBlocking() throws Exception {
String expectedBody = TestUtils.randomAlphaString(20);
CountDownLatch receivedLatch = new CountDownLatch(1);
startNodes(2);
vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
assertEquals(expectedBody, msg.body());
receivedLatch.countDown();
}).completionHandler(ar -> {
assertTrue(ar.succeeded());
vertices[0].executeBlocking(fut -> {
vertices[0].eventBus().send(ADDRESS1, expectedBody);
try {
awaitLatch(receivedLatch);
} catch (InterruptedException e) {
Thread.interrupted();
fut.fail(e);
}
fut.complete();
}, ar2 -> {
if (ar2.succeeded()) {
testComplete();
} else {
fail(ar2.cause());
}
});
});
await();
}
use of java.util.function.Consumer in project vert.x by eclipse.
the class EventBusTestBase method testReplyFromWorker.
@Test
public void testReplyFromWorker() throws Exception {
String expectedBody = TestUtils.randomAlphaString(20);
startNodes(2);
CountDownLatch latch = new CountDownLatch(1);
vertices[0].deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
vertices[1].eventBus().<String>consumer(ADDRESS1, msg -> {
msg.reply(expectedBody);
}).completionHandler(ar -> {
assertTrue(ar.succeeded());
latch.countDown();
});
}
}, new DeploymentOptions().setWorker(true));
awaitLatch(latch);
vertices[0].eventBus().send(ADDRESS1, "whatever", reply -> {
assertTrue(reply.succeeded());
assertEquals(expectedBody, reply.result().body());
testComplete();
});
await();
}
Aggregations