use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class Http1xTest method testContexts.
@Test
public void testContexts() throws Exception {
int numReqs = 4;
Buffer data = randomBuffer(1024);
AtomicInteger cnt = new AtomicInteger();
// Server connect handler should always be called with same context
Context serverCtx = vertx.getOrCreateContext();
CountDownLatch latch = new CountDownLatch(1);
List<HttpServerRequest> requests = Collections.synchronizedList(new ArrayList<>());
Map<String, CompletionStage<Long>> requestResumeMap = new HashMap<>();
Map<String, CompletionStage<Void>> responseResumeMap = new HashMap<>();
serverCtx.runOnContext(v1 -> {
server.requestHandler(req -> {
req.pause();
assertSameEventLoop(serverCtx, Vertx.currentContext());
Buffer body = Buffer.buffer();
requestResumeMap.get(req.path()).thenAccept(amount -> {
req.resume();
req.endHandler(v2 -> {
assertSameEventLoop(serverCtx, Vertx.currentContext());
assertEquals((long) amount, body.length());
requests.add(req);
if (requests.size() == numReqs) {
requests.forEach(req_ -> {
HttpServerResponse resp = req_.response();
CompletableFuture<Void> cf = new CompletableFuture<>();
responseResumeMap.put(req_.path(), cf);
resp.setChunked(true);
fill(data, resp, sent -> {
cf.complete(null);
resp.drainHandler(v -> {
assertSameEventLoop(serverCtx, Vertx.currentContext());
resp.end();
});
});
});
}
});
});
req.handler(chunk -> {
assertSameEventLoop(serverCtx, Vertx.currentContext());
body.appendBuffer(chunk);
});
});
server.listen(testAddress, onSuccess(s -> {
assertSameEventLoop(serverCtx, Vertx.currentContext());
latch.countDown();
}));
});
awaitLatch(latch);
CountDownLatch latch2 = new CountDownLatch(1);
int numConns = 4;
client.close();
client = null;
Context clientCtx = vertx.getOrCreateContext();
clientCtx.runOnContext(v -> {
client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(numConns));
});
waitUntil(() -> client != null);
// There should be a context per request
List<EventLoopContext> contexts = Stream.generate(() -> ((VertxInternal) vertx).createEventLoopContext()).limit(4).collect(Collectors.toList());
Set<Thread> expectedThreads = new HashSet<>();
for (Context ctx : contexts) {
CompletableFuture<Thread> th = new CompletableFuture<>();
ctx.runOnContext(v -> {
th.complete(Thread.currentThread());
});
expectedThreads.add(th.get());
}
Set<Thread> threads = new ConcurrentHashSet<>();
for (int i = 0; i < numReqs; i++) {
Context requestCtx = contexts.get(i);
CompletableFuture<Long> cf = new CompletableFuture<>();
String path = "/" + i;
requestResumeMap.put(path, cf);
requestCtx.runOnContext(v -> {
Thread t = Thread.currentThread();
client.request(new RequestOptions(requestOptions).setURI(path)).onComplete(onSuccess(req -> {
assertSame(t, Thread.currentThread());
req.response(onSuccess(resp -> {
assertSameEventLoop(requestCtx, Vertx.currentContext());
assertEquals(200, resp.statusCode());
threads.add(Thread.currentThread());
resp.pause();
responseResumeMap.get(path).thenAccept(v2 -> resp.resume());
resp.handler(chunk -> {
assertSameEventLoop(requestCtx, Vertx.currentContext());
});
resp.exceptionHandler(this::fail);
resp.endHandler(v2 -> {
assertSameEventLoop(requestCtx, Vertx.currentContext());
if (cnt.incrementAndGet() == numReqs) {
assertEquals(expectedThreads, new HashSet<>(threads));
latch2.countDown();
}
});
})).setChunked(true).exceptionHandler(this::fail);
req.drainHandler(v2 -> {
assertSameEventLoop(requestCtx, Vertx.currentContext());
req.end();
});
req.sendHead(version -> {
assertSameEventLoop(requestCtx, Vertx.currentContext());
fill(data, req, cf::complete);
});
}));
});
}
awaitLatch(latch2, 40, TimeUnit.SECONDS);
// Close should be in own context
new Thread(() -> {
server.close(onSuccess(v -> {
ContextInternal closeContext = ((VertxInternal) vertx).getContext();
assertFalse(contexts.contains(closeContext));
assertNotSame(serverCtx, closeContext);
assertFalse(contexts.contains(serverCtx));
testComplete();
}));
server = null;
}).start();
await();
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class FileResolverTestBase method testCacheDirDeletedOnVertxClose.
@Test
public void testCacheDirDeletedOnVertxClose() {
VertxInternal vertx2 = (VertxInternal) vertx();
File file = vertx2.resolveFile("webroot/somefile.html");
assertTrue(file.exists());
File cacheDir = file.getParentFile().getParentFile();
assertTrue(cacheDir.exists());
vertx2.close(onSuccess(v -> {
assertFalse(cacheDir.exists());
testComplete();
}));
await();
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class NettyCompatTest method testAddressResolver.
@Test
public void testAddressResolver() {
VertxInternal vertx = (VertxInternal) super.vertx;
vertx.resolveAddress("localhost", onSuccess(v -> testComplete()));
await();
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class SSLEngineTest method doTest.
private void doTest(SSLEngineOptions engine, boolean useAlpn, HttpVersion version, String error, String expectedSslContext, boolean expectCause) {
server.close();
HttpServerOptions options = new HttpServerOptions().setSslEngineOptions(engine).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setKeyCertOptions(Cert.SERVER_PEM.get()).setSsl(true).setUseAlpn(useAlpn);
try {
server = vertx.createHttpServer(options);
} catch (VertxException e) {
e.printStackTrace();
if (error == null) {
fail(e);
} else {
assertEquals(error, e.getMessage());
if (expectCause) {
assertNotSame(e, e.getCause());
}
}
return;
}
server.requestHandler(req -> {
assertEquals(req.version(), version);
assertTrue(req.isSSL());
req.response().end();
});
server.listen(onSuccess(s -> {
HttpServerImpl impl = (HttpServerImpl) s;
SSLHelper sslHelper = impl.getSslHelper();
SslContext ctx = sslHelper.getContext((VertxInternal) vertx);
switch(expectedSslContext) {
case "jdk":
assertTrue(ctx instanceof JdkSslContext);
break;
case "openssl":
assertTrue(ctx instanceof OpenSslContext);
break;
}
client = vertx.createHttpClient(new HttpClientOptions().setSslEngineOptions(engine).setSsl(true).setUseAlpn(useAlpn).setTrustAll(true).setProtocolVersion(version));
client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> {
assertEquals(200, resp.statusCode());
testComplete();
});
}));
await();
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class AsyncMultiMapTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
startNodes(1);
clusterManager = ((VertxInternal) vertices[0]).getClusterManager();
CountDownLatch latch = new CountDownLatch(1);
clusterManager.<String, ServerID>getAsyncMultiMap("mymap", onSuccess(res -> {
map = res;
latch.countDown();
}));
awaitLatch(latch);
}
Aggregations