|
| 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 | +} |
0 commit comments