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.http.impl.nio.pool;
28  
29  import org.apache.http.HttpHost;
30  import org.apache.http.config.ConnectionConfig;
31  import org.apache.http.nio.NHttpClientConnection;
32  import org.apache.http.nio.pool.NIOConnFactory;
33  import org.apache.http.nio.reactor.ConnectingIOReactor;
34  import org.apache.http.nio.reactor.IOSession;
35  import org.apache.http.nio.reactor.SessionRequest;
36  import org.apache.http.nio.reactor.SessionRequestCallback;
37  import org.junit.After;
38  import org.junit.Assert;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.mockito.Matchers;
42  import org.mockito.Mock;
43  import org.mockito.Mockito;
44  import org.mockito.MockitoAnnotations;
45  
46  import java.net.SocketAddress;
47  import java.util.concurrent.Future;
48  import java.util.concurrent.TimeUnit;
49  
50  public class TestBasicNIOConnPool {
51  
52      static class LocalPool extends BasicNIOConnPool {
53  
54          public LocalPool(
55                  final ConnectingIOReactor ioreactor,
56                  final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
57                  final int connectTimeout) {
58              super(ioreactor, connFactory, connectTimeout);
59          }
60  
61          public LocalPool(
62                  final ConnectingIOReactor ioreactor,
63                  final ConnectionConfig config) {
64              super(ioreactor, config);
65          }
66  
67          @Override
68          public void requestCompleted(final SessionRequest request) {
69              super.requestCompleted(request);
70          }
71  
72      }
73  
74      private BasicNIOConnFactory connFactory;
75      private LocalPool pool;
76      private HttpHost route;
77      @Mock private ConnectingIOReactor reactor;
78      @Mock private IOSession session;
79  
80      @Before
81      public void setUp() throws Exception {
82          MockitoAnnotations.initMocks(this);
83  
84          route = new HttpHost("localhost", 80, "http");
85          connFactory = new BasicNIOConnFactory(ConnectionConfig.DEFAULT);
86          pool = new LocalPool(reactor, connFactory, 0);
87      }
88  
89      @After
90      public void tearDown() throws Exception {
91      }
92  
93      @Test(expected=IllegalArgumentException.class)
94      public void testNullConstructor() throws Exception {
95          pool = new LocalPool(null, ConnectionConfig.DEFAULT);
96      }
97  
98      @Test
99      public void testCreateConnection() throws Exception {
100         connFactory.create(route, session);
101     }
102 
103     @Test
104     public void testCreateEntry() throws Exception {
105         final NHttpClientConnection conn = connFactory.create(route, session);
106         final BasicNIOPoolEntry entry = pool.createEntry(route, conn);
107         entry.close();
108     }
109 
110     @Test
111     public void testTimeoutOnLeaseRelease() throws Exception {
112         final HttpHost host = new HttpHost("somehost");
113         final SessionRequest sessionRequest = Mockito.mock(SessionRequest.class);
114         Mockito.when(sessionRequest.getSession()).thenReturn(session);
115         Mockito.when(sessionRequest.getAttachment()).thenReturn(host);
116         Mockito.when(reactor.connect(
117                 Matchers.any(SocketAddress.class),
118                 Matchers.any(SocketAddress.class),
119                 Matchers.eq(host),
120                 Matchers.any(SessionRequestCallback.class))).
121                 thenReturn(sessionRequest);
122 
123         Mockito.when(session.getSocketTimeout()).thenReturn(999);
124 
125         final Future<BasicNIOPoolEntry> future1 = pool.lease(host, null, 10, TimeUnit.SECONDS, null);
126         Mockito.verify(sessionRequest).setConnectTimeout(10000);
127 
128         pool.requestCompleted(sessionRequest);
129 
130         final BasicNIOPoolEntry entry1 = future1.get();
131         final NHttpClientConnection conn1 = entry1.getConnection();
132         Assert.assertNotNull(entry1);
133         Assert.assertNotNull(conn1);
134         Assert.assertEquals(999, entry1.getSocketTimeout());
135         Assert.assertEquals(999, conn1.getSocketTimeout());
136 
137         Mockito.when(session.getSocketTimeout()).thenReturn(888);
138 
139         pool.release(entry1, true);
140         Assert.assertEquals(888, entry1.getSocketTimeout());
141         Mockito.verify(session).setSocketTimeout(0);
142 
143         final Future<BasicNIOPoolEntry> future2 = pool.lease(host, null, 10, TimeUnit.SECONDS, null);
144         final BasicNIOPoolEntry entry2 = future2.get();
145         final NHttpClientConnection conn2 = entry2.getConnection();
146         Assert.assertNotNull(entry2);
147         Assert.assertNotNull(conn2);
148 
149         Assert.assertEquals(888, entry1.getSocketTimeout());
150         Mockito.verify(session).setSocketTimeout(888);
151     }
152 
153 
154 }