Skip to content

Commit 8584966

Browse files
author
Justin Steffy
committed
Clean up http client when setting a new one.
1 parent 9b51c26 commit 8584966

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/main/java/com/orgsync/api/ApiClientImpl.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616
package com.orgsync.api;
1717

1818
import java.io.IOException;
@@ -70,7 +70,14 @@
7070

7171
@Override
7272
public void destroy() {
73-
getHttpClient().close();
73+
closeHttpClient();
74+
}
75+
76+
private void closeHttpClient() {
77+
if (getHttpClient() != null) {
78+
getHttpClient().close();
79+
}
80+
7481
}
7582

7683
@Override
@@ -85,6 +92,10 @@ public AsyncHttpClient getHttpClient() {
8592

8693
@Override
8794
public ApiClientImpl setHttpClient(final AsyncHttpClient client) {
95+
Util.checkNotNull(client);
96+
97+
closeHttpClient();
98+
8899
this.client = client;
89100
return this;
90101
}

src/test/java/com/orgsync/api/ApiClientImplTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/
15+
*/
1616
package com.orgsync.api;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertSame;
1920
import static org.mockito.Matchers.any;
2021
import static org.mockito.Mockito.mock;
2122
import static org.mockito.Mockito.verify;
@@ -72,6 +73,20 @@ public void testDestroy() {
7273
verify(http).close();
7374
}
7475

76+
@Test
77+
public void testSetHttpClient() throws Exception {
78+
AsyncHttpClient newClient = mock(AsyncHttpClient.class);
79+
80+
client.setHttpClient(newClient);
81+
82+
assertSame(newClient, client.getHttpClient());
83+
84+
client.destroy();
85+
86+
verify(http).close();
87+
verify(newClient).close();
88+
}
89+
7590
@Test
7691
@SuppressWarnings({ "rawtypes", "unchecked" })
7792
public void testGetResource() throws Exception {

0 commit comments

Comments
 (0)