@Test public void cacheResultForShouldCacheErrorForSetTime() throws InterruptedException { final HealthCheckResult expectedFirstResult = failure("first result"); final HealthCheckResult expectedSecondResult = expectedFirstResult; final HealthCheckResult expectedThirdResult = OK; final int okCache = 1; final int errorCache = 2; final HealthCheckProvider mockProvider = mockToReturn(expectedFirstResult, expectedThirdResult); final HealthCheckProvider testedProvider = mockProvider.map( cacheResultFor(okCache, errorCache, TimeUnit.SECONDS)); assertEquals("first result does not match", expectedFirstResult, testedProvider.get()); assertEquals("second result does not come from the cache", expectedSecondResult, testedProvider.get()); Thread.sleep(errorCache * 1000L + 500L); assertEquals("third result does come from the cache", expectedThirdResult, testedProvider.get()); }
public static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor( int resultValidFor, int errorResultValidFor, TimeUnit timeUnit) { return (healthCheckProvider) -> new HealthCheckProvider() { private Supplier<HealthCheckResult> resultSupplier = Suppliers .memoizeWithExpiration(healthCheckProvider::get, resultValidFor, timeUnit); HealthCheckResult previousResult = null; @Override public HealthCheckResult get() { HealthCheckResult result = resultSupplier.get(); if (Optional.ofNullable(previousResult).orElse(OK).isOk() != result.isOk() && resultValidFor != errorResultValidFor) { resetCacheAndPut(result); } previousResult = result; return result; } private void resetCacheAndPut(HealthCheckResult result) { int validityTime = (result.isOk()) ? resultValidFor : errorResultValidFor; resultSupplier = Suppliers .memoizeWithExpiration(cacheResultOnce(healthCheckProvider, result)::get, validityTime, timeUnit); } }; }
HealthChecks { public static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor( int resultValidFor, int errorResultValidFor, TimeUnit timeUnit) { return (healthCheckProvider) -> new HealthCheckProvider() { private Supplier<HealthCheckResult> resultSupplier = Suppliers .memoizeWithExpiration(healthCheckProvider::get, resultValidFor, timeUnit); HealthCheckResult previousResult = null; @Override public HealthCheckResult get() { HealthCheckResult result = resultSupplier.get(); if (Optional.ofNullable(previousResult).orElse(OK).isOk() != result.isOk() && resultValidFor != errorResultValidFor) { resetCacheAndPut(result); } previousResult = result; return result; } private void resetCacheAndPut(HealthCheckResult result) { int validityTime = (result.isOk()) ? resultValidFor : errorResultValidFor; resultSupplier = Suppliers .memoizeWithExpiration(cacheResultOnce(healthCheckProvider, result)::get, validityTime, timeUnit); } }; } }
HealthChecks { public static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor( int resultValidFor, int errorResultValidFor, TimeUnit timeUnit) { return (healthCheckProvider) -> new HealthCheckProvider() { private Supplier<HealthCheckResult> resultSupplier = Suppliers .memoizeWithExpiration(healthCheckProvider::get, resultValidFor, timeUnit); HealthCheckResult previousResult = null; @Override public HealthCheckResult get() { HealthCheckResult result = resultSupplier.get(); if (Optional.ofNullable(previousResult).orElse(OK).isOk() != result.isOk() && resultValidFor != errorResultValidFor) { resetCacheAndPut(result); } previousResult = result; return result; } private void resetCacheAndPut(HealthCheckResult result) { int validityTime = (result.isOk()) ? resultValidFor : errorResultValidFor; resultSupplier = Suppliers .memoizeWithExpiration(cacheResultOnce(healthCheckProvider, result)::get, validityTime, timeUnit); } }; } private  HealthChecks(); }
HealthChecks { public static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor( int resultValidFor, int errorResultValidFor, TimeUnit timeUnit) { return (healthCheckProvider) -> new HealthCheckProvider() { private Supplier<HealthCheckResult> resultSupplier = Suppliers .memoizeWithExpiration(healthCheckProvider::get, resultValidFor, timeUnit); HealthCheckResult previousResult = null; @Override public HealthCheckResult get() { HealthCheckResult result = resultSupplier.get(); if (Optional.ofNullable(previousResult).orElse(OK).isOk() != result.isOk() && resultValidFor != errorResultValidFor) { resetCacheAndPut(result); } previousResult = result; return result; } private void resetCacheAndPut(HealthCheckResult result) { int validityTime = (result.isOk()) ? resultValidFor : errorResultValidFor; resultSupplier = Suppliers .memoizeWithExpiration(cacheResultOnce(healthCheckProvider, result)::get, validityTime, timeUnit); } }; } private  HealthChecks(); static HealthCheckProvider checkAuthKeyOcspStatus(); static HealthCheckProvider checkServerConfDatabaseStatus(); static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor(
            int resultValidFor, int errorResultValidFor, TimeUnit timeUnit); static HealthCheckProvider cacheResultOnce(HealthCheckProvider provider,
                                                      HealthCheckResult cachedOnceResult); }
HealthChecks { public static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor( int resultValidFor, int errorResultValidFor, TimeUnit timeUnit) { return (healthCheckProvider) -> new HealthCheckProvider() { private Supplier<HealthCheckResult> resultSupplier = Suppliers .memoizeWithExpiration(healthCheckProvider::get, resultValidFor, timeUnit); HealthCheckResult previousResult = null; @Override public HealthCheckResult get() { HealthCheckResult result = resultSupplier.get(); if (Optional.ofNullable(previousResult).orElse(OK).isOk() != result.isOk() && resultValidFor != errorResultValidFor) { resetCacheAndPut(result); } previousResult = result; return result; } private void resetCacheAndPut(HealthCheckResult result) { int validityTime = (result.isOk()) ? resultValidFor : errorResultValidFor; resultSupplier = Suppliers .memoizeWithExpiration(cacheResultOnce(healthCheckProvider, result)::get, validityTime, timeUnit); } }; } private  HealthChecks(); static HealthCheckProvider checkAuthKeyOcspStatus(); static HealthCheckProvider checkServerConfDatabaseStatus(); static Function<HealthCheckProvider, HealthCheckProvider> cacheResultFor(
            int resultValidFor, int errorResultValidFor, TimeUnit timeUnit); static HealthCheckProvider cacheResultOnce(HealthCheckProvider provider,
                                                      HealthCheckResult cachedOnceResult);  }