| 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.conn; |
| 28 |
|
| 29 |
import java.net.InetAddress; |
| 30 |
import java.net.InetSocketAddress; |
| 31 |
import java.util.Calendar; |
| 32 |
import java.util.concurrent.CancellationException; |
| 33 |
import java.util.concurrent.ExecutionException; |
| 34 |
import java.util.concurrent.Future; |
| 35 |
import java.util.concurrent.TimeUnit; |
| 36 |
|
| 37 |
import org.apache.commons.logging.Log; |
| 38 |
import org.apache.http.HttpHost; |
| 39 |
import org.apache.http.concurrent.FutureCallback; |
| 40 |
import org.apache.http.config.ConnectionConfig; |
| 41 |
import org.apache.http.config.Registry; |
| 42 |
import org.apache.http.config.RegistryBuilder; |
| 43 |
import org.apache.http.conn.DnsResolver; |
| 44 |
import org.apache.http.conn.SchemePortResolver; |
| 45 |
import org.apache.http.conn.UnsupportedSchemeException; |
| 46 |
import org.apache.http.conn.routing.HttpRoute; |
| 47 |
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.ConfigData; |
| 48 |
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.InternalAddressResolver; |
| 49 |
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.InternalConnectionFactory; |
| 50 |
import org.apache.http.nio.NHttpClientConnection; |
| 51 |
import org.apache.http.nio.conn.ManagedNHttpClientConnection; |
| 52 |
import org.apache.http.nio.conn.NHttpConnectionFactory; |
| 53 |
import org.apache.http.nio.conn.SchemeIOSessionStrategy; |
| 54 |
import org.apache.http.nio.reactor.ConnectingIOReactor; |
| 55 |
import org.apache.http.nio.reactor.IOSession; |
| 56 |
import org.apache.http.nio.reactor.SessionRequest; |
| 57 |
import org.apache.http.protocol.BasicHttpContext; |
| 58 |
import org.apache.http.protocol.HttpContext; |
| 59 |
import org.junit.Assert; |
| 60 |
import org.junit.Before; |
| 61 |
import org.junit.Test; |
| 62 |
import org.mockito.ArgumentCaptor; |
| 63 |
import org.mockito.Captor; |
| 64 |
import org.mockito.Matchers; |
| 65 |
import org.mockito.Mock; |
| 66 |
import org.mockito.Mockito; |
| 67 |
import org.mockito.MockitoAnnotations; |
| 68 |
|
| 69 |
public class TestPoolingHttpClientAsyncConnectionManager { |
| 70 |
|
| 71 |
@Mock |
| 72 |
private ConnectingIOReactor ioReactor; |
| 73 |
@Mock |
| 74 |
private CPool pool; |
| 75 |
@Mock |
| 76 |
private SchemeIOSessionStrategy noopStrategy; |
| 77 |
@Mock |
| 78 |
private SchemeIOSessionStrategy sslStrategy; |
| 79 |
@Mock |
| 80 |
private SchemePortResolver schemePortResolver; |
| 81 |
@Mock |
| 82 |
private DnsResolver dnsResolver; |
| 83 |
@Mock |
| 84 |
private FutureCallback<NHttpClientConnection> connCallback; |
| 85 |
@Captor |
| 86 |
private ArgumentCaptor<FutureCallback<CPoolEntry>> poolEntryCallbackCaptor; |
| 87 |
@Mock |
| 88 |
private ManagedNHttpClientConnection conn; |
| 89 |
@Mock |
| 90 |
private NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory; |
| 91 |
@Mock |
| 92 |
private SessionRequest sessionRequest; |
| 93 |
@Mock |
| 94 |
private IOSession ioSession; |
| 95 |
|
| 96 |
private Registry<SchemeIOSessionStrategy> layeringStrategyRegistry; |
| 97 |
private PoolingNHttpClientConnectionManager connman; |
| 98 |
|
| 99 |
@Before |
| 100 |
public void setUp() throws Exception { |
| 101 |
MockitoAnnotations.initMocks(this); |
| 102 |
Mockito.when(sslStrategy.isLayeringRequired()).thenReturn(Boolean.TRUE); |
| 103 |
|
| 104 |
layeringStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create() |
| 105 |
.register("http", noopStrategy) |
| 106 |
.register("https", sslStrategy) |
| 107 |
.build(); |
| 108 |
connman = new PoolingNHttpClientConnectionManager( |
| 109 |
ioReactor, pool, layeringStrategyRegistry); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void testShutdown() throws Exception { |
| 114 |
connman.shutdown(); |
| 115 |
|
| 116 |
Mockito.verify(pool).shutdown(2000); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void testShutdownMs() throws Exception { |
| 121 |
connman.shutdown(500); |
| 122 |
|
| 123 |
Mockito.verify(pool).shutdown(500); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void testRequestReleaseConnection() throws Exception { |
| 128 |
final HttpHost target = new HttpHost("localhost"); |
| 129 |
final HttpRoute route = new HttpRoute(target); |
| 130 |
final Future<NHttpClientConnection> future = connman.requestConnection( |
| 131 |
route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, connCallback); |
| 132 |
Assert.assertNotNull(future); |
| 133 |
|
| 134 |
Mockito.verify(pool).lease( |
| 135 |
Matchers.same(route), |
| 136 |
Matchers.eq("some state"), |
| 137 |
Matchers.eq(1000L), |
| 138 |
Matchers.eq(2000L), |
| 139 |
Matchers.eq(TimeUnit.MILLISECONDS), |
| 140 |
poolEntryCallbackCaptor.capture()); |
| 141 |
final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue(); |
| 142 |
final Log log = Mockito.mock(Log.class); |
| 143 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 144 |
poolentry.markRouteComplete(); |
| 145 |
callaback.completed(poolentry); |
| 146 |
|
| 147 |
Assert.assertTrue(future.isDone()); |
| 148 |
final NHttpClientConnection managedConn = future.get(); |
| 149 |
Mockito.verify(connCallback).completed(Matchers.<NHttpClientConnection>any()); |
| 150 |
|
| 151 |
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE); |
| 152 |
connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS); |
| 153 |
|
| 154 |
Mockito.verify(pool).release(poolentry, true); |
| 155 |
Assert.assertEquals("new state", poolentry.getState()); |
| 156 |
final Calendar cal = Calendar.getInstance(); |
| 157 |
cal.setTimeInMillis(poolentry.getUpdated()); |
| 158 |
cal.add(Calendar.SECOND, 5); |
| 159 |
Assert.assertEquals(cal.getTimeInMillis(), poolentry.getExpiry()); |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void testReleaseConnectionIncompleteRoute() throws Exception { |
| 164 |
final HttpHost target = new HttpHost("localhost"); |
| 165 |
final HttpRoute route = new HttpRoute(target); |
| 166 |
final Future<NHttpClientConnection> future = connman.requestConnection( |
| 167 |
route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, connCallback); |
| 168 |
Assert.assertNotNull(future); |
| 169 |
|
| 170 |
Mockito.verify(pool).lease( |
| 171 |
Matchers.same(route), |
| 172 |
Matchers.eq("some state"), |
| 173 |
Matchers.eq(1000L), |
| 174 |
Matchers.eq(2000L), |
| 175 |
Matchers.eq(TimeUnit.MILLISECONDS), |
| 176 |
poolEntryCallbackCaptor.capture()); |
| 177 |
final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue(); |
| 178 |
final Log log = Mockito.mock(Log.class); |
| 179 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 180 |
callaback.completed(poolentry); |
| 181 |
|
| 182 |
Assert.assertTrue(future.isDone()); |
| 183 |
final NHttpClientConnection managedConn = future.get(); |
| 184 |
Mockito.verify(connCallback).completed(Matchers.<NHttpClientConnection>any()); |
| 185 |
|
| 186 |
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE); |
| 187 |
connman.releaseConnection(managedConn, "new state", 5, TimeUnit.SECONDS); |
| 188 |
|
| 189 |
Mockito.verify(pool).release(poolentry, false); |
| 190 |
} |
| 191 |
|
| 192 |
@Test |
| 193 |
public void testRequestConnectionFutureCancelled() throws Exception { |
| 194 |
final HttpHost target = new HttpHost("localhost"); |
| 195 |
final HttpRoute route = new HttpRoute(target); |
| 196 |
final Future<NHttpClientConnection> future = connman.requestConnection( |
| 197 |
route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null); |
| 198 |
Assert.assertNotNull(future); |
| 199 |
future.cancel(true); |
| 200 |
|
| 201 |
Mockito.verify(pool).lease( |
| 202 |
Matchers.same(route), |
| 203 |
Matchers.eq("some state"), |
| 204 |
Matchers.eq(1000L), |
| 205 |
Matchers.eq(2000L), |
| 206 |
Matchers.eq(TimeUnit.MILLISECONDS), |
| 207 |
poolEntryCallbackCaptor.capture()); |
| 208 |
final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue(); |
| 209 |
final Log log = Mockito.mock(Log.class); |
| 210 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 211 |
poolentry.markRouteComplete(); |
| 212 |
callaback.completed(poolentry); |
| 213 |
|
| 214 |
Mockito.verify(pool).release(poolentry, true); |
| 215 |
} |
| 216 |
|
| 217 |
@Test(expected=ExecutionException.class) |
| 218 |
public void testRequestConnectionFailed() throws Exception { |
| 219 |
final HttpHost target = new HttpHost("localhost"); |
| 220 |
final HttpRoute route = new HttpRoute(target); |
| 221 |
final Future<NHttpClientConnection> future = connman.requestConnection( |
| 222 |
route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null); |
| 223 |
Assert.assertNotNull(future); |
| 224 |
|
| 225 |
Mockito.verify(pool).lease( |
| 226 |
Matchers.same(route), |
| 227 |
Matchers.eq("some state"), |
| 228 |
Matchers.eq(1000L), |
| 229 |
Matchers.eq(2000L), |
| 230 |
Matchers.eq(TimeUnit.MILLISECONDS), |
| 231 |
poolEntryCallbackCaptor.capture()); |
| 232 |
final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue(); |
| 233 |
callaback.failed(new Exception()); |
| 234 |
|
| 235 |
Assert.assertTrue(future.isDone()); |
| 236 |
future.get(); |
| 237 |
} |
| 238 |
|
| 239 |
@Test(expected = CancellationException.class) |
| 240 |
public void testRequestConnectionCancelled() throws Exception { |
| 241 |
final HttpHost target = new HttpHost("localhost"); |
| 242 |
final HttpRoute route = new HttpRoute(target); |
| 243 |
final Future<NHttpClientConnection> future = connman.requestConnection( |
| 244 |
route, "some state", 1000L, 2000L, TimeUnit.MILLISECONDS, null); |
| 245 |
Assert.assertNotNull(future); |
| 246 |
|
| 247 |
Mockito.verify(pool).lease( |
| 248 |
Matchers.same(route), |
| 249 |
Matchers.eq("some state"), |
| 250 |
Matchers.eq(1000L), |
| 251 |
Matchers.eq(2000L), |
| 252 |
Matchers.eq(TimeUnit.MILLISECONDS), |
| 253 |
poolEntryCallbackCaptor.capture()); |
| 254 |
final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue(); |
| 255 |
callaback.cancelled(); |
| 256 |
|
| 257 |
Assert.assertTrue(future.isDone()); |
| 258 |
Assert.assertTrue(future.isCancelled()); |
| 259 |
future.get(); |
| 260 |
} |
| 261 |
|
| 262 |
@Test |
| 263 |
public void testConnectionInitialize() throws Exception { |
| 264 |
final HttpHost target = new HttpHost("somehost", -1, "http"); |
| 265 |
final HttpRoute route = new HttpRoute(target); |
| 266 |
final HttpContext context = new BasicHttpContext(); |
| 267 |
|
| 268 |
final Log log = Mockito.mock(Log.class); |
| 269 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 270 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 271 |
|
| 272 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 273 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 274 |
|
| 275 |
connman.startRoute(managedConn, route, context); |
| 276 |
|
| 277 |
Mockito.verify(noopStrategy, Mockito.never()).upgrade(target, ioSession); |
| 278 |
Mockito.verify(conn, Mockito.never()).bind(ioSession); |
| 279 |
|
| 280 |
Assert.assertFalse(connman.isRouteComplete(managedConn)); |
| 281 |
} |
| 282 |
|
| 283 |
@Test |
| 284 |
public void testConnectionInitializeHttps() throws Exception { |
| 285 |
final HttpHost target = new HttpHost("somehost", 443, "https"); |
| 286 |
final HttpRoute route = new HttpRoute(target, null, true); |
| 287 |
final HttpContext context = new BasicHttpContext(); |
| 288 |
|
| 289 |
final Log log = Mockito.mock(Log.class); |
| 290 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 291 |
poolentry.markRouteComplete(); |
| 292 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 293 |
|
| 294 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 295 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 296 |
|
| 297 |
connman.startRoute(managedConn, route, context); |
| 298 |
|
| 299 |
Mockito.verify(sslStrategy).upgrade(target, ioSession); |
| 300 |
Mockito.verify(conn).bind(ioSession); |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void testConnectionInitializeContextSpecific() throws Exception { |
| 305 |
final HttpHost target = new HttpHost("somehost", 80, "http11"); |
| 306 |
final HttpRoute route = new HttpRoute(target); |
| 307 |
final HttpContext context = new BasicHttpContext(); |
| 308 |
|
| 309 |
final Registry<SchemeIOSessionStrategy> reg = RegistryBuilder.<SchemeIOSessionStrategy>create() |
| 310 |
.register("http11", noopStrategy) |
| 311 |
.build(); |
| 312 |
context.setAttribute(PoolingNHttpClientConnectionManager.IOSESSION_FACTORY_REGISTRY, reg); |
| 313 |
|
| 314 |
final Log log = Mockito.mock(Log.class); |
| 315 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 316 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 317 |
|
| 318 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 319 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 320 |
|
| 321 |
connman.startRoute(managedConn, route, context); |
| 322 |
|
| 323 |
Mockito.verify(noopStrategy, Mockito.never()).upgrade(target, ioSession); |
| 324 |
Mockito.verify(conn, Mockito.never()).bind(ioSession); |
| 325 |
|
| 326 |
Assert.assertFalse(connman.isRouteComplete(managedConn)); |
| 327 |
} |
| 328 |
|
| 329 |
@Test(expected=UnsupportedSchemeException.class) |
| 330 |
public void testConnectionInitializeUnknownScheme() throws Exception { |
| 331 |
final HttpHost target = new HttpHost("somehost", -1, "whatever"); |
| 332 |
final HttpRoute route = new HttpRoute(target, null, true); |
| 333 |
final HttpContext context = new BasicHttpContext(); |
| 334 |
|
| 335 |
final Log log = Mockito.mock(Log.class); |
| 336 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 337 |
poolentry.markRouteComplete(); |
| 338 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 339 |
|
| 340 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 341 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 342 |
|
| 343 |
connman.startRoute(managedConn, route, context); |
| 344 |
} |
| 345 |
|
| 346 |
@Test |
| 347 |
public void testConnectionUpgrade() throws Exception { |
| 348 |
final HttpHost target = new HttpHost("somehost", 443, "https"); |
| 349 |
final HttpRoute route = new HttpRoute(target); |
| 350 |
final HttpContext context = new BasicHttpContext(); |
| 351 |
|
| 352 |
final Log log = Mockito.mock(Log.class); |
| 353 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 354 |
poolentry.markRouteComplete(); |
| 355 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 356 |
|
| 357 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 358 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 359 |
|
| 360 |
connman.upgrade(managedConn, route, context); |
| 361 |
|
| 362 |
Mockito.verify(sslStrategy).upgrade(target, ioSession); |
| 363 |
Mockito.verify(conn).bind(ioSession); |
| 364 |
} |
| 365 |
|
| 366 |
@Test(expected=UnsupportedSchemeException.class) |
| 367 |
public void testConnectionUpgradeUnknownScheme() throws Exception { |
| 368 |
final HttpHost target = new HttpHost("somehost", -1, "whatever"); |
| 369 |
final HttpRoute route = new HttpRoute(target); |
| 370 |
final HttpContext context = new BasicHttpContext(); |
| 371 |
|
| 372 |
final Log log = Mockito.mock(Log.class); |
| 373 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 374 |
poolentry.markRouteComplete(); |
| 375 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 376 |
|
| 377 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 378 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 379 |
|
| 380 |
connman.upgrade(managedConn, route, context); |
| 381 |
} |
| 382 |
|
| 383 |
@Test(expected=UnsupportedSchemeException.class) |
| 384 |
public void testConnectionUpgradeIllegalScheme() throws Exception { |
| 385 |
final HttpHost target = new HttpHost("somehost", 80, "http"); |
| 386 |
final HttpRoute route = new HttpRoute(target); |
| 387 |
final HttpContext context = new BasicHttpContext(); |
| 388 |
|
| 389 |
final Log log = Mockito.mock(Log.class); |
| 390 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 391 |
poolentry.markRouteComplete(); |
| 392 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 393 |
|
| 394 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 395 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 396 |
|
| 397 |
connman.upgrade(managedConn, route, context); |
| 398 |
} |
| 399 |
|
| 400 |
@Test |
| 401 |
public void testConnectionRouteComplete() throws Exception { |
| 402 |
final HttpHost target = new HttpHost("somehost", 80, "http"); |
| 403 |
final HttpRoute route = new HttpRoute(target); |
| 404 |
final HttpContext context = new BasicHttpContext(); |
| 405 |
|
| 406 |
final Log log = Mockito.mock(Log.class); |
| 407 |
final CPoolEntry poolentry = new CPoolEntry(log, "some-id", route, conn, -1, TimeUnit.MILLISECONDS); |
| 408 |
poolentry.markRouteComplete(); |
| 409 |
final NHttpClientConnection managedConn = CPoolProxy.newProxy(poolentry); |
| 410 |
|
| 411 |
Mockito.when(conn.getIOSession()).thenReturn(ioSession); |
| 412 |
Mockito.when(sslStrategy.upgrade(target, ioSession)).thenReturn(ioSession); |
| 413 |
|
| 414 |
connman.startRoute(managedConn, route, context); |
| 415 |
connman.routeComplete(managedConn, route, context); |
| 416 |
|
| 417 |
Assert.assertTrue(connman.isRouteComplete(managedConn)); |
| 418 |
} |
| 419 |
|
| 420 |
@Test |
| 421 |
public void testDelegationToCPool() throws Exception { |
| 422 |
connman.closeExpiredConnections(); |
| 423 |
Mockito.verify(pool).closeExpired(); |
| 424 |
|
| 425 |
connman.closeIdleConnections(3, TimeUnit.SECONDS); |
| 426 |
Mockito.verify(pool).closeIdle(3, TimeUnit.SECONDS); |
| 427 |
|
| 428 |
connman.getMaxTotal(); |
| 429 |
Mockito.verify(pool).getMaxTotal(); |
| 430 |
|
| 431 |
connman.getDefaultMaxPerRoute(); |
| 432 |
Mockito.verify(pool).getDefaultMaxPerRoute(); |
| 433 |
|
| 434 |
final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80)); |
| 435 |
connman.getMaxPerRoute(route); |
| 436 |
Mockito.verify(pool).getMaxPerRoute(route); |
| 437 |
|
| 438 |
connman.setMaxTotal(200); |
| 439 |
Mockito.verify(pool).setMaxTotal(200); |
| 440 |
|
| 441 |
connman.setDefaultMaxPerRoute(100); |
| 442 |
Mockito.verify(pool).setDefaultMaxPerRoute(100); |
| 443 |
|
| 444 |
connman.setMaxPerRoute(route, 150); |
| 445 |
Mockito.verify(pool).setMaxPerRoute(route, 150); |
| 446 |
|
| 447 |
connman.getTotalStats(); |
| 448 |
Mockito.verify(pool).getTotalStats(); |
| 449 |
|
| 450 |
connman.getStats(route); |
| 451 |
Mockito.verify(pool).getStats(route); |
| 452 |
} |
| 453 |
|
| 454 |
@Test |
| 455 |
public void testInternalConnFactoryCreate() throws Exception { |
| 456 |
final ConfigData configData = new ConfigData(); |
| 457 |
final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory( |
| 458 |
configData, connFactory); |
| 459 |
|
| 460 |
final HttpRoute route = new HttpRoute(new HttpHost("somehost", 80)); |
| 461 |
internalConnFactory.create(route, ioSession); |
| 462 |
|
| 463 |
Mockito.verify(sslStrategy, Mockito.never()).upgrade(Matchers.eq(new HttpHost("somehost", 80)), |
| 464 |
Matchers.<IOSession>any()); |
| 465 |
Mockito.verify(connFactory).create(Matchers.same(ioSession), Matchers.<ConnectionConfig>any()); |
| 466 |
} |
| 467 |
|
| 468 |
@Test |
| 469 |
public void testInternalConnFactoryCreateViaProxy() throws Exception { |
| 470 |
final ConfigData configData = new ConfigData(); |
| 471 |
final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory( |
| 472 |
configData, connFactory); |
| 473 |
|
| 474 |
final HttpHost target = new HttpHost("somehost", 80); |
| 475 |
final HttpHost proxy = new HttpHost("someproxy", 8888); |
| 476 |
final HttpRoute route = new HttpRoute(target, null, proxy, false); |
| 477 |
|
| 478 |
final ConnectionConfig config = ConnectionConfig.custom().build(); |
| 479 |
configData.setConnectionConfig(proxy, config); |
| 480 |
|
| 481 |
internalConnFactory.create(route, ioSession); |
| 482 |
|
| 483 |
Mockito.verify(connFactory).create(ioSession, config); |
| 484 |
} |
| 485 |
|
| 486 |
@Test |
| 487 |
public void testInternalConnFactoryCreateDirect() throws Exception { |
| 488 |
final ConfigData configData = new ConfigData(); |
| 489 |
final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory( |
| 490 |
configData, connFactory); |
| 491 |
|
| 492 |
final HttpHost target = new HttpHost("somehost", 80); |
| 493 |
final HttpRoute route = new HttpRoute(target); |
| 494 |
|
| 495 |
final ConnectionConfig config = ConnectionConfig.custom().build(); |
| 496 |
configData.setConnectionConfig(target, config); |
| 497 |
|
| 498 |
internalConnFactory.create(route, ioSession); |
| 499 |
|
| 500 |
Mockito.verify(connFactory).create(ioSession, config); |
| 501 |
} |
| 502 |
|
| 503 |
@Test |
| 504 |
public void testInternalConnFactoryCreateDefaultConfig() throws Exception { |
| 505 |
final ConfigData configData = new ConfigData(); |
| 506 |
final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory( |
| 507 |
configData, connFactory); |
| 508 |
|
| 509 |
final HttpHost target = new HttpHost("somehost", 80); |
| 510 |
final HttpRoute route = new HttpRoute(target); |
| 511 |
|
| 512 |
final ConnectionConfig config = ConnectionConfig.custom().build(); |
| 513 |
configData.setDefaultConnectionConfig(config); |
| 514 |
|
| 515 |
internalConnFactory.create(route, ioSession); |
| 516 |
|
| 517 |
Mockito.verify(connFactory).create(ioSession, config); |
| 518 |
} |
| 519 |
|
| 520 |
@Test |
| 521 |
public void testInternalConnFactoryCreateGlobalDefaultConfig() throws Exception { |
| 522 |
final ConfigData configData = new ConfigData(); |
| 523 |
final InternalConnectionFactory internalConnFactory = new InternalConnectionFactory( |
| 524 |
configData, connFactory); |
| 525 |
|
| 526 |
final HttpHost target = new HttpHost("somehost", 80); |
| 527 |
final HttpRoute route = new HttpRoute(target); |
| 528 |
|
| 529 |
configData.setDefaultConnectionConfig(null); |
| 530 |
|
| 531 |
internalConnFactory.create(route, ioSession); |
| 532 |
|
| 533 |
Mockito.verify(connFactory).create(ioSession, ConnectionConfig.DEFAULT); |
| 534 |
} |
| 535 |
|
| 536 |
@Test |
| 537 |
public void testResolveLocalAddress() throws Exception { |
| 538 |
final InternalAddressResolver addressResolver = new InternalAddressResolver( |
| 539 |
schemePortResolver, dnsResolver); |
| 540 |
|
| 541 |
final HttpHost target = new HttpHost("localhost"); |
| 542 |
final byte[] ip = new byte[] {10, 0, 0, 10}; |
| 543 |
final HttpRoute route = new HttpRoute(target, InetAddress.getByAddress(ip), false); |
| 544 |
final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveLocalAddress(route); |
| 545 |
|
| 546 |
Assert.assertNotNull(address); |
| 547 |
Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); |
| 548 |
Assert.assertEquals(0, address.getPort()); |
| 549 |
} |
| 550 |
|
| 551 |
@Test |
| 552 |
public void testResolveLocalAddressNull() throws Exception { |
| 553 |
final InternalAddressResolver addressResolver = new InternalAddressResolver( |
| 554 |
schemePortResolver, dnsResolver); |
| 555 |
|
| 556 |
final HttpHost target = new HttpHost("localhost"); |
| 557 |
final HttpRoute route = new HttpRoute(target); |
| 558 |
final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveLocalAddress(route); |
| 559 |
|
| 560 |
Assert.assertNull(address); |
| 561 |
} |
| 562 |
|
| 563 |
@Test |
| 564 |
public void testResolveRemoteAddress() throws Exception { |
| 565 |
final InternalAddressResolver addressResolver = new InternalAddressResolver( |
| 566 |
schemePortResolver, dnsResolver); |
| 567 |
|
| 568 |
final HttpHost target = new HttpHost("somehost", 80); |
| 569 |
final HttpRoute route = new HttpRoute(target); |
| 570 |
|
| 571 |
Mockito.when(schemePortResolver.resolve(target)).thenReturn(123); |
| 572 |
final byte[] ip = new byte[] {10, 0, 0, 10}; |
| 573 |
Mockito.when(dnsResolver.resolve("somehost")).thenReturn(new InetAddress[] {InetAddress.getByAddress(ip)}); |
| 574 |
|
| 575 |
final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route); |
| 576 |
|
| 577 |
Assert.assertNotNull(address); |
| 578 |
Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); |
| 579 |
Assert.assertEquals(123, address.getPort()); |
| 580 |
} |
| 581 |
|
| 582 |
@Test |
| 583 |
public void testResolveRemoteAddressViaProxy() throws Exception { |
| 584 |
final InternalAddressResolver addressResolver = new InternalAddressResolver( |
| 585 |
schemePortResolver, dnsResolver); |
| 586 |
|
| 587 |
final HttpHost target = new HttpHost("somehost", 80); |
| 588 |
final HttpHost proxy = new HttpHost("someproxy"); |
| 589 |
final HttpRoute route = new HttpRoute(target, null, proxy, false); |
| 590 |
|
| 591 |
Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8888); |
| 592 |
final byte[] ip = new byte[] {10, 0, 0, 10}; |
| 593 |
Mockito.when(dnsResolver.resolve("someproxy")).thenReturn(new InetAddress[] {InetAddress.getByAddress(ip)}); |
| 594 |
|
| 595 |
final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route); |
| 596 |
|
| 597 |
Assert.assertNotNull(address); |
| 598 |
Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); |
| 599 |
Assert.assertEquals(8888, address.getPort()); |
| 600 |
} |
| 601 |
|
| 602 |
} |