View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.pool;
28  
29  import java.util.concurrent.TimeUnit;
30  import java.util.concurrent.atomic.AtomicLong;
31  
32  import org.apache.hc.core5.function.Supplier;
33  import org.apache.hc.core5.http.HttpConnection;
34  import org.apache.hc.core5.io.ShutdownType;
35  import org.apache.hc.core5.util.TimeValue;
36  import org.junit.Assert;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.mockito.Mockito;
40  
41  public class TestPoolEntry {
42  
43      private AtomicLong count;
44      private Supplier<Long> currentTimeSupplier;
45  
46      @Before
47      public void setup() {
48          count = new AtomicLong(1);
49          currentTimeSupplier = new Supplier<Long>() {
50  
51              @Override
52              public Long get() {
53                  return count.addAndGet(1);
54              }
55  
56          };
57      }
58  
59      @Test
60      public void testBasics() throws Exception {
61          final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
62                  "route1", TimeValue.of(10L, TimeUnit.MILLISECONDS), currentTimeSupplier);
63  
64          Assert.assertEquals("route1", entry1.getRoute());
65          Assert.assertEquals(0, entry1.getUpdated());
66          Assert.assertEquals(0, entry1.getExpiry());
67  
68          entry1.assignConnection(Mockito.mock(HttpConnection.class));
69          final long now = System.currentTimeMillis();
70          Assert.assertEquals("route1", entry1.getRoute());
71          Assert.assertTrue(now >= entry1.getUpdated());
72          Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiry());
73          Assert.assertEquals(entry1.getUpdated() + 10L, entry1.getValidityDeadline());
74  
75          entry1.discardConnection(ShutdownType.IMMEDIATE);
76          Assert.assertEquals(0, entry1.getUpdated());
77          Assert.assertEquals(0, entry1.getExpiry());
78      }
79  
80      @Test(expected = IllegalArgumentException.class)
81      public void testInvalidConstruction() throws Exception {
82          new PoolEntry<String, HttpConnection>(null);
83      }
84  
85      @Test
86      public void testValidInfinitely() throws Exception {
87          final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
88                  "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
89          entry1.assignConnection(Mockito.mock(HttpConnection.class));
90          Assert.assertEquals(Long.MAX_VALUE, entry1.getValidityDeadline());
91          Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiry());
92      }
93  
94      @Test
95      public void testExpiry() throws Exception {
96          final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
97                  "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
98          entry1.assignConnection(Mockito.mock(HttpConnection.class));
99          Assert.assertEquals(Long.MAX_VALUE, entry1.getExpiry());
100         entry1.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
101         Assert.assertEquals(entry1.getUpdated() + 50L, entry1.getExpiry());
102         entry1.updateExpiry(TimeValue.ZERO_MILLISECONDS);
103         Assert.assertEquals(Long.MAX_VALUE, entry1.getExpiry());
104 
105         final PoolEntry<String, HttpConnection> entry2 = new PoolEntry<>(
106                 "route1", TimeValue.of(100L, TimeUnit.MILLISECONDS), currentTimeSupplier);
107         entry2.assignConnection(Mockito.mock(HttpConnection.class));
108         final long validityDeadline = entry2.getValidityDeadline();
109         Assert.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiry());
110         entry2.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
111         Assert.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiry());
112         entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS));
113         Assert.assertEquals(validityDeadline, entry2.getExpiry());
114     }
115 
116     @Test(expected=IllegalArgumentException.class)
117     public void testInvalidExpiry() throws Exception {
118         final PoolEntry<String, HttpConnection> entry = new PoolEntry<>(
119                 "route1", TimeValue.of(0L, TimeUnit.MILLISECONDS), currentTimeSupplier);
120         entry.updateExpiry(null);
121     }
122 
123     @Test
124     public void testExpiryDoesNotOverflow() {
125         final PoolEntry<String, HttpConnection> entry = new PoolEntry<>(
126                 "route1", TimeValue.of(Long.MAX_VALUE, TimeUnit.MILLISECONDS), currentTimeSupplier);
127         entry.assignConnection(Mockito.mock(HttpConnection.class));
128         Assert.assertEquals(Long.MAX_VALUE, entry.getValidityDeadline());
129     }
130 
131 }