use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class NetTest method testContexts.
@Test
public void testContexts() throws Exception {
int numConnections = 10;
CountDownLatch serverLatch = new CountDownLatch(numConnections);
AtomicReference<Context> serverConnectContext = new AtomicReference<>();
server.connectHandler(sock -> {
// Server connect handler should always be called with same context
Context serverContext = Vertx.currentContext();
if (serverConnectContext.get() != null) {
assertSame(serverConnectContext.get(), serverContext);
} else {
serverConnectContext.set(serverContext);
}
serverLatch.countDown();
});
CountDownLatch listenLatch = new CountDownLatch(1);
AtomicReference<Context> listenContext = new AtomicReference<>();
server.listen(testAddress, onSuccess(v -> {
listenContext.set(Vertx.currentContext());
listenLatch.countDown();
}));
awaitLatch(listenLatch);
Set<Context> contexts = new ConcurrentHashSet<>();
AtomicInteger connectCount = new AtomicInteger();
CountDownLatch clientLatch = new CountDownLatch(1);
// Each connect should be in its own context
for (int i = 0; i < numConnections; i++) {
Context context = ((VertxInternal) vertx).createEventLoopContext();
context.runOnContext(v -> {
client.connect(testAddress, conn -> {
contexts.add(Vertx.currentContext());
if (connectCount.incrementAndGet() == numConnections) {
assertEquals(numConnections, contexts.size());
clientLatch.countDown();
}
});
});
}
awaitLatch(clientLatch);
awaitLatch(serverLatch);
// Close should be in own context
server.close(ar -> {
assertTrue(ar.succeeded());
Context closeContext = Vertx.currentContext();
assertFalse(contexts.contains(closeContext));
assertSame(serverConnectContext.get(), closeContext);
assertFalse(contexts.contains(listenContext.get()));
assertSame(serverConnectContext.get(), listenContext.get());
testComplete();
});
await();
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class SSLHelperTest method testPreserveEnabledCipherSuitesOrder.
@Test
public void testPreserveEnabledCipherSuitesOrder() throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
SSLEngine engine = context.createSSLEngine();
HttpServerOptions options = new HttpServerOptions();
for (String suite : engine.getEnabledCipherSuites()) {
options.addEnabledCipherSuite(suite);
}
assertEquals(new ArrayList<>(options.getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
assertEquals(new ArrayList<>(new HttpServerOptions(options).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
JsonObject json = options.toJson();
assertEquals(new ArrayList<>(new HttpServerOptions(json).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
SSLHelper helper = new SSLHelper(options, Cert.SERVER_JKS.get(), null);
assertEquals(Arrays.asList(helper.createEngine((VertxInternal) vertx).getEnabledCipherSuites()), Arrays.asList(engine.getEnabledCipherSuites()));
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class ClusteredAsynchronousLockTest method testLockReleasedForKilledNode.
/**
* Cannot run with the fake cluster manager.
* Subclasses need to override the method and call <code>super.testLockReleasedForKilledNode()</code>.
*/
@Test
@Ignore
public void testLockReleasedForKilledNode() throws Exception {
testLockReleased(latch -> {
VertxInternal vi = (VertxInternal) vertices[0];
Promise<Void> promise = vi.getOrCreateContext().promise();
vi.getClusterManager().leave(promise);
promise.future().onComplete(onSuccess(v -> {
latch.countDown();
}));
});
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class HostnameResolutionTest method testResolveMissingLocalhost.
@Test
public void testResolveMissingLocalhost() throws Exception {
InetAddress localhost = InetAddress.getByName("localhost");
// Set a dns resolver that won't resolve localhost
dnsServer.testResolveASameServer("127.0.0.1");
// Test using the resolver API
VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()).setOptResourceEnabled(false)));
CompletableFuture<Void> test1 = new CompletableFuture<>();
vertx.resolveAddress("localhost", ar -> {
if (ar.succeeded()) {
InetAddress resolved = ar.result();
if (resolved.equals(localhost)) {
test1.complete(null);
} else {
test1.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
}
} else {
test1.completeExceptionally(ar.cause());
}
});
test1.get(10, TimeUnit.SECONDS);
CompletableFuture<Void> test2 = new CompletableFuture<>();
vertx.resolveAddress("LOCALHOST", ar -> {
if (ar.succeeded()) {
InetAddress resolved = ar.result();
if (resolved.equals(localhost)) {
test2.complete(null);
} else {
test2.completeExceptionally(new AssertionError("Unexpected localhost value " + resolved));
}
} else {
test2.completeExceptionally(ar.cause());
}
});
test2.get(10, TimeUnit.SECONDS);
// Test using bootstrap
CompletableFuture<Void> test3 = new CompletableFuture<>();
NetServer server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost(localhost.getHostAddress()));
try {
server.connectHandler(so -> {
so.end(Buffer.buffer("hello"));
});
server.listen(ar -> {
if (ar.succeeded()) {
test3.complete(null);
} else {
test3.completeExceptionally(ar.cause());
}
});
test3.get(10, TimeUnit.SECONDS);
CompletableFuture<Void> test4 = new CompletableFuture<>();
NetClient client = vertx.createNetClient();
client.connect(1234, "localhost", ar -> {
if (ar.succeeded()) {
test4.complete(null);
} else {
test4.completeExceptionally(ar.cause());
}
});
test4.get(10, TimeUnit.SECONDS);
} finally {
server.close();
}
}
use of io.vertx.core.impl.VertxInternal in project vert.x by eclipse.
the class HostnameResolutionTest method testAsyncResolveConnectIsNotifiedOnChannelEventLoop.
@Test
public void testAsyncResolveConnectIsNotifiedOnChannelEventLoop() throws Exception {
CountDownLatch listenLatch = new CountDownLatch(1);
NetServer server = vertx.createNetServer().connectHandler(so -> {
});
try {
server.listen(1234, "localhost", onSuccess(v -> listenLatch.countDown()));
awaitLatch(listenLatch);
AtomicReference<Thread> channelThread = new AtomicReference<>();
CountDownLatch connectLatch = new CountDownLatch(1);
Bootstrap bootstrap = new Bootstrap();
bootstrap.channelFactory(((VertxInternal) vertx).transport().channelFactory(false));
bootstrap.group(vertx.nettyEventLoopGroup());
bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup());
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
channelThread.set(Thread.currentThread());
connectLatch.countDown();
}
});
ChannelFuture channelFut = bootstrap.connect("localhost", 1234);
awaitLatch(connectLatch);
channelFut.addListener(v -> {
assertTrue(v.isSuccess());
assertEquals(channelThread.get(), Thread.currentThread());
testComplete();
});
await();
} finally {
server.close();
}
}
Aggregations