@Test
public void emitProperExceptionOnPoolDepletion()
throws Exception
{
setParameters();
HttpClientFactoryImpl httpClientFactory = null;
try {
// the foreplay: setting up
final RemoteStorageContext globalRemoteStorageContext = new DefaultRemoteStorageContext(null);
final DefaultRemoteConnectionSettings rcs = new DefaultRemoteConnectionSettings();
rcs.setConnectionTimeout(86400000);
globalRemoteStorageContext.setRemoteConnectionSettings(new DefaultRemoteConnectionSettings());
globalRemoteStorageContext.setRemoteProxySettings(mock(RemoteProxySettings.class));
// real provider and initializing it with NexusStarted event
httpClientFactory = new HttpClientFactoryImpl(
Providers.of(mock(SystemStatus.class)),
Providers.of(globalRemoteStorageContext),
mock(EventBus.class),
mock(PoolingClientConnectionManagerMBeanInstaller.class),
null
);
// the RRS instance we test
final HttpClientRemoteStorage underTest =
new HttpClientRemoteStorage(Providers.of(mock(SystemStatus.class)),
mock(MimeSupport.class), mock(QueryStringBuilder.class), new HttpClientManagerImpl(httpClientFactory));
// a mock proxy repository with some mocks to make RRS work
final RemoteStorageContext proxyContext = new DefaultRemoteStorageContext(globalRemoteStorageContext);
final ProxyRepository repository = mock(ProxyRepository.class);
when(repository.getId()).thenReturn("foo");
when(repository.getName()).thenReturn("foo");
when(repository.getRemoteStorageContext()).thenReturn(proxyContext);
// a mock remote server that will simply "hang" to occupy the request socket
final Server server =
Server.withPort(0).serve("/").withBehaviours(Behaviours.pause(Time.days(1))).start();
// the URL we will try to connect to
final String url = "http://foo.com:" + server.getPort() + "/foo/bar.jar";
// the requesting logic packed as Runnable
final Runnable request = new RequesterRunnable(underTest, repository, url);
try {
// we fire 1st request as a Thread, this thread will be blocked as Server will simply "pause"
// this also means, that connection stays leased from pool, and since pool size is 1, we
// intentionally depleted the connection pool (reached max connection count)
final Thread blockedThread = new Thread(request);
blockedThread.start();
// give some time to thread above
Thread.sleep(200);
try {
// in current thread we try to establish 2nd connection
// this here will need to fail, as connection pool is depleted
// ConnectionPoolTimeoutException should be thrown by HC4
// that RRS "repackages" into RemoteStorageTransportOverloadedException
request.run();
// fail if no exception
Assert.fail("RemoteStorageTransportOverloadedException expected!");
}
catch (IllegalStateException e) {
Assert.assertNotNull("We except the cause be RemoteStorageTransportOverloadedException!",
e.getCause());
Assert.assertEquals(RemoteStorageTransportOverloadedException.class, e.getCause().getClass());
}
}
finally {
server.stop();
}
}
finally {
if (httpClientFactory != null) {
httpClientFactory.shutdown();
}
unsetParameters();
}
}