-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable connection and read timeouts
- Loading branch information
Lauri Lehtinen
committed
Jul 28, 2015
1 parent
de9ee69
commit 0336efe
Showing
5 changed files
with
99 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.bugsnag.http; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.net.Socket; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.bugsnag.Configuration; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class HttpClientTest { | ||
|
||
Configuration configuration = new Configuration(); | ||
HttpClient httpClient = new HttpClient(configuration); | ||
|
||
ServerSocket serverSocket; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
// create server with random port and room for single connection | ||
serverSocket = new ServerSocket(0, 1); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
serverSocket.close(); | ||
} | ||
|
||
@Test | ||
public void testConnectionTimeout() throws Exception { | ||
configuration.setConnectionTimeout(10); | ||
|
||
// reserve the only available connection | ||
new Socket().connect(serverSocket.getLocalSocketAddress()); | ||
|
||
try { | ||
httpClient.post("http://localhost:" + serverSocket.getLocalPort(), new ByteArrayInputStream("foo".getBytes())); | ||
} catch (NetworkException e) { | ||
assertEquals("connect timed out", e.getCause().getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testReadTimeout() throws Exception { | ||
configuration.setReadTimeout(10); | ||
|
||
try { | ||
httpClient.post("http://localhost:" + serverSocket.getLocalPort(), new ByteArrayInputStream("foo".getBytes())); | ||
} catch (NetworkException e) { | ||
assertEquals("Read timed out", e.getCause().getMessage()); | ||
} | ||
} | ||
} |