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.CloseMode;
35  import org.apache.hc.core5.util.Deadline;
36  import org.apache.hc.core5.util.TimeValue;
37  import org.junit.Assert;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.mockito.Mockito;
41  
42  public class TestPoolEntry {
43  
44      private AtomicLong count;
45      private Supplier<Long> currentTimeSupplier;
46  
47      @Before
48      public void setup() {
49          count = new AtomicLong(1);
50          currentTimeSupplier = new Supplier<Long>() {
51  
52              @Override
53              public Long get() {
54                  return count.addAndGet(1);
55              }
56  
57          };
58      }
59  
60      @Test
61      public void testBasics() throws Exception {
62          final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
63                  "route1", TimeValue.of(10L, TimeUnit.MILLISECONDS), currentTimeSupplier);
64  
65          Assert.assertEquals("route1", entry1.getRoute());
66          Assert.assertEquals(0, entry1.getUpdated());
67          Assert.assertEquals(Deadline.MIN_VALUE, entry1.getExpiryDeadline());
68  
69          entry1.assignConnection(Mockito.mock(HttpConnection.class));
70          final long now = System.currentTimeMillis();
71          Assert.assertEquals("route1", entry1.getRoute());
72          Assert.assertTrue(now >= entry1.getUpdated());
73          Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiryDeadline());
74          Assert.assertEquals(entry1.getUpdated() + 10L, entry1.getValidityDeadline().getValue());
75  
76          entry1.discardConnection(CloseMode.IMMEDIATE);
77          Assert.assertEquals(0, entry1.getUpdated());
78          Assert.assertEquals(Deadline.MIN_VALUE, entry1.getExpiryDeadline());
79      }
80  
81      @Test(expected = NullPointerException.class)
82      public void testNullConstructor() throws Exception {
83          new PoolEntry<String, HttpConnection>(null);
84      }
85  
86      @Test
87      public void testValidInfinitely() throws Exception {
88          final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
89                  "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
90          entry1.assignConnection(Mockito.mock(HttpConnection.class));
91          Assert.assertEquals(Deadline.MAX_VALUE, entry1.getValidityDeadline());
92          Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiryDeadline());
93      }
94  
95      @Test
96      public void testExpiry() throws Exception {
97          final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>(
98                  "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
99          entry1.assignConnection(Mockito.mock(HttpConnection.class));
100         Assert.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline());
101         entry1.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
102         Assert.assertEquals(entry1.getUpdated() + 50L, entry1.getExpiryDeadline().getValue());
103         entry1.updateExpiry(TimeValue.ZERO_MILLISECONDS);
104         Assert.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline());
105 
106         final PoolEntry<String, HttpConnection> entry2 = new PoolEntry<>(
107                 "route1", TimeValue.of(100L, TimeUnit.MILLISECONDS), currentTimeSupplier);
108         entry2.assignConnection(Mockito.mock(HttpConnection.class));
109         final Deadline validityDeadline = entry2.getValidityDeadline();
110         Assert.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiryDeadline().getValue());
111         entry2.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
112         Assert.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiryDeadline().getValue());
113         entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS));
114         Assert.assertEquals(validityDeadline, entry2.getExpiryDeadline());
115     }
116 
117     @Test(expected=NullPointerException.class)
118     public void testInvalidExpiry() throws Exception {
119         final PoolEntry<String, HttpConnection> entry = new PoolEntry<>(
120                 "route1", TimeValue.of(0L, TimeUnit.MILLISECONDS), currentTimeSupplier);
121         entry.updateExpiry(null);
122     }
123 
124     @Test
125     public void testExpiryDoesNotOverflow() {
126         final PoolEntry<String, HttpConnection> entry = new PoolEntry<>(
127                 "route1", TimeValue.of(Long.MAX_VALUE, TimeUnit.MILLISECONDS), currentTimeSupplier);
128         entry.assignConnection(Mockito.mock(HttpConnection.class));
129         Assert.assertEquals(Deadline.MAX_VALUE, entry.getValidityDeadline());
130     }
131 
132 }