public static void toJson(KeyValue obj, java.util.Map<String, Object> json) { json.put("createIndex", obj.getCreateIndex()); json.put("flags", obj.getFlags()); if (obj.getKey() != null) { json.put("key", obj.getKey()); } json.put("lockIndex", obj.getLockIndex()); json.put("modifyIndex", obj.getModifyIndex()); if (obj.getSession() != null) { json.put("session", obj.getSession()); } if (obj.getValue() != null) { json.put("value", obj.getValue()); } } }
@Override void wait(long index, Handler<AsyncResult<State<KeyValue>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.getValueWithOptions(key, options, h -> handler.handle(h.map(kv -> new State<KeyValue>(kv, kv.getModifyIndex())))); } }
@Test public void canGetValueBlocking(TestContext tc) { blockingQuery(tc, (key, h) -> ctx.readClient().getValue(key, tc.asyncAssertSuccess(list -> h.handle(list.getModifyIndex())))); }
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 checkKeyValue(KeyValue expected, KeyValue actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getKey(), actual.getKey()); assertEquals(expected.getValue(), actual.getValue()); assertEquals(expected.getSession(), actual.getSession()); assertEquals(expected.getCreateIndex(), actual.getCreateIndex()); assertEquals(expected.getFlags(), actual.getFlags()); assertEquals(expected.getModifyIndex(), actual.getModifyIndex()); assertEquals(expected.getLockIndex(), actual.getLockIndex()); }
@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()); })); })); })); })); })); })); }
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())); }); })); } }