} catch (Exception e) {
long now = System.nanoTime();
latency = now - startTime;
ConnectionException connectionException;
if (!(e instanceof ConnectionException))
connectionException = new UnknownException(e);
else
connectionException = (ConnectionException)e;
connectionException.setLatency(latency);
if (!(connectionException instanceof IsTimeoutException)) {
pool.addLatencySample(latency, now);
}
else {
pool.addLatencySample(TimeUnit.NANOSECONDS.convert(config.getSocketTimeout(), TimeUnit.MILLISECONDS), System.nanoTime());
}
lastException = connectionException;
throw lastException;
}
}
@Override
public void close() {
if (isOpen) {
monitor.incConnectionClosed(getHost(), lastException);
executor.submit(new Runnable() {
@Override
public void run() {
final TestHostType type = TestHostType
.get(getHost().getPort());
type.close();
isOpen = false;
}
});
}
}
@Override
public HostConnectionPool<TestClient> getHostConnectionPool() {
return pool;
}
@Override
public ConnectionException getLastException() {
return lastException;
}
@Override
public void open() throws ConnectionException {
TestHostType type = TestHostType.get(getHost().getPort());
try {
type.open(0);
isOpen = true;
monitor.incConnectionCreated(getHost());
} catch (ConnectionException e) {
lastException = e;
e.setHost(getHost());
monitor.incConnectionCreateFailed(getHost(), e);
throw e;
}
}
@Override
public void openAsync(final AsyncOpenCallback<TestClient> callback) {
final Connection<TestClient> This = this;
executor.submit(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("MockConnectionFactory");
try {
open();
callback.success(This);
} catch (ConnectionException e) {
callback.failure(This, e);
} catch (Exception e) {
callback.failure(This, new UnknownException(
"Error openning async connection", e));
}
}
});
}