use of java.util.concurrent.Future 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.concurrent.Future in project che by eclipse.
the class ProjectServiceTest method testDeleteProjectsConcurrently.
@Test
public void testDeleteProjectsConcurrently() throws Exception {
int threadNumber = 5 * (Runtime.getRuntime().availableProcessors() + 1);
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
List<Future<ContainerResponse>> futures = new LinkedList<>();
for (int i = 0; i < threadNumber; i++) {
addMockedProjectConfigDto(ptRegistry.getProjectType("my_project_type"), "my_project_name" + i);
}
IntStream.range(0, threadNumber).forEach(i -> {
futures.add(executor.submit(() -> {
countDownLatch.countDown();
countDownLatch.await();
try {
return launcher.service(DELETE, "http://localhost:8080/api/project/my_project_name" + i, "http://localhost:8080/api", null, null, null);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}));
});
boolean isNotDone;
do {
isNotDone = false;
for (Future<ContainerResponse> future : futures) {
if (!future.isDone()) {
isNotDone = true;
}
}
} while (isNotDone);
for (Future<ContainerResponse> future : futures) {
assertEquals(future.get().getStatus(), 204, "Error: " + future.get().getEntity());
}
executor.shutdown();
}
use of java.util.concurrent.Future in project che by eclipse.
the class RmiClient method stopAll.
public void stopAll(boolean wait) {
List<ProcessInfo> processList;
synchronized (infoMap) {
processList = (infoMap.values().stream().filter(info -> info.processHandler != null).collect(Collectors.toList()));
}
if (processList.isEmpty()) {
return;
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<?> future = executorService.submit(() -> {
for (ProcessInfo processInfo : processList) {
processInfo.processHandler.destroyProcess();
}
if (wait) {
for (ProcessInfo processInfo : processList) {
processInfo.processHandler.waitFor();
}
}
});
if (wait) {
try {
future.get();
} catch (InterruptedException ignore) {
} catch (java.util.concurrent.ExecutionException e) {
LOG.error(e.getMessage(), e);
}
}
executorService.shutdown();
}
use of java.util.concurrent.Future in project jetty.project by eclipse.
the class Server method doStop.
/* ------------------------------------------------------------ */
@Override
protected void doStop() throws Exception {
if (isDumpBeforeStop())
dumpStdErr();
if (LOG.isDebugEnabled())
LOG.debug("doStop {}", this);
MultiException mex = new MultiException();
// list if graceful futures
List<Future<Void>> futures = new ArrayList<>();
// First close the network connectors to stop accepting new connections
for (Connector connector : _connectors) futures.add(connector.shutdown());
// Then tell the contexts that we are shutting down
Handler[] gracefuls = getChildHandlersByClass(Graceful.class);
for (Handler graceful : gracefuls) futures.add(((Graceful) graceful).shutdown());
// Shall we gracefully wait for zero connections?
long stopTimeout = getStopTimeout();
if (stopTimeout > 0) {
long stop_by = System.currentTimeMillis() + stopTimeout;
if (LOG.isDebugEnabled())
LOG.debug("Graceful shutdown {} by ", this, new Date(stop_by));
// Wait for shutdowns
for (Future<Void> future : futures) {
try {
if (!future.isDone())
future.get(Math.max(1L, stop_by - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
} catch (Exception e) {
mex.add(e);
}
}
}
// Cancel any shutdowns not done
for (Future<Void> future : futures) if (!future.isDone())
future.cancel(true);
// Now stop the connectors (this will close existing connections)
for (Connector connector : _connectors) {
try {
connector.stop();
} catch (Throwable e) {
mex.add(e);
}
}
// And finally stop everything else
try {
super.doStop();
} catch (Throwable e) {
mex.add(e);
}
if (getStopAtShutdown())
ShutdownThread.deregister(this);
//Unregister the Server with the handler thread for receiving
//remote stop commands as we are stopped already
ShutdownMonitor.deregister(this);
mex.ifExceptionThrow();
}
use of java.util.concurrent.Future in project jetty.project by eclipse.
the class QoSFilterTest method testQosFilter.
@Test
public void testQosFilter() throws Exception {
FilterHolder holder = new FilterHolder(QoSFilter2.class);
holder.setAsyncSupported(true);
holder.setInitParameter(QoSFilter.MAX_REQUESTS_INIT_PARAM, String.valueOf(MAX_QOS));
_tester.getContext().getServletHandler().addFilterWithMapping(holder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
List<Worker2> workers = new ArrayList<>();
for (int i = 0; i < NUM_CONNECTIONS; ++i) {
workers.add(new Worker2(i));
}
ExecutorService executor = Executors.newFixedThreadPool(NUM_CONNECTIONS);
List<Future<Void>> futures = executor.invokeAll(workers, 20, TimeUnit.SECONDS);
rethrowExceptions(futures);
if (TestServlet.__maxSleepers < MAX_QOS)
LOG.warn("TEST WAS NOT PARALLEL ENOUGH!");
else
Assert.assertEquals(TestServlet.__maxSleepers, MAX_QOS);
}
Aggregations