private void retrieveIndividualServices(ServiceList list, Future<List<ImportedConsulService>> completed) { List<Future> futures = new ArrayList<>(); list.getList().forEach(service -> {
private void retrieveIndividualServices(ServiceList list, Future<List<ImportedConsulService>> completed) { List<Future> futures = new ArrayList<>(); list.getList().forEach(service -> {
@Override public void getRecords(Handler<AsyncResult<List<Record>>> resultHandler) { Future<ServiceList> nameList = Future.future(); client.catalogServices(nameList); nameList.map(ServiceList::getList) .map(l -> { List<Future> recordFutureList = new ArrayList<>(); l.forEach(s -> { if (!"consul".equals(s.getName())) { ServiceQueryOptions opt = new ServiceQueryOptions(); if (!s.getTags().isEmpty()) { opt.setTag(s.getTags().get(0)); } Future<ServiceList> serviceList = Future.future(); client.catalogServiceNodesWithOptions(s.getName(), opt, serviceList); recordFutureList.add(serviceList); } }); return recordFutureList; }) .compose(CompositeFuture::all) .map(c -> c.<ServiceList>list().stream().flatMap(l -> l.getList().stream()).map(this::serviceToRecord).collect(Collectors.toList())) .compose(CompositeFuture::all) .map(c -> c.list().stream().map(o -> (Record) o).collect(Collectors.toList())) .setHandler(resultHandler); }
@Test public void findConsul() { ServiceList localConsulList = getAsync(h -> ctx.writeClient().catalogServiceNodes("consul", h)); assertEquals(localConsulList.getList().size(), 1); List<Service> catalogConsulList = Utils.<ServiceList>getAsync(h -> ctx.writeClient().catalogServices(h)) .getList().stream().filter(s -> s.getName().equals("consul")).collect(Collectors.toList()); assertEquals(1, catalogConsulList.size()); assertEquals(0, catalogConsulList.get(0).getTags().size()); }
private void checkServiceList(ServiceList expected, ServiceList actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getIndex(), actual.getIndex()); List<Service> expectedList = expected.getList(); List<Service> actualList = actual.getList(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
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)); }
tc.assertEquals(c.getNotes(), opts.getCheckOptions().getNotes()); ctx.writeClient().catalogNodeServices(ctx.nodeName(), tc.asyncAssertSuccess(nodeServices -> { tc.assertEquals(2, nodeServices.getList().size()); checkService(tc, nodeServices.getList(), serviceName, opts); Async async = tc.async(2); ServiceQueryOptions knownOpts = new ServiceQueryOptions().setTag(opts.getTags().get(0)); ctx.writeClient().catalogServiceNodesWithOptions(serviceName, knownOpts, tc.asyncAssertSuccess(nodeServicesWithKnownTag -> { tc.assertEquals(1, nodeServicesWithKnownTag.getList().size()); async.countDown(); })); ServiceQueryOptions unknownOpts = new ServiceQueryOptions().setTag("unknownTag"); ctx.writeClient().catalogServiceNodesWithOptions(serviceName, unknownOpts, tc.asyncAssertSuccess(nodeServicesWithUnknownTag -> { tc.assertEquals(0, nodeServicesWithUnknownTag.getList().size()); async.countDown(); }));
@Test public void watchServices() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); ServiceOptions service = new ServiceOptions() .setId(randomAlphaString(10)) .setName(randomAlphaString(10)); Watch<ServiceList> watch = Watch.services(vertx, ctx.readClientOptions()) .setHandler(list -> { if (list.succeeded()) { consumer.consume(list.nextResult().getList() .stream().map(Service::getName).filter(s -> s.equals(service.getName())) .findFirst().orElse("")); } }) .start(); consumer.await(""); runAsync(h -> ctx.writeClient().registerService(service, h)); consumer.await(service.getName()); consumer.check(); watch.stop(); runAsync(h -> ctx.writeClient().deregisterService(service.getId(), h)); }