Skip to content

Commit f5a600a

Browse files
authored
fix(DatafileClient): add timeouts to UrlConnections (#432)
1 parent 2487c3e commit f5a600a

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

datafile-handler/src/androidTest/java/com/optimizely/ab/android/datafile_handler/DatafileClientTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.junit.Assert.assertEquals;
3838
import static org.junit.Assert.assertNull;
3939
import static org.mockito.Matchers.any;
40+
import static org.mockito.Matchers.anyInt;
4041
import static org.mockito.Matchers.contains;
4142
import static org.mockito.Matchers.eq;
4243
import static org.mockito.Mockito.doThrow;
@@ -193,8 +194,6 @@ else if (url == url2) {
193194
verify(client).saveLastModified(urlConnection2);
194195
verify(client).readStream(urlConnection2);
195196
verify(urlConnection2).disconnect();
196-
197-
198197
}
199198

200199

@@ -303,4 +302,23 @@ public void handlesEmptyStringResponse() throws MalformedURLException {
303302
when(client.execute(any(Client.Request.class), eq(DatafileClient.REQUEST_BACKOFF_TIMEOUT), eq(DatafileClient.REQUEST_RETRIES_POWER))).thenReturn("");
304303
assertEquals("", datafileClient.request(url.toString()));
305304
}
305+
306+
@Test
307+
public void handlesConnectionAndReadTimeout() throws IOException {
308+
URL url = new URL(new DatafileConfig("1", null).getUrl());
309+
when(client.openConnection(url)).thenReturn(urlConnection);
310+
when(urlConnection.getResponseCode()).thenReturn(200);
311+
doThrow(new IOException()).when(urlConnection).connect();
312+
313+
datafileClient.request(url.toString());
314+
ArgumentCaptor<Client.Request> captor1 = ArgumentCaptor.forClass(Client.Request.class);
315+
verify(client).execute(captor1.capture(), anyInt(), anyInt());
316+
Object response = captor1.getValue().execute();
317+
318+
verify(logger).error(contains("Error making request"), any(IOException.class));
319+
verify(urlConnection).setConnectTimeout(10*1000);
320+
verify(urlConnection).setReadTimeout(60*1000);
321+
verify(urlConnection).disconnect();
322+
}
323+
306324
}

datafile-handler/src/main/java/com/optimizely/ab/android/datafile_handler/DatafileClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
*/
3434
public class DatafileClient {
3535
// easy way to set the connection timeout
36-
public static int CONNECTION_TIMEOUT = 5 * 1000;
36+
public static int CONNECTION_TIMEOUT = 10 * 1000;
37+
public static int READ_TIMEOUT = 60 * 1000;
38+
3739
// the numerical base for the exponential backoff
3840
public static final int REQUEST_BACKOFF_TIMEOUT = 2;
3941
// power the number of retries
@@ -81,8 +83,10 @@ public String execute() {
8183
}
8284

8385
client.setIfModifiedSince(urlConnection);
84-
86+
// set timeouts for releasing failed connections (default is 0 = no timeout).
8587
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
88+
urlConnection.setReadTimeout(READ_TIMEOUT);
89+
8690
urlConnection.connect();
8791

8892
int status = urlConnection.getResponseCode();

event-handler/src/androidTest/java/com/optimizely/ab/android/event_handler/EventClientTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.Before;
2525
import org.junit.Test;
2626
import org.junit.runner.RunWith;
27+
import org.mockito.ArgumentCaptor;
2728
import org.mockito.Mock;
2829
import org.slf4j.Logger;
2930

@@ -33,10 +34,10 @@
3334
import java.net.HttpURLConnection;
3435
import java.net.URL;
3536

36-
import static junit.framework.Assert.assertEquals;
37-
import static junit.framework.Assert.assertFalse;
38-
import static junit.framework.Assert.assertTrue;
3937
import static org.mockito.Matchers.any;
38+
import static org.mockito.Matchers.anyInt;
39+
import static org.mockito.Matchers.contains;
40+
import static org.mockito.Mockito.doThrow;
4041
import static org.mockito.Mockito.mock;
4142
import static org.mockito.Mockito.verify;
4243
import static org.mockito.Mockito.when;
@@ -71,4 +72,23 @@ public void testEventClient() {
7172

7273
verify(logger).debug("SendEvent completed: {}", event);
7374
}
75+
76+
@Test
77+
public void testConnectionAndReadTimeout() throws IOException {
78+
when(client.openConnection(event.getURL())).thenReturn(urlConnection);
79+
when(urlConnection.getResponseCode()).thenReturn(200);
80+
doThrow(new IOException()).when(urlConnection).getOutputStream();
81+
82+
eventClient.sendEvent(event);
83+
84+
85+
ArgumentCaptor<Client.Request> captor1 = ArgumentCaptor.forClass(Client.Request.class);
86+
verify(client).execute(captor1.capture(), anyInt(), anyInt());
87+
Object response = captor1.getValue().execute();
88+
89+
verify(logger).error(contains("Unable to send event"), any(Event.class), any(IOException.class));
90+
verify(urlConnection).setConnectTimeout(10*1000);
91+
verify(urlConnection).setReadTimeout(60*1000);
92+
verify(urlConnection).disconnect();
93+
}
7494
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
* Makes network requests related to events
3131
*/
3232
class EventClient {
33+
// easy way to set the connection timeout
34+
public static int CONNECTION_TIMEOUT = 10 * 1000;
35+
public static int READ_TIMEOUT = 60 * 1000;
36+
3337
private final Client client;
3438
// Package private and non final so it can easily be mocked for tests
3539
private final Logger logger;
@@ -57,6 +61,10 @@ public Boolean execute() {
5761
return Boolean.FALSE;
5862
}
5963

64+
// set timeouts for releasing failed connections (default is 0 = no timeout).
65+
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT);
66+
urlConnection.setReadTimeout(READ_TIMEOUT);
67+
6068
urlConnection.setRequestMethod("POST");
6169
urlConnection.setRequestProperty("Content-Type", "application/json");
6270
urlConnection.setDoOutput(true);

0 commit comments

Comments
 (0)