case "name": if (member.getValue() instanceof String) { obj.setName((String)member.getValue());
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)); }
@Override String createCheck(CheckOptions opts) { String serviceId = "serviceId"; ServiceOptions service = new ServiceOptions() .setName("serviceName") .setId(serviceId) .setTags(Arrays.asList("tag1", "tag2")) .setCheckOptions(opts) .setAddress("10.0.0.1") .setPort(8080); runAsync(h -> ctx.writeClient().registerService(service, h)); return "service:" + serviceId; }
private ServiceOptions recordToServiceOptions(Record record, String uuid) { ServiceOptions serviceOptions = new ServiceOptions(); serviceOptions.setName(record.getName()); JsonArray tags = new JsonArray(); if (record.getMetadata() != null) {
@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)); }
@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)); }
@Test public void bindCheckToService() { String serviceName = "serviceName"; ServiceOptions service = new ServiceOptions() .setName(serviceName) .setAddress("10.0.0.1") .setPort(8080); runAsync(h -> ctx.writeClient().registerService(service, h)); List<Service> services = getAsync(h -> ctx.writeClient().localServices(h)); Service s = services.stream().filter(i -> "serviceName".equals(i.getName())).findFirst().get(); String serviceId = s.getId(); assertEquals(s.getAddress(), "10.0.0.1"); assertEquals(s.getPort(), 8080); CheckOptions check = new CheckOptions() .setId("checkId") .setName("checkName") .setNotes("checkNotes") .setServiceId(serviceId) .setStatus(CheckStatus.PASSING) .setTtl("10s"); runAsync(h -> ctx.writeClient().registerCheck(check, h)); List<Check> checks = getAsync(h -> ctx.writeClient().localChecks(h)); Check c = checks.stream().filter(i -> "checkId".equals(i.getId())).findFirst().get(); assertEquals(c.getServiceId(), serviceId); assertEquals(c.getStatus(), CheckStatus.PASSING); assertEquals(c.getNotes(), "checkNotes"); runAsync(h -> ctx.writeClient().deregisterService(serviceId, h)); }
@Test public void healthServices() throws InterruptedException { runAsync(h -> ctx.writeClient().registerService(new ServiceOptions() .setName("service").setId("id1").setTags(Collections.singletonList("tag1")) .setCheckOptions(new CheckOptions().setTtl("5s").setStatus(CheckStatus.PASSING)), h)); runAsync(h -> ctx.writeClient().registerService(new ServiceOptions() .setName("service").setId("id2").setTags(Collections.singletonList("tag2")) .setCheckOptions(new CheckOptions().setTtl("5s").setStatus(CheckStatus.PASSING)), h));
@Test public void watchService() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); ServiceOptions service = new ServiceOptions() .setCheckOptions(new CheckOptions() .setStatus(CheckStatus.PASSING) .setTtl("4s") .setName(randomAlphaString(10))) .setId(randomAlphaString(10)) .setName(randomAlphaString(10)); Watch<ServiceEntryList> watch = Watch.service(service.getName(), vertx, ctx.readClientOptions()) .setHandler(list -> { if (list.succeeded()) { consumer.consume(list.nextResult().getList() .stream().filter(s -> s.getService().getName().equals(service.getName())) .map(e -> e.getService().getName() + "/" + e.getChecks().stream() .filter(c -> c.getId().equals("service:" + service.getId())) .map(c -> c.getStatus().name()).findFirst().orElse("")) .findFirst().orElse("")); } }) .start(); consumer.await(""); runAsync(h -> ctx.writeClient().registerService(service, h)); consumer.await(service.getName() + "/" + CheckStatus.PASSING.name()); consumer.await(service.getName() + "/" + CheckStatus.CRITICAL.name()); consumer.check(); watch.stop(); runAsync(h -> ctx.writeClient().deregisterService(service.getId(), h)); }
@Test public void healthChecks() throws InterruptedException { ServiceOptions opts = new ServiceOptions() .setName("serviceName") .setId("serviceId") .setTags(Collections.singletonList("tag1"));
@Test public void createLocalService(TestContext tc) { String serviceName = randomAlphaString(10); ServiceOptions opts = randomServiceOptions().setName(serviceName).setId(null); ctx.writeClient().registerService(opts, tc.asyncAssertSuccess(reg -> { ctx.writeClient().localServices(tc.asyncAssertSuccess(services -> {
public static ServiceOptions randomServiceOptions() { return new ServiceOptions() .setId(randomAlphaString(10)) .setName(randomAlphaString(10)) .setTags(randomStringList(2)) .setCheckOptions(randomCheckOptions()) .setAddress(randomAlphaString(10)) .setMeta(randomMeta()) .setPort(randomPortInt()); }
String serviceId = "serviceId"; ServiceOptions service = new ServiceOptions() .setName("serviceName") .setId(serviceId) .setAddress("10.0.0.1")
@Test public void execute(TestContext tc) { PreparedQueryDefinition def = new PreparedQueryDefinition() .setService("service-${match(1)}-${match(2)}") .setTemplateType("name_prefix_match") .setTemplateRegexp("^find_(.+?)_(.+?)$"); ctx.writeClient().createPreparedQuery(def, tc.asyncAssertSuccess(qid -> { ctx.writeClient().executePreparedQuery("find_1_2", tc.asyncAssertSuccess(resp1 -> { tc.assertEquals(resp1.getNodes().size(), 0); ctx.writeClient() .registerService(new ServiceOptions().setName("service-1-2"), tc.asyncAssertSuccess(v1 -> { ctx.writeClient() .executePreparedQuery("find_1_2", tc.asyncAssertSuccess(resp2 -> { tc.assertEquals(resp2.getNodes().size(), 1); ctx.writeClient() .deregisterService("service-1-2", tc.asyncAssertSuccess(v2 -> { ctx.writeClient() .deletePreparedQuery(qid, tc.asyncAssertSuccess()); })); })); })); })); })); } }