@Override public void start() throws Exception { vertx.createNetServer().connectHandler(sock -> { // Create a pump Pump.pump(sock, sock).start(); }).listen(1234); System.out.println("Echo server is now listening"); } }
@Test public void testDummyNetServerMetrics() { NetServer server = vertx.createNetServer(new NetServerOptions()); assertFalse(server.isMetricsEnabled()); }
@Test public void testDomainSocketServer() throws Exception { File sock = TestUtils.tmpFile(".sock"); vertx = Vertx.vertx(); NetServer server = vertx.createNetServer(); server.connectHandler(so -> {}); server.listen(SocketAddress.domainSocketAddress(sock.getAbsolutePath()), onFailure(err -> { assertEquals(err.getClass(), IllegalArgumentException.class); testComplete(); })); await(); }
@Override public void start() throws Exception { NetServerOptions options = new NetServerOptions() .setSsl(true).setKeyStoreOptions(new JksOptions().setPath("server-keystore.jks").setPassword("wibble")); vertx.createNetServer(options).connectHandler(sock -> { // Create a pump Pump.pump(sock, sock).start(); }).listen(1234); System.out.println("Echo server is now listening"); } }
@Test public void testClosingVertxCloseSharedServers() throws Exception { int numServers = 2; Vertx vertx = Vertx.vertx(getOptions()); List<NetServerImpl> servers = new ArrayList<>(); for (int i = 0;i < numServers;i++) { NetServer server = vertx.createNetServer().connectHandler(so -> { fail(); }); startServer(server); servers.add((NetServerImpl) server); } CountDownLatch latch = new CountDownLatch(1); vertx.close(onSuccess(v -> { latch.countDown(); })); awaitLatch(latch); servers.forEach(server -> { assertTrue(server.isClosed()); }); }
@Test public void testSharedServersRoundRobinButFirstStartAndStopServer() throws Exception { // Start and stop a server on the same port/host before hand to make sure it doesn't interact server.close(); CountDownLatch latch = new CountDownLatch(1); server = vertx.createNetServer(); server.connectHandler(sock -> { fail("Should not connect"); }).listen(testAddress, ar -> { if (ar.succeeded()) { latch.countDown(); } else { fail("Failed to bind server"); } }); awaitLatch(latch); CountDownLatch closeLatch = new CountDownLatch(1); server.close(ar -> { assertTrue(ar.succeeded()); closeLatch.countDown(); }); assertTrue(closeLatch.await(10, TimeUnit.SECONDS)); testSharedServersRoundRobin(); }
@Test public void testListenInvalidPort() { /* Port 80 is free to use by any application on Windows, so this test fails. */ Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows")); server.close(); server = vertx.createNetServer(new NetServerOptions().setPort(80)); server.connectHandler((netSocket) -> { }).listen(ar -> { assertTrue(ar.failed()); assertFalse(ar.succeeded()); assertNotNull(ar.cause()); testComplete(); }); await(); }
@Test public void testListenInvalidHost() { server.close(); server = vertx.createNetServer(new NetServerOptions().setPort(1234).setHost("uhqwduhqwudhqwuidhqwiudhqwudqwiuhd")); server.connectHandler(netSocket -> { }).listen(ar -> { assertTrue(ar.failed()); assertFalse(ar.succeeded()); assertNotNull(ar.cause()); testComplete(); }); await(); }
@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { CountDownLatch latch = new CountDownLatch(1); // Have a server running on a different port to make sure it doesn't interact server.close(); server = vertx.createNetServer(new NetServerOptions().setPort(4321)); server.connectHandler(sock -> { fail("Should not connect"); }).listen(ar2 -> { if (ar2.succeeded()) { latch.countDown(); } else { fail("Failed to bind server"); } }); awaitLatch(latch); testSharedServersRoundRobin(); }
public void setUp() throws Exception { super.setUp(); if (USE_DOMAIN_SOCKETS) { assertTrue("Native transport not enabled", USE_NATIVE_TRANSPORT); tmp = TestUtils.tmpFile(".sock"); testAddress = SocketAddress.domainSocketAddress(tmp.getAbsolutePath()); } else { testAddress = SocketAddress.inetSocketAddress(1234, "localhost"); } client = vertx.createNetClient(new NetClientOptions().setConnectTimeout(1000)); server = vertx.createNetServer(); }
@Test public void testListenOnWildcardPort() { server.close(); server = vertx.createNetServer(new NetServerOptions().setPort(0)); server.connectHandler((netSocket) -> { }).listen(ar -> { assertFalse(ar.failed()); assertTrue(ar.succeeded()); assertNull(ar.cause()); assertTrue(server.actualPort() > 1024); assertEquals(server, ar.result()); testComplete(); }); await(); }
@Test public void testMultipleServerClose() { this.server = vertx.createNetServer(); AtomicInteger times = new AtomicInteger(); // We assume the endHandler and the close completion handler are invoked in the same context task ThreadLocal stack = new ThreadLocal(); stack.set(true); server.connectStream().endHandler(v -> { assertNull(stack.get()); assertTrue(Vertx.currentContext().isEventLoopContext()); times.incrementAndGet(); }); server.close(ar1 -> { assertNull(stack.get()); assertTrue(Vertx.currentContext().isEventLoopContext()); server.close(ar2 -> { server.close(ar3 -> { assertEquals(1, times.get()); testComplete(); }); }); }); await(); }
@Test public void testServerIdleTimeout() { server.close(); NetServerOptions netServerOptions = new NetServerOptions(); netServerOptions.setIdleTimeout(1000); netServerOptions.setIdleTimeoutUnit(TimeUnit.MILLISECONDS); server = vertx.createNetServer(netServerOptions); server.connectHandler(s -> {}).listen(testAddress, ar -> { assertTrue(ar.succeeded()); client.connect(testAddress, res -> { assertTrue(res.succeeded()); NetSocket socket = res.result(); socket.closeHandler(v -> testComplete()); }); }); await(); }
@Test public void testServerLogging() throws Exception { server.close(); server = vertx.createNetServer(new NetServerOptions().setLogActivity(true)); TestLoggerFactory factory = testLogging(); assertTrue(factory.hasName("io.netty.handler.logging.LoggingHandler")); }
@Test public void testNetBindError() { RuntimeException cause = new RuntimeException(); vertx = VertxImpl.vertx(new VertxOptions(), new Transport() { @Override public ChannelFactory<? extends ServerChannel> serverChannelFactory(boolean domainSocket) { return (ChannelFactory<ServerChannel>) () -> { throw cause; }; } }); vertx.createNetServer() .connectHandler(so -> fail()) .listen(1234, "localhost", onFailure(err -> { testComplete(); })); await(); }
@Test public void testServerOptionsCopiedBeforeUse() { server.close(); NetServerOptions options = new NetServerOptions().setPort(1234); server = vertx.createNetServer(options); // Now change something - but server should still listen at previous port options.setPort(1235); server.connectHandler(sock -> { testComplete(); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.connect(1234, "localhost", ar2 -> { assertTrue(ar2.succeeded()); }); }); await(); }
private void testConnectErrorNotifiesOnEventLoop(NetClientOptions options) { RuntimeException cause = new RuntimeException(); vertx = VertxImpl.vertx(new VertxOptions(), new Transport() { @Override public ChannelFactory<? extends Channel> channelFactory(boolean domainSocket) { return (ChannelFactory<Channel>) () -> { throw cause; }; } }); vertx.createNetServer().connectHandler(so -> { fail(); }).listen(1234, "localhost", onSuccess(v -> { vertx.createNetClient(options).connect(1234, "localhost", onFailure(err -> { assertSame(err, cause); testComplete(); })); })); await(); }
@Test public void testNetServerInternalTLS() throws Exception { server.close(); server = vertx.createNetServer(new NetServerOptions() .setPort(1234) .setHost("localhost") .setSsl(true) .setKeyStoreOptions(Cert.SERVER_JKS.get())); testNetServerInternal_(new HttpClientOptions() .setSsl(true) .setTrustStoreOptions(Trust.SERVER_JKS.get()) , true); }
private void testNetServer(VertxOptions options) { vertx = Vertx.vertx(options); NetServer server = vertx.createNetServer(); server.connectHandler(so -> { so.handler(buff -> { assertEquals("ping", buff.toString()); so.write("pong"); }); so.closeHandler(v -> { testComplete(); }); }); server.listen(1234, onSuccess(v -> { NetClient client = vertx.createNetClient(); client.connect(1234, "localhost", onSuccess(so -> { so.write("ping"); so.handler(buff -> { assertEquals("pong", buff.toString()); so.close(); }); })); })); await(); }
@Test public void testCloseCompletionHandlerNotCalledWhenActualServerFailed() { server.close(); server = vertx.createNetServer( new NetServerOptions() .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("invalid"))) .connectHandler(c -> { }); try { server.listen(10000, r -> fail()); } catch (Exception ignore) { // Expected } server.close(onSuccess(v -> { testComplete(); })); await(); }