public static void toJson(ServiceList obj, java.util.Map<String, Object> json) { json.put("index", obj.getIndex()); if (obj.getList() != null) { JsonArray array = new JsonArray(); obj.getList().forEach(item -> array.add(item.toJson())); json.put("list", array); } } }
@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())))); } }
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)); }
@Test public void testServiceListCopy() { List<Service> services = new ArrayList<>(); services.add(randomService()); services.add(randomService()); ServiceList list = new ServiceList() .setIndex(randomPositiveInt()) .setList(services); checkServiceList(list, new ServiceList(list)); checkServiceList(list, new ServiceList(list.toJson())); List<Service> shuffledServices = new ArrayList<>(); shuffledServices.add(services.get(1)); shuffledServices.add(services.get(0)); ServiceList shuffled = new ServiceList() .setIndex(list.getIndex()) .setList(shuffledServices); checkServiceList(list, shuffled); }
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)); }