@Override public void start() throws Exception { ConsulClient consulClient = ConsulClient.create(vertx); consulClient.putValue("key11", "value11", putResult -> { if (putResult.succeeded()) { System.out.println("KV pair saved"); consulClient.getValue("key11", getResult -> { if (getResult.succeeded()) { System.out.println("KV pair retrieved"); System.out.println(getResult.result().getValue()); } else { getResult.cause().printStackTrace(); } }); } else { putResult.cause().printStackTrace(); } }); } }
@Test public void checkAndSet(TestContext tc) { String key = randomFooBarAlpha(); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair1 -> { ctx.writeClient().putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair2 -> { tc.assertTrue(pair1.getModifyIndex() < pair2.getModifyIndex()); ctx.writeClient().putValueWithOptions(key, randomAlphaString(10), new KeyValueOptions().setCasIndex(pair1.getModifyIndex()), tc.asyncAssertSuccess(b3 -> { tc.assertFalse(b3); ctx.writeClient().putValueWithOptions(key, randomAlphaString(10), new KeyValueOptions().setCasIndex(pair2.getModifyIndex()), tc.asyncAssertSuccess(b4 -> { tc.assertTrue(b4); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); })); })); })); })); }
/** * Adds specified key/value pair * @param key the key * @param value the value * @param resultHandler will be provided with success of operation * @return reference to this, for fluency */ public io.vertx.rxjava.ext.consul.ConsulClient putValue(String key, String value, Handler<AsyncResult<Boolean>> resultHandler) { delegate.putValue(key, value, resultHandler); return this; }
/** * Adds specified key/value pair * @param key the key * @param value the value * @param resultHandler will be provided with success of operation * @return reference to this, for fluency */ public io.vertx.rxjava.ext.consul.ConsulClient putValue(String key, String value, Handler<AsyncResult<Boolean>> resultHandler) { delegate.putValue(key, value, resultHandler); return this; }
@Override public ConsulService putValue(String key, String value, Handler<AsyncResult<Boolean>> resultHandler) { consulClient.putValue(key, value, resultHandler); return this; }
@Test public void readClientCantWriteOneValue(TestContext tc) { ctx.readClient().putValue("foo/bar1", "value1", tc.asyncAssertFailure()); }
@Test public void getSimpleConfig(TestContext tc) { Async async = tc.async(); createRetriever(); client.putValue("foo/bar", "value", ar -> { tc.assertTrue(ar.succeeded()); retriever.getConfig(json2 -> { assertThat(json2.succeeded()).isTrue(); JsonObject config2 = json2.result(); tc.assertTrue(!config2.isEmpty()); tc.assertEquals(config2.getString("bar"), "value"); client.deleteValues("foo", h -> async.complete()); }); }); }
@Test public void getSimpleConfig(TestContext tc) { Async async = tc.async(); createRetriever(); client.putValue("foo/bar", "value", ar -> { tc.assertTrue(ar.succeeded()); retriever.getConfig(json2 -> { assertThat(json2.succeeded()).isTrue(); JsonObject config2 = json2.result(); tc.assertTrue(!config2.isEmpty()); tc.assertEquals(config2.getString("bar"), "value"); client.deleteValues("foo", h -> async.complete()); }); }); }
private void createKV(TestContext tc, String key, String value, Handler<Long> resultHandler) { ctx.writeClient().putValue(key, value, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { resultHandler.handle(pair.getModifyIndex()); })); })); } }
private void valuesAccess(TestContext tc, ConsulClient accessClient) { String key1 = randomFooBarAlpha(); String value1 = randomAlphaString(10); String key2 = randomFooBarAlpha(); String value2 = randomAlphaString(10); ctx.writeClient().putValue(key1, value1, tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); ctx.writeClient().putValue(key2, value2, tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); accessClient.getValues("foo/bar", tc.asyncAssertSuccess(kvList -> { List<KeyValue> list = kvList.getList(); tc.assertEquals(list.size(), 2); tc.assertTrue(list.stream() .filter(kv -> kv.getKey().equals(key1) && kv.getValue().equals(value1)) .count() == 1); tc.assertTrue(list.stream() .filter(kv -> kv.getKey().equals(key2) && kv.getValue().equals(value2)) .count() == 1); ctx.writeClient().deleteValues("foo/bar", tc.asyncAssertSuccess()); })); })); })); }
@Test public void listenConfigChange(TestContext tc) { Async async = tc.async(); createRetriever(); client.putValue("foo/bar", "value", ar -> { tc.assertTrue(ar.succeeded()); retriever.getConfig(init -> { retriever.listen(change -> { JsonObject prev = change.getPreviousConfiguration(); tc.assertTrue(!prev.isEmpty()); tc.assertEquals(prev.getString("bar"), "value"); JsonObject next = change.getNewConfiguration(); tc.assertTrue(!next.isEmpty()); tc.assertEquals(next.getString("bar"), "new_value"); client.deleteValues("foo", h -> async.complete()); }); client.putValue("foo/bar", "new_value", ignore -> {}); }); }); }
@Test public void listenConfigChange(TestContext tc) { Async async = tc.async(); createRetriever(); client.putValue("foo/bar", "value", ar -> { tc.assertTrue(ar.succeeded()); retriever.getConfig(init -> { retriever.listen(change -> { JsonObject prev = change.getPreviousConfiguration(); tc.assertTrue(!prev.isEmpty()); tc.assertEquals(prev.getString("bar"), "value"); JsonObject next = change.getNewConfiguration(); tc.assertTrue(!next.isEmpty()); tc.assertEquals(next.getString("bar"), "new_value"); client.deleteValues("foo", h -> async.complete()); }); client.putValue("foo/bar", "new_value", ignore -> {}); }); }); }
private void go(TestContext tc, boolean trustAll, PemTrustOptions trustOptions) { ConsulClient secureClient = ctx.createSecureClient(trustAll, trustOptions); secureClient.putValue("foo/bars42", "value42", tc.asyncAssertSuccess(b -> { tc.assertTrue(b); secureClient.getValue("foo/bars42", tc.asyncAssertSuccess(pair -> { tc.assertEquals(pair.getValue(), "value42"); ctx.closeClient(secureClient); })); })); } }
@Test public void testKeyPrefix() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String keyPrefix = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); String k1 = keyPrefix + randomAlphaString(10); String k2 = keyPrefix + randomAlphaString(10); String v1 = randomAlphaString(10); String v2 = randomAlphaString(10); assertTrue(getAsync(h -> ctx.writeClient().putValue(k1, v1, h))); Watch<KeyValueList> watch = Watch.keyPrefix(keyPrefix, vertx, ctx.readClientOptions()) .setHandler(kv -> { if (kv.succeeded()) { if (kv.nextResult().isPresent()) { consumer.consume(kv.nextResult().getList().stream().map(KeyValue::getValue).sorted().collect(Collectors.joining("/"))); } else { consumer.consume(EMPTY_MESSAGE); } } else { consumer.consume(kv.cause().getMessage()); } }) .start(); consumer.await(v1); assertTrue(getAsync(h -> ctx.writeClient().putValue(k2, v2, h))); consumer.await(Stream.of(v1, v2).sorted().collect(Collectors.joining("/"))); runAsync(h -> ctx.writeClient().deleteValues(keyPrefix, h)); consumer.await(EMPTY_MESSAGE); consumer.check(); watch.stop(); }
@Test public void canGetKeysList(TestContext tc) { String keyPrefix = randomFooBarAlpha(); String key = keyPrefix + randomAlphaString(10); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getKeys(keyPrefix, tc.asyncAssertSuccess(list -> { tc.assertTrue(list.contains(key)); ctx.writeClient().deleteValues(keyPrefix, tc.asyncAssertSuccess()); })); })); }
private void blockingQuery(TestContext tc, BiConsumer<String, Handler<Long>> indexSupplier) { String key = randomFooBarAlpha(); String value = randomAlphaString(10); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); indexSupplier.accept(key, consulIndex -> { Async async = tc.async(2); vertx.setTimer(TimeUnit.SECONDS.toMillis(2), l -> { ctx.writeClient().putValue(key, value, tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); ctx.readClient().getValueWithOptions(key, new BlockingQueryOptions().setIndex(consulIndex), tc.asyncAssertSuccess(kv -> { tc.assertTrue(kv.getModifyIndex() > consulIndex); tc.assertEquals(kv.getValue(), value); async.countDown(); })); ctx.readClient().getValuesWithOptions("foo/bar", new BlockingQueryOptions().setIndex(consulIndex), tc.asyncAssertSuccess(kv -> { tc.assertTrue(kv.getIndex() > consulIndex); tc.assertTrue(kv.getList().size() == 1); async.countDown(); })); })); }); async.handler(v -> ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess())); }); })); } }
@Test public void readClientEmptyValue(TestContext tc) { String key = randomFooBarAlpha(); ctx.writeClient() .putValue(key, "", tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals("", pair.getValue()); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); }
@Test public void readClientCanReadOneValue(TestContext tc) { String key = randomFooBarUnicode(); String value = randomUnicodeString(10); ctx.writeClient() .putValue(key, value, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals(value, pair.getValue()); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); }
@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(); }
@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(); }