You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Gary Gregory <ga...@gmail.com> on 2019/12/15 16:21:02 UTC

Re: [httpcomponents-client] branch HTTPCLIENT-2036 updated: More work

AFK, but 2147483648L looks like Integer.MAX_VALUE?

Gary

On Sun, Dec 15, 2019, 11:06 <mi...@apache.org> wrote:

> This is an automated email from the ASF dual-hosted git repository.
>
> michaelo pushed a commit to branch HTTPCLIENT-2036
> in repository
> https://gitbox.apache.org/repos/asf/httpcomponents-client.git
>
>
> The following commit(s) were added to refs/heads/HTTPCLIENT-2036 by this
> push:
>      new 0a02e41  More work
> 0a02e41 is described below
>
> commit 0a02e410505ecf347911ce00dbbc225417eea5e9
> Author: Michael Osipov <mi...@apache.org>
> AuthorDate: Sun Dec 15 16:52:53 2019 +0100
>
>     More work
> ---
>  .../http/impl/cache/CacheValidityPolicy.java       | 16 ++--
>  .../impl/cache/CachedHttpResponseGenerator.java    | 11 +--
>  .../cache/CachedResponseSuitabilityChecker.java    | 18 ++--
>  .../client5/http/impl/cache/CachingExecBase.java   | 10 ++-
>  .../http/impl/cache/TestCacheValidityPolicy.java   | 98
> +++++++++++-----------
>  .../cache/TestCachedHttpResponseGenerator.java     | 13 +--
>  .../http/impl/cache/TestCachingExecChain.java      |  9 +-
>  7 files changed, 94 insertions(+), 81 deletions(-)
>
> diff --git
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> index 5bd00d1..fdb0f7e 100644
> ---
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> +++
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> @@ -43,14 +43,14 @@ import org.apache.hc.core5.util.TimeValue;
>
>  class CacheValidityPolicy {
>
> -    public static final long MAX_AGE = 2147483648L;
> +    public static final TimeValue MAX_AGE =
> TimeValue.ofSeconds(2147483648L);
>
>      CacheValidityPolicy() {
>          super();
>      }
>
>      public TimeValue getCurrentAge(final HttpCacheEntry entry, final Date
> now) {
> -        return
> TimeValue.ofMilliseconds(getCorrectedInitialAge(entry).toMillis() +
> getResidentTime(entry, now).toMillis());
> +        return
> TimeValue.ofSeconds(getCorrectedInitialAge(entry).toSeconds() +
> getResidentTime(entry, now).toSeconds());
>      }
>
>      public TimeValue getFreshnessLifetime(final HttpCacheEntry entry) {
> @@ -73,7 +73,7 @@ class CacheValidityPolicy {
>      }
>
>      public boolean isResponseFresh(final HttpCacheEntry entry, final Date
> now) {
> -        return (getCurrentAge(entry,
> now).compareTo(getFreshnessLifetime(entry)) == -1);
> +        return getCurrentAge(entry,
> now).compareTo(getFreshnessLifetime(entry)) == -1;
>      }
>
>      /**
> @@ -92,7 +92,7 @@ class CacheValidityPolicy {
>       */
>      public boolean isResponseHeuristicallyFresh(final HttpCacheEntry
> entry,
>              final Date now, final float coefficient, final TimeValue
> defaultLifetime) {
> -        return (getCurrentAge(entry,
> now).compareTo(getHeuristicFreshnessLifetime(entry, coefficient,
> defaultLifetime)) == -1);
> +        return getCurrentAge(entry,
> now).compareTo(getHeuristicFreshnessLifetime(entry, coefficient,
> defaultLifetime)) == -1;
>      }
>
>      public TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry
> entry,
> @@ -199,7 +199,7 @@ class CacheValidityPolicy {
>      protected TimeValue getApparentAge(final HttpCacheEntry entry) {
>          final Date dateValue = entry.getDate();
>          if (dateValue == null) {
> -            return TimeValue.ofSeconds(MAX_AGE);
> +            return MAX_AGE;
>          }
>          final long diff = entry.getResponseDate().getTime() -
> dateValue.getTime();
>          if (diff < 0L) {
> @@ -216,10 +216,10 @@ class CacheValidityPolicy {
>              try {
>                  hdrAge = Long.parseLong(hdr.getValue());
>                  if (hdrAge < 0) {
> -                    hdrAge = MAX_AGE;
> +                    hdrAge = MAX_AGE.toSeconds();
>                  }
>              } catch (final NumberFormatException nfe) {
> -                hdrAge = MAX_AGE;
> +                hdrAge = MAX_AGE.toSeconds();
>              }
>              ageValue = (hdrAge > ageValue) ? hdrAge : ageValue;
>          }
> @@ -284,7 +284,7 @@ class CacheValidityPolicy {
>          if (age.compareTo(freshness) <= 0) {
>              return TimeValue.ZERO_MILLISECONDS;
>          }
> -        return TimeValue.ofMilliseconds(age.toMillis() -
> freshness.toMillis());
> +        return TimeValue.ofSeconds(age.toSeconds() -
> freshness.toSeconds());
>      }
>
>
> diff --git
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> index 1f8bd42..614ed0e 100644
> ---
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> +++
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> @@ -42,6 +42,7 @@ import org.apache.hc.core5.http.HttpResponse;
>  import org.apache.hc.core5.http.HttpStatus;
>  import org.apache.hc.core5.http.HttpVersion;
>  import org.apache.hc.core5.http.message.BasicHeader;
> +import org.apache.hc.core5.util.TimeValue;
>
>  /**
>   * Rebuilds an {@link HttpResponse} from a {@link HttpCacheEntry}
> @@ -78,12 +79,12 @@ class CachedHttpResponseGenerator {
>              response.setBodyBytes(content, contentType);
>          }
>
> -        final long age = this.validityStrategy.getCurrentAge(entry, now);
> -        if (age > 0) {
> -            if (age >= Integer.MAX_VALUE) {
> -                response.setHeader(HeaderConstants.AGE, "2147483648");
> +        final TimeValue age = this.validityStrategy.getCurrentAge(entry,
> now);
> +        if (TimeValue.isPositive(age)) {
> +            if (age.compareTo(CacheValidityPolicy.MAX_AGE) >= 0) {
> +                response.setHeader(HeaderConstants.AGE, "" +
> CacheValidityPolicy.MAX_AGE.toSeconds());
>              } else {
> -                response.setHeader(HeaderConstants.AGE, "" + ((int) age));
> +                response.setHeader(HeaderConstants.AGE, "" +
> age.toSeconds());
>              }
>          }
>
> diff --git
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> index 9f2a33c..f680b5a 100644
> ---
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> +++
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> @@ -75,7 +75,7 @@ class CachedResponseSuitabilityChecker {
>              return true;
>          }
>          if (useHeuristicCaching &&
> -                validityStrategy.isResponseHeuristicallyFresh(entry, now,
> heuristicCoefficient, heuristicDefaultLifetime.toSeconds())) {
> +                validityStrategy.isResponseHeuristicallyFresh(entry, now,
> heuristicCoefficient, heuristicDefaultLifetime)) {
>              return true;
>          }
>          if (originInsistsOnFreshness(entry)) {
> @@ -85,7 +85,7 @@ class CachedResponseSuitabilityChecker {
>          if (maxStale == -1) {
>              return false;
>          }
> -        return (maxStale > validityStrategy.getStaleness(entry, now));
> +        return (maxStale > validityStrategy.getStaleness(entry,
> now).toSeconds());
>      }
>
>      private boolean originInsistsOnFreshness(final HttpCacheEntry entry) {
> @@ -100,6 +100,7 @@ class CachedResponseSuitabilityChecker {
>      }
>
>      private long getMaxStale(final HttpRequest request) {
> +        // This is a header value, we leave as-is
>          long maxStale = -1;
>          final Iterator<HeaderElement> it =
> MessageSupport.iterate(request, HeaderConstants.CACHE_CONTROL);
>          while (it.hasNext()) {
> @@ -186,8 +187,9 @@ class CachedResponseSuitabilityChecker {
>
>              if
> (HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
>                  try {
> +                    // in seconds
>                      final int maxAge = Integer.parseInt(elt.getValue());
> -                    if (validityStrategy.getCurrentAge(entry, now) >
> maxAge) {
> +                    if (validityStrategy.getCurrentAge(entry,
> now).toSeconds() > maxAge) {
>                          log.debug("Response from cache was not suitable
> due to max age");
>                          return false;
>                      }
> @@ -200,8 +202,9 @@ class CachedResponseSuitabilityChecker {
>
>              if
> (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
>                  try {
> +                    // in seconds
>                      final int maxStale = Integer.parseInt(elt.getValue());
> -                    if (validityStrategy.getFreshnessLifetime(entry) >
> maxStale) {
> +                    if
> (validityStrategy.getFreshnessLifetime(entry).toSeconds() > maxStale) {
>                          log.debug("Response from cache was not suitable
> due to max stale freshness");
>                          return false;
>                      }
> @@ -214,13 +217,14 @@ class CachedResponseSuitabilityChecker {
>
>              if
> (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())) {
>                  try {
> +                    // in seconds
>                      final long minFresh = Long.parseLong(elt.getValue());
>                      if (minFresh < 0L) {
>                          return false;
>                      }
> -                    final long age =
> validityStrategy.getCurrentAge(entry, now);
> -                    final long freshness =
> validityStrategy.getFreshnessLifetime(entry);
> -                    if (freshness - age < minFresh) {
> +                    final TimeValue age =
> validityStrategy.getCurrentAge(entry, now);
> +                    final TimeValue freshness =
> validityStrategy.getFreshnessLifetime(entry);
> +                    if (freshness.toSeconds() - age.toSeconds() <
> minFresh) {
>                          log.debug("Response from cache was not suitable
> due to min fresh " +
>                                  "freshness requirement");
>                          return false;
> diff --git
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> index 2b7991d..e2ee72d 100644
> ---
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> +++
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> @@ -54,6 +54,7 @@ import org.apache.hc.core5.http.ProtocolVersion;
>  import org.apache.hc.core5.http.URIScheme;
>  import org.apache.hc.core5.http.message.MessageSupport;
>  import org.apache.hc.core5.http.protocol.HttpContext;
> +import org.apache.hc.core5.util.TimeValue;
>  import org.apache.hc.core5.util.VersionInfo;
>  import org.slf4j.Logger;
>  import org.slf4j.LoggerFactory;
> @@ -189,7 +190,7 @@ public class CachingExecBase {
>              cachedResponse = responseGenerator.generateResponse(request,
> entry);
>          }
>          setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
> -        if (validityPolicy.getStaleness(entry, now) > 0L) {
> +        if (TimeValue.isPositive(validityPolicy.getStaleness(entry,
> now))) {
>              cachedResponse.addHeader(HeaderConstants.WARNING,"110
> localhost \"Response is stale\"");
>          }
>          return cachedResponse;
> @@ -247,10 +248,11 @@ public class CachingExecBase {
>              final HeaderElement elt = it.next();
>              if
> (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
>                  try {
> +                    // in seconds
>                      final int maxStale = Integer.parseInt(elt.getValue());
> -                    final long age = validityPolicy.getCurrentAge(entry,
> now);
> -                    final long lifetime =
> validityPolicy.getFreshnessLifetime(entry);
> -                    if (age - lifetime > maxStale) {
> +                    final TimeValue age =
> validityPolicy.getCurrentAge(entry, now);
> +                    final TimeValue lifetime =
> validityPolicy.getFreshnessLifetime(entry);
> +                    if (age.toSeconds() - lifetime.toSeconds() >
> maxStale) {
>                          return true;
>                      }
>                  } catch (final NumberFormatException nfe) {
> diff --git
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> index 93922bb..2389644 100644
> ---
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> +++
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> @@ -40,6 +40,7 @@ import org.apache.hc.core5.http.HttpHeaders;
>  import org.apache.hc.core5.http.HttpRequest;
>  import org.apache.hc.core5.http.message.BasicHeader;
>  import org.apache.hc.core5.http.message.BasicHttpRequest;
> +import org.apache.hc.core5.util.TimeValue;
>  import org.junit.Before;
>  import org.junit.Test;
>
> @@ -68,7 +69,7 @@ public class TestCacheValidityPolicy {
>                  new BasicHeader("Server", "MockServer/1.0")
>          };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(2147483648L, impl.getApparentAge(entry));
> +        assertEquals(CacheValidityPolicy.MAX_AGE,
> impl.getApparentAge(entry));
>      }
>
>      @Test
> @@ -77,15 +78,15 @@ public class TestCacheValidityPolicy {
>                  .formatDate(tenSecondsAgo)) };
>
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now,
> sixSecondsAgo, headers);
> -        assertEquals(4, impl.getApparentAge(entry));
> +        assertEquals(TimeValue.ofSeconds(4), impl.getApparentAge(entry));
>      }
>
>      @Test
>      public void testNegativeApparentAgeIsBroughtUpToZero() {
>          final Header[] headers = new Header[] { new BasicHeader("Date",
> DateUtils
>                  .formatDate(sixSecondsAgo)) };
> -        final HttpCacheEntry entry  =
> HttpTestUtils.makeCacheEntry(now,tenSecondsAgo,headers);
> -        assertEquals(0, impl.getApparentAge(entry));
> +        final HttpCacheEntry entry  = HttpTestUtils.makeCacheEntry(now,
> tenSecondsAgo, headers);
> +        assertEquals(TimeValue.ofSeconds(0), impl.getApparentAge(entry));
>      }
>
>      @Test
> @@ -94,11 +95,11 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
>          impl = new CacheValidityPolicy() {
>              @Override
> -            protected long getApparentAge(final HttpCacheEntry ent) {
> -                return 6;
> +            protected TimeValue getApparentAge(final HttpCacheEntry ent) {
> +                return TimeValue.ofSeconds(6);
>              }
>          };
> -        assertEquals(10, impl.getCorrectedReceivedAge(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getCorrectedReceivedAge(entry));
>      }
>
>      @Test
> @@ -107,17 +108,17 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
>          impl = new CacheValidityPolicy() {
>              @Override
> -            protected long getApparentAge(final HttpCacheEntry ent) {
> -                return 10;
> +            protected TimeValue getApparentAge(final HttpCacheEntry ent) {
> +                return TimeValue.ofSeconds(10);
>              }
>          };
> -        assertEquals(10, impl.getCorrectedReceivedAge(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getCorrectedReceivedAge(entry));
>      }
>
>      @Test
>      public void
> testResponseDelayIsDifferenceBetweenResponseAndRequestTimes() {
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(tenSecondsAgo, sixSecondsAgo);
> -        assertEquals(4, impl.getResponseDelay(entry));
> +        assertEquals(TimeValue.ofSeconds(4),
> impl.getResponseDelay(entry));
>      }
>
>      @Test
> @@ -125,22 +126,22 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>          impl = new CacheValidityPolicy() {
>              @Override
> -            protected long getCorrectedReceivedAge(final HttpCacheEntry
> ent) {
> -                return 7;
> +            protected TimeValue getCorrectedReceivedAge(final
> HttpCacheEntry ent) {
> +                return TimeValue.ofSeconds(7);
>              }
>
>              @Override
> -            protected long getResponseDelay(final HttpCacheEntry ent) {
> -                return 13;
> +            protected TimeValue getResponseDelay(final HttpCacheEntry
> ent) {
> +                return TimeValue.ofSeconds(13);
>              }
>          };
> -        assertEquals(20, impl.getCorrectedInitialAge(entry));
> +        assertEquals(TimeValue.ofSeconds(20),
> impl.getCorrectedInitialAge(entry));
>      }
>
>      @Test
>      public void testResidentTimeSecondsIsTimeSinceResponseTime() {
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now,
> sixSecondsAgo);
> -        assertEquals(6, impl.getResidentTime(entry, now));
> +        assertEquals(TimeValue.ofSeconds(6), impl.getResidentTime(entry,
> now));
>      }
>
>      @Test
> @@ -148,29 +149,29 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>          impl = new CacheValidityPolicy() {
>              @Override
> -            protected long getCorrectedInitialAge(final HttpCacheEntry
> ent) {
> -                return 11;
> +            protected TimeValue getCorrectedInitialAge(final
> HttpCacheEntry ent) {
> +                return TimeValue.ofSeconds(11);
>              }
>              @Override
> -            protected long getResidentTime(final HttpCacheEntry ent,
> final Date d) {
> -                return 17;
> +            protected TimeValue getResidentTime(final HttpCacheEntry ent,
> final Date d) {
> +                return TimeValue.ofSeconds(17);
>              }
>          };
> -        assertEquals(28, impl.getCurrentAge(entry, new Date()));
> +        assertEquals(TimeValue.ofSeconds(28), impl.getCurrentAge(entry,
> new Date()));
>      }
>
>      @Test
>      public void testFreshnessLifetimeIsSMaxAgeIfPresent() {
>          final Header[] headers = new Header[] { new
> BasicHeader("Cache-Control", "s-maxage=10") };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getFreshnessLifetime(entry));
>      }
>
>      @Test
>      public void testFreshnessLifetimeIsMaxAgeIfPresent() {
>          final Header[] headers = new Header[] { new
> BasicHeader("Cache-Control", "max-age=10") };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getFreshnessLifetime(entry));
>      }
>
>      @Test
> @@ -178,12 +179,12 @@ public class TestCacheValidityPolicy {
>          Header[] headers = new Header[] { new
> BasicHeader("Cache-Control", "max-age=10"),
>                  new BasicHeader("Cache-Control", "s-maxage=20") };
>          HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getFreshnessLifetime(entry));
>
>          headers = new Header[] { new BasicHeader("Cache-Control",
> "max-age=20"),
>                  new BasicHeader("Cache-Control", "s-maxage=10") };
>          entry = HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getFreshnessLifetime(entry));
>      }
>
>      @Test
> @@ -193,7 +194,7 @@ public class TestCacheValidityPolicy {
>                  new BasicHeader("Expires",
> DateUtils.formatDate(sixSecondsAgo)) };
>
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getFreshnessLifetime(entry));
>      }
>
>      @Test
> @@ -203,7 +204,7 @@ public class TestCacheValidityPolicy {
>                  new BasicHeader("Expires",
> DateUtils.formatDate(sixSecondsAgo)) };
>
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(10),
> impl.getFreshnessLifetime(entry));
>      }
>
>      @Test
> @@ -213,7 +214,7 @@ public class TestCacheValidityPolicy {
>                  new BasicHeader("Expires",
> DateUtils.formatDate(sixSecondsAgo)) };
>
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(4, impl.getFreshnessLifetime(entry));
> +        assertEquals(TimeValue.ofSeconds(4),
> impl.getFreshnessLifetime(entry));
>      }
>
>      @Test
> @@ -223,12 +224,12 @@ public class TestCacheValidityPolicy {
>                  new BasicHeader("Last-Modified",
> DateUtils.formatDate(elevenSecondsAgo))
>          };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(1, impl.getHeuristicFreshnessLifetime(entry, 0.1f,
> 0));
> +        assertEquals(TimeValue.ofSeconds(1),
> impl.getHeuristicFreshnessLifetime(entry, 0.1f,
> TimeValue.ZERO_MILLISECONDS));
>      }
>
>      @Test
>      public void testHeuristicFreshnessLifetimeDefaultsProperly() {
> -        final long defaultFreshness = 10;
> +        final TimeValue defaultFreshness = TimeValue.ofSeconds(10);
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>          assertEquals(defaultFreshness,
> impl.getHeuristicFreshnessLifetime(entry, 0.1f, defaultFreshness));
>      }
> @@ -241,7 +242,7 @@ public class TestCacheValidityPolicy {
>          };
>
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertTrue(impl.getHeuristicFreshnessLifetime(entry, 0.1f, 10) >=
> 0);
> +
> assertTrue(TimeValue.isNonNegative(impl.getHeuristicFreshnessLifetime(entry,
> 0.1f, TimeValue.ofSeconds(10))));
>      }
>
>      @Test
> @@ -249,15 +250,15 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>          impl = new CacheValidityPolicy() {
>              @Override
> -            public long getCurrentAge(final HttpCacheEntry e, final Date
> d) {
> +            public TimeValue getCurrentAge(final HttpCacheEntry e, final
> Date d) {
>                  assertSame(entry, e);
>                  assertEquals(now, d);
> -                return 6;
> +                return TimeValue.ofSeconds(6);
>              }
>              @Override
> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry e)
> {
>                  assertSame(entry, e);
> -                return 10;
> +                return TimeValue.ofSeconds(10);
>              }
>          };
>          assertTrue(impl.isResponseFresh(entry, now));
> @@ -268,15 +269,15 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>          impl = new CacheValidityPolicy() {
>              @Override
> -            public long getCurrentAge(final HttpCacheEntry e, final Date
> d) {
> +            public TimeValue getCurrentAge(final HttpCacheEntry e, final
> Date d) {
>                  assertEquals(now, d);
>                  assertSame(entry, e);
> -                return 6;
> +                return TimeValue.ofSeconds(6);
>              }
>              @Override
> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry e)
> {
>                  assertSame(entry, e);
> -                return 6;
> +                return TimeValue.ofSeconds(6);
>              }
>          };
>          assertFalse(impl.isResponseFresh(entry, now));
> @@ -287,15 +288,15 @@ public class TestCacheValidityPolicy {
>          final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>          impl = new CacheValidityPolicy() {
>              @Override
> -            public long getCurrentAge(final HttpCacheEntry e, final Date
> d) {
> +            public TimeValue getCurrentAge(final HttpCacheEntry e, final
> Date d) {
>                  assertEquals(now, d);
>                  assertSame(entry, e);
> -                return 10;
> +                return TimeValue.ofSeconds(10);
>              }
>              @Override
> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry e)
> {
>                  assertSame(entry, e);
> -                return 6;
> +                return TimeValue.ofSeconds(6);
>              }
>          };
>          assertFalse(impl.isResponseFresh(entry, now));
> @@ -364,21 +365,24 @@ public class TestCacheValidityPolicy {
>      public void testNegativeAgeHeaderValueReturnsMaxAge() {
>          final Header[] headers = new Header[] { new BasicHeader("Age",
> "-100") };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(CacheValidityPolicy.MAX_AGE,
> impl.getAgeValue(entry));
> +        // in seconds
> +        assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
> impl.getAgeValue(entry));
>      }
>
>      @Test
>      public void testMalformedAgeHeaderValueReturnsMaxAge() {
>          final Header[] headers = new Header[] { new BasicHeader("Age",
> "asdf") };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(CacheValidityPolicy.MAX_AGE,
> impl.getAgeValue(entry));
> +        // in seconds
> +        assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
> impl.getAgeValue(entry));
>      }
>
>      @Test
>      public void testMalformedCacheControlMaxAgeHeaderReturnsZero() {
>          final Header[] headers = new Header[] { new
> BasicHeader("Cache-Control", "max-age=asdf") };
>          final HttpCacheEntry entry =
> HttpTestUtils.makeCacheEntry(headers);
> -        assertEquals(0, impl.getMaxAge(entry));
> +        // in seconds
> +        assertEquals(0L, impl.getMaxAge(entry));
>      }
>
>      @Test
> diff --git
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> index 42d7ce9..ace354b 100644
> ---
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> +++
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> @@ -40,6 +40,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntry;
>  import org.apache.hc.core5.http.ClassicHttpRequest;
>  import org.apache.hc.core5.http.Header;
>  import org.apache.hc.core5.http.message.BasicHeader;
> +import org.apache.hc.core5.util.TimeValue;
>  import org.junit.Assert;
>  import org.junit.Before;
>  import org.junit.Test;
> @@ -108,7 +109,7 @@ public class TestCachedHttpResponseGenerator {
>
>      @Test
>      public void
> testAgeHeaderIsPopulatedWithCurrentAgeOfCacheEntryIfNonZero() throws
> Exception {
> -        currentAge(10L);
> +        currentAge(TimeValue.ofSeconds(10L));
>
>          final SimpleHttpResponse response =
> impl.generateResponse(request, entry);
>
> @@ -121,7 +122,7 @@ public class TestCachedHttpResponseGenerator {
>
>      @Test
>      public void
> testAgeHeaderIsNotPopulatedIfCurrentAgeOfCacheEntryIsZero() throws
> Exception {
> -        currentAge(0L);
> +        currentAge(TimeValue.ofSeconds(0L));
>
>          final SimpleHttpResponse response =
> impl.generateResponse(request, entry);
>
> @@ -133,7 +134,7 @@ public class TestCachedHttpResponseGenerator {
>
>      @Test
>      public void testAgeHeaderIsPopulatedWithMaxAgeIfCurrentAgeTooBig()
> throws Exception {
> -        currentAge(CacheValidityPolicy.MAX_AGE + 1L);
> +
> currentAge(TimeValue.ofSeconds(CacheValidityPolicy.MAX_AGE.toSeconds() +
> 1L));
>
>          final SimpleHttpResponse response =
> impl.generateResponse(request, entry);
>
> @@ -141,13 +142,13 @@ public class TestCachedHttpResponseGenerator {
>
>          final Header ageHdr = response.getFirstHeader("Age");
>          Assert.assertNotNull(ageHdr);
> -        Assert.assertEquals(CacheValidityPolicy.MAX_AGE,
> Long.parseLong(ageHdr.getValue()));
> +        Assert.assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
> Long.parseLong(ageHdr.getValue()));
>      }
>
> -    private void currentAge(final long sec) {
> +    private void currentAge(final TimeValue age) {
>          when(
>                  mockValidityPolicy.getCurrentAge(same(entry),
> -                        isA(Date.class))).thenReturn(sec);
> +                        isA(Date.class))).thenReturn(age);
>      }
>
>      @Test
> diff --git
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> index 2faa5ed..2ddfbf7 100644
> ---
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> +++
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> @@ -78,6 +78,7 @@ import
> org.apache.hc.core5.http.message.BasicClassicHttpResponse;
>  import org.apache.hc.core5.http.message.BasicHeader;
>  import org.apache.hc.core5.net.URIAuthority;
>  import org.apache.hc.core5.util.ByteArrayBuffer;
> +import org.apache.hc.core5.util.TimeValue;
>  import org.easymock.Capture;
>  import org.easymock.EasyMock;
>  import org.easymock.IExpectationSetters;
> @@ -315,7 +316,7 @@ public abstract class TestCachingExecChain {
>          cacheEntrySuitable(true);
>
>  responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
>          requestIsFatallyNonCompliant(null);
> -        entryHasStaleness(0L);
> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
>
>          replayMocks();
>          final ClassicHttpResponse result = execute(request);
> @@ -352,7 +353,7 @@ public abstract class TestCachingExecChain {
>          cacheEntrySuitable(true);
>          getCacheEntryReturns(mockCacheEntry);
>
>  responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
> -        entryHasStaleness(0L);
> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
>
>          replayMocks();
>          execute(request);
> @@ -1347,7 +1348,7 @@ public abstract class TestCachingExecChain {
>          getCacheEntryReturns(entry);
>          cacheEntrySuitable(true);
>
>  responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
> -        entryHasStaleness(0);
> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
>
>          replayMocks();
>          final ClassicHttpResponse resp = execute(request);
> @@ -1674,7 +1675,7 @@ public abstract class TestCachingExecChain {
>              .andReturn(suitable);
>      }
>
> -    private void entryHasStaleness(final long staleness) {
> +    private void entryHasStaleness(final TimeValue staleness) {
>          expect(
>              mockValidityPolicy.getStaleness((HttpCacheEntry) anyObject(),
> (Date) anyObject()))
>              .andReturn(staleness);
>
>

Re: [httpcomponents-client] branch HTTPCLIENT-2036 updated: More work

Posted by Gary Gregory <ga...@gmail.com>.
Any reason not to use the Integer constant? Seems less magical, esp since
the current definition has no helpful comment.

Gary

On Sun, Dec 15, 2019, 11:25 Michael Osipov <mi...@apache.org> wrote:

> Am 2019-12-15 um 17:21 schrieb Gary Gregory:
> > AFK, but 2147483648L looks like Integer.MAX_VALUE?
>
> Almost, Integer.MAX_VALUE is one less: 2147483647.
>
> M
>
> > On Sun, Dec 15, 2019, 11:06 <mi...@apache.org> wrote:
> >
> >> This is an automated email from the ASF dual-hosted git repository.
> >>
> >> michaelo pushed a commit to branch HTTPCLIENT-2036
> >> in repository
> >> https://gitbox.apache.org/repos/asf/httpcomponents-client.git
> >>
> >>
> >> The following commit(s) were added to refs/heads/HTTPCLIENT-2036 by this
> >> push:
> >>       new 0a02e41  More work
> >> 0a02e41 is described below
> >>
> >> commit 0a02e410505ecf347911ce00dbbc225417eea5e9
> >> Author: Michael Osipov <mi...@apache.org>
> >> AuthorDate: Sun Dec 15 16:52:53 2019 +0100
> >>
> >>      More work
> >> ---
> >>   .../http/impl/cache/CacheValidityPolicy.java       | 16 ++--
> >>   .../impl/cache/CachedHttpResponseGenerator.java    | 11 +--
> >>   .../cache/CachedResponseSuitabilityChecker.java    | 18 ++--
> >>   .../client5/http/impl/cache/CachingExecBase.java   | 10 ++-
> >>   .../http/impl/cache/TestCacheValidityPolicy.java   | 98
> >> +++++++++++-----------
> >>   .../cache/TestCachedHttpResponseGenerator.java     | 13 +--
> >>   .../http/impl/cache/TestCachingExecChain.java      |  9 +-
> >>   7 files changed, 94 insertions(+), 81 deletions(-)
> >>
> >> diff --git
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> >> index 5bd00d1..fdb0f7e 100644
> >> ---
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> >> +++
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
> >> @@ -43,14 +43,14 @@ import org.apache.hc.core5.util.TimeValue;
> >>
> >>   class CacheValidityPolicy {
> >>
> >> -    public static final long MAX_AGE = 2147483648L;
> >> +    public static final TimeValue MAX_AGE =
> >> TimeValue.ofSeconds(2147483648L);
> >>
> >>       CacheValidityPolicy() {
> >>           super();
> >>       }
> >>
> >>       public TimeValue getCurrentAge(final HttpCacheEntry entry, final
> Date
> >> now) {
> >> -        return
> >> TimeValue.ofMilliseconds(getCorrectedInitialAge(entry).toMillis() +
> >> getResidentTime(entry, now).toMillis());
> >> +        return
> >> TimeValue.ofSeconds(getCorrectedInitialAge(entry).toSeconds() +
> >> getResidentTime(entry, now).toSeconds());
> >>       }
> >>
> >>       public TimeValue getFreshnessLifetime(final HttpCacheEntry entry)
> {
> >> @@ -73,7 +73,7 @@ class CacheValidityPolicy {
> >>       }
> >>
> >>       public boolean isResponseFresh(final HttpCacheEntry entry, final
> Date
> >> now) {
> >> -        return (getCurrentAge(entry,
> >> now).compareTo(getFreshnessLifetime(entry)) == -1);
> >> +        return getCurrentAge(entry,
> >> now).compareTo(getFreshnessLifetime(entry)) == -1;
> >>       }
> >>
> >>       /**
> >> @@ -92,7 +92,7 @@ class CacheValidityPolicy {
> >>        */
> >>       public boolean isResponseHeuristicallyFresh(final HttpCacheEntry
> >> entry,
> >>               final Date now, final float coefficient, final TimeValue
> >> defaultLifetime) {
> >> -        return (getCurrentAge(entry,
> >> now).compareTo(getHeuristicFreshnessLifetime(entry, coefficient,
> >> defaultLifetime)) == -1);
> >> +        return getCurrentAge(entry,
> >> now).compareTo(getHeuristicFreshnessLifetime(entry, coefficient,
> >> defaultLifetime)) == -1;
> >>       }
> >>
> >>       public TimeValue getHeuristicFreshnessLifetime(final
> HttpCacheEntry
> >> entry,
> >> @@ -199,7 +199,7 @@ class CacheValidityPolicy {
> >>       protected TimeValue getApparentAge(final HttpCacheEntry entry) {
> >>           final Date dateValue = entry.getDate();
> >>           if (dateValue == null) {
> >> -            return TimeValue.ofSeconds(MAX_AGE);
> >> +            return MAX_AGE;
> >>           }
> >>           final long diff = entry.getResponseDate().getTime() -
> >> dateValue.getTime();
> >>           if (diff < 0L) {
> >> @@ -216,10 +216,10 @@ class CacheValidityPolicy {
> >>               try {
> >>                   hdrAge = Long.parseLong(hdr.getValue());
> >>                   if (hdrAge < 0) {
> >> -                    hdrAge = MAX_AGE;
> >> +                    hdrAge = MAX_AGE.toSeconds();
> >>                   }
> >>               } catch (final NumberFormatException nfe) {
> >> -                hdrAge = MAX_AGE;
> >> +                hdrAge = MAX_AGE.toSeconds();
> >>               }
> >>               ageValue = (hdrAge > ageValue) ? hdrAge : ageValue;
> >>           }
> >> @@ -284,7 +284,7 @@ class CacheValidityPolicy {
> >>           if (age.compareTo(freshness) <= 0) {
> >>               return TimeValue.ZERO_MILLISECONDS;
> >>           }
> >> -        return TimeValue.ofMilliseconds(age.toMillis() -
> >> freshness.toMillis());
> >> +        return TimeValue.ofSeconds(age.toSeconds() -
> >> freshness.toSeconds());
> >>       }
> >>
> >>
> >> diff --git
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> >> index 1f8bd42..614ed0e 100644
> >> ---
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> >> +++
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
> >> @@ -42,6 +42,7 @@ import org.apache.hc.core5.http.HttpResponse;
> >>   import org.apache.hc.core5.http.HttpStatus;
> >>   import org.apache.hc.core5.http.HttpVersion;
> >>   import org.apache.hc.core5.http.message.BasicHeader;
> >> +import org.apache.hc.core5.util.TimeValue;
> >>
> >>   /**
> >>    * Rebuilds an {@link HttpResponse} from a {@link HttpCacheEntry}
> >> @@ -78,12 +79,12 @@ class CachedHttpResponseGenerator {
> >>               response.setBodyBytes(content, contentType);
> >>           }
> >>
> >> -        final long age = this.validityStrategy.getCurrentAge(entry,
> now);
> >> -        if (age > 0) {
> >> -            if (age >= Integer.MAX_VALUE) {
> >> -                response.setHeader(HeaderConstants.AGE, "2147483648");
> >> +        final TimeValue age =
> this.validityStrategy.getCurrentAge(entry,
> >> now);
> >> +        if (TimeValue.isPositive(age)) {
> >> +            if (age.compareTo(CacheValidityPolicy.MAX_AGE) >= 0) {
> >> +                response.setHeader(HeaderConstants.AGE, "" +
> >> CacheValidityPolicy.MAX_AGE.toSeconds());
> >>               } else {
> >> -                response.setHeader(HeaderConstants.AGE, "" + ((int)
> age));
> >> +                response.setHeader(HeaderConstants.AGE, "" +
> >> age.toSeconds());
> >>               }
> >>           }
> >>
> >> diff --git
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> >> index 9f2a33c..f680b5a 100644
> >> ---
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> >> +++
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
> >> @@ -75,7 +75,7 @@ class CachedResponseSuitabilityChecker {
> >>               return true;
> >>           }
> >>           if (useHeuristicCaching &&
> >> -                validityStrategy.isResponseHeuristicallyFresh(entry,
> now,
> >> heuristicCoefficient, heuristicDefaultLifetime.toSeconds())) {
> >> +                validityStrategy.isResponseHeuristicallyFresh(entry,
> now,
> >> heuristicCoefficient, heuristicDefaultLifetime)) {
> >>               return true;
> >>           }
> >>           if (originInsistsOnFreshness(entry)) {
> >> @@ -85,7 +85,7 @@ class CachedResponseSuitabilityChecker {
> >>           if (maxStale == -1) {
> >>               return false;
> >>           }
> >> -        return (maxStale > validityStrategy.getStaleness(entry, now));
> >> +        return (maxStale > validityStrategy.getStaleness(entry,
> >> now).toSeconds());
> >>       }
> >>
> >>       private boolean originInsistsOnFreshness(final HttpCacheEntry
> entry) {
> >> @@ -100,6 +100,7 @@ class CachedResponseSuitabilityChecker {
> >>       }
> >>
> >>       private long getMaxStale(final HttpRequest request) {
> >> +        // This is a header value, we leave as-is
> >>           long maxStale = -1;
> >>           final Iterator<HeaderElement> it =
> >> MessageSupport.iterate(request, HeaderConstants.CACHE_CONTROL);
> >>           while (it.hasNext()) {
> >> @@ -186,8 +187,9 @@ class CachedResponseSuitabilityChecker {
> >>
> >>               if
> >> (HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
> >>                   try {
> >> +                    // in seconds
> >>                       final int maxAge =
> Integer.parseInt(elt.getValue());
> >> -                    if (validityStrategy.getCurrentAge(entry, now) >
> >> maxAge) {
> >> +                    if (validityStrategy.getCurrentAge(entry,
> >> now).toSeconds() > maxAge) {
> >>                           log.debug("Response from cache was not
> suitable
> >> due to max age");
> >>                           return false;
> >>                       }
> >> @@ -200,8 +202,9 @@ class CachedResponseSuitabilityChecker {
> >>
> >>               if
> >> (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
> >>                   try {
> >> +                    // in seconds
> >>                       final int maxStale =
> Integer.parseInt(elt.getValue());
> >> -                    if (validityStrategy.getFreshnessLifetime(entry) >
> >> maxStale) {
> >> +                    if
> >> (validityStrategy.getFreshnessLifetime(entry).toSeconds() > maxStale) {
> >>                           log.debug("Response from cache was not
> suitable
> >> due to max stale freshness");
> >>                           return false;
> >>                       }
> >> @@ -214,13 +217,14 @@ class CachedResponseSuitabilityChecker {
> >>
> >>               if
> >> (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())) {
> >>                   try {
> >> +                    // in seconds
> >>                       final long minFresh =
> Long.parseLong(elt.getValue());
> >>                       if (minFresh < 0L) {
> >>                           return false;
> >>                       }
> >> -                    final long age =
> >> validityStrategy.getCurrentAge(entry, now);
> >> -                    final long freshness =
> >> validityStrategy.getFreshnessLifetime(entry);
> >> -                    if (freshness - age < minFresh) {
> >> +                    final TimeValue age =
> >> validityStrategy.getCurrentAge(entry, now);
> >> +                    final TimeValue freshness =
> >> validityStrategy.getFreshnessLifetime(entry);
> >> +                    if (freshness.toSeconds() - age.toSeconds() <
> >> minFresh) {
> >>                           log.debug("Response from cache was not
> suitable
> >> due to min fresh " +
> >>                                   "freshness requirement");
> >>                           return false;
> >> diff --git
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> >> index 2b7991d..e2ee72d 100644
> >> ---
> >>
> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> >> +++
> >>
> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
> >> @@ -54,6 +54,7 @@ import org.apache.hc.core5.http.ProtocolVersion;
> >>   import org.apache.hc.core5.http.URIScheme;
> >>   import org.apache.hc.core5.http.message.MessageSupport;
> >>   import org.apache.hc.core5.http.protocol.HttpContext;
> >> +import org.apache.hc.core5.util.TimeValue;
> >>   import org.apache.hc.core5.util.VersionInfo;
> >>   import org.slf4j.Logger;
> >>   import org.slf4j.LoggerFactory;
> >> @@ -189,7 +190,7 @@ public class CachingExecBase {
> >>               cachedResponse =
> responseGenerator.generateResponse(request,
> >> entry);
> >>           }
> >>           setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
> >> -        if (validityPolicy.getStaleness(entry, now) > 0L) {
> >> +        if (TimeValue.isPositive(validityPolicy.getStaleness(entry,
> >> now))) {
> >>               cachedResponse.addHeader(HeaderConstants.WARNING,"110
> >> localhost \"Response is stale\"");
> >>           }
> >>           return cachedResponse;
> >> @@ -247,10 +248,11 @@ public class CachingExecBase {
> >>               final HeaderElement elt = it.next();
> >>               if
> >> (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
> >>                   try {
> >> +                    // in seconds
> >>                       final int maxStale =
> Integer.parseInt(elt.getValue());
> >> -                    final long age =
> validityPolicy.getCurrentAge(entry,
> >> now);
> >> -                    final long lifetime =
> >> validityPolicy.getFreshnessLifetime(entry);
> >> -                    if (age - lifetime > maxStale) {
> >> +                    final TimeValue age =
> >> validityPolicy.getCurrentAge(entry, now);
> >> +                    final TimeValue lifetime =
> >> validityPolicy.getFreshnessLifetime(entry);
> >> +                    if (age.toSeconds() - lifetime.toSeconds() >
> >> maxStale) {
> >>                           return true;
> >>                       }
> >>                   } catch (final NumberFormatException nfe) {
> >> diff --git
> >>
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> >>
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> >> index 93922bb..2389644 100644
> >> ---
> >>
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> >> +++
> >>
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
> >> @@ -40,6 +40,7 @@ import org.apache.hc.core5.http.HttpHeaders;
> >>   import org.apache.hc.core5.http.HttpRequest;
> >>   import org.apache.hc.core5.http.message.BasicHeader;
> >>   import org.apache.hc.core5.http.message.BasicHttpRequest;
> >> +import org.apache.hc.core5.util.TimeValue;
> >>   import org.junit.Before;
> >>   import org.junit.Test;
> >>
> >> @@ -68,7 +69,7 @@ public class TestCacheValidityPolicy {
> >>                   new BasicHeader("Server", "MockServer/1.0")
> >>           };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(2147483648L, impl.getApparentAge(entry));
> >> +        assertEquals(CacheValidityPolicy.MAX_AGE,
> >> impl.getApparentAge(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -77,15 +78,15 @@ public class TestCacheValidityPolicy {
> >>                   .formatDate(tenSecondsAgo)) };
> >>
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now,
> >> sixSecondsAgo, headers);
> >> -        assertEquals(4, impl.getApparentAge(entry));
> >> +        assertEquals(TimeValue.ofSeconds(4),
> impl.getApparentAge(entry));
> >>       }
> >>
> >>       @Test
> >>       public void testNegativeApparentAgeIsBroughtUpToZero() {
> >>           final Header[] headers = new Header[] { new
> BasicHeader("Date",
> >> DateUtils
> >>                   .formatDate(sixSecondsAgo)) };
> >> -        final HttpCacheEntry entry  =
> >> HttpTestUtils.makeCacheEntry(now,tenSecondsAgo,headers);
> >> -        assertEquals(0, impl.getApparentAge(entry));
> >> +        final HttpCacheEntry entry  = HttpTestUtils.makeCacheEntry(now,
> >> tenSecondsAgo, headers);
> >> +        assertEquals(TimeValue.ofSeconds(0),
> impl.getApparentAge(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -94,11 +95,11 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            protected long getApparentAge(final HttpCacheEntry ent) {
> >> -                return 6;
> >> +            protected TimeValue getApparentAge(final HttpCacheEntry
> ent) {
> >> +                return TimeValue.ofSeconds(6);
> >>               }
> >>           };
> >> -        assertEquals(10, impl.getCorrectedReceivedAge(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getCorrectedReceivedAge(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -107,17 +108,17 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            protected long getApparentAge(final HttpCacheEntry ent) {
> >> -                return 10;
> >> +            protected TimeValue getApparentAge(final HttpCacheEntry
> ent) {
> >> +                return TimeValue.ofSeconds(10);
> >>               }
> >>           };
> >> -        assertEquals(10, impl.getCorrectedReceivedAge(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getCorrectedReceivedAge(entry));
> >>       }
> >>
> >>       @Test
> >>       public void
> >> testResponseDelayIsDifferenceBetweenResponseAndRequestTimes() {
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(tenSecondsAgo, sixSecondsAgo);
> >> -        assertEquals(4, impl.getResponseDelay(entry));
> >> +        assertEquals(TimeValue.ofSeconds(4),
> >> impl.getResponseDelay(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -125,22 +126,22 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            protected long getCorrectedReceivedAge(final HttpCacheEntry
> >> ent) {
> >> -                return 7;
> >> +            protected TimeValue getCorrectedReceivedAge(final
> >> HttpCacheEntry ent) {
> >> +                return TimeValue.ofSeconds(7);
> >>               }
> >>
> >>               @Override
> >> -            protected long getResponseDelay(final HttpCacheEntry ent) {
> >> -                return 13;
> >> +            protected TimeValue getResponseDelay(final HttpCacheEntry
> >> ent) {
> >> +                return TimeValue.ofSeconds(13);
> >>               }
> >>           };
> >> -        assertEquals(20, impl.getCorrectedInitialAge(entry));
> >> +        assertEquals(TimeValue.ofSeconds(20),
> >> impl.getCorrectedInitialAge(entry));
> >>       }
> >>
> >>       @Test
> >>       public void testResidentTimeSecondsIsTimeSinceResponseTime() {
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now,
> >> sixSecondsAgo);
> >> -        assertEquals(6, impl.getResidentTime(entry, now));
> >> +        assertEquals(TimeValue.ofSeconds(6),
> impl.getResidentTime(entry,
> >> now));
> >>       }
> >>
> >>       @Test
> >> @@ -148,29 +149,29 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            protected long getCorrectedInitialAge(final HttpCacheEntry
> >> ent) {
> >> -                return 11;
> >> +            protected TimeValue getCorrectedInitialAge(final
> >> HttpCacheEntry ent) {
> >> +                return TimeValue.ofSeconds(11);
> >>               }
> >>               @Override
> >> -            protected long getResidentTime(final HttpCacheEntry ent,
> >> final Date d) {
> >> -                return 17;
> >> +            protected TimeValue getResidentTime(final HttpCacheEntry
> ent,
> >> final Date d) {
> >> +                return TimeValue.ofSeconds(17);
> >>               }
> >>           };
> >> -        assertEquals(28, impl.getCurrentAge(entry, new Date()));
> >> +        assertEquals(TimeValue.ofSeconds(28), impl.getCurrentAge(entry,
> >> new Date()));
> >>       }
> >>
> >>       @Test
> >>       public void testFreshnessLifetimeIsSMaxAgeIfPresent() {
> >>           final Header[] headers = new Header[] { new
> >> BasicHeader("Cache-Control", "s-maxage=10") };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getFreshnessLifetime(entry));
> >>       }
> >>
> >>       @Test
> >>       public void testFreshnessLifetimeIsMaxAgeIfPresent() {
> >>           final Header[] headers = new Header[] { new
> >> BasicHeader("Cache-Control", "max-age=10") };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getFreshnessLifetime(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -178,12 +179,12 @@ public class TestCacheValidityPolicy {
> >>           Header[] headers = new Header[] { new
> >> BasicHeader("Cache-Control", "max-age=10"),
> >>                   new BasicHeader("Cache-Control", "s-maxage=20") };
> >>           HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getFreshnessLifetime(entry));
> >>
> >>           headers = new Header[] { new BasicHeader("Cache-Control",
> >> "max-age=20"),
> >>                   new BasicHeader("Cache-Control", "s-maxage=10") };
> >>           entry = HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getFreshnessLifetime(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -193,7 +194,7 @@ public class TestCacheValidityPolicy {
> >>                   new BasicHeader("Expires",
> >> DateUtils.formatDate(sixSecondsAgo)) };
> >>
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getFreshnessLifetime(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -203,7 +204,7 @@ public class TestCacheValidityPolicy {
> >>                   new BasicHeader("Expires",
> >> DateUtils.formatDate(sixSecondsAgo)) };
> >>
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(10, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(10),
> >> impl.getFreshnessLifetime(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -213,7 +214,7 @@ public class TestCacheValidityPolicy {
> >>                   new BasicHeader("Expires",
> >> DateUtils.formatDate(sixSecondsAgo)) };
> >>
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(4, impl.getFreshnessLifetime(entry));
> >> +        assertEquals(TimeValue.ofSeconds(4),
> >> impl.getFreshnessLifetime(entry));
> >>       }
> >>
> >>       @Test
> >> @@ -223,12 +224,12 @@ public class TestCacheValidityPolicy {
> >>                   new BasicHeader("Last-Modified",
> >> DateUtils.formatDate(elevenSecondsAgo))
> >>           };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(1, impl.getHeuristicFreshnessLifetime(entry, 0.1f,
> >> 0));
> >> +        assertEquals(TimeValue.ofSeconds(1),
> >> impl.getHeuristicFreshnessLifetime(entry, 0.1f,
> >> TimeValue.ZERO_MILLISECONDS));
> >>       }
> >>
> >>       @Test
> >>       public void testHeuristicFreshnessLifetimeDefaultsProperly() {
> >> -        final long defaultFreshness = 10;
> >> +        final TimeValue defaultFreshness = TimeValue.ofSeconds(10);
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
> >>           assertEquals(defaultFreshness,
> >> impl.getHeuristicFreshnessLifetime(entry, 0.1f, defaultFreshness));
> >>       }
> >> @@ -241,7 +242,7 @@ public class TestCacheValidityPolicy {
> >>           };
> >>
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertTrue(impl.getHeuristicFreshnessLifetime(entry, 0.1f, 10)
> >=
> >> 0);
> >> +
> >>
> assertTrue(TimeValue.isNonNegative(impl.getHeuristicFreshnessLifetime(entry,
> >> 0.1f, TimeValue.ofSeconds(10))));
> >>       }
> >>
> >>       @Test
> >> @@ -249,15 +250,15 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            public long getCurrentAge(final HttpCacheEntry e, final
> Date
> >> d) {
> >> +            public TimeValue getCurrentAge(final HttpCacheEntry e,
> final
> >> Date d) {
> >>                   assertSame(entry, e);
> >>                   assertEquals(now, d);
> >> -                return 6;
> >> +                return TimeValue.ofSeconds(6);
> >>               }
> >>               @Override
> >> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
> >> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry
> e)
> >> {
> >>                   assertSame(entry, e);
> >> -                return 10;
> >> +                return TimeValue.ofSeconds(10);
> >>               }
> >>           };
> >>           assertTrue(impl.isResponseFresh(entry, now));
> >> @@ -268,15 +269,15 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            public long getCurrentAge(final HttpCacheEntry e, final
> Date
> >> d) {
> >> +            public TimeValue getCurrentAge(final HttpCacheEntry e,
> final
> >> Date d) {
> >>                   assertEquals(now, d);
> >>                   assertSame(entry, e);
> >> -                return 6;
> >> +                return TimeValue.ofSeconds(6);
> >>               }
> >>               @Override
> >> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
> >> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry
> e)
> >> {
> >>                   assertSame(entry, e);
> >> -                return 6;
> >> +                return TimeValue.ofSeconds(6);
> >>               }
> >>           };
> >>           assertFalse(impl.isResponseFresh(entry, now));
> >> @@ -287,15 +288,15 @@ public class TestCacheValidityPolicy {
> >>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
> >>           impl = new CacheValidityPolicy() {
> >>               @Override
> >> -            public long getCurrentAge(final HttpCacheEntry e, final
> Date
> >> d) {
> >> +            public TimeValue getCurrentAge(final HttpCacheEntry e,
> final
> >> Date d) {
> >>                   assertEquals(now, d);
> >>                   assertSame(entry, e);
> >> -                return 10;
> >> +                return TimeValue.ofSeconds(10);
> >>               }
> >>               @Override
> >> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
> >> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry
> e)
> >> {
> >>                   assertSame(entry, e);
> >> -                return 6;
> >> +                return TimeValue.ofSeconds(6);
> >>               }
> >>           };
> >>           assertFalse(impl.isResponseFresh(entry, now));
> >> @@ -364,21 +365,24 @@ public class TestCacheValidityPolicy {
> >>       public void testNegativeAgeHeaderValueReturnsMaxAge() {
> >>           final Header[] headers = new Header[] { new BasicHeader("Age",
> >> "-100") };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(CacheValidityPolicy.MAX_AGE,
> >> impl.getAgeValue(entry));
> >> +        // in seconds
> >> +        assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
> >> impl.getAgeValue(entry));
> >>       }
> >>
> >>       @Test
> >>       public void testMalformedAgeHeaderValueReturnsMaxAge() {
> >>           final Header[] headers = new Header[] { new BasicHeader("Age",
> >> "asdf") };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(CacheValidityPolicy.MAX_AGE,
> >> impl.getAgeValue(entry));
> >> +        // in seconds
> >> +        assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
> >> impl.getAgeValue(entry));
> >>       }
> >>
> >>       @Test
> >>       public void testMalformedCacheControlMaxAgeHeaderReturnsZero() {
> >>           final Header[] headers = new Header[] { new
> >> BasicHeader("Cache-Control", "max-age=asdf") };
> >>           final HttpCacheEntry entry =
> >> HttpTestUtils.makeCacheEntry(headers);
> >> -        assertEquals(0, impl.getMaxAge(entry));
> >> +        // in seconds
> >> +        assertEquals(0L, impl.getMaxAge(entry));
> >>       }
> >>
> >>       @Test
> >> diff --git
> >>
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> >>
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> >> index 42d7ce9..ace354b 100644
> >> ---
> >>
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> >> +++
> >>
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
> >> @@ -40,6 +40,7 @@ import
> org.apache.hc.client5.http.cache.HttpCacheEntry;
> >>   import org.apache.hc.core5.http.ClassicHttpRequest;
> >>   import org.apache.hc.core5.http.Header;
> >>   import org.apache.hc.core5.http.message.BasicHeader;
> >> +import org.apache.hc.core5.util.TimeValue;
> >>   import org.junit.Assert;
> >>   import org.junit.Before;
> >>   import org.junit.Test;
> >> @@ -108,7 +109,7 @@ public class TestCachedHttpResponseGenerator {
> >>
> >>       @Test
> >>       public void
> >> testAgeHeaderIsPopulatedWithCurrentAgeOfCacheEntryIfNonZero() throws
> >> Exception {
> >> -        currentAge(10L);
> >> +        currentAge(TimeValue.ofSeconds(10L));
> >>
> >>           final SimpleHttpResponse response =
> >> impl.generateResponse(request, entry);
> >>
> >> @@ -121,7 +122,7 @@ public class TestCachedHttpResponseGenerator {
> >>
> >>       @Test
> >>       public void
> >> testAgeHeaderIsNotPopulatedIfCurrentAgeOfCacheEntryIsZero() throws
> >> Exception {
> >> -        currentAge(0L);
> >> +        currentAge(TimeValue.ofSeconds(0L));
> >>
> >>           final SimpleHttpResponse response =
> >> impl.generateResponse(request, entry);
> >>
> >> @@ -133,7 +134,7 @@ public class TestCachedHttpResponseGenerator {
> >>
> >>       @Test
> >>       public void testAgeHeaderIsPopulatedWithMaxAgeIfCurrentAgeTooBig()
> >> throws Exception {
> >> -        currentAge(CacheValidityPolicy.MAX_AGE + 1L);
> >> +
> >> currentAge(TimeValue.ofSeconds(CacheValidityPolicy.MAX_AGE.toSeconds() +
> >> 1L));
> >>
> >>           final SimpleHttpResponse response =
> >> impl.generateResponse(request, entry);
> >>
> >> @@ -141,13 +142,13 @@ public class TestCachedHttpResponseGenerator {
> >>
> >>           final Header ageHdr = response.getFirstHeader("Age");
> >>           Assert.assertNotNull(ageHdr);
> >> -        Assert.assertEquals(CacheValidityPolicy.MAX_AGE,
> >> Long.parseLong(ageHdr.getValue()));
> >> +        Assert.assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
> >> Long.parseLong(ageHdr.getValue()));
> >>       }
> >>
> >> -    private void currentAge(final long sec) {
> >> +    private void currentAge(final TimeValue age) {
> >>           when(
> >>                   mockValidityPolicy.getCurrentAge(same(entry),
> >> -                        isA(Date.class))).thenReturn(sec);
> >> +                        isA(Date.class))).thenReturn(age);
> >>       }
> >>
> >>       @Test
> >> diff --git
> >>
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> >>
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> >> index 2faa5ed..2ddfbf7 100644
> >> ---
> >>
> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> >> +++
> >>
> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
> >> @@ -78,6 +78,7 @@ import
> >> org.apache.hc.core5.http.message.BasicClassicHttpResponse;
> >>   import org.apache.hc.core5.http.message.BasicHeader;
> >>   import org.apache.hc.core5.net.URIAuthority;
> >>   import org.apache.hc.core5.util.ByteArrayBuffer;
> >> +import org.apache.hc.core5.util.TimeValue;
> >>   import org.easymock.Capture;
> >>   import org.easymock.EasyMock;
> >>   import org.easymock.IExpectationSetters;
> >> @@ -315,7 +316,7 @@ public abstract class TestCachingExecChain {
> >>           cacheEntrySuitable(true);
> >>
> >>
>  responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
> >>           requestIsFatallyNonCompliant(null);
> >> -        entryHasStaleness(0L);
> >> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
> >>
> >>           replayMocks();
> >>           final ClassicHttpResponse result = execute(request);
> >> @@ -352,7 +353,7 @@ public abstract class TestCachingExecChain {
> >>           cacheEntrySuitable(true);
> >>           getCacheEntryReturns(mockCacheEntry);
> >>
> >>
>  responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
> >> -        entryHasStaleness(0L);
> >> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
> >>
> >>           replayMocks();
> >>           execute(request);
> >> @@ -1347,7 +1348,7 @@ public abstract class TestCachingExecChain {
> >>           getCacheEntryReturns(entry);
> >>           cacheEntrySuitable(true);
> >>
> >>
>  responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
> >> -        entryHasStaleness(0);
> >> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
> >>
> >>           replayMocks();
> >>           final ClassicHttpResponse resp = execute(request);
> >> @@ -1674,7 +1675,7 @@ public abstract class TestCachingExecChain {
> >>               .andReturn(suitable);
> >>       }
> >>
> >> -    private void entryHasStaleness(final long staleness) {
> >> +    private void entryHasStaleness(final TimeValue staleness) {
> >>           expect(
> >>               mockValidityPolicy.getStaleness((HttpCacheEntry)
> anyObject(),
> >> (Date) anyObject()))
> >>               .andReturn(staleness);
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
> For additional commands, e-mail: dev-help@hc.apache.org
>
>

Re: [httpcomponents-client] branch HTTPCLIENT-2036 updated: More work

Posted by Michael Osipov <mi...@apache.org>.
Am 2019-12-15 um 17:21 schrieb Gary Gregory:
> AFK, but 2147483648L looks like Integer.MAX_VALUE?

Almost, Integer.MAX_VALUE is one less: 2147483647.

M

> On Sun, Dec 15, 2019, 11:06 <mi...@apache.org> wrote:
> 
>> This is an automated email from the ASF dual-hosted git repository.
>>
>> michaelo pushed a commit to branch HTTPCLIENT-2036
>> in repository
>> https://gitbox.apache.org/repos/asf/httpcomponents-client.git
>>
>>
>> The following commit(s) were added to refs/heads/HTTPCLIENT-2036 by this
>> push:
>>       new 0a02e41  More work
>> 0a02e41 is described below
>>
>> commit 0a02e410505ecf347911ce00dbbc225417eea5e9
>> Author: Michael Osipov <mi...@apache.org>
>> AuthorDate: Sun Dec 15 16:52:53 2019 +0100
>>
>>      More work
>> ---
>>   .../http/impl/cache/CacheValidityPolicy.java       | 16 ++--
>>   .../impl/cache/CachedHttpResponseGenerator.java    | 11 +--
>>   .../cache/CachedResponseSuitabilityChecker.java    | 18 ++--
>>   .../client5/http/impl/cache/CachingExecBase.java   | 10 ++-
>>   .../http/impl/cache/TestCacheValidityPolicy.java   | 98
>> +++++++++++-----------
>>   .../cache/TestCachedHttpResponseGenerator.java     | 13 +--
>>   .../http/impl/cache/TestCachingExecChain.java      |  9 +-
>>   7 files changed, 94 insertions(+), 81 deletions(-)
>>
>> diff --git
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
>> index 5bd00d1..fdb0f7e 100644
>> ---
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
>> +++
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CacheValidityPolicy.java
>> @@ -43,14 +43,14 @@ import org.apache.hc.core5.util.TimeValue;
>>
>>   class CacheValidityPolicy {
>>
>> -    public static final long MAX_AGE = 2147483648L;
>> +    public static final TimeValue MAX_AGE =
>> TimeValue.ofSeconds(2147483648L);
>>
>>       CacheValidityPolicy() {
>>           super();
>>       }
>>
>>       public TimeValue getCurrentAge(final HttpCacheEntry entry, final Date
>> now) {
>> -        return
>> TimeValue.ofMilliseconds(getCorrectedInitialAge(entry).toMillis() +
>> getResidentTime(entry, now).toMillis());
>> +        return
>> TimeValue.ofSeconds(getCorrectedInitialAge(entry).toSeconds() +
>> getResidentTime(entry, now).toSeconds());
>>       }
>>
>>       public TimeValue getFreshnessLifetime(final HttpCacheEntry entry) {
>> @@ -73,7 +73,7 @@ class CacheValidityPolicy {
>>       }
>>
>>       public boolean isResponseFresh(final HttpCacheEntry entry, final Date
>> now) {
>> -        return (getCurrentAge(entry,
>> now).compareTo(getFreshnessLifetime(entry)) == -1);
>> +        return getCurrentAge(entry,
>> now).compareTo(getFreshnessLifetime(entry)) == -1;
>>       }
>>
>>       /**
>> @@ -92,7 +92,7 @@ class CacheValidityPolicy {
>>        */
>>       public boolean isResponseHeuristicallyFresh(final HttpCacheEntry
>> entry,
>>               final Date now, final float coefficient, final TimeValue
>> defaultLifetime) {
>> -        return (getCurrentAge(entry,
>> now).compareTo(getHeuristicFreshnessLifetime(entry, coefficient,
>> defaultLifetime)) == -1);
>> +        return getCurrentAge(entry,
>> now).compareTo(getHeuristicFreshnessLifetime(entry, coefficient,
>> defaultLifetime)) == -1;
>>       }
>>
>>       public TimeValue getHeuristicFreshnessLifetime(final HttpCacheEntry
>> entry,
>> @@ -199,7 +199,7 @@ class CacheValidityPolicy {
>>       protected TimeValue getApparentAge(final HttpCacheEntry entry) {
>>           final Date dateValue = entry.getDate();
>>           if (dateValue == null) {
>> -            return TimeValue.ofSeconds(MAX_AGE);
>> +            return MAX_AGE;
>>           }
>>           final long diff = entry.getResponseDate().getTime() -
>> dateValue.getTime();
>>           if (diff < 0L) {
>> @@ -216,10 +216,10 @@ class CacheValidityPolicy {
>>               try {
>>                   hdrAge = Long.parseLong(hdr.getValue());
>>                   if (hdrAge < 0) {
>> -                    hdrAge = MAX_AGE;
>> +                    hdrAge = MAX_AGE.toSeconds();
>>                   }
>>               } catch (final NumberFormatException nfe) {
>> -                hdrAge = MAX_AGE;
>> +                hdrAge = MAX_AGE.toSeconds();
>>               }
>>               ageValue = (hdrAge > ageValue) ? hdrAge : ageValue;
>>           }
>> @@ -284,7 +284,7 @@ class CacheValidityPolicy {
>>           if (age.compareTo(freshness) <= 0) {
>>               return TimeValue.ZERO_MILLISECONDS;
>>           }
>> -        return TimeValue.ofMilliseconds(age.toMillis() -
>> freshness.toMillis());
>> +        return TimeValue.ofSeconds(age.toSeconds() -
>> freshness.toSeconds());
>>       }
>>
>>
>> diff --git
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
>> index 1f8bd42..614ed0e 100644
>> ---
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
>> +++
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedHttpResponseGenerator.java
>> @@ -42,6 +42,7 @@ import org.apache.hc.core5.http.HttpResponse;
>>   import org.apache.hc.core5.http.HttpStatus;
>>   import org.apache.hc.core5.http.HttpVersion;
>>   import org.apache.hc.core5.http.message.BasicHeader;
>> +import org.apache.hc.core5.util.TimeValue;
>>
>>   /**
>>    * Rebuilds an {@link HttpResponse} from a {@link HttpCacheEntry}
>> @@ -78,12 +79,12 @@ class CachedHttpResponseGenerator {
>>               response.setBodyBytes(content, contentType);
>>           }
>>
>> -        final long age = this.validityStrategy.getCurrentAge(entry, now);
>> -        if (age > 0) {
>> -            if (age >= Integer.MAX_VALUE) {
>> -                response.setHeader(HeaderConstants.AGE, "2147483648");
>> +        final TimeValue age = this.validityStrategy.getCurrentAge(entry,
>> now);
>> +        if (TimeValue.isPositive(age)) {
>> +            if (age.compareTo(CacheValidityPolicy.MAX_AGE) >= 0) {
>> +                response.setHeader(HeaderConstants.AGE, "" +
>> CacheValidityPolicy.MAX_AGE.toSeconds());
>>               } else {
>> -                response.setHeader(HeaderConstants.AGE, "" + ((int) age));
>> +                response.setHeader(HeaderConstants.AGE, "" +
>> age.toSeconds());
>>               }
>>           }
>>
>> diff --git
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
>> index 9f2a33c..f680b5a 100644
>> ---
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
>> +++
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachedResponseSuitabilityChecker.java
>> @@ -75,7 +75,7 @@ class CachedResponseSuitabilityChecker {
>>               return true;
>>           }
>>           if (useHeuristicCaching &&
>> -                validityStrategy.isResponseHeuristicallyFresh(entry, now,
>> heuristicCoefficient, heuristicDefaultLifetime.toSeconds())) {
>> +                validityStrategy.isResponseHeuristicallyFresh(entry, now,
>> heuristicCoefficient, heuristicDefaultLifetime)) {
>>               return true;
>>           }
>>           if (originInsistsOnFreshness(entry)) {
>> @@ -85,7 +85,7 @@ class CachedResponseSuitabilityChecker {
>>           if (maxStale == -1) {
>>               return false;
>>           }
>> -        return (maxStale > validityStrategy.getStaleness(entry, now));
>> +        return (maxStale > validityStrategy.getStaleness(entry,
>> now).toSeconds());
>>       }
>>
>>       private boolean originInsistsOnFreshness(final HttpCacheEntry entry) {
>> @@ -100,6 +100,7 @@ class CachedResponseSuitabilityChecker {
>>       }
>>
>>       private long getMaxStale(final HttpRequest request) {
>> +        // This is a header value, we leave as-is
>>           long maxStale = -1;
>>           final Iterator<HeaderElement> it =
>> MessageSupport.iterate(request, HeaderConstants.CACHE_CONTROL);
>>           while (it.hasNext()) {
>> @@ -186,8 +187,9 @@ class CachedResponseSuitabilityChecker {
>>
>>               if
>> (HeaderConstants.CACHE_CONTROL_MAX_AGE.equals(elt.getName())) {
>>                   try {
>> +                    // in seconds
>>                       final int maxAge = Integer.parseInt(elt.getValue());
>> -                    if (validityStrategy.getCurrentAge(entry, now) >
>> maxAge) {
>> +                    if (validityStrategy.getCurrentAge(entry,
>> now).toSeconds() > maxAge) {
>>                           log.debug("Response from cache was not suitable
>> due to max age");
>>                           return false;
>>                       }
>> @@ -200,8 +202,9 @@ class CachedResponseSuitabilityChecker {
>>
>>               if
>> (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
>>                   try {
>> +                    // in seconds
>>                       final int maxStale = Integer.parseInt(elt.getValue());
>> -                    if (validityStrategy.getFreshnessLifetime(entry) >
>> maxStale) {
>> +                    if
>> (validityStrategy.getFreshnessLifetime(entry).toSeconds() > maxStale) {
>>                           log.debug("Response from cache was not suitable
>> due to max stale freshness");
>>                           return false;
>>                       }
>> @@ -214,13 +217,14 @@ class CachedResponseSuitabilityChecker {
>>
>>               if
>> (HeaderConstants.CACHE_CONTROL_MIN_FRESH.equals(elt.getName())) {
>>                   try {
>> +                    // in seconds
>>                       final long minFresh = Long.parseLong(elt.getValue());
>>                       if (minFresh < 0L) {
>>                           return false;
>>                       }
>> -                    final long age =
>> validityStrategy.getCurrentAge(entry, now);
>> -                    final long freshness =
>> validityStrategy.getFreshnessLifetime(entry);
>> -                    if (freshness - age < minFresh) {
>> +                    final TimeValue age =
>> validityStrategy.getCurrentAge(entry, now);
>> +                    final TimeValue freshness =
>> validityStrategy.getFreshnessLifetime(entry);
>> +                    if (freshness.toSeconds() - age.toSeconds() <
>> minFresh) {
>>                           log.debug("Response from cache was not suitable
>> due to min fresh " +
>>                                   "freshness requirement");
>>                           return false;
>> diff --git
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
>> index 2b7991d..e2ee72d 100644
>> ---
>> a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
>> +++
>> b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExecBase.java
>> @@ -54,6 +54,7 @@ import org.apache.hc.core5.http.ProtocolVersion;
>>   import org.apache.hc.core5.http.URIScheme;
>>   import org.apache.hc.core5.http.message.MessageSupport;
>>   import org.apache.hc.core5.http.protocol.HttpContext;
>> +import org.apache.hc.core5.util.TimeValue;
>>   import org.apache.hc.core5.util.VersionInfo;
>>   import org.slf4j.Logger;
>>   import org.slf4j.LoggerFactory;
>> @@ -189,7 +190,7 @@ public class CachingExecBase {
>>               cachedResponse = responseGenerator.generateResponse(request,
>> entry);
>>           }
>>           setResponseStatus(context, CacheResponseStatus.CACHE_HIT);
>> -        if (validityPolicy.getStaleness(entry, now) > 0L) {
>> +        if (TimeValue.isPositive(validityPolicy.getStaleness(entry,
>> now))) {
>>               cachedResponse.addHeader(HeaderConstants.WARNING,"110
>> localhost \"Response is stale\"");
>>           }
>>           return cachedResponse;
>> @@ -247,10 +248,11 @@ public class CachingExecBase {
>>               final HeaderElement elt = it.next();
>>               if
>> (HeaderConstants.CACHE_CONTROL_MAX_STALE.equals(elt.getName())) {
>>                   try {
>> +                    // in seconds
>>                       final int maxStale = Integer.parseInt(elt.getValue());
>> -                    final long age = validityPolicy.getCurrentAge(entry,
>> now);
>> -                    final long lifetime =
>> validityPolicy.getFreshnessLifetime(entry);
>> -                    if (age - lifetime > maxStale) {
>> +                    final TimeValue age =
>> validityPolicy.getCurrentAge(entry, now);
>> +                    final TimeValue lifetime =
>> validityPolicy.getFreshnessLifetime(entry);
>> +                    if (age.toSeconds() - lifetime.toSeconds() >
>> maxStale) {
>>                           return true;
>>                       }
>>                   } catch (final NumberFormatException nfe) {
>> diff --git
>> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
>> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
>> index 93922bb..2389644 100644
>> ---
>> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
>> +++
>> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCacheValidityPolicy.java
>> @@ -40,6 +40,7 @@ import org.apache.hc.core5.http.HttpHeaders;
>>   import org.apache.hc.core5.http.HttpRequest;
>>   import org.apache.hc.core5.http.message.BasicHeader;
>>   import org.apache.hc.core5.http.message.BasicHttpRequest;
>> +import org.apache.hc.core5.util.TimeValue;
>>   import org.junit.Before;
>>   import org.junit.Test;
>>
>> @@ -68,7 +69,7 @@ public class TestCacheValidityPolicy {
>>                   new BasicHeader("Server", "MockServer/1.0")
>>           };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(2147483648L, impl.getApparentAge(entry));
>> +        assertEquals(CacheValidityPolicy.MAX_AGE,
>> impl.getApparentAge(entry));
>>       }
>>
>>       @Test
>> @@ -77,15 +78,15 @@ public class TestCacheValidityPolicy {
>>                   .formatDate(tenSecondsAgo)) };
>>
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now,
>> sixSecondsAgo, headers);
>> -        assertEquals(4, impl.getApparentAge(entry));
>> +        assertEquals(TimeValue.ofSeconds(4), impl.getApparentAge(entry));
>>       }
>>
>>       @Test
>>       public void testNegativeApparentAgeIsBroughtUpToZero() {
>>           final Header[] headers = new Header[] { new BasicHeader("Date",
>> DateUtils
>>                   .formatDate(sixSecondsAgo)) };
>> -        final HttpCacheEntry entry  =
>> HttpTestUtils.makeCacheEntry(now,tenSecondsAgo,headers);
>> -        assertEquals(0, impl.getApparentAge(entry));
>> +        final HttpCacheEntry entry  = HttpTestUtils.makeCacheEntry(now,
>> tenSecondsAgo, headers);
>> +        assertEquals(TimeValue.ofSeconds(0), impl.getApparentAge(entry));
>>       }
>>
>>       @Test
>> @@ -94,11 +95,11 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            protected long getApparentAge(final HttpCacheEntry ent) {
>> -                return 6;
>> +            protected TimeValue getApparentAge(final HttpCacheEntry ent) {
>> +                return TimeValue.ofSeconds(6);
>>               }
>>           };
>> -        assertEquals(10, impl.getCorrectedReceivedAge(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getCorrectedReceivedAge(entry));
>>       }
>>
>>       @Test
>> @@ -107,17 +108,17 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            protected long getApparentAge(final HttpCacheEntry ent) {
>> -                return 10;
>> +            protected TimeValue getApparentAge(final HttpCacheEntry ent) {
>> +                return TimeValue.ofSeconds(10);
>>               }
>>           };
>> -        assertEquals(10, impl.getCorrectedReceivedAge(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getCorrectedReceivedAge(entry));
>>       }
>>
>>       @Test
>>       public void
>> testResponseDelayIsDifferenceBetweenResponseAndRequestTimes() {
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(tenSecondsAgo, sixSecondsAgo);
>> -        assertEquals(4, impl.getResponseDelay(entry));
>> +        assertEquals(TimeValue.ofSeconds(4),
>> impl.getResponseDelay(entry));
>>       }
>>
>>       @Test
>> @@ -125,22 +126,22 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            protected long getCorrectedReceivedAge(final HttpCacheEntry
>> ent) {
>> -                return 7;
>> +            protected TimeValue getCorrectedReceivedAge(final
>> HttpCacheEntry ent) {
>> +                return TimeValue.ofSeconds(7);
>>               }
>>
>>               @Override
>> -            protected long getResponseDelay(final HttpCacheEntry ent) {
>> -                return 13;
>> +            protected TimeValue getResponseDelay(final HttpCacheEntry
>> ent) {
>> +                return TimeValue.ofSeconds(13);
>>               }
>>           };
>> -        assertEquals(20, impl.getCorrectedInitialAge(entry));
>> +        assertEquals(TimeValue.ofSeconds(20),
>> impl.getCorrectedInitialAge(entry));
>>       }
>>
>>       @Test
>>       public void testResidentTimeSecondsIsTimeSinceResponseTime() {
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(now,
>> sixSecondsAgo);
>> -        assertEquals(6, impl.getResidentTime(entry, now));
>> +        assertEquals(TimeValue.ofSeconds(6), impl.getResidentTime(entry,
>> now));
>>       }
>>
>>       @Test
>> @@ -148,29 +149,29 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            protected long getCorrectedInitialAge(final HttpCacheEntry
>> ent) {
>> -                return 11;
>> +            protected TimeValue getCorrectedInitialAge(final
>> HttpCacheEntry ent) {
>> +                return TimeValue.ofSeconds(11);
>>               }
>>               @Override
>> -            protected long getResidentTime(final HttpCacheEntry ent,
>> final Date d) {
>> -                return 17;
>> +            protected TimeValue getResidentTime(final HttpCacheEntry ent,
>> final Date d) {
>> +                return TimeValue.ofSeconds(17);
>>               }
>>           };
>> -        assertEquals(28, impl.getCurrentAge(entry, new Date()));
>> +        assertEquals(TimeValue.ofSeconds(28), impl.getCurrentAge(entry,
>> new Date()));
>>       }
>>
>>       @Test
>>       public void testFreshnessLifetimeIsSMaxAgeIfPresent() {
>>           final Header[] headers = new Header[] { new
>> BasicHeader("Cache-Control", "s-maxage=10") };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(10, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getFreshnessLifetime(entry));
>>       }
>>
>>       @Test
>>       public void testFreshnessLifetimeIsMaxAgeIfPresent() {
>>           final Header[] headers = new Header[] { new
>> BasicHeader("Cache-Control", "max-age=10") };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(10, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getFreshnessLifetime(entry));
>>       }
>>
>>       @Test
>> @@ -178,12 +179,12 @@ public class TestCacheValidityPolicy {
>>           Header[] headers = new Header[] { new
>> BasicHeader("Cache-Control", "max-age=10"),
>>                   new BasicHeader("Cache-Control", "s-maxage=20") };
>>           HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(10, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getFreshnessLifetime(entry));
>>
>>           headers = new Header[] { new BasicHeader("Cache-Control",
>> "max-age=20"),
>>                   new BasicHeader("Cache-Control", "s-maxage=10") };
>>           entry = HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(10, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getFreshnessLifetime(entry));
>>       }
>>
>>       @Test
>> @@ -193,7 +194,7 @@ public class TestCacheValidityPolicy {
>>                   new BasicHeader("Expires",
>> DateUtils.formatDate(sixSecondsAgo)) };
>>
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(10, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getFreshnessLifetime(entry));
>>       }
>>
>>       @Test
>> @@ -203,7 +204,7 @@ public class TestCacheValidityPolicy {
>>                   new BasicHeader("Expires",
>> DateUtils.formatDate(sixSecondsAgo)) };
>>
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(10, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(10),
>> impl.getFreshnessLifetime(entry));
>>       }
>>
>>       @Test
>> @@ -213,7 +214,7 @@ public class TestCacheValidityPolicy {
>>                   new BasicHeader("Expires",
>> DateUtils.formatDate(sixSecondsAgo)) };
>>
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(4, impl.getFreshnessLifetime(entry));
>> +        assertEquals(TimeValue.ofSeconds(4),
>> impl.getFreshnessLifetime(entry));
>>       }
>>
>>       @Test
>> @@ -223,12 +224,12 @@ public class TestCacheValidityPolicy {
>>                   new BasicHeader("Last-Modified",
>> DateUtils.formatDate(elevenSecondsAgo))
>>           };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(1, impl.getHeuristicFreshnessLifetime(entry, 0.1f,
>> 0));
>> +        assertEquals(TimeValue.ofSeconds(1),
>> impl.getHeuristicFreshnessLifetime(entry, 0.1f,
>> TimeValue.ZERO_MILLISECONDS));
>>       }
>>
>>       @Test
>>       public void testHeuristicFreshnessLifetimeDefaultsProperly() {
>> -        final long defaultFreshness = 10;
>> +        final TimeValue defaultFreshness = TimeValue.ofSeconds(10);
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>>           assertEquals(defaultFreshness,
>> impl.getHeuristicFreshnessLifetime(entry, 0.1f, defaultFreshness));
>>       }
>> @@ -241,7 +242,7 @@ public class TestCacheValidityPolicy {
>>           };
>>
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertTrue(impl.getHeuristicFreshnessLifetime(entry, 0.1f, 10) >=
>> 0);
>> +
>> assertTrue(TimeValue.isNonNegative(impl.getHeuristicFreshnessLifetime(entry,
>> 0.1f, TimeValue.ofSeconds(10))));
>>       }
>>
>>       @Test
>> @@ -249,15 +250,15 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            public long getCurrentAge(final HttpCacheEntry e, final Date
>> d) {
>> +            public TimeValue getCurrentAge(final HttpCacheEntry e, final
>> Date d) {
>>                   assertSame(entry, e);
>>                   assertEquals(now, d);
>> -                return 6;
>> +                return TimeValue.ofSeconds(6);
>>               }
>>               @Override
>> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
>> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry e)
>> {
>>                   assertSame(entry, e);
>> -                return 10;
>> +                return TimeValue.ofSeconds(10);
>>               }
>>           };
>>           assertTrue(impl.isResponseFresh(entry, now));
>> @@ -268,15 +269,15 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            public long getCurrentAge(final HttpCacheEntry e, final Date
>> d) {
>> +            public TimeValue getCurrentAge(final HttpCacheEntry e, final
>> Date d) {
>>                   assertEquals(now, d);
>>                   assertSame(entry, e);
>> -                return 6;
>> +                return TimeValue.ofSeconds(6);
>>               }
>>               @Override
>> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
>> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry e)
>> {
>>                   assertSame(entry, e);
>> -                return 6;
>> +                return TimeValue.ofSeconds(6);
>>               }
>>           };
>>           assertFalse(impl.isResponseFresh(entry, now));
>> @@ -287,15 +288,15 @@ public class TestCacheValidityPolicy {
>>           final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry();
>>           impl = new CacheValidityPolicy() {
>>               @Override
>> -            public long getCurrentAge(final HttpCacheEntry e, final Date
>> d) {
>> +            public TimeValue getCurrentAge(final HttpCacheEntry e, final
>> Date d) {
>>                   assertEquals(now, d);
>>                   assertSame(entry, e);
>> -                return 10;
>> +                return TimeValue.ofSeconds(10);
>>               }
>>               @Override
>> -            public long getFreshnessLifetime(final HttpCacheEntry e) {
>> +            public TimeValue getFreshnessLifetime(final HttpCacheEntry e)
>> {
>>                   assertSame(entry, e);
>> -                return 6;
>> +                return TimeValue.ofSeconds(6);
>>               }
>>           };
>>           assertFalse(impl.isResponseFresh(entry, now));
>> @@ -364,21 +365,24 @@ public class TestCacheValidityPolicy {
>>       public void testNegativeAgeHeaderValueReturnsMaxAge() {
>>           final Header[] headers = new Header[] { new BasicHeader("Age",
>> "-100") };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(CacheValidityPolicy.MAX_AGE,
>> impl.getAgeValue(entry));
>> +        // in seconds
>> +        assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
>> impl.getAgeValue(entry));
>>       }
>>
>>       @Test
>>       public void testMalformedAgeHeaderValueReturnsMaxAge() {
>>           final Header[] headers = new Header[] { new BasicHeader("Age",
>> "asdf") };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(CacheValidityPolicy.MAX_AGE,
>> impl.getAgeValue(entry));
>> +        // in seconds
>> +        assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
>> impl.getAgeValue(entry));
>>       }
>>
>>       @Test
>>       public void testMalformedCacheControlMaxAgeHeaderReturnsZero() {
>>           final Header[] headers = new Header[] { new
>> BasicHeader("Cache-Control", "max-age=asdf") };
>>           final HttpCacheEntry entry =
>> HttpTestUtils.makeCacheEntry(headers);
>> -        assertEquals(0, impl.getMaxAge(entry));
>> +        // in seconds
>> +        assertEquals(0L, impl.getMaxAge(entry));
>>       }
>>
>>       @Test
>> diff --git
>> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
>> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
>> index 42d7ce9..ace354b 100644
>> ---
>> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
>> +++
>> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachedHttpResponseGenerator.java
>> @@ -40,6 +40,7 @@ import org.apache.hc.client5.http.cache.HttpCacheEntry;
>>   import org.apache.hc.core5.http.ClassicHttpRequest;
>>   import org.apache.hc.core5.http.Header;
>>   import org.apache.hc.core5.http.message.BasicHeader;
>> +import org.apache.hc.core5.util.TimeValue;
>>   import org.junit.Assert;
>>   import org.junit.Before;
>>   import org.junit.Test;
>> @@ -108,7 +109,7 @@ public class TestCachedHttpResponseGenerator {
>>
>>       @Test
>>       public void
>> testAgeHeaderIsPopulatedWithCurrentAgeOfCacheEntryIfNonZero() throws
>> Exception {
>> -        currentAge(10L);
>> +        currentAge(TimeValue.ofSeconds(10L));
>>
>>           final SimpleHttpResponse response =
>> impl.generateResponse(request, entry);
>>
>> @@ -121,7 +122,7 @@ public class TestCachedHttpResponseGenerator {
>>
>>       @Test
>>       public void
>> testAgeHeaderIsNotPopulatedIfCurrentAgeOfCacheEntryIsZero() throws
>> Exception {
>> -        currentAge(0L);
>> +        currentAge(TimeValue.ofSeconds(0L));
>>
>>           final SimpleHttpResponse response =
>> impl.generateResponse(request, entry);
>>
>> @@ -133,7 +134,7 @@ public class TestCachedHttpResponseGenerator {
>>
>>       @Test
>>       public void testAgeHeaderIsPopulatedWithMaxAgeIfCurrentAgeTooBig()
>> throws Exception {
>> -        currentAge(CacheValidityPolicy.MAX_AGE + 1L);
>> +
>> currentAge(TimeValue.ofSeconds(CacheValidityPolicy.MAX_AGE.toSeconds() +
>> 1L));
>>
>>           final SimpleHttpResponse response =
>> impl.generateResponse(request, entry);
>>
>> @@ -141,13 +142,13 @@ public class TestCachedHttpResponseGenerator {
>>
>>           final Header ageHdr = response.getFirstHeader("Age");
>>           Assert.assertNotNull(ageHdr);
>> -        Assert.assertEquals(CacheValidityPolicy.MAX_AGE,
>> Long.parseLong(ageHdr.getValue()));
>> +        Assert.assertEquals(CacheValidityPolicy.MAX_AGE.toSeconds(),
>> Long.parseLong(ageHdr.getValue()));
>>       }
>>
>> -    private void currentAge(final long sec) {
>> +    private void currentAge(final TimeValue age) {
>>           when(
>>                   mockValidityPolicy.getCurrentAge(same(entry),
>> -                        isA(Date.class))).thenReturn(sec);
>> +                        isA(Date.class))).thenReturn(age);
>>       }
>>
>>       @Test
>> diff --git
>> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
>> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
>> index 2faa5ed..2ddfbf7 100644
>> ---
>> a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
>> +++
>> b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestCachingExecChain.java
>> @@ -78,6 +78,7 @@ import
>> org.apache.hc.core5.http.message.BasicClassicHttpResponse;
>>   import org.apache.hc.core5.http.message.BasicHeader;
>>   import org.apache.hc.core5.net.URIAuthority;
>>   import org.apache.hc.core5.util.ByteArrayBuffer;
>> +import org.apache.hc.core5.util.TimeValue;
>>   import org.easymock.Capture;
>>   import org.easymock.EasyMock;
>>   import org.easymock.IExpectationSetters;
>> @@ -315,7 +316,7 @@ public abstract class TestCachingExecChain {
>>           cacheEntrySuitable(true);
>>
>>   responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
>>           requestIsFatallyNonCompliant(null);
>> -        entryHasStaleness(0L);
>> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
>>
>>           replayMocks();
>>           final ClassicHttpResponse result = execute(request);
>> @@ -352,7 +353,7 @@ public abstract class TestCachingExecChain {
>>           cacheEntrySuitable(true);
>>           getCacheEntryReturns(mockCacheEntry);
>>
>>   responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
>> -        entryHasStaleness(0L);
>> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
>>
>>           replayMocks();
>>           execute(request);
>> @@ -1347,7 +1348,7 @@ public abstract class TestCachingExecChain {
>>           getCacheEntryReturns(entry);
>>           cacheEntrySuitable(true);
>>
>>   responseIsGeneratedFromCache(SimpleHttpResponse.create(HttpStatus.SC_OK));
>> -        entryHasStaleness(0);
>> +        entryHasStaleness(TimeValue.ZERO_MILLISECONDS);
>>
>>           replayMocks();
>>           final ClassicHttpResponse resp = execute(request);
>> @@ -1674,7 +1675,7 @@ public abstract class TestCachingExecChain {
>>               .andReturn(suitable);
>>       }
>>
>> -    private void entryHasStaleness(final long staleness) {
>> +    private void entryHasStaleness(final TimeValue staleness) {
>>           expect(
>>               mockValidityPolicy.getStaleness((HttpCacheEntry) anyObject(),
>> (Date) anyObject()))
>>               .andReturn(staleness);
>>
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org