55import io .reactivex .Observer ;
66import io .reactivex .disposables .Disposable ;
77import junit .framework .TestCase ;
8+ import okhttp3 .OkHttpClient ;
9+ import okhttp3 .Request ;
10+ import okhttp3 .Response ;
811
912import 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