### Question:
CacheValidityPolicy { public TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry, final float coefficient, final TimeValue defaultLifetime) { final Date dateValue = entry.getDate(); final Date lastModifiedValue = DateUtils.parseDate(entry, HeaderConstants.LAST_MODIFIED); if (dateValue != null && lastModifiedValue != null) { final long diff = dateValue.getTime() - lastModifiedValue.getTime(); if (diff < 0) { return TimeValue.ZERO_MILLISECONDS; } return TimeValue.ofSeconds((long) (coefficient * diff / 1000)); } return defaultLifetime; } CacheValidityPolicy(); TimeValue getCurrentAge(final HttpCacheEntry entry, final Date now); TimeValue getFreshnessLifetime(final HttpCacheEntry entry); boolean isResponseFresh(final HttpCacheEntry entry, final Date now); boolean isResponseHeuristicallyFresh(final HttpCacheEntry entry,
            final Date now, final float coefficient, final TimeValue defaultLifetime); TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry entry,
            final float coefficient, final TimeValue defaultLifetime); boolean isRevalidatable(final HttpCacheEntry entry); boolean mustRevalidate(final HttpCacheEntry entry); boolean proxyRevalidate(final HttpCacheEntry entry); boolean mayReturnStaleWhileRevalidating(final HttpCacheEntry entry, final Date now); boolean mayReturnStaleIfError(final HttpRequest request, final HttpCacheEntry entry, final Date now); boolean hasCacheControlDirective(final HttpCacheEntry entry, final String directive); TimeValue getStaleness(final HttpCacheEntry entry, final Date now); static final TimeValue MAX_AGE; }### Answer:
@Test public void testHeuristicFreshnessLifetime() { final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(oneSecondAgo)), new BasicHeader("Last-Modified", DateUtils.formatDate(elevenSecondsAgo)) }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers); assertEquals(TimeValue.ofSeconds(1), impl.getHeuristicFreshnessLifetime(entry, 0.1f, TimeValue.ZERO_MILLISECONDS)); }
@Test public void testHeuristicFreshnessLifetimeDefaultsProperly() { final TimeValue defaultFreshness = TimeValue.ofSeconds(10); final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); assertEquals(defaultFreshness, impl.getHeuristicFreshnessLifetime(entry, 0.1f, defaultFreshness)); }
@Test public void testHeuristicFreshnessLifetimeIsNonNegative() { final Header[] headers = new Header[] { new BasicHeader("Date", DateUtils.formatDate(elevenSecondsAgo)), new BasicHeader("Last-Modified", DateUtils.formatDate(oneSecondAgo)) }; final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers); assertTrue(TimeValue.isNonNegative(impl.getHeuristicFreshnessLifetime(entry, 0.1f, TimeValue.ofSeconds(10)))); }