Skip to content

Commit 335598c

Browse files
committed
Close client instances!
1 parent f402385 commit 335598c

File tree

2 files changed

+83
-72
lines changed

2 files changed

+83
-72
lines changed

extras/registry/src/test/java/org/asynchttpclient/extras/registry/AbstractAsyncHttpClientFactoryTest.java

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.asynchttpclient.extras.registry;
1414

15+
import java.io.IOException;
1516
import java.lang.reflect.InvocationTargetException;
1617

1718
import junit.extensions.PA;
@@ -34,7 +35,7 @@ public abstract class AbstractAsyncHttpClientFactoryTest {
3435
public static final String TEST_CLIENT_CLASS_NAME = "org.asynchttpclient.extras.registry.TestAsyncHttpClient";
3536
public static final String BAD_CLIENT_CLASS_NAME = "org.asynchttpclient.extras.registry.BadAsyncHttpClient";
3637
public static final String NON_EXISTENT_CLIENT_CLASS_NAME = "org.asynchttpclient.extras.registry.NonExistentAsyncHttpClient";
37-
38+
3839
private Server server;
3940
private int port;
4041

@@ -62,9 +63,7 @@ public void tearDown() throws Exception {
6263
}
6364

6465
/**
65-
* If the property is not found via the system property or properties file
66-
* the default instance of AsyncHttpClient should be returned.
67-
* @throws Exception
66+
* If the property is not found via the system property or properties file the default instance of AsyncHttpClient should be returned.
6867
*/
6968
// ================================================================================================================
7069
@Test(groups = "standalone")
@@ -94,104 +93,107 @@ public void testGetAsyncHttpClientProvider() throws Exception {
9493
// ==================================================================================================================================
9594

9695
/**
97-
* If the class is specified via a system property then that class should be
98-
* returned
96+
* If the class is specified via a system property then that class should be returned
9997
*/
10098
// ===================================================================================================================================
10199
@Test(groups = "standalone")
102-
public void testFactoryWithSystemProperty() {
100+
public void testFactoryWithSystemProperty() throws IOException {
103101
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME);
104102
AsyncHttpClientConfigHelper.reloadProperties();
105-
Assert.assertTrue(AsyncHttpClientFactory.getAsyncHttpClient().getClass().equals(TestAsyncHttpClient.class));
103+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
104+
Assert.assertTrue(ahc.getClass().equals(TestAsyncHttpClient.class));
105+
}
106106
}
107107

108108
@Test(groups = "standalone")
109-
public void testGetAsyncHttpClientConfigWithSystemProperty() {
109+
public void testGetAsyncHttpClientConfigWithSystemProperty() throws IOException {
110110
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME);
111111
AsyncHttpClientConfigHelper.reloadProperties();
112-
AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient();
113-
Assert.assertTrue(asyncHttpClient.getClass().equals(TestAsyncHttpClient.class));
112+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
113+
Assert.assertTrue(ahc.getClass().equals(TestAsyncHttpClient.class));
114+
}
114115
}
115116

116117
@Test(groups = "standalone")
117-
public void testGetAsyncHttpClientProviderWithSystemProperty() {
118+
public void testGetAsyncHttpClientProviderWithSystemProperty() throws IOException {
118119
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, TEST_CLIENT_CLASS_NAME);
119120
AsyncHttpClientConfigHelper.reloadProperties();
120-
AsyncHttpClient asyncHttpClient = AsyncHttpClientFactory.getAsyncHttpClient();
121-
Assert.assertTrue(asyncHttpClient.getClass().equals(TestAsyncHttpClient.class));
121+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
122+
Assert.assertTrue(ahc.getClass().equals(TestAsyncHttpClient.class));
123+
}
122124
}
123125

124126
// ===================================================================================================================================
125127

126128
/**
127-
* If any of the constructors of the class fail then a
128-
* AsyncHttpClientException is thrown.
129+
* If any of the constructors of the class fail then a AsyncHttpClientException is thrown.
129130
*/
130131
// ===================================================================================================================================
131132
@Test(groups = "standalone", expectedExceptions = BadAsyncHttpClientException.class)
132-
public void testFactoryWithBadAsyncHttpClient() {
133+
public void testFactoryWithBadAsyncHttpClient() throws IOException {
133134
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME);
134135
AsyncHttpClientConfigHelper.reloadProperties();
135-
AsyncHttpClientFactory.getAsyncHttpClient();
136-
Assert.fail("BadAsyncHttpClientException should have been thrown before this point");
136+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
137+
Assert.fail("BadAsyncHttpClientException should have been thrown before this point");
138+
}
137139
}
138140

139141
@Test(groups = "standalone")
140-
public void testGetAsyncHttpClientConfigWithBadAsyncHttpClient() {
142+
public void testGetAsyncHttpClientConfigWithBadAsyncHttpClient() throws IOException {
141143
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME);
142144
AsyncHttpClientConfigHelper.reloadProperties();
143-
try {
144-
AsyncHttpClientFactory.getAsyncHttpClient();
145+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
146+
//
145147
} catch (AsyncHttpClientImplException e) {
146148
assertException(e);
147149
}
148-
//Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
150+
// Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
149151
}
150152

151153
@Test(groups = "standalone")
152-
public void testGetAsyncHttpClientProviderWithBadAsyncHttpClient() {
154+
public void testGetAsyncHttpClientProviderWithBadAsyncHttpClient() throws IOException {
153155
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, BAD_CLIENT_CLASS_NAME);
154156
AsyncHttpClientConfigHelper.reloadProperties();
155-
try {
156-
AsyncHttpClientFactory.getAsyncHttpClient();
157+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
158+
//
157159
} catch (AsyncHttpClientImplException e) {
158160
assertException(e);
159161
}
160-
//Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
162+
// Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
161163
}
162164

163165
// ===================================================================================================================================
164166

165167
/*
166-
* If the system property exists instantiate the class else if the class is
167-
* not found throw an AsyncHttpClientException.
168+
* If the system property exists instantiate the class else if the class is not found throw an AsyncHttpClientException.
168169
*/
169170
@Test(groups = "standalone", expectedExceptions = AsyncHttpClientImplException.class)
170-
public void testFactoryWithNonExistentAsyncHttpClient() {
171+
public void testFactoryWithNonExistentAsyncHttpClient() throws IOException {
171172
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, NON_EXISTENT_CLIENT_CLASS_NAME);
172173
AsyncHttpClientConfigHelper.reloadProperties();
173-
AsyncHttpClientFactory.getAsyncHttpClient();
174+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
175+
//
176+
}
174177
Assert.fail("AsyncHttpClientImplException should have been thrown before this point");
175178
}
176179

177180
/**
178-
* If property is specified but the class can’t be created or found for any
179-
* reason subsequent calls should throw an AsyncClientException.
181+
* If property is specified but the class can’t be created or found for any reason subsequent calls should throw an AsyncClientException.
180182
*/
181183
@Test(groups = "standalone", expectedExceptions = AsyncHttpClientImplException.class)
182-
public void testRepeatedCallsToBadAsyncHttpClient() {
184+
public void testRepeatedCallsToBadAsyncHttpClient() throws IOException {
183185
boolean exceptionCaught = false;
184186
System.setProperty(AsyncImplHelper.ASYNC_HTTP_CLIENT_IMPL_SYSTEM_PROPERTY, NON_EXISTENT_CLIENT_CLASS_NAME);
185187
AsyncHttpClientConfigHelper.reloadProperties();
186-
try {
187-
AsyncHttpClientFactory.getAsyncHttpClient();
188+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
189+
//
188190
} catch (AsyncHttpClientImplException e) {
189191
exceptionCaught = true;
190192
}
191193
Assert.assertTrue(exceptionCaught, "Didn't catch exception the first time");
192194
exceptionCaught = false;
193-
try {
194-
AsyncHttpClientFactory.getAsyncHttpClient();
195+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
196+
//
195197
} catch (AsyncHttpClientImplException e) {
196198
exceptionCaught = true;
197199
}

extras/registry/src/test/java/org/asynchttpclient/extras/registry/AsyncHttpClientRegistryTest.java

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.asynchttpclient.extras.registry;
1414

15+
import java.io.IOException;
16+
1517
import org.asynchttpclient.AsyncHttpClient;
1618
import org.asynchttpclient.config.AsyncHttpClientConfigHelper;
1719
import org.asynchttpclient.extras.registry.AsyncHttpClientFactory;
@@ -49,49 +51,56 @@ public void tearDown() {
4951
}
5052

5153
@Test(groups = "standalone")
52-
public void testGetAndRegister() {
53-
AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient();
54-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
55-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
56-
Assert.assertNotNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
54+
public void testGetAndRegister() throws IOException {
55+
try(AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
56+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
57+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
58+
Assert.assertNotNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
59+
}
5760
}
5861

5962
@Test(groups = "standalone")
60-
public void testDeRegister() {
61-
AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient();
62-
Assert.assertFalse(AsyncHttpClientRegistryImpl.getInstance().unregister(TEST_AHC));
63-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
64-
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().unregister(TEST_AHC));
65-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
63+
public void testDeRegister() throws IOException {
64+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
65+
Assert.assertFalse(AsyncHttpClientRegistryImpl.getInstance().unregister(TEST_AHC));
66+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
67+
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().unregister(TEST_AHC));
68+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
69+
}
6670
}
6771

6872
@Test(groups = "standalone")
69-
public void testRegisterIfNew() {
70-
AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient();
71-
AsyncHttpClient ahc2 = AsyncHttpClientFactory.getAsyncHttpClient();
72-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
73-
Assert.assertFalse(AsyncHttpClientRegistryImpl.getInstance().registerIfNew(TEST_AHC, ahc2));
74-
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC) == ahc);
75-
Assert.assertNotNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc2));
76-
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC) == ahc2);
77-
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().registerIfNew(TEST_AHC + 1, ahc));
78-
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 1) == ahc);
73+
public void testRegisterIfNew() throws IOException {
74+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
75+
try (AsyncHttpClient ahc2 = AsyncHttpClientFactory.getAsyncHttpClient()) {
76+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
77+
Assert.assertFalse(AsyncHttpClientRegistryImpl.getInstance().registerIfNew(TEST_AHC, ahc2));
78+
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC) == ahc);
79+
Assert.assertNotNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc2));
80+
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC) == ahc2);
81+
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().registerIfNew(TEST_AHC + 1, ahc));
82+
Assert.assertTrue(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 1) == ahc);
83+
}
84+
}
7985
}
8086

8187
@Test(groups = "standalone")
82-
public void testClearAllInstances() {
83-
AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient();
84-
AsyncHttpClient ahc2 = AsyncHttpClientFactory.getAsyncHttpClient();
85-
AsyncHttpClient ahc3 = AsyncHttpClientFactory.getAsyncHttpClient();
86-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
87-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC + 2, ahc2));
88-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC + 3, ahc3));
89-
Assert.assertEquals(3, AsyncHttpClientRegistryImpl.getInstance().getAllRegisteredNames().size());
90-
AsyncHttpClientRegistryImpl.getInstance().clearAllInstances();
91-
Assert.assertEquals(0, AsyncHttpClientRegistryImpl.getInstance().getAllRegisteredNames().size());
92-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
93-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 2));
94-
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 3));
88+
public void testClearAllInstances() throws IOException {
89+
try (AsyncHttpClient ahc = AsyncHttpClientFactory.getAsyncHttpClient()) {
90+
try (AsyncHttpClient ahc2 = AsyncHttpClientFactory.getAsyncHttpClient()) {
91+
try (AsyncHttpClient ahc3 = AsyncHttpClientFactory.getAsyncHttpClient()) {
92+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC, ahc));
93+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC + 2, ahc2));
94+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().addOrReplace(TEST_AHC + 3, ahc3));
95+
Assert.assertEquals(3, AsyncHttpClientRegistryImpl.getInstance().getAllRegisteredNames().size());
96+
AsyncHttpClientRegistryImpl.getInstance().clearAllInstances();
97+
Assert.assertEquals(0, AsyncHttpClientRegistryImpl.getInstance().getAllRegisteredNames().size());
98+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC));
99+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 2));
100+
Assert.assertNull(AsyncHttpClientRegistryImpl.getInstance().get(TEST_AHC + 3));
101+
}
102+
}
103+
}
95104
}
96105

97106
@Test(groups = "standalone")

0 commit comments

Comments
 (0)