You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2017/03/31 22:39:45 UTC

svn commit: r1789758 - in /httpcomponents/httpcore/trunk/httpcore5/src: main/java/org/apache/hc/core5/http/impl/bootstrap/ main/java/org/apache/hc/core5/util/ test/java/org/apache/hc/core5/util/

Author: ggregory
Date: Fri Mar 31 22:39:45 2017
New Revision: 1789758

URL: http://svn.apache.org/viewvc?rev=1789758&view=rev
Log:
HTTPCORE-451: Add a TimeValue class to wrap a long and a TimeUnit. Add unit test. Fix checkstyles.

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java?rev=1789758&r1=1789757&r2=1789758&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java Fri Mar 31 22:39:45 2017
@@ -65,7 +65,6 @@ import org.apache.hc.core5.pool.ConnPool
 import org.apache.hc.core5.pool.ControlledConnPool;
 import org.apache.hc.core5.pool.PoolEntry;
 import org.apache.hc.core5.util.Args;
-import org.apache.hc.core5.util.TimeValue;
 
 /**
  * @since 5.0
@@ -203,7 +202,7 @@ public class HttpRequester implements Au
                 final Socket socket = createSocket(targetHost);
                 connection = connectFactory.createConnection(socket);
                 poolEntry.assignConnection(connection);
-                socket.connect(toEndpoint(targetHost), TimeValue.asBoundInt(socketConfig.getConnectTimeout().toMillis()));
+                socket.connect(toEndpoint(targetHost), socketConfig.getConnectTimeout().toMillisIntBound());
             }
             final ClassicHttpResponse response = execute(connection, request, context);
             final HttpEntity entity = response.getEntity();

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java?rev=1789758&r1=1789757&r2=1789758&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java Fri Mar 31 22:39:45 2017
@@ -37,7 +37,6 @@ import org.apache.hc.core5.http.config.S
 import org.apache.hc.core5.http.impl.io.HttpService;
 import org.apache.hc.core5.http.io.HttpConnectionFactory;
 import org.apache.hc.core5.http.io.HttpServerConnection;
-import org.apache.hc.core5.util.TimeValue;
 
 /**
  * @since 4.4
@@ -73,7 +72,7 @@ class RequestListener implements Runnabl
         try {
             while (!isTerminated() && !Thread.interrupted()) {
                 final Socket socket = this.serversocket.accept();
-                socket.setSoTimeout(TimeValue.asBoundInt(this.socketConfig.getSoTimeout().toMillis()));
+                socket.setSoTimeout(this.socketConfig.getSoTimeout().toMillisIntBound());
                 socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
                 socket.setTcpNoDelay(this.socketConfig.isTcpNoDelay());
                 if (this.socketConfig.getRcvBufSize() > 0) {
@@ -83,7 +82,7 @@ class RequestListener implements Runnabl
                     socket.setSendBufferSize(this.socketConfig.getSndBufSize());
                 }
                 if (this.socketConfig.getSoLinger().toSeconds() >= 0) {
-                    socket.setSoLinger(true, TimeValue.asBoundInt(this.socketConfig.getSoLinger().toSeconds()));
+                    socket.setSoLinger(true, this.socketConfig.getSoLinger().toSecondsIntBound());
                 }
                 final HttpServerConnection conn = this.connectionFactory.createConnection(socket);
                 final Worker worker = new Worker(this.httpService, conn, this.exceptionListener);

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java?rev=1789758&r1=1789757&r2=1789758&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java Fri Mar 31 22:39:45 2017
@@ -95,6 +95,10 @@ public class TimeValue {
         return timeUnit.toMillis(duration);
     }
 
+    public int toMillisIntBound() {
+        return asBoundInt(toMillis());
+    }
+
     public long toMinutes() {
         return timeUnit.toMinutes(duration);
     }
@@ -107,6 +111,10 @@ public class TimeValue {
         return timeUnit.toSeconds(duration);
     }
 
+    public int toSecondsIntBound() {
+        return asBoundInt(toSeconds());
+    }
+
     @Override
     public String toString() {
         return String.format("%,d %s", Long.valueOf(duration), timeUnit);

Added: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java?rev=1789758&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeValue.java Fri Mar 31 22:39:45 2017
@@ -0,0 +1,102 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.util;
+
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestTimeValue {
+
+    private void checkToSeconds(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toSeconds(value), new TimeValue(value, timeUnit).toSeconds());
+    }
+
+    private void checkToDays(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toDays(value), new TimeValue(value, timeUnit).toDays());
+    }
+
+    private void checkToHours(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toHours(value), new TimeValue(value, timeUnit).toHours());
+    }
+
+    private void checkToMinutes(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toMinutes(value), new TimeValue(value, timeUnit).toMinutes());
+    }
+
+    private void checkToMillis(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toMillis(value), new TimeValue(value, timeUnit).toMillis());
+    }
+
+    private void checkToMicros(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toMicros(value), new TimeValue(value, timeUnit).toMicros());
+    }
+
+    private void checkToNanos(final long value, final TimeUnit timeUnit) {
+        Assert.assertEquals(timeUnit.toNanos(value), new TimeValue(value, timeUnit).toNanos());
+    }
+
+    @Test
+    public void test0() {
+        test(0);
+    }
+
+    @Test
+    public void test1() {
+        test(1);
+    }
+
+    @Test
+    public void test1Negative1() {
+        test(-1);
+    }
+
+    @Test
+    public void testMaxInt() {
+        test(Integer.MAX_VALUE);
+    }
+
+    @Test
+    public void testMaxLong() {
+        test(Long.MAX_VALUE);
+    }
+
+    private void test(final long value) {
+        for (final TimeUnit timeUnit : TimeUnit.values()) {
+            checkToDays(value, timeUnit);
+            checkToHours(value, timeUnit);
+            checkToMinutes(value, timeUnit);
+            checkToSeconds(value, timeUnit);
+            checkToMillis(value, timeUnit);
+            checkToMicros(value, timeUnit);
+            checkToNanos(value, timeUnit);
+        }
+    }
+
+}