Skip to content

Commit 55a944c

Browse files
jwfingleeyeh
authored andcommitted
enable LeanCloud#setNetworkTimeout(seconds) method
1 parent a4cd17a commit 55a944c

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

core/src/main/java/cn/leancloud/core/AppConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class AppConfiguration {
2020
public interface SchedulerCreator{
2121
Scheduler create();
2222
}
23-
public static final int DEFAULT_NETWORK_TIMEOUT = 30;
23+
public static final int DEFAULT_NETWORK_TIMEOUT = 10;
2424

2525
private static LCACL defaultACL;
2626
private static int networkTimeout = DEFAULT_NETWORK_TIMEOUT;
@@ -49,8 +49,11 @@ public interface SchedulerCreator{
4949
private static final String DEFAULT_USER_AGENT = "LeanCloud-Java-SDK/" + SDK_VERSION;
5050

5151
public static void setNetworkTimeout(int seconds) {
52-
networkTimeout = seconds;
52+
if (seconds > 0) {
53+
networkTimeout = seconds;
54+
}
5355
}
56+
5457
public static int getNetworkTimeout() {
5558
return networkTimeout;
5659
}

core/src/main/java/cn/leancloud/core/PaasClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public class PaasClient {
2626
public static OkHttpClient getGlobalOkHttpClient() {
2727
if (null == globalHttpClient) {
2828
globalHttpClient = new OkHttpClient.Builder()
29-
.connectTimeout(15, TimeUnit.SECONDS)
30-
.readTimeout(10, TimeUnit.SECONDS)
31-
.writeTimeout(10, TimeUnit.SECONDS)
29+
.connectTimeout(AppConfiguration.getNetworkTimeout(), TimeUnit.SECONDS)
30+
.readTimeout(AppConfiguration.getNetworkTimeout(), TimeUnit.SECONDS)
31+
.writeTimeout(AppConfiguration.getNetworkTimeout(), TimeUnit.SECONDS)
3232
.addInterceptor(new RequestPaddingInterceptor())
3333
.addInterceptor(new LoggingInterceptor())
3434
.dns(new DNSDetoxicant())
@@ -91,4 +91,13 @@ public static PushClient getPushClient() {
9191
}
9292
return pushClient;
9393
}
94+
95+
// add method for unit tests in LeanCloudTest.java
96+
static void cleanup() {
97+
apiService = null;
98+
storageClient = null;
99+
pushService = null;
100+
pushClient = null;
101+
globalHttpClient = null;
102+
}
94103
}

core/src/test/java/cn/leancloud/core/LeanCloudTest.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import io.reactivex.Observer;
66
import io.reactivex.disposables.Disposable;
77
import junit.framework.TestCase;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
811

912
import java.util.concurrent.CountDownLatch;
1013

@@ -14,7 +17,6 @@ public class LeanCloudTest extends TestCase {
1417

1518
public LeanCloudTest(String name) {
1619
super(name);
17-
Configure.initializeRuntime();
1820
}
1921

2022
@Override
@@ -23,7 +25,67 @@ protected void setUp() throws Exception {
2325
testSucceed = false;
2426
}
2527

28+
public void testNetworkTimeout() throws Exception {
29+
PaasClient.cleanup();
30+
LeanCloud.setNetworkTimeout(10); // recover to default value.
31+
Configure.initializeRuntime();
32+
OkHttpClient okhttpClient = PaasClient.getGlobalOkHttpClient();
33+
34+
Request request = new Request.Builder().url("https://httpbin.org/delay/15").build();
35+
try {
36+
okhttpClient.newCall(request).execute();
37+
} catch (Exception ex) {
38+
System.out.println("failed to get response, due to time-out");
39+
ex.printStackTrace();
40+
testSucceed = true;
41+
}
42+
assertTrue(testSucceed);
43+
44+
request = new Request.Builder().url("https://httpbin.org/delay/5").build();
45+
try {
46+
Response response = okhttpClient.newCall(request).execute();
47+
System.out.println("Succeed to get response...." + response.body().string());
48+
testSucceed = true;
49+
} catch (Exception ex) {
50+
ex.printStackTrace();
51+
testSucceed = false;
52+
}
53+
assertTrue(testSucceed);
54+
}
55+
56+
public void testNetworkTimeoutWithSpecifiedValue() throws Exception {
57+
PaasClient.cleanup();
58+
LeanCloud.setNetworkTimeout(6);
59+
Configure.initializeRuntime();
60+
OkHttpClient okhttpClient = PaasClient.getGlobalOkHttpClient();
61+
62+
Request request = new Request.Builder().url("https://httpbin.org/delay/8").build();
63+
try {
64+
Response response = okhttpClient.newCall(request).execute();
65+
System.out.println("Succeed to get response...." + response.body().string());
66+
} catch (Exception ex) {
67+
System.out.println("failed to get response, due to time-out");
68+
ex.printStackTrace();
69+
testSucceed = true;
70+
}
71+
assertTrue(testSucceed);
72+
73+
request = new Request.Builder().url("https://httpbin.org/delay/5").build();
74+
try {
75+
Response response = okhttpClient.newCall(request).execute();
76+
System.out.println("Succeed to get response...." + response.body().string());
77+
testSucceed = true;
78+
} catch (Exception ex) {
79+
ex.printStackTrace();
80+
testSucceed = false;
81+
}
82+
assertTrue(testSucceed);
83+
}
84+
2685
public void testServerDate() throws Exception {
86+
PaasClient.cleanup();
87+
LeanCloud.setNetworkTimeout(10); // recover to default value.
88+
Configure.initializeRuntime();
2789
LeanCloud.getServerDateInBackground().subscribe(new Observer<LCDate>() {
2890
@Override
2991
public void onSubscribe(Disposable disposable) {

0 commit comments

Comments
 (0)