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.client5.http.impl.nio;
28  
29  import org.apache.hc.core5.http.HttpConnection;
30  import org.apache.hc.core5.io.CloseMode;
31  import org.apache.hc.core5.pool.PoolEntry;
32  import org.junit.jupiter.api.Assertions;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  import org.mockito.Mockito;
36  
37  public class H2SharingPerRoutePoolTest {
38  
39      static PoolEntry<String, HttpConnection> createMockEntry() {
40          final PoolEntry<String, HttpConnection> poolEntry = new PoolEntry<>("some route");
41          final HttpConnection conn = Mockito.mock(HttpConnection.class);
42          Mockito.when(conn.isOpen()).thenReturn(true);
43          poolEntry.assignConnection(conn);
44          return poolEntry;
45      }
46  
47      H2SharingConnPool.PerRoutePool<String, HttpConnection> pool;
48      PoolEntry<String, HttpConnection> poolEntry1;
49      PoolEntry<String, HttpConnection> poolEntry2;
50  
51      @BeforeEach
52      void setup() {
53          pool = new H2SharingConnPool.PerRoutePool<>();
54          poolEntry1 = createMockEntry();
55          poolEntry2 = createMockEntry();
56      }
57  
58      @Test
59      void testKeep() {
60          Assertions.assertEquals(1, pool.track(poolEntry1));
61          Assertions.assertEquals(2, pool.track(poolEntry1));
62          Assertions.assertEquals(1, pool.track(poolEntry2));
63          Assertions.assertEquals(3, pool.track(poolEntry1));
64      }
65  
66      @Test
67      void testLeaseLeastUsed() {
68          pool.track(poolEntry1);
69          pool.track(poolEntry1);
70          pool.track(poolEntry2);
71          Assertions.assertSame(poolEntry2, pool.lease());
72          Assertions.assertEquals(2, pool.getCount(poolEntry2));
73  
74          final PoolEntry<String, HttpConnection> poolEntry = pool.lease();
75          Assertions.assertEquals(3, pool.getCount(poolEntry));
76      }
77  
78      @Test
79      void testLeaseEmptyPool() {
80          Assertions.assertNull(pool.lease());
81      }
82  
83      @Test
84      void testReleaseReusable() {
85          pool.track(poolEntry1);
86          pool.track(poolEntry1);
87          pool.track(poolEntry1);
88  
89          Assertions.assertEquals(2, pool.release(poolEntry1, true));
90          Assertions.assertEquals(1, pool.release(poolEntry1, true));
91          Assertions.assertEquals(0, pool.release(poolEntry1, true));
92          Assertions.assertEquals(0, pool.release(poolEntry1, true));
93      }
94  
95      @Test
96      void testReleaseNonReusable() {
97          pool.track(poolEntry1);
98          pool.track(poolEntry1);
99          pool.track(poolEntry1);
100 
101         Assertions.assertEquals(0, pool.release(poolEntry1, false));
102     }
103 
104     @Test
105     void testReleaseNonPresent() {
106         Assertions.assertEquals(0, pool.release(poolEntry1, true));
107         Assertions.assertEquals(0, pool.release(poolEntry2, true));
108     }
109 
110     @Test
111     void testReleaseConnectionClosed() {
112         pool.track(poolEntry1);
113         pool.track(poolEntry1);
114         pool.track(poolEntry1);
115 
116         Mockito.when(poolEntry1.getConnection().isOpen()).thenReturn(false);
117         Assertions.assertEquals(0, pool.release(poolEntry1, true));
118     }
119 
120     @Test
121     void testReleaseConnectionMissing() {
122         pool.track(poolEntry1);
123         pool.track(poolEntry1);
124         pool.track(poolEntry1);
125 
126         poolEntry1.discardConnection(CloseMode.IMMEDIATE);
127         Assertions.assertEquals(0, pool.release(poolEntry1, true));
128     }
129 
130 }