hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [09/10] httpcomponents-core git commit: Removed dependency on System#currentTimeMillis from TestPoolEntry
Date Wed, 09 Aug 2017 18:15:18 GMT
Removed dependency on System#currentTimeMillis from TestPoolEntry


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/3fb67cb5
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/3fb67cb5
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/3fb67cb5

Branch: refs/heads/master
Commit: 3fb67cb5aa83e15500a839eaa187b8bd1d880587
Parents: c43586f
Author: Oleg Kalnichevski <olegk@apache.org>
Authored: Wed Aug 9 16:34:14 2017 +0200
Committer: Oleg Kalnichevski <olegk@apache.org>
Committed: Wed Aug 9 16:34:14 2017 +0200

----------------------------------------------------------------------
 .../org/apache/hc/core5/pool/PoolEntry.java     | 29 +++++++++-----
 .../org/apache/hc/core5/pool/TestPoolEntry.java | 42 ++++++++++++++++----
 2 files changed, 53 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/3fb67cb5/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java
index 3656de0..376b057 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java
@@ -26,10 +26,9 @@
  */
 package org.apache.hc.core5.pool;
 
-import static java.lang.System.currentTimeMillis;
-
 import java.util.concurrent.atomic.AtomicReference;
 
+import org.apache.hc.core5.function.Supplier;
 import org.apache.hc.core5.io.GracefullyCloseable;
 import org.apache.hc.core5.io.ShutdownType;
 import org.apache.hc.core5.util.Args;
@@ -58,6 +57,19 @@ public final class PoolEntry<T, C extends GracefullyCloseable> {
     private volatile long updated;
     private volatile long expiry;
     private volatile long validityDeadline;
+    private Supplier<Long> currentTimeSupplier;
+
+    PoolEntry(final T route, final TimeValue timeToLive, final Supplier<Long> currentTimeSupplier)
{
+        super();
+        this.route = Args.notNull(route, "Route");
+        this.timeToLive = TimeValue.defaultsToNegativeOneMillisecond(timeToLive);
+        this.connRef = new AtomicReference<>(null);
+        this.currentTimeSupplier = currentTimeSupplier;
+    }
+
+    long getCurrentTime() {
+        return currentTimeSupplier != null ? currentTimeSupplier.get() : System.currentTimeMillis();
+    }
 
     /**
      * Creates new {@code PoolEntry} instance.
@@ -67,10 +79,7 @@ public final class PoolEntry<T, C extends GracefullyCloseable> {
      *   does not have an expiry deadline.
      */
     public PoolEntry(final T route, final TimeValue timeToLive) {
-        super();
-        this.route = Args.notNull(route, "Route");
-        this.timeToLive = TimeValue.defaultsToNegativeOneMillisecond(timeToLive);
-        this.connRef = new AtomicReference<>(null);
+        this(route, timeToLive, null);
     }
 
     public PoolEntry(final T route) {
@@ -117,7 +126,7 @@ public final class PoolEntry<T, C extends GracefullyCloseable> {
     public void assignConnection(final C conn) {
         Args.notNull(conn, "connection");
         if (this.connRef.compareAndSet(null, conn)) {
-            this.created = currentTimeMillis();
+            this.created = getCurrentTime();
             this.updated = this.created;
             this.validityDeadline = TimeValue.calculateDeadline(this.created, this.timeToLive);
             this.expiry = this.validityDeadline;
@@ -147,9 +156,9 @@ public final class PoolEntry<T, C extends GracefullyCloseable> {
      */
     public void updateExpiry(final TimeValue expiryTime) {
         Args.notNull(expiryTime, "Expiry time");
-        final long currentTime = System.currentTimeMillis();
+        final long currentTime = getCurrentTime();
         final long newExpiry = TimeValue.calculateDeadline(currentTime, expiryTime);
-        this.expiry = Math.min(newExpiry, getValidityDeadline());
+        this.expiry = Math.min(newExpiry, this.validityDeadline);
         this.updated = currentTime;
     }
 
@@ -158,7 +167,7 @@ public final class PoolEntry<T, C extends GracefullyCloseable> {
      */
     public void updateState(final Object state) {
         this.state = state;
-        this.updated = System.currentTimeMillis();
+        this.updated = getCurrentTime();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/3fb67cb5/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolEntry.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolEntry.java b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolEntry.java
index ba4d14a..0de60ca 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolEntry.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestPoolEntry.java
@@ -27,19 +27,39 @@
 package org.apache.hc.core5.pool;
 
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.hc.core5.function.Supplier;
 import org.apache.hc.core5.http.HttpConnection;
 import org.apache.hc.core5.io.ShutdownType;
 import org.apache.hc.core5.util.TimeValue;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 
 public class TestPoolEntry {
 
+    private AtomicLong count;
+    private Supplier<Long> currentTimeSupplier;
+
+    @Before
+    public void setup() {
+        count = new AtomicLong(1);
+        currentTimeSupplier = new Supplier<Long>() {
+
+            @Override
+            public Long get() {
+                return count.addAndGet(1);
+            }
+
+        };
+    }
+
     @Test
     public void testBasics() throws Exception {
-        final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>("route1",
TimeValue.of(10L, TimeUnit.MILLISECONDS));
+        final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
+                "route1", TimeValue.of(10L, TimeUnit.MILLISECONDS), currentTimeSupplier);
 
         Assert.assertEquals("route1", entry1.getRoute());
         Assert.assertEquals(0, entry1.getUpdated());
@@ -64,7 +84,8 @@ public class TestPoolEntry {
 
     @Test
     public void testValidInfinitely() throws Exception {
-        final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>("route1",
TimeValue.ZERO_MILLISECONDS);
+        final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
+                "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
         entry1.assignConnection(Mockito.mock(HttpConnection.class));
         Assert.assertEquals(Long.MAX_VALUE, entry1.getValidityDeadline());
         Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiry());
@@ -72,7 +93,8 @@ public class TestPoolEntry {
 
     @Test
     public void testExpiry() throws Exception {
-        final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>("route1",
TimeValue.ZERO_MILLISECONDS);
+        final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
+                "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
         entry1.assignConnection(Mockito.mock(HttpConnection.class));
         Assert.assertEquals(Long.MAX_VALUE, entry1.getExpiry());
         entry1.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
@@ -80,24 +102,28 @@ public class TestPoolEntry {
         entry1.updateExpiry(TimeValue.ZERO_MILLISECONDS);
         Assert.assertEquals(Long.MAX_VALUE, entry1.getExpiry());
 
-        final PoolEntry<String, HttpConnection> entry2 = new PoolEntry<>("route1",
TimeValue.of(100L, TimeUnit.MILLISECONDS));
+        final PoolEntry<String, HttpConnection> entry2 = new PoolEntry<>(
+                "route1", TimeValue.of(100L, TimeUnit.MILLISECONDS), currentTimeSupplier);
         entry2.assignConnection(Mockito.mock(HttpConnection.class));
-        Assert.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiry());
-        entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS));
+        final long validityDeadline = entry2.getValidityDeadline();
         Assert.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiry());
         entry2.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
         Assert.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiry());
+        entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS));
+        Assert.assertEquals(validityDeadline, entry2.getExpiry());
     }
 
     @Test(expected=IllegalArgumentException.class)
     public void testInvalidExpiry() throws Exception {
-        final PoolEntry<String, HttpConnection> entry = new PoolEntry<>("route1",
TimeValue.of(0L, TimeUnit.MILLISECONDS));
+        final PoolEntry<String, HttpConnection> entry = new PoolEntry<>(
+                "route1", TimeValue.of(0L, TimeUnit.MILLISECONDS), currentTimeSupplier);
         entry.updateExpiry(null);
     }
 
     @Test
     public void testExpiryDoesNotOverflow() {
-        final PoolEntry<String, HttpConnection> entry = new PoolEntry<>("route1",
TimeValue.of(Long.MAX_VALUE, TimeUnit.MILLISECONDS));
+        final PoolEntry<String, HttpConnection> entry = new PoolEntry<>(
+                "route1", TimeValue.of(Long.MAX_VALUE, TimeUnit.MILLISECONDS), currentTimeSupplier);
         entry.assignConnection(Mockito.mock(HttpConnection.class));
         Assert.assertEquals(Long.MAX_VALUE, entry.getValidityDeadline());
     }


Mime
View raw message