case "addressResolverOptions": if (member.getValue() instanceof JsonObject) { obj.setAddressResolverOptions(new io.vertx.core.dns.AddressResolverOptions((JsonObject)member.getValue()));
@Test public void testResolveFromFile() { File f = new File(new File(new File(new File("src"), "test"), "resources"), "hosts_config.txt"); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath(f.getAbsolutePath()))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testResolveLocalhost() { AddressResolver resolver = new AddressResolver((VertxImpl) vertx, new AddressResolverOptions()); resolver.resolveHostname("LOCALHOST", res -> { if (res.succeeded()) { assertEquals("localhost", res.result().getHostName().toLowerCase(Locale.ENGLISH)); resolver.resolveHostname("LocalHost", res2 -> { if (res2.succeeded()) { assertEquals("localhost", res2.result().getHostName().toLowerCase(Locale.ENGLISH)); resolver.resolveHostname("localhost", res3 -> { if (res3.succeeded()) { assertEquals("localhost", res3.result().getHostName().toLowerCase(Locale.ENGLISH)); testComplete(); } else { fail(res3.cause()); } }); } else { fail(res2.cause()); } }); } else { fail(res.cause()); } }); await(); }
@Test public void testServerFailover() throws Exception { FakeDNSServer server = new FakeDNSServer().store(FakeDNSServer.A_store(Collections.singletonMap("vertx.io", "127.0.0.1"))).port(FakeDNSServer.PORT + 2); try { AddressResolverOptions options = new AddressResolverOptions(); options.setOptResourceEnabled(false); options.setMaxQueries(4); // 2 + 2 server.start(); InetSocketAddress dnsServerAddress = server.localAddress(); // First server is unreachable options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + (FakeDNSServer.PORT + 1)); // Second server is the failed over server options.addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()); AddressResolver resolver = new AddressResolver((VertxImpl) vertx, options); CompletableFuture<InetAddress> result = new CompletableFuture<>(); resolver.resolveHostname("vertx.io", ar -> { if (ar.succeeded()) { result.complete(ar.result()); } else { result.completeExceptionally(ar.cause()); } }); String resolved = result.get(10, TimeUnit.SECONDS).getHostAddress(); assertEquals("127.0.0.1", resolved); } finally { server.stop(); } } }
dnsServers.add(server); AddressResolverOptions options = new AddressResolverOptions(); options.setRotateServers(rotateServers); options.setOptResourceEnabled(false);
new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false)
@Test public void testNetSearchDomain() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1.foo.com", "127.0.0.1"); dnsServer.testResolveA(records); vertx.close(); vertx = vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). setHostsValue(Buffer.buffer()). setNdots(1). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com") )); testNet("host1"); }
@Test public void testSearchDomainWithNdots2() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1.sub.foo.com", "127.0.0.1"); records.put("host2.sub.foo.com", "127.0.0.2"); records.put("host2.sub", "127.0.0.3"); dnsServer.testResolveA(records); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com"). setNdots(2) )); CountDownLatch latch1 = new CountDownLatch(1); vertx.resolveAddress("host1.sub", onSuccess(resolved -> { assertEquals("127.0.0.1", resolved.getHostAddress()); latch1.countDown(); })); awaitLatch(latch1); // "host2.sub" is resolved with the foo.com search domain as ndots = 2 CountDownLatch latch2 = new CountDownLatch(1); vertx.resolveAddress("host2.sub", onSuccess(resolved -> { assertEquals("127.0.0.2", resolved.getHostAddress()); latch2.countDown(); })); awaitLatch(latch2); }
@Test public void testResolveFromBuffer() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsValue(Buffer.buffer("192.168.0.15 server.net")))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testSearchDomainWithNdots0() throws Exception { Map<String, String> records = new HashMap<>(); records.put("host1", "127.0.0.2"); records.put("host1.foo.com", "127.0.0.3"); dnsServer.testResolveA(records); VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions( new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false). addSearchDomain("foo.com"). setNdots(0) )); // "host1" resolves directly as ndots = 0 CountDownLatch latch1 = new CountDownLatch(1); vertx.resolveAddress("host1", onSuccess(resolved -> { assertEquals("127.0.0.2", resolved.getHostAddress()); latch1.countDown(); })); awaitLatch(latch1); // "host1.foo.com" resolves to host1.foo.com CountDownLatch latch2 = new CountDownLatch(1); vertx.resolveAddress("host1.foo.com", onSuccess(resolved -> { assertEquals("127.0.0.3", resolved.getHostAddress()); latch2.countDown(); })); awaitLatch(latch2); }
@Test public void testClientSynchronousConnectFailures() { System.setProperty("vertx.disableDnsResolver", "true"); Vertx vertx = Vertx.vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setQueryTimeout(100))); try { int poolSize = 2; HttpClient client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(poolSize)); AtomicInteger failures = new AtomicInteger(); vertx.runOnContext(v -> { for (int i = 0; i < (poolSize + 1); i++) { AtomicBoolean f = new AtomicBoolean(); client.getAbs("http://invalid-host-name.foo.bar", onFailure(resp -> { if (f.compareAndSet(false, true)) { if (failures.incrementAndGet() == poolSize + 1) { testComplete(); } } })).end(); } }); await(); } finally { vertx.close(); System.setProperty("vertx.disableDnsResolver", "false"); } }
@Test public void testInvalidHostsConfig() { try { AddressResolverOptions options = new AddressResolverOptions().setHostsPath("whatever.txt"); vertx(new VertxOptions().setAddressResolverOptions(options)); fail(); } catch (VertxException ignore) { } }
@Test public void testCaseInsensitiveResolveFromHosts() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt"))); vertx.resolveAddress("SERVER.NET", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
@Test public void testResolveFromClasspath() { VertxInternal vertx = (VertxInternal) vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setHostsPath("hosts_config.txt"))); vertx.resolveAddress("server.net", onSuccess(addr -> { assertEquals("192.168.0.15", addr.getHostAddress()); assertEquals("server.net", addr.getHostName()); testComplete(); })); await(); }
new AddressResolverOptions(). setHostsValue(Buffer.buffer()). setNdots(1).
new AddressResolverOptions(). addServer(dnsServerAddress.getAddress().getHostAddress() + ":" + dnsServerAddress.getPort()). setOptResourceEnabled(false).
/** * Copy constructor * * @param other The other {@code VertxOptions} to copy when creating this */ public VertxOptions(VertxOptions other) { this.eventLoopPoolSize = other.getEventLoopPoolSize(); this.workerPoolSize = other.getWorkerPoolSize(); this.blockedThreadCheckInterval = other.getBlockedThreadCheckInterval(); this.maxEventLoopExecuteTime = other.getMaxEventLoopExecuteTime(); this.maxWorkerExecuteTime = other.getMaxWorkerExecuteTime(); this.internalBlockingPoolSize = other.getInternalBlockingPoolSize(); this.clusterManager = other.getClusterManager(); this.haEnabled = other.isHAEnabled(); this.quorumSize = other.getQuorumSize(); this.haGroup = other.getHAGroup(); this.metricsOptions = other.getMetricsOptions() != null ? new MetricsOptions(other.getMetricsOptions()) : null; this.fileSystemOptions = other.getFileSystemOptions() != null ? new FileSystemOptions(other.getFileSystemOptions()) : null; this.warningExceptionTime = other.warningExceptionTime; this.eventBusOptions = new EventBusOptions(other.eventBusOptions); this.addressResolverOptions = other.addressResolverOptions != null ? new AddressResolverOptions() : null; this.maxEventLoopExecuteTimeUnit = other.maxEventLoopExecuteTimeUnit; this.maxWorkerExecuteTimeUnit = other.maxWorkerExecuteTimeUnit; this.warningExceptionTimeUnit = other.warningExceptionTimeUnit; this.blockedThreadCheckIntervalUnit = other.blockedThreadCheckIntervalUnit; }
@Test public void testDefaultJsonOptions() { AddressResolverOptions options = new AddressResolverOptions(new JsonObject()); assertEquals(AddressResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED, options.isOptResourceEnabled()); assertEquals(AddressResolverOptions.DEFAULT_SERVERS, options.getServers()); assertEquals(AddressResolverOptions.DEFAULT_CACHE_MIN_TIME_TO_LIVE, options.getCacheMinTimeToLive()); assertEquals(AddressResolverOptions.DEFAULT_CACHE_MAX_TIME_TO_LIVE, options.getCacheMaxTimeToLive()); assertEquals(AddressResolverOptions.DEFAULT_CACHE_NEGATIVE_TIME_TO_LIVE, options.getCacheNegativeTimeToLive()); assertEquals(AddressResolverOptions.DEFAULT_QUERY_TIMEOUT, options.getQueryTimeout()); assertEquals(AddressResolverOptions.DEFAULT_MAX_QUERIES, options.getMaxQueries()); assertEquals(AddressResolverOptions.DEFAULT_RD_FLAG, options.getRdFlag()); assertEquals(AddressResolverOptions.DEFAULT_SEACH_DOMAINS, options.getSearchDomains()); assertEquals(AddressResolverOptions.DEFAULT_NDOTS, options.getNdots()); }
@Test public void testOptions() { AddressResolverOptions options = new AddressResolverOptions(); assertEquals(AddressResolverOptions.DEFAULT_OPT_RESOURCE_ENABLED, options.isOptResourceEnabled()); assertEquals(AddressResolverOptions.DEFAULT_SERVERS, options.getServers()); AddressResolverOptions copy = new AddressResolverOptions(options); AddressResolverOptions jsonCopy = new AddressResolverOptions(options.toJson());
@Test public void testInvalidHostsConfig() { try { AddressResolverOptions options = new AddressResolverOptions().setHostsPath("whatever.txt"); vertx(new VertxOptions().setAddressResolverOptions(options)); fail(); } catch (VertxException ignore) { } }