Skip to content

Commit 4467eb4

Browse files
committed
BAEL-11410: Add timeout OkHttp live tests
1 parent 2b9a239 commit 4467eb4

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.baeldung.okhttp;
2+
3+
import okhttp3.MediaType;
4+
import okhttp3.OkHttpClient;
5+
import okhttp3.Request;
6+
import okhttp3.RequestBody;
7+
import org.junit.Test;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.io.InterruptedIOException;
12+
import java.net.SocketTimeoutException;
13+
import java.util.concurrent.TimeUnit;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.catchThrowable;
17+
18+
public class OkHttpTimeoutLiveTest {
19+
20+
private static Logger logger = LoggerFactory.getLogger(OkHttpTimeoutLiveTest.class);
21+
22+
private static final String HTTP_NON_ROUTABLE_ADDRESS = "http://203.0.113.1";
23+
private static final String HTTPS_ADDRESS_DELAY_2 = "https://httpbin.org/delay/2";
24+
25+
@Test
26+
public void whenConnectTimeoutExceededThenSocketTimeoutException() {
27+
// Given
28+
final OkHttpClient client = new OkHttpClient.Builder()
29+
.connectTimeout(10, TimeUnit.MILLISECONDS)
30+
.build();
31+
32+
Request request = new Request.Builder()
33+
.url(HTTP_NON_ROUTABLE_ADDRESS)
34+
.build();
35+
36+
// When
37+
Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
38+
39+
// Then
40+
assertThat(thrown).isInstanceOf(SocketTimeoutException.class);
41+
42+
logThrown(thrown);
43+
}
44+
45+
@Test
46+
public void whenReadTimeoutExceededThenSocketTimeoutException() {
47+
// Given
48+
final OkHttpClient client = new OkHttpClient.Builder()
49+
.readTimeout(10, TimeUnit.MILLISECONDS)
50+
.build();
51+
52+
Request request = new Request.Builder()
53+
.url(HTTPS_ADDRESS_DELAY_2)
54+
.build();
55+
56+
// When
57+
Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
58+
59+
// Then
60+
assertThat(thrown).isInstanceOf(SocketTimeoutException.class);
61+
62+
logThrown(thrown);
63+
}
64+
65+
@Test
66+
public void whenWriteTimeoutExceededThenSocketTimeoutException() {
67+
// Given
68+
final OkHttpClient client = new OkHttpClient.Builder()
69+
.writeTimeout(10, TimeUnit.MILLISECONDS)
70+
.build();
71+
72+
Request request = new Request.Builder()
73+
.url(HTTPS_ADDRESS_DELAY_2)
74+
.post(RequestBody.create(MediaType.parse("text/plain"), create1MBString()))
75+
.build();
76+
77+
// When
78+
Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
79+
80+
// Then
81+
assertThat(thrown).isInstanceOf(SocketTimeoutException.class);
82+
83+
logThrown(thrown);
84+
}
85+
86+
@Test
87+
public void whenCallTimeoutExceededThenSocketTimeoutException() {
88+
// Given
89+
final OkHttpClient client = new OkHttpClient.Builder()
90+
.callTimeout(1, TimeUnit.SECONDS)
91+
.build();
92+
93+
Request request = new Request.Builder()
94+
.url(HTTPS_ADDRESS_DELAY_2)
95+
.build();
96+
97+
// When
98+
Throwable thrown = catchThrowable(() -> client.newCall(request).execute());
99+
100+
// Then
101+
assertThat(thrown).isInstanceOf(InterruptedIOException.class);
102+
103+
logThrown(thrown);
104+
}
105+
106+
private void logThrown(Throwable thrown) {
107+
logger.info("Thrown: ", thrown);
108+
}
109+
110+
private String create1MBString() {
111+
return new String(new char[512 * 1024]);
112+
}
113+
}

0 commit comments

Comments
 (0)