Skip to content

Commit d944a4b

Browse files
author
Javen
committed
Add tests for connect timed out and read timed out
1 parent 5a12eb9 commit d944a4b

File tree

7 files changed

+162
-38
lines changed

7 files changed

+162
-38
lines changed

src/cn/jpush/api/common/APIConnectionException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
public class APIConnectionException extends Exception {
1313
private static final long serialVersionUID = -2615370590441195647L;
1414
private boolean readTimedout = false;
15+
private int doneRetriedTimes = 0;
1516

1617
public APIConnectionException(String message, Throwable e) {
1718
super(message, e);
1819
}
1920

21+
public APIConnectionException(String message, Throwable e, int doneRetriedTimes) {
22+
super(message, e);
23+
this.doneRetriedTimes = doneRetriedTimes;
24+
}
25+
2026
public APIConnectionException(String message, Throwable e, boolean readTimedout) {
2127
super(message, e);
2228
this.readTimedout = readTimedout;
@@ -26,6 +32,9 @@ public boolean isReadTimedout() {
2632
return readTimedout;
2733
}
2834

35+
public int getDoneRetriedTimes() {
36+
return this.doneRetriedTimes;
37+
}
2938
}
3039

3140

src/cn/jpush/api/common/IHttpClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ public enum RequestMethod {
2828
+ "If the problem persists, please let us know at [email protected].";
2929

3030
public static final String CONNECT_TIMED_OUT_MESSAGE = "connect timed out. \n"
31-
+ "Connect to JPush Server timed out, and already retried three times. "
31+
+ "Connect to JPush Server timed out, and already retried some times. \n"
3232
+ "Please ensure your internet connection is ok. \n"
3333
+ "If the problem persists, please let us know at [email protected].";
3434

3535
public static final String READ_TIMED_OUT_MESSAGE = "Read timed out. \n"
36-
+ "Read response from JPush Server timed out. For Push action, maybe you could not retry. "
37-
+ "Maybe JPush server is a little slowly. \n"
36+
+ "Read response from JPush Server timed out. \n"
37+
+ "If this is a Push action, you may not want to retry. \n"
38+
+ "It may be due to slowly response from JPush server, or unstable connection. \n"
3839
+ "If the problem persists, please let us know at [email protected].";
3940

4041
public static Gson _gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

src/cn/jpush/api/common/NativeHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public ResponseWrapper sendRequest(String url, String content,
6161
throw new APIConnectionException(READ_TIMED_OUT_MESSAGE, e, true);
6262
} else { // connect timed out
6363
if (retryTimes >= _maxRetryTimes) {
64-
throw new APIConnectionException(CONNECT_TIMED_OUT_MESSAGE, e);
64+
throw new APIConnectionException(CONNECT_TIMED_OUT_MESSAGE, e, retryTimes);
6565
} else {
6666
LOG.debug("connect timed out - retry again - " + (retryTimes + 1));
6767
}

test/cn/jpush/api/push/mock/BaseMockTest.java

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import java.io.IOException;
66
import java.net.URL;
7-
import java.util.ArrayList;
8-
import java.util.List;
97
import java.util.Map;
108

119
import org.junit.After;
@@ -16,7 +14,6 @@
1614
import cn.jpush.api.common.APIConnectionException;
1715
import cn.jpush.api.common.APIRequestException;
1816
import cn.jpush.api.push.PushClient;
19-
import cn.jpush.api.push.PushResult;
2017

2118
import com.google.gson.JsonArray;
2219
import com.google.gson.JsonElement;
@@ -27,16 +24,7 @@
2724
import com.squareup.okhttp.mockwebserver.MockWebServer;
2825
import com.squareup.okhttp.mockwebserver.RecordedRequest;
2926

30-
public class BaseMockTest {
31-
public static final String REMOTE_HOST = "https://localhost:8081";
32-
public static final String REMOTE_PATH = "/v3/push";
33-
34-
public static final String appKey ="dd1066407b044738b6479275";
35-
public static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
36-
37-
public static final String CONTENT_TYPE_JSON = "application/json";
38-
public static final List<String> SUPPORT_PLATFORM = new ArrayList<String>();
39-
public static final List<String> SUPPORT_AUDIENCE = new ArrayList<String>();
27+
public class BaseMockTest implements IMockTest {
4028

4129
static {
4230
SUPPORT_PLATFORM.add("android");
@@ -50,25 +38,6 @@ public class BaseMockTest {
5038
SUPPORT_AUDIENCE.add("segment");
5139
}
5240

53-
public static final int SUCCEED_RESULT_CODE = 0;
54-
public static final int LACK_OF_PARAMS = 1002;
55-
public static final int INVALID_PARAMS = 1003;
56-
public static final int AUTHENTICATION_FAIL = 1004;
57-
public static final int TOO_BIG = 1005;
58-
public static final int APPKEY_NOT_EXIST = 1008;
59-
60-
public static final String ALERT = "JPush Test - alert";
61-
public static final String MSG_CONTENT = "JPush Test - msgContent";
62-
63-
public static final String TAG1 = "tag1";
64-
public static final String TAG2 = "tag2";
65-
public static final String TAG_ALL = "tag_all";
66-
public static final String TAG_NO = "tag_no";
67-
public static final String ALIAS1 = "alias1";
68-
public static final String ALIAS2 = "alias2";
69-
public static final String ALIAS_NO = "alias_no";
70-
public static final String REGISTRATION_ID1 = "0900e8d85ef";
71-
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
7241

7342
protected static PushClient _client = null;
7443
protected static MockWebServer _mockServer = null;
@@ -179,7 +148,7 @@ public void after() {
179148
}
180149

181150
try {
182-
PushResult result = _client.sendPush(_currentPayload);
151+
_client.sendPush(_currentPayload);
183152

184153
} catch (APIConnectionException e) {
185154
e.printStackTrace();
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package cn.jpush.api.push.mock;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
import java.net.URL;
7+
8+
import org.junit.After;
9+
import org.junit.Test;
10+
11+
import cn.jpush.api.common.APIConnectionException;
12+
import cn.jpush.api.common.APIRequestException;
13+
import cn.jpush.api.common.IHttpClient;
14+
import cn.jpush.api.push.PushClient;
15+
import cn.jpush.api.push.model.PushPayload;
16+
17+
import com.google.gson.JsonObject;
18+
import com.google.gson.JsonPrimitive;
19+
import com.squareup.okhttp.mockwebserver.MockResponse;
20+
import com.squareup.okhttp.mockwebserver.MockWebServer;
21+
import com.squareup.okhttp.mockwebserver.SocketPolicy;
22+
23+
/**
24+
* Record: MockResponse->throttleBody will delay response body, but sometimes has no effect.
25+
* MockResponse->setBodyDelayTimeMs has no effect for delay test.
26+
*
27+
*/
28+
public class ConnectionExceptionTest implements IMockTest {
29+
private PushClient _client = null;
30+
private MockWebServer _server = new MockWebServer();
31+
32+
private static final String SIMPLE_CONTNET = PushPayload.alertAll("alert").toString();
33+
34+
@Test
35+
public void test_read_timeout_disconnect_at_start() throws Exception {
36+
// disconnect at start. This will cause "Read timed out"
37+
_server.enqueue(new MockResponse()
38+
.setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)
39+
);
40+
connect_read_timeout();
41+
}
42+
43+
@Test
44+
public void test_connect_timeout_and_retry() throws Exception {
45+
_server.enqueue(new MockResponse()
46+
.setBody(SIMPLE_CONTNET)
47+
);
48+
49+
_server.play();
50+
_client = new PushClient(masterSecret, appKey);
51+
// connecting to a un-existed address will cause "connect timed out"
52+
_client.setBaseUrl("http://192.132.143.11" + ":81/");
53+
54+
long start = System.currentTimeMillis();
55+
try {
56+
_client.sendPush(SIMPLE_CONTNET);
57+
fail("Should not go here for connection exception.");
58+
} catch (APIConnectionException e) {
59+
e.printStackTrace();
60+
assertFalse("connect timed out", e.isReadTimedout());
61+
assertEquals("Retried", IHttpClient.DEFAULT_MAX_RETRY_TIMES, e.getDoneRetriedTimes());
62+
63+
long end = System.currentTimeMillis();
64+
assertTrue("Retried", (end - start) > IHttpClient.DEFAULT_CONNECTION_TIMEOUT
65+
* IHttpClient.DEFAULT_MAX_RETRY_TIMES);
66+
67+
} catch (APIRequestException e) {
68+
}
69+
}
70+
71+
72+
73+
74+
public void connect_read_timeout() throws IOException {
75+
init();
76+
77+
try {
78+
_client.sendPush(SIMPLE_CONTNET);
79+
fail("Should not go here for connection exception.");
80+
} catch (APIConnectionException e) {
81+
e.printStackTrace();
82+
assertTrue("Read timed out", e.isReadTimedout());
83+
} catch (APIRequestException e) {
84+
}
85+
}
86+
87+
88+
public void init() throws IOException {
89+
_server.play();
90+
91+
URL mockUrl = _server.getUrl("/v3/push/");
92+
System.out.println("Server Url - " + mockUrl);
93+
94+
_client = new PushClient(masterSecret, appKey);
95+
_client.setBaseUrl(mockUrl.toString());
96+
}
97+
98+
@After
99+
public void after() throws IOException {
100+
_server.shutdown();
101+
}
102+
103+
protected String getResponseOK(int msgid, int sendno) {
104+
JsonObject json = new JsonObject();
105+
json.add("msg_id", new JsonPrimitive(msgid));
106+
json.add("sendno", new JsonPrimitive(sendno));
107+
return json.toString();
108+
}
109+
110+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.jpush.api.push.mock;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public interface IMockTest {
7+
public static final String REMOTE_PATH = "/v3/push";
8+
9+
public static final String appKey ="dd1066407b044738b6479275";
10+
public static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
11+
12+
public static final String CONTENT_TYPE_JSON = "application/json";
13+
public static final List<String> SUPPORT_PLATFORM = new ArrayList<String>();
14+
public static final List<String> SUPPORT_AUDIENCE = new ArrayList<String>();
15+
16+
public static final int SUCCEED_RESULT_CODE = 0;
17+
public static final int LACK_OF_PARAMS = 1002;
18+
public static final int INVALID_PARAMS = 1003;
19+
public static final int AUTHENTICATION_FAIL = 1004;
20+
public static final int TOO_BIG = 1005;
21+
public static final int APPKEY_NOT_EXIST = 1008;
22+
23+
public static final String ALERT = "JPush Test - alert";
24+
public static final String MSG_CONTENT = "JPush Test - msgContent";
25+
26+
public static final String TAG1 = "tag1";
27+
public static final String TAG2 = "tag2";
28+
public static final String TAG_ALL = "tag_all";
29+
public static final String TAG_NO = "tag_no";
30+
public static final String ALIAS1 = "alias1";
31+
public static final String ALIAS2 = "alias2";
32+
public static final String ALIAS_NO = "alias_no";
33+
public static final String REGISTRATION_ID1 = "0900e8d85ef";
34+
public static final String REGISTRATION_ID2 = "0a04ad7d8b4";
35+
}

test/cn/jpush/api/push/mock/ExceptionTest.java renamed to test/cn/jpush/api/push/mock/ResponseErrorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.google.gson.JsonObject;
1010
import com.google.gson.JsonPrimitive;
1111

12-
public class ExceptionTest extends BaseMockTest {
12+
public class ResponseErrorTest extends BaseMockTest {
1313

1414
// @Test
1515
// public void appKeyNotExist() {

0 commit comments

Comments
 (0)