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.http.pool;
28
29 import java.io.IOException;
30 import java.util.concurrent.TimeUnit;
31
32 import org.apache.http.HttpConnection;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36
37 public class TestPoolEntry {
38
39 static class MockPoolEntry extends PoolEntry<String, HttpConnection> {
40
41 public MockPoolEntry(final String route,
42 final long timeToLive, final TimeUnit timeUnit) {
43 super(null, route, Mockito.mock(HttpConnection.class), timeToLive, timeUnit);
44 }
45
46 public MockPoolEntry(final String route, final HttpConnection conn,
47 final long timeToLive, final TimeUnit timeUnit) {
48 super(null, route, conn, timeToLive, timeUnit);
49 }
50
51 @Override
52 public void close() {
53 try {
54 getConnection().close();
55 } catch (final IOException ignore) {
56 }
57 }
58
59 @Override
60 public boolean isClosed() {
61 return !getConnection().isOpen();
62 }
63
64 }
65
66 @Test
67 public void testBasics() throws Exception {
68 final MockPoolEntry entry1 = new MockPoolEntry("route1", 10L, TimeUnit.MILLISECONDS);
69 final long now = System.currentTimeMillis();
70 Assert.assertEquals("route1", entry1.getRoute());
71 Assert.assertTrue(now >= entry1.getCreated());
72 Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiry());
73 Assert.assertEquals(entry1.getCreated() + 10L, entry1.getValidityDeadline());
74 }
75
76 @Test
77 public void testInvalidConstruction() throws Exception {
78 try {
79 new MockPoolEntry(null, Mockito.mock(HttpConnection.class), 0L, TimeUnit.MILLISECONDS);
80 Assert.fail("IllegalArgumentException should have been thrown");
81 } catch (final IllegalArgumentException expected) {
82 }
83 try {
84 new MockPoolEntry("stuff", null, 0L, TimeUnit.MILLISECONDS);
85 Assert.fail("IllegalArgumentException should have been thrown");
86 } catch (final IllegalArgumentException expected) {
87 }
88 try {
89 new MockPoolEntry("stuff", Mockito.mock(HttpConnection.class), 0L, null);
90 Assert.fail("IllegalArgumentException should have been thrown");
91 } catch (final IllegalArgumentException expected) {
92 }
93 }
94
95 @Test
96 public void testValidInfinitely() throws Exception {
97 final MockPoolEntry entry1 = new MockPoolEntry("route1", 0L, TimeUnit.MILLISECONDS);
98 Assert.assertEquals(Long.MAX_VALUE, entry1.getValidityDeadline());
99 Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiry());
100 }
101
102 @Test
103 public void testExpiry() throws Exception {
104 final MockPoolEntry entry1 = new MockPoolEntry("route1", 0L, TimeUnit.MILLISECONDS);
105 Assert.assertEquals(Long.MAX_VALUE, entry1.getExpiry());
106 entry1.updateExpiry(50L, TimeUnit.MILLISECONDS);
107 Assert.assertEquals(entry1.getUpdated() + 50L, entry1.getExpiry());
108 entry1.updateExpiry(0L, TimeUnit.MILLISECONDS);
109 Assert.assertEquals(Long.MAX_VALUE, entry1.getExpiry());
110
111 final MockPoolEntry entry2 = new MockPoolEntry("route1", 100L, TimeUnit.MILLISECONDS);
112 Assert.assertEquals(entry2.getCreated() + 100L, entry2.getExpiry());
113 entry2.updateExpiry(150L, TimeUnit.MILLISECONDS);
114 Assert.assertEquals(entry2.getCreated() + 100L, entry2.getExpiry());
115 entry2.updateExpiry(50L, TimeUnit.MILLISECONDS);
116 Assert.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiry());
117 }
118
119 @Test(expected=IllegalArgumentException.class)
120 public void testInvalidExpiry() throws Exception {
121 final MockPoolEntry entry1 = new MockPoolEntry("route1", 0L, TimeUnit.MILLISECONDS);
122 entry1.updateExpiry(50L, null);
123 }
124
125 @Test
126 public void testExpiryDoesNotOverflow() {
127 final MockPoolEntry entry = new MockPoolEntry("route1", Long.MAX_VALUE, TimeUnit.MILLISECONDS);
128 Assert.assertEquals(entry.getValidityDeadline(), Long.MAX_VALUE);
129 }
130
131 }