Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #157 from karldmoore/master
Browse files Browse the repository at this point in the history
Added a connection and read timeout to Sender
  • Loading branch information
dgnemo authored Jan 14, 2017
2 parents 2fec73a + 204ba12 commit c40d821
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public class Sender {
Logger.getLogger(Sender.class.getName());

private final String key;

private int connectTimeout;
private int readTimeout;

/**
* Default constructor.
*
Expand All @@ -102,6 +104,34 @@ public Sender(String key) {
this.key = nonNull(key);
}

/**
* Set the underlying URLConnection's connect timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.
* <p>
* Default is the system's default timeout.
*
* @see java.net.URLConnection#setConnectTimeout(int)
*/
public final void setConnectTimeout(int connectTimeout) {
if (connectTimeout < 0) {
throw new IllegalArgumentException("timeout can not be negative");
}
this.connectTimeout = connectTimeout;
}

/**
* Set the underlying URLConnection's read timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.
* <p>
* Default is the system's default timeout.
*
* @see java.net.URLConnection#setReadTimeout(int)
*/
public final void setReadTimeout(int readTimeout) {
if (readTimeout < 0) {
throw new IllegalArgumentException("timeout can not be negative");
}
this.readTimeout = readTimeout;
}

/**
* Sends a message to one device, retrying in case of unavailability.
*
Expand Down Expand Up @@ -625,7 +655,10 @@ protected static void addParameter(StringBuilder body, String name,
* Gets an {@link HttpURLConnection} given an URL.
*/
protected HttpURLConnection getConnection(String url) throws IOException {
return (HttpURLConnection) new URL(url).openConnection();
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(readTimeout);
return conn;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ public void testConstructor_null() {
new Sender(null);
}

@Test(expected = IllegalArgumentException.class)
public void testSetConnectTimeout_lessThanZero() {
sender.setConnectTimeout(-1);
}

@Test
public void testSetConnectTimeout() throws IOException {
int connectTimeout = 5000;
sender.setConnectTimeout(connectTimeout);

// does not establish the actual network connection on creation see java.util.URL#openConnection
HttpURLConnection connection = sender.getConnection("http://www.google.com");
assertEquals(connectTimeout, connection.getConnectTimeout());
}

@Test(expected = IllegalArgumentException.class)
public void testSetReadTimeout_lessThanZero() {
sender.setReadTimeout(-1);
}

@Test
public void testSetReadTimeout() throws IOException {
int readTimeout = 5000;
sender.setReadTimeout(readTimeout);

// does not establish the actual network connection on creation see java.util.URL#openConnection
HttpURLConnection connection = sender.getConnection("http://www.google.com");
assertEquals(readTimeout, connection.getReadTimeout());
}

@Test
public void testSend_noRetryOk() throws Exception {
doNotSleep();
Expand Down

0 comments on commit c40d821

Please sign in to comment.