/** * Constructor from {@link URI}. * The datacenter and the acl token can be defined in the query; the scheme will be ignored. * * For example: * <p><code> * consul://consul.example.com/?dc=dc1&acl=00000000-0000-0000-0000-000000000000 * </code></p> * @param uri the URI */ public ConsulClientOptions(URI uri) { setHost(uri.getHost()); setPort(uri.getPort() < 0 ? CONSUL_DEFAULT_PORT : uri.getPort()); Map<String, List<String>> params = params(uri); setDc(getParam(params, "dc")); setAclToken(getParam(params, "acl", "aclToken")); }
/** * Constructor from JSON * * @param json the JSON */ public ConsulClientOptions(JsonObject json) { super(json); ConsulClientOptionsConverter.fromJson(json, this); if (json.getValue("host") instanceof String) { setHost((String)json.getValue("host")); } else { setHost(CONSUL_DEFAULT_HOST); } if (json.getValue("port") instanceof Number) { setPort(((Number)json.getValue("port")).intValue()); } else { setPort(CONSUL_DEFAULT_PORT); } }
private void createMasterClient(ConsulTestNode node) { AtomicReference<ConsulTestNode> usedNode = new AtomicReference<>(node); masterClient = ConsulClient.create(vertx, new ConsulClientOptions().setAclToken(MASTER_TOKEN).setDc(TEST_DC).setHost(LOCALHOST).setPort(usedNode.get().httpPort()).setSsl(false)); vertx.setPeriodic(5000, l -> { if ((usedNode.get() == null || !usedNode.get().isRunning()) && masterClient != null) { masterClient.close(); masterClient = null; } }); } }
.setPort(configuration.getInteger("port", 8500)) .setDc(configuration.getString("dc")) .setAclToken(configuration.getString("acl_token"));
.setPort(configuration.getInteger("port", 8500)) .setDc(configuration.getString("dc")) .setAclToken(configuration.getString("acl_token"));
@Override public Future<ConsulClientOptions> clientOptionsForNode(ConsulTestNode node, String rules) { Future<ConsulTestNode> usedNode = Future.future(); if (node == null || !node.isRunning()) { getServerNode().setHandler(usedNode); } else { usedNode.complete(node); } return usedNode.compose(n -> createToken(n, rules)).compose(token -> Future.succeededFuture(new ConsulClientOptions() .setAclToken(token) .setDc(TEST_DC) .setHost(LOCALHOST) .setPort(usedNode.result().httpPort()) .setSsl(false)) ); }
/** * Copy constructor * * @param options the one to copy */ public ConsulClientOptions(ConsulClientOptions options) { super(options); setHost(options.getHost()); setPort(options.getPort()); setAclToken(options.getAclToken()); setDc(options.getDc()); setTimeout(options.getTimeout()); }
@Before public void setUp(TestContext tc) throws Exception { vertx = Vertx.vertx(); vertx.exceptionHandler(tc.exceptionHandler()); client = ConsulClient.create(vertx, new ConsulClientOptions().setPort(consulProcess.getHttpPort())); }
@Before public void setUp(TestContext tc) throws Exception { vertx = Vertx.vertx(); vertx.exceptionHandler(tc.exceptionHandler()); client = ConsulClient.create(vertx, new ConsulClientOptions().setPort(consulProcess.getHttpPort())); }
@Test public void unknownPort(TestContext tc) { ConsulClient unknownPort = ctx.createClient(new ConsulClientOptions().setPort(Utils.getFreePort())); tryClient(tc, unknownPort, "Connection refused"); }
@Test public void closedConnection(TestContext tc) { BrokenHttpServer brokenConsul = new BrokenHttpServer(vertx); ConsulClient client = ctx.createClient(new ConsulClientOptions().setPort(brokenConsul.port())); client.agentInfo(tc.asyncAssertFailure(t -> { ctx.closeClient(client); brokenConsul.close(); tc.assertTrue(t.getMessage().contains("Connection was closed")); })); }
@Test public void timeout(TestContext tc) { SlowHttpServer slowConsul = new SlowHttpServer(vertx, 10000); ConsulClient client = ctx.createClient(new ConsulClientOptions().setPort(slowConsul.port()).setTimeout(1000)); client.agentInfo(tc.asyncAssertFailure(t -> { ctx.closeClient(client); slowConsul.close(); tc.assertTrue(t.getMessage().contains("The timeout period of 1000ms")); })); }
@Test public void toJson() { ConsulClientOptions options = new ConsulClientOptions() .setHost("host") .setPort(42) .setTimeout(33) .setUserAgent("ag") .setAclToken("tok") .setDc("d"); JsonObject json = options.toJson(); checkJson(options, json); }
@Test public void connectionRefused() throws InterruptedException { StateConsumer<Long> consumer = new StateConsumer<>(); String key = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); long t0 = System.currentTimeMillis(); Watch<KeyValue> watch = Watch.key(key, vertx, new ConsulClientOptions().setPort(Utils.getFreePort())) .setHandler(h -> { if (h.succeeded()) { fail(); } else { assertTrue(h.failed()); assertTrue(h.cause().getMessage().contains(CONNECTION_REFUSED)); consumer.consume(System.currentTimeMillis() - t0); } }) .start(); for (int i = 0; i < 5; i++) { consumer.awaitAny(); } consumer.check(); watch.stop(); List<Long> ticks = consumer.getConsumed(); List<Long> diff; diff = diff(ticks); // parabolic diff = diff(diff); // proportionality diff = diff(diff); // constant (~2000) long zero = diff(diff).get(0); // zero System.out.println("zero: " + zero); assertTrue(Math.abs(zero) < 1000); }
private static ConsulClientOptions config(String token, boolean secure) { return new ConsulClientOptions() .setAclToken(token) .setDc(ConsulCluster.dc().getName()) .setHost("localhost") .setPort(secure ? ConsulCluster.getDefaultAgent().getHttpsPort() : ConsulCluster.getDefaultAgent().getHttpPort()) .setSsl(secure); }
@Test public void copy() { ConsulClientOptions options = new ConsulClientOptions() .setHost("host") .setPort(42) .setTimeout(33) .setUserAgent("ag") .setAclToken("tok") .setDc("d"); ConsulClientOptions copy = new ConsulClientOptions(options); assertEquals(options.getHost(), copy.getHost()); assertEquals(options.getPort(), copy.getPort()); assertEquals(options.getTimeout(), copy.getTimeout()); assertEquals(options.getUserAgent(), copy.getUserAgent()); assertEquals(options.getAclToken(), copy.getAclToken()); assertEquals(options.getDc(), copy.getDc()); }