public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, BlockingQueryOptions obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "index": if (member.getValue() instanceof Number) { obj.setIndex(((Number)member.getValue()).longValue()); } break; case "wait": if (member.getValue() instanceof String) { obj.setWait((String)member.getValue()); } break; } } }
@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())))); } }
@Override void wait(long index, Handler<AsyncResult<State<KeyValueList>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.getValuesWithOptions(keyPrefix, options, h -> handler.handle(h.map(kv -> new State<KeyValueList>(kv, kv.getIndex())))); } }
@Override void wait(long index, Handler<AsyncResult<State<ServiceList>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.catalogServicesWithOptions(options, h -> handler.handle(h.map(services -> new State<ServiceList>(services, services.getIndex())))); } }
@Override void wait(long index, Handler<AsyncResult<State<ServiceEntryList>>> handler) { BlockingQueryOptions bOpts = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); ServiceQueryOptions sOpts = new ServiceQueryOptions().setBlockingOptions(bOpts); consulClient.healthServiceNodesWithOptions(service, false, sOpts, h -> handler.handle(h.map(services -> new State<ServiceEntryList>(services, services.getIndex())))); } }
@Override void wait(long index, Handler<AsyncResult<State<NodeList>>> handler) { BlockingQueryOptions bOpts = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); NodeQueryOptions qOpts = new NodeQueryOptions().setBlockingOptions(bOpts); consulClient.catalogNodesWithOptions(qOpts, h -> handler.handle(h.map(nodes -> new State<NodeList>(nodes, nodes.getIndex())))); } }
@Override void wait(long index, Handler<AsyncResult<State<EventList>>> handler) { BlockingQueryOptions bOpts = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); EventListOptions eOpts = new EventListOptions().setBlockingOptions(bOpts).setName(event); consulClient.listEventsWithOptions(eOpts, h -> handler.handle(h.map(events -> new State<EventList>(events, events.getIndex())))); } }
private void testServicesBlocking(Consumer<Handler<AsyncResult<ServiceList>>> runner, BiConsumer<BlockingQueryOptions, Handler<AsyncResult<ServiceList>>> request) throws InterruptedException { runAsync(h -> ctx.writeClient().registerService(new ServiceOptions().setName("service1").setId("id1"), h)); ServiceList list1 = getAsync(runner); list1.getList().forEach(s -> System.out.println("--- " + s.toJson().encode())); CountDownLatch latch = new CountDownLatch(1); waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { request.accept(new BlockingQueryOptions().setIndex(idx), h -> { h.result().getList().forEach(s -> System.out.println("-+- " + s.toJson().encode())); List<String> names = h.result().getList().stream().map(Service::getName).collect(Collectors.toList()); waitComplete(vertx, fut, h.result().getIndex(), names.contains("service2")); }); }); sleep(vertx, 4000); assertEquals(latch.getCount(), 1); runAsync(h -> ctx.writeClient().registerService(new ServiceOptions().setName("service2").setId("id2"), h)); awaitLatch(latch); runAsync(h -> ctx.writeClient().deregisterService("id1", h)); runAsync(h -> ctx.writeClient().deregisterService("id2", h)); }
.setBlockingOptions(new BlockingQueryOptions().setIndex(nodes1.getIndex())); ctx.readClient().catalogNodesWithOptions(blockingQueryOptions1, h -> { System.out.println(">>>>>>> new node event received"); ctx.readClient().catalogNodes(tc.asyncAssertSuccess(nodes2 -> { NodeQueryOptions blockingQueryOptions2 = new NodeQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(nodes2.getIndex())); System.out.println(">>>>>>> wait for new node detaching"); ctx.readClient().catalogNodesWithOptions(blockingQueryOptions2, tc.asyncAssertSuccess());
private void testSessionsBlocking(TestContext tc, BiConsumer<BlockingQueryOptions, Handler<AsyncResult<SessionList>>> request) { ctx.writeClient().createSession(tc.asyncAssertSuccess(id1 -> { ctx.readClient().listSessions(tc.asyncAssertSuccess(list1 -> { Async async = tc.async(); request.accept(new BlockingQueryOptions().setIndex(list1.getIndex()), h -> { List<String> ids = h.result().getList().stream().map(Session::getId).collect(Collectors.toList()); assertTrue(ids.contains(id1)); async.countDown(); }); vertx.setTimer(1000, l -> { assertEquals(async.count(), 1); ctx.writeClient().createSession(tc.asyncAssertSuccess(id2 -> { async.handler(a -> { ctx.writeClient().destroySession(id1, tc.asyncAssertSuccess(d1 -> { ctx.writeClient().destroySession(id2, tc.asyncAssertSuccess()); })); }); })); }); })); })); }
BlockingQueryOptions opts = new BlockingQueryOptions().setIndex(idx); ctx.readClient().coordinateNodesWithOptions(opts, h -> { boolean success = h.result().getList().size() == 2;
@Test public void healthState() throws InterruptedException { String serviceName = randomAlphaString(10); ServiceOptions opts = new ServiceOptions() .setName(serviceName) .setId(serviceName) .setCheckOptions(new CheckOptions().setTtl("1m")); runAsync(h -> ctx.writeClient().registerService(opts, h)); CheckList list1 = getAsync(h -> ctx.readClient().healthState(HealthState.CRITICAL, h)); CountDownLatch latch = new CountDownLatch(1); waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { CheckQueryOptions options = new CheckQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(idx)); ctx.readClient().healthStateWithOptions(HealthState.PASSING, options, h -> { List<String> names = h.result().getList().stream().map(Check::getServiceName).collect(Collectors.toList()); waitComplete(vertx, fut, h.result().getIndex(), names.contains(serviceName)); }); }); runAsync(h -> ctx.writeClient().passCheck("service:" + serviceName, h)); awaitLatch(latch); runAsync(h -> ctx.writeClient().deregisterService(serviceName, h)); }
waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { CheckQueryOptions options = new CheckQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(idx)); ctx.readClient().healthChecksWithOptions("serviceName", options, h -> { List<String> ids = h.result().getList().stream().map(Check::getId).collect(Collectors.toList());
waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { ServiceQueryOptions options = new ServiceQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(idx)); ctx.readClient().healthServiceNodesWithOptions("service", true, options, h -> { waitComplete(vertx, fut, h.result().getIndex(), h.result().getList().size() == 1);
Async async = tc.async(2); BlockingQueryOptions blockingQueryOptions = new BlockingQueryOptions() .setIndex(list1.getIndex()); if (timeout) { blockingQueryOptions.setWait("2s");
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 sessionInfoBlocking(TestContext tc) { ctx.writeClient().createSession(tc.asyncAssertSuccess(id -> { ctx.readClient().infoSession(id, tc.asyncAssertSuccess(s1 -> { Async async = tc.async(); ctx.readClient().infoSessionWithOptions(id, new BlockingQueryOptions().setIndex(s1.getIndex()), h -> async.countDown()); vertx.setTimer(1000, l -> { assertEquals(async.count(), 1); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); }); })); })); }