[(applysockettimeout)] Apply socket and connection timeout in OkHttpRestfulClientFactory.

This commit is contained in:
Petro Mykhaylyshyn 2019-07-08 14:44:24 +03:00 committed by James Agnew
parent cc20893018
commit 459ff0e64c
2 changed files with 22 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/*
* #%L
@ -62,7 +63,12 @@ public class OkHttpRestfulClientFactory extends RestfulClientFactory {
public synchronized Call.Factory getNativeClient() {
if (myNativeClient == null) {
myNativeClient = new OkHttpClient();
myNativeClient = new OkHttpClient()
.newBuilder()
.connectTimeout(getConnectTimeout(), TimeUnit.MILLISECONDS)
.readTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS)
.writeTimeout(getSocketTimeout(), TimeUnit.MILLISECONDS)
.build();
}
return myNativeClient;

View File

@ -42,4 +42,18 @@ public class OkHttpRestfulClientFactoryTest {
assertSame(okHttpClient, clientFactory.getNativeClient());
}
}
@Test
public void testSocketTimeout() {
clientFactory.setSocketTimeout(1515);
assertEquals(1515, ((OkHttpClient)clientFactory.getNativeClient()).readTimeoutMillis());
assertEquals(1515, ((OkHttpClient)clientFactory.getNativeClient()).writeTimeoutMillis());
}
@Test
public void testConnectTimeout() {
clientFactory.setConnectTimeout(1516);
assertEquals(1516, ((OkHttpClient)clientFactory.getNativeClient()).connectTimeoutMillis());
}
}