1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }