use of java.util.concurrent.CompletableFuture in project presto by prestodb.
the class TestBackupManager method testSimple.
@Test
public void testSimple() throws Exception {
List<CompletableFuture<?>> futures = new ArrayList<>();
List<UUID> uuids = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
File file = new File(temporary, "file" + i);
Files.write("hello world", file, UTF_8);
uuids.add(randomUUID());
futures.add(backupManager.submit(uuids.get(i), file));
}
futures.forEach(CompletableFuture::join);
for (UUID uuid : uuids) {
assertTrue(store.shardExists(uuid));
}
}
use of java.util.concurrent.CompletableFuture in project jetty.project by eclipse.
the class SlowClientsTest method testSlowClientsWithSmallThreadPool.
@Test(timeout = 10000)
public void testSlowClientsWithSmallThreadPool() throws Exception {
File keystore = MavenTestingUtils.getTestResourceFile("keystore");
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keystore.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
sslContextFactory.setKeyManagerPassword("keypwd");
int maxThreads = 6;
int contentLength = 8 * 1024 * 1024;
QueuedThreadPool serverThreads = new QueuedThreadPool(maxThreads);
serverThreads.setDetailedDump(true);
Server server = new Server(serverThreads);
try {
ServerConnector connector = new ServerConnector(server, 1, 1, sslContextFactory);
connector.setPort(8888);
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
logger.info("SERVING {}", target);
// Write some big content.
response.getOutputStream().write(new byte[contentLength]);
logger.info("SERVED {}", target);
}
});
server.start();
SSLContext sslContext = sslContextFactory.getSslContext();
CompletableFuture[] futures = new CompletableFuture[2 * maxThreads];
ExecutorService executor = Executors.newFixedThreadPool(futures.length);
for (int i = 0; i < futures.length; i++) {
int k = i;
futures[i] = CompletableFuture.runAsync(() -> {
try (SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort())) {
socket.setSoTimeout(contentLength / 1024);
OutputStream output = socket.getOutputStream();
String target = "/" + k;
String request = "GET " + target + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
while (serverThreads.getIdleThreads() > 0) Thread.sleep(50);
InputStream input = socket.getInputStream();
while (true) {
int read = input.read();
if (read < 0)
break;
}
logger.info("FINISHED {}", target);
} catch (IOException x) {
throw new UncheckedIOException(x);
} catch (InterruptedException x) {
throw new UncheckedIOException(new InterruptedIOException());
}
}, executor);
}
CompletableFuture.allOf(futures).join();
} finally {
server.stop();
}
}
use of java.util.concurrent.CompletableFuture in project vert.x by eclipse.
the class Http2ClientTest method testClientRequestWriteability.
@Test
public void testClientRequestWriteability() throws Exception {
Buffer content = Buffer.buffer();
Buffer expected = Buffer.buffer();
String chunk = TestUtils.randomAlphaString(100);
CompletableFuture<Void> done = new CompletableFuture<>();
AtomicBoolean paused = new AtomicBoolean();
AtomicInteger numPause = new AtomicInteger();
server.requestHandler(req -> {
Context ctx = vertx.getOrCreateContext();
done.thenAccept(v1 -> {
paused.set(false);
ctx.runOnContext(v2 -> {
req.resume();
});
});
numPause.incrementAndGet();
req.pause();
paused.set(true);
req.handler(content::appendBuffer);
req.endHandler(v -> {
assertEquals(expected, content);
req.response().end();
});
});
startServer();
HttpClientRequest req = client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
testComplete();
}).setChunked(true).exceptionHandler(err -> {
fail();
});
AtomicInteger sent = new AtomicInteger();
AtomicInteger count = new AtomicInteger();
AtomicInteger drained = new AtomicInteger();
vertx.setPeriodic(1, timerID -> {
Context ctx = vertx.getOrCreateContext();
if (req.writeQueueFull()) {
assertTrue(paused.get());
assertEquals(1, numPause.get());
req.drainHandler(v -> {
assertOnIOContext(ctx);
assertEquals(0, drained.getAndIncrement());
assertEquals(1, numPause.get());
assertFalse(paused.get());
req.end();
});
vertx.cancelTimer(timerID);
done.complete(null);
} else {
count.incrementAndGet();
expected.appendString(chunk);
req.write(chunk);
sent.addAndGet(chunk.length());
}
});
await();
}
use of java.util.concurrent.CompletableFuture in project vert.x by eclipse.
the class Http1xTest method testInvalidHttpResponse.
@Test
public void testInvalidHttpResponse() {
waitFor(2);
AtomicInteger count = new AtomicInteger(0);
CompletableFuture<Void> sendResp = new CompletableFuture<>();
NetServer server = vertx.createNetServer();
String match = "GET /somepath HTTP/1.1\r\nHost: localhost:8080\r\n\r\n";
server.connectHandler(so -> {
StringBuilder content = new StringBuilder();
so.handler(buff -> {
content.append(buff);
while (content.toString().startsWith(match)) {
content.delete(0, match.length());
switch(count.getAndIncrement()) {
case 0:
sendResp.thenAccept(v -> {
});
break;
case 1:
Buffer resp1 = Buffer.buffer(TestUtils.randomAlphaString(40) + "\r\n");
Buffer resp2 = Buffer.buffer("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
so.write(Buffer.buffer().appendBuffer(resp1).appendBuffer(resp2));
break;
default:
fail();
break;
}
}
});
}).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1));
AtomicBoolean fail1 = new AtomicBoolean();
HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
fail();
}).exceptionHandler(err -> {
if (fail1.compareAndSet(false, true)) {
assertEquals(IllegalArgumentException.class, err.getClass());
complete();
}
});
AtomicBoolean fail2 = new AtomicBoolean();
HttpClientRequest req2 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
resp.bodyHandler(buff -> {
assertEquals("okusa", buff.toString());
testComplete();
});
}).exceptionHandler(err -> {
if (fail2.compareAndSet(false, true)) {
assertEquals(VertxException.class, err.getClass());
complete();
}
});
req1.end();
req2.end();
}));
await();
}
use of java.util.concurrent.CompletableFuture in project crate by crate.
the class AlterTableOperation method executeAlterTable.
public CompletableFuture<Long> executeAlterTable(AlterTableAnalyzedStatement analysis) {
DocTableInfo table = analysis.table();
if (table.isAlias() && !table.isPartitioned()) {
return CompletableFutures.failedFuture(new AlterTableAliasException(table.ident().fqn()));
}
List<CompletableFuture<Long>> results = new ArrayList<>(3);
if (table.isPartitioned()) {
// create new filtered partition table settings
PartitionedTableParameterInfo tableSettingsInfo = (PartitionedTableParameterInfo) table.tableParameterInfo();
TableParameter parameterWithFilteredSettings = new TableParameter(analysis.tableParameter().settings(), tableSettingsInfo.partitionTableSettingsInfo().supportedInternalSettings());
Optional<PartitionName> partitionName = analysis.partitionName();
if (partitionName.isPresent()) {
String index = partitionName.get().asIndexName();
results.add(updateMapping(analysis.tableParameter().mappings(), index));
results.add(updateSettings(parameterWithFilteredSettings, index));
} else {
// template gets all changes unfiltered
results.add(updateTemplate(analysis.tableParameter(), table.ident()));
if (!analysis.excludePartitions()) {
String[] indices = table.concreteIndices();
results.add(updateMapping(analysis.tableParameter().mappings(), indices));
results.add(updateSettings(parameterWithFilteredSettings, indices));
}
}
} else {
results.add(updateMapping(analysis.tableParameter().mappings(), table.ident().indexName()));
results.add(updateSettings(analysis.tableParameter(), table.ident().indexName()));
}
final CompletableFuture<Long> result = new CompletableFuture<>();
applyMultiFutureCallback(result, results);
return result;
}
Aggregations