/** * Creates {@code Watch} to monitoring a specific key in the KV store. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * * @param key the key * @param vertx the {@code Vertx} instance * @return the {@code Watch} instance */ static Watch<KeyValue> key(String key, Vertx vertx) { return key(key, vertx, new ConsulClientOptions()); }
/** * Creates <code>Watch</code> to monitoring a specific key in the KV store. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * @param key the key * @param vertx the <code>Vertx</code> instance * @param options the options to create underlying Consul client * @return the <code>Watch</code> instance */ public static io.vertx.rxjava.ext.consul.Watch<KeyValue> key(String key, io.vertx.rxjava.core.Vertx vertx, ConsulClientOptions options) { io.vertx.rxjava.ext.consul.Watch<KeyValue> ret = io.vertx.rxjava.ext.consul.Watch.newInstance(io.vertx.ext.consul.Watch.key(key, vertx.getDelegate(), options), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * Creates <code>Watch</code> to monitoring a specific key in the KV store. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * @param key the key * @param vertx the <code>Vertx</code> instance * @param options the options to create underlying Consul client * @return the <code>Watch</code> instance */ public static io.vertx.rxjava.ext.consul.Watch<KeyValue> key(String key, io.vertx.rxjava.core.Vertx vertx, ConsulClientOptions options) { io.vertx.rxjava.ext.consul.Watch<KeyValue> ret = io.vertx.rxjava.ext.consul.Watch.newInstance(io.vertx.ext.consul.Watch.key(key, vertx.getDelegate(), options), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
/** * Creates <code>Watch</code> to monitoring a specific key in the KV store. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * @param key the key * @param vertx the <code>Vertx</code> instance * @return the <code>Watch</code> instance */ public static io.vertx.rxjava.ext.consul.Watch<KeyValue> key(String key, io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.consul.Watch<KeyValue> ret = io.vertx.rxjava.ext.consul.Watch.newInstance(io.vertx.ext.consul.Watch.key(key, vertx.getDelegate()), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
public static io.vertx.ext.consul.Watch<io.vertx.ext.consul.KeyValue> key(io.vertx.ext.consul.Watch<Object> j_receiver, java.lang.String key, io.vertx.core.Vertx vertx, java.util.Map<String, Object> options) { return io.vertx.core.impl.ConversionHelper.fromObject(io.vertx.ext.consul.Watch.key(key, vertx, options != null ? new io.vertx.ext.consul.ConsulClientOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(options)) : null)); } public static io.vertx.ext.consul.Watch<io.vertx.ext.consul.KeyValueList> keyPrefix(io.vertx.ext.consul.Watch<Object> j_receiver, java.lang.String keyPrefix, io.vertx.core.Vertx vertx, java.util.Map<String, Object> options) {
/** * Creates <code>Watch</code> to monitoring a specific key in the KV store. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * @param key the key * @param vertx the <code>Vertx</code> instance * @return the <code>Watch</code> instance */ public static io.vertx.rxjava.ext.consul.Watch<KeyValue> key(String key, io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.consul.Watch<KeyValue> ret = io.vertx.rxjava.ext.consul.Watch.newInstance(io.vertx.ext.consul.Watch.key(key, vertx.getDelegate()), io.vertx.lang.rx.TypeArg.unknown()); return ret; }
@Test(expected = IllegalStateException.class) public void throwStop() { Watch<KeyValue> watch = Watch.key(randomAlphaString(10), vertx); watch.stop(); }
@Test(expected = IllegalStateException.class) public void throwStartStopStop() { Watch<KeyValue> watch = Watch.key(randomAlphaString(10), vertx); watch.start().stop(); watch.stop(); }
@Test public void throwStartStart() { Watch<KeyValue> watch = Watch.key(randomAlphaString(10), vertx); try { watch.start().start(); } catch (IllegalStateException e) { watch.stop(); return; } fail(); }
@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); }
@Test public void watchExistingKey() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String key = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); String v1 = randomAlphaString(10); String v2 = randomAlphaString(10); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v1, h))); Watch<KeyValue> watch = Watch.key(key, vertx, ctx.readClientOptions()) .setHandler(kv -> { if (kv.succeeded()) { consumer.consume(kv.nextResult().isPresent() ? kv.nextResult().getValue() : EMPTY_MESSAGE); } else { consumer.consume(kv.cause().getMessage()); } }) .start(); consumer.await(v1); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v2, h))); consumer.await(v2); runAsync(h -> ctx.writeClient().deleteValue(key, h)); consumer.await(EMPTY_MESSAGE); consumer.check(); watch.stop(); }
@Test public void watchCreatedKey() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String key = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); String v1 = randomAlphaString(10); String v2 = randomAlphaString(10); Watch<KeyValue> watch = Watch.key(key, vertx, ctx.readClientOptions()) .setHandler(kv -> { if (kv.succeeded()) { consumer.consume(kv.nextResult().isPresent() ? kv.nextResult().getValue() : EMPTY_MESSAGE); } else { consumer.consume(kv.cause().getMessage()); } }) .start(); consumer.await(EMPTY_MESSAGE); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v1, h))); consumer.await(v1); assertTrue(getAsync(h -> ctx.writeClient().putValue(key, v2, h))); consumer.await(v2); runAsync(h -> ctx.writeClient().deleteValue(key, h)); consumer.await(EMPTY_MESSAGE); consumer.check(); watch.stop(); }