use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class HttpNetSocket method sendFile.
@Override
public NetSocket sendFile(String filename, long offset, long length, Handler<AsyncResult<Void>> resultHandler) {
VertxInternal vertx = conn.getContext().owner();
Handler<AsyncResult<Void>> h;
if (resultHandler != null) {
Context resultCtx = vertx.getOrCreateContext();
h = ar -> {
resultCtx.runOnContext((v) -> {
resultHandler.handle(ar);
});
};
} else {
h = ar -> {
};
}
HttpUtils.resolveFile(vertx, filename, offset, length, ar -> {
if (ar.succeeded()) {
AsyncFile file = ar.result();
file.pipe().endOnComplete(false).to(this, ar1 -> file.close(ar2 -> {
Throwable failure = ar1.failed() ? ar1.cause() : ar2.failed() ? ar2.cause() : null;
if (failure == null)
h.handle(ar1);
else
h.handle(Future.failedFuture(failure));
}));
} else {
h.handle(ar.mapEmpty());
}
});
return this;
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class NodeInfoTest method testFailedFutureForUnknownNode.
@Test
public void testFailedFutureForUnknownNode() {
startNodes(2);
ClusterManager clusterManager = ((VertxInternal) vertices[0]).getClusterManager();
// Create unknown node identifier
String unknown = String.join("", clusterManager.getNodes());
// Needed as callback might be done from non Vert.x thread
disableThreadChecks();
Promise<NodeInfo> promise = Promise.promise();
clusterManager.getNodeInfo(unknown, promise);
promise.future().onComplete(onFailure(t -> testComplete()));
await();
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class FileResolverTestBase method testCaching.
private void testCaching(boolean enabled) throws Exception {
VertxInternal vertx = (VertxInternal) Vertx.vertx(new VertxOptions().setFileSystemOptions(new FileSystemOptions().setFileCachingEnabled(enabled)));
File tmp = File.createTempFile("vertx", ".bin");
tmp.deleteOnExit();
URL url = tmp.toURI().toURL();
Files.write(tmp.toPath(), "foo".getBytes());
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(new ClassLoader() {
@Override
public URL getResource(String name) {
if ("foo".equals(name)) {
return url;
}
return super.getResource(name);
}
});
File f = vertx.resolveFile("foo");
assertEquals("foo", new String(Files.readAllBytes(f.toPath())));
Files.write(tmp.toPath(), "bar".getBytes());
f = vertx.resolveFile("foo");
if (enabled) {
assertEquals("foo", new String(Files.readAllBytes(f.toPath())));
} else {
assertEquals("bar", new String(Files.readAllBytes(f.toPath())));
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class FileResolverTestBase method testResolveFileFromClasspathDisableCaching.
@Test
public void testResolveFileFromClasspathDisableCaching() throws Exception {
VertxInternal vertx = (VertxInternal) Vertx.vertx(new VertxOptions().setFileSystemOptions(new FileSystemOptions().setFileCachingEnabled(true)));
try {
for (int i = 0; i < 2; i++) {
File file = vertx.resolveFile("afile.html");
assertTrue(file.exists());
assertTrue(file.getPath().startsWith(cacheBaseDir + "-"));
assertFalse(file.isDirectory());
assertEquals("<html><body>afile</body></html>", readFile(file));
}
} finally {
vertx.close();
}
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class MetricsTest method testThreadPoolMetricsWithInternalExecuteBlocking.
@Test
public void testThreadPoolMetricsWithInternalExecuteBlocking() {
Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-internal-blocking");
assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getInternalBlockingPoolSize()));
int num = VertxOptions.DEFAULT_INTERNAL_BLOCKING_POOL_SIZE;
int count = num * 5;
AtomicBoolean hadWaitingQueue = new AtomicBoolean();
AtomicBoolean hadIdle = new AtomicBoolean();
AtomicBoolean hadRunning = new AtomicBoolean();
VertxInternal v = (VertxInternal) vertx;
Map<Integer, CountDownLatch> latches = new HashMap<>();
for (int i = 0; i < count; i++) {
CountDownLatch latch = latches.computeIfAbsent(i / num, k -> new CountDownLatch(num));
v.executeBlockingInternal(fut -> {
latch.countDown();
try {
awaitLatch(latch);
} catch (InterruptedException e) {
fail(e);
Thread.currentThread().interrupt();
}
if (metrics.numberOfRunningTasks() > 0) {
hadRunning.set(true);
}
if (metrics.numberOfWaitingTasks() > 0) {
hadWaitingQueue.set(true);
}
fut.complete();
}, false, ar -> {
if (metrics.numberOfIdleThreads() > 0) {
hadIdle.set(true);
}
});
}
assertWaitUntil(() -> metrics.numberOfSubmittedTask() == 100);
assertWaitUntil(() -> metrics.numberOfCompletedTasks() == 100);
assertTrue(hadIdle.get());
assertTrue(hadWaitingQueue.get());
assertTrue(hadRunning.get());
assertEquals(metrics.numberOfIdleThreads(), getOptions().getWorkerPoolSize());
assertEquals(metrics.numberOfRunningTasks(), 0);
assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Aggregations