HealthCheckService implements TransientHttpService { public static HealthCheckService of(HealthChecker... healthCheckers) { return builder().checkers(healthCheckers).build(); } HealthCheckService(Iterable<HealthChecker> healthCheckers,
                       AggregatedHttpResponse healthyResponse, AggregatedHttpResponse unhealthyResponse,
                       long maxLongPollingTimeoutMillis, double longPollingTimeoutJitterRate,
                       long pingIntervalMillis, @Nullable HealthCheckUpdateHandler updateHandler); static HealthCheckService of(HealthChecker... healthCheckers); static HealthCheckService of(Iterable<? extends HealthChecker> healthCheckers); static HealthCheckServiceBuilder builder(); @Override void serviceAdded(ServiceConfig cfg); @Override HttpResponse serve(ServiceRequestContext ctx, HttpRequest req);  }
@Test void waitUntilUnhealthyWithImmediateWakeup() throws Exception { checker.setHealthy(false); final CompletableFuture<AggregatedHttpResponse> f = sendLongPollingGet("healthy"); assertThat(f.get()).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.SERVICE_UNAVAILABLE, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("{\"healthy\":false}"))); }
@Test void waitUntilHealthyWithImmediateWakeUp() throws Exception { final CompletableFuture<AggregatedHttpResponse> f = sendLongPollingGet("unhealthy"); assertThat(f.get()).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("{\"healthy\":true}"))); }
@Test void waitWithWrongMethod() throws Exception { final WebClient client = WebClient.of(server.httpUri()); final CompletableFuture<AggregatedHttpResponse> f = client.execute( RequestHeaders.of(HttpMethod.POST, "/hc_custom", HttpHeaderNames.PREFER, "wait=60", HttpHeaderNames.IF_NONE_MATCH, "\"healthy\"")).aggregate(); assertThat(f.get().status()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED); }
@Test void longPollingDisabled() throws Exception { final WebClient client = WebClient.of(server.httpUri()); final CompletableFuture<AggregatedHttpResponse> f = client.execute( RequestHeaders.of(HttpMethod.GET, "/hc_long_polling_disabled", HttpHeaderNames.PREFER, "wait=60", HttpHeaderNames.IF_NONE_MATCH, "\"healthy\"")).aggregate(); assertThat(f.get(10, TimeUnit.SECONDS)).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "0, 0"), HttpData.ofUtf8("{\"healthy\":true}"))); }
@Test void notUpdatableByDefault() throws Exception { final WebClient client = WebClient.of(server.httpUri()); final AggregatedHttpResponse res = client.execute(RequestHeaders.of(HttpMethod.POST, "/hc"), "{\"healthy\":false}").aggregate().join(); assertThat(res.status()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED); }
@Test void updateUsingPutOrPost() { final WebClient client = WebClient.of(server.httpUri()); final AggregatedHttpResponse res1 = client.execute(RequestHeaders.of(HttpMethod.PUT, "/hc_updatable"), "{\"healthy\":false}").aggregate().join(); assertThat(res1).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.SERVICE_UNAVAILABLE, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("{\"healthy\":false}"))); final AggregatedHttpResponse res2 = client.execute(RequestHeaders.of(HttpMethod.POST, "/hc_updatable"), "{\"healthy\":true}").aggregate().join(); assertThat(res2).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("{\"healthy\":true}"))); }
@Test void updateUsingPatch() { final WebClient client = WebClient.of(server.httpUri()); final AggregatedHttpResponse res1 = client.execute( RequestHeaders.of(HttpMethod.PATCH, "/hc_updatable"), "[{\"op\":\"replace\",\"path\":\"/healthy\",\"value\":false}]").aggregate().join(); assertThat(res1).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.SERVICE_UNAVAILABLE, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("{\"healthy\":false}"))); final AggregatedHttpResponse res2 = client.execute( RequestHeaders.of(HttpMethod.PATCH, "/hc_updatable"), "[{\"op\":\"replace\",\"path\":\"/healthy\",\"value\":true}]").aggregate().join(); assertThat(res2).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("{\"healthy\":true}"))); }
@Test void custom() { final WebClient client = WebClient.of(server.httpUri()); final AggregatedHttpResponse res1 = client.execute(RequestHeaders.of(HttpMethod.PUT, "/hc_custom"), "KO").aggregate().join(); assertThat(res1).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.SERVICE_UNAVAILABLE, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("not ok"))); final AggregatedHttpResponse res2 = client.execute(RequestHeaders.of(HttpMethod.PUT, "/hc_custom"), "OK").aggregate().join(); assertThat(res2).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("ok"))); final AggregatedHttpResponse res3 = client.execute(RequestHeaders.of(HttpMethod.PUT, "/hc_custom"), "NOOP").aggregate().join(); assertThat(res3).isEqualTo(AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.OK, HttpHeaderNames.CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8, "armeria-lphc", "60, 5"), HttpData.ofUtf8("ok"))); }
@Test void customError() { final WebClient client = WebClient.of(server.httpUri()); final AggregatedHttpResponse res1 = client.execute(RequestHeaders.of(HttpMethod.PATCH, "/hc_custom")) .aggregate().join(); assertThat(res1.status()).isEqualTo(HttpStatus.METHOD_NOT_ALLOWED); final AggregatedHttpResponse res2 = client.execute(RequestHeaders.of(HttpMethod.PUT, "/hc_custom"), "BAD").aggregate().join(); assertThat(res2.status()).isEqualTo(HttpStatus.BAD_REQUEST); }