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