void getCheckInfo(TestContext tc, String id, Handler<Check> resultHandler) { ctx.writeClient().localChecks(tc.asyncAssertSuccess(list -> { resultHandler.handle(list.stream() .filter(check -> check.getId().equals(id)) .findFirst() .orElseThrow(NoSuchElementException::new)); })); }
Check getCheckInfo(String id) { List<Check> checks = getAsync(h -> ctx.writeClient().localChecks(h)); return checks.stream() .filter(check -> check.getId().equals(id)) .findFirst() .get(); }
@Test public void writeClientHaveFullAccessToValues(TestContext tc) { valuesAccess(tc, ctx.writeClient()); }
@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()); }
@Test public void unknownSession(TestContext tc) { ctx.writeClient().infoSession("00000000-0000-0000-0000-000000000000", tc.asyncAssertFailure()); }
@Override void createCheck(TestContext tc, CheckOptions opts, Handler<String> idHandler) { ServiceOptions options = randomServiceOptions().setCheckOptions(opts); ctx.writeClient().registerService(options, tc.asyncAssertSuccess(v -> idHandler.handle("service:" + options.getId()))); } }
@Test public void createDefaultSession(TestContext tc) { ctx.writeClient().createSession(tc.asyncAssertSuccess(id -> { ctx.writeClient().infoSession(id, tc.asyncAssertSuccess(session -> { tc.assertEquals(id, session.getId()); tc.assertEquals(ctx.nodeName(), session.getNode()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); })); })); }
@Test public void listSessions(TestContext tc) { ctx.writeClient().createSession(tc.asyncAssertSuccess(id -> { ctx.writeClient().infoSession(id, tc.asyncAssertSuccess(session -> { ctx.writeClient().listSessions(tc.asyncAssertSuccess(list -> { tc.assertEquals(session.getId(), list.getList().get(0).getId()); ctx.writeClient().listNodeSessions(session.getNode(), tc.asyncAssertSuccess(nodeSesions -> { tc.assertEquals(session.getId(), nodeSesions.getList().get(0).getId()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); })); })); })); })); }
@Override String createCheck(CheckOptions opts) { String id = opts.getId(); if (id == null) { id = "checkId"; opts.setId(id); } runAsync(h -> ctx.writeClient().registerCheck(opts, h)); return id; }
@Override void createCheck(TestContext tc, CheckOptions opts, Handler<String> idHandler) { String id = opts.getId() == null ? randomAlphaString(10) : opts.getId(); opts.setId(id); ctx.writeClient().registerCheck(opts, tc.asyncAssertSuccess(v -> idHandler.handle(id))); }
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()); })); })); } }
@Test public void createSessionWithOptionsWithDefaultLockDelay(TestContext tc) { SessionOptions opt = new SessionOptions() .setBehavior(SessionBehavior.DELETE) .setName("optName") .setTtl(442); ctx.writeClient().createSessionWithOptions(opt, tc.asyncAssertSuccess(id -> { ctx.writeClient().infoSession(id, tc.asyncAssertSuccess(session -> { List<String> checks = session.getChecks(); tc.assertEquals(1, checks.size()); tc.assertTrue("serfHealth".equals(checks.get(0))); tc.assertEquals(15L, session.getLockDelay()); tc.assertEquals(ctx.nodeName(), session.getNode()); ctx.writeClient().destroySession(id, tc.asyncAssertSuccess()); })); })); }
@Test public void unknownNode(TestContext tc) { ctx.writeClient().createSessionWithOptions(new SessionOptions().setNode("unknownNode"), tc.asyncAssertFailure()); }
@Test public void canGetKeysList(TestContext tc) { String keyPrefix = randomFooBarAlpha(); String key = keyPrefix + randomAlphaString(10); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getKeys(keyPrefix, tc.asyncAssertSuccess(list -> { tc.assertTrue(list.contains(key)); ctx.writeClient().deleteValues(keyPrefix, tc.asyncAssertSuccess()); })); })); }
@Test public void createUpdateAndDestroy(TestContext tc) { String service1 = randomFooBarAlpha(); String service2 = randomFooBarAlpha(); ctx.writeClient() .createPreparedQuery(new PreparedQueryDefinition().setService(service1), tc.asyncAssertSuccess(id -> { ctx.readClient() .getPreparedQuery(id, tc.asyncAssertSuccess(def1 -> { tc.assertTrue(def1.getService().equals(service1)); ctx.writeClient().updatePreparedQuery(def1.setService(service2), tc.asyncAssertSuccess(updated -> { ctx.readClient().getPreparedQuery(id, tc.asyncAssertSuccess(def2 -> { tc.assertTrue(def2.getService().equals(service2)); ctx.writeClient().deletePreparedQuery(id, tc.asyncAssertSuccess()); })); })); })); })); }
@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; }
@Test public void readClientEmptyValue(TestContext tc) { String key = randomFooBarAlpha(); ctx.writeClient() .putValue(key, "", tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals("", pair.getValue()); ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess()); })); })); }
@Test public void readClientCanReadOneValue(TestContext tc) { String key = randomFooBarUnicode(); String value = randomUnicodeString(10); ctx.writeClient() .putValue(key, value, tc.asyncAssertSuccess(b -> { tc.assertTrue(b); ctx.readClient().getValue(key, tc.asyncAssertSuccess(pair -> { tc.assertEquals(key, pair.getKey()); tc.assertEquals(value, pair.getValue()); 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()); }); })); })); }
@Test public void tcpCheckLifecycle() { HttpHealthReporter reporter = new HttpHealthReporter(vertx); CheckOptions opts = new CheckOptions() .setTcp("localhost:" + reporter.port()) .setInterval("2s") .setName("checkName"); String checkId = createCheck(opts); sleep(vertx, 3000); Check check = getCheckInfo(checkId); assertEquals(CheckStatus.PASSING, check.getStatus()); reporter.close(); sleep(vertx, 3000); check = getCheckInfo(checkId); assertEquals(CheckStatus.CRITICAL, check.getStatus()); runAsync(h -> ctx.writeClient().deregisterCheck(checkId, h)); }