HTTPCLIENT-1923: fixed incorrect connection close on shutdown + fixed corresponding test

This commit is contained in:
Aleksei Arsenev 2018-05-11 13:14:47 +03:00 committed by Oleg Kalnichevski
parent 460b60607d
commit eb27f9ee29
2 changed files with 8 additions and 22 deletions

View File

@ -157,7 +157,9 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
@Override @Override
public void close() { public void close() {
shutdown(); if (this.isShutdown.compareAndSet(false, true)) {
closeConnection();
}
} }
HttpRoute getRoute() { HttpRoute getRoute() {
@ -206,7 +208,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
}; };
} }
private void closeConnection() { private synchronized void closeConnection() {
if (this.conn != null) { if (this.conn != null) {
this.log.debug("Closing connection"); this.log.debug("Closing connection");
try { try {
@ -220,20 +222,6 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
} }
} }
private void shutdownConnection() {
if (this.conn != null) {
this.log.debug("Shutting down connection");
try {
this.conn.shutdown();
} catch (final IOException iox) {
if (this.log.isDebugEnabled()) {
this.log.debug("I/O exception shutting down connection", iox);
}
}
this.conn = null;
}
}
private void checkExpiry() { private void checkExpiry() {
if (this.conn != null && System.currentTimeMillis() >= this.expiry) { if (this.conn != null && System.currentTimeMillis() >= this.expiry) {
if (this.log.isDebugEnabled()) { if (this.log.isDebugEnabled()) {
@ -373,10 +361,8 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
} }
@Override @Override
public synchronized void shutdown() { public void shutdown() {
if (this.isShutdown.compareAndSet(false, true)) { close();
shutdownConnection();
}
} }
} }

View File

@ -278,7 +278,7 @@ public class TestBasicHttpClientConnectionManager {
mgr.shutdown(); mgr.shutdown();
Mockito.verify(conn, Mockito.times(1)).shutdown(); Mockito.verify(conn, Mockito.times(1)).close();
try { try {
final ConnectionRequest connRequest2 = mgr.requestConnection(route, null); final ConnectionRequest connRequest2 = mgr.requestConnection(route, null);
@ -292,7 +292,7 @@ public class TestBasicHttpClientConnectionManager {
mgr.closeIdleConnections(0L, TimeUnit.MILLISECONDS); mgr.closeIdleConnections(0L, TimeUnit.MILLISECONDS);
mgr.shutdown(); mgr.shutdown();
Mockito.verify(conn, Mockito.times(1)).shutdown(); Mockito.verify(conn, Mockito.times(1)).close();
} }
@Test @Test