@RequestMapping(value = "/healthy-nodes/", summary = "List of healthy nodes", description = "List of nodes that are healthy.", returnDescription = "List of healthy nodes") public void findAllHealthyNodes(final Callback<List<String>> callback) { healthService.findHealthyNodes(callback::accept); healthService.clientProxyFlush(); }
@Test public void testCheckInOk() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final AtomicBoolean result = new AtomicBoolean(); healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkInOk("foo"); healthService.findHealthyNodes(names -> { result.set(names.stream().anyMatch(s -> s.equals("foo"))); countDownLatch.countDown(); } ); countDownLatch.await(10, TimeUnit.SECONDS); assertTrue("foo is in healthy list", result.get()); }
@Test public void transitionFromPassToFail() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final AtomicBoolean result = new AtomicBoolean(); healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); healthService.findHealthyNodes(names -> { result.set(names.stream().anyMatch(s -> s.equals("foo"))); countDownLatch.countDown(); } ); countDownLatch.await(10, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.FAIL); final CountDownLatch countDownLatch2 = new CountDownLatch(1); result.set(false); healthService.findHealthyNodes(names -> { result.set(!names.stream().anyMatch(s -> s.equals("foo"))); countDownLatch2.countDown(); } ); countDownLatch2.await(10, TimeUnit.SECONDS); assertTrue("foo is NOT found among the healthy ", result.get()); }
@Test public void testCheckInOkUsingCheckIn() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final AtomicBoolean result = new AtomicBoolean(); healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); healthService.findHealthyNodes(names -> { result.set(names.stream().anyMatch(s -> s.equals("foo"))); countDownLatch.countDown(); } ); ServiceProxyUtils.flushServiceProxy(healthService); countDownLatch.await(10, TimeUnit.SECONDS); assertTrue("foo is found among the healthy ", result.get()); }
@Test public void forceTTLExpire() throws Exception { final CountDownLatch countDownLatch = new CountDownLatch(1); final AtomicBoolean result = new AtomicBoolean(); healthService.register("foo", 1, TimeUnit.SECONDS); healthService.checkIn("foo", HealthStatus.PASS); healthService.findAllNodes(names -> { result.set(names.stream().anyMatch(s -> s.equals("foo"))); countDownLatch.countDown(); } ); countDownLatch.await(10, TimeUnit.SECONDS); assertTrue("foo is found", result.get()); timer.setTime(); timer.minutes(1); Sys.sleep(1000); healthService.clientProxyFlush(); final CountDownLatch countDownLatch2 = new CountDownLatch(1); result.set(false); healthService.findHealthyNodes(names -> { result.set(!names.stream().anyMatch(s -> s.equals("foo"))); countDownLatch2.countDown(); } ); countDownLatch2.await(10, TimeUnit.SECONDS); assertTrue("foo should not be found", result.get()); }