Okhttp: MockWebServer timeout issues

Created on 13 Jun 2016  路  5Comments  路  Source: square/okhttp

MockWebServer timeout issue

Since I have upgraded from 3.2.0 to 3.3.1, our CI has timed out while using the MockWebServer.

Code:

The test case is similar to: https://github.com/JakeWharton/retrofit1-okhttp3-client/blob/master/src/test/java/com/jakewharton/retrofit/Ok3ClientIntegrationTest.java#L23.

public final class ServiceTest {

    @Rule public final MockWebServer server = new MockWebServer();
    protected String mockEndPoint;

    @Before
    public void setUp() throws Exception {
        mockEndPoint = server.url("/").toString();
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testAcquireToken() {
        // Response
        final String mockResponse = "{\n" +
                "  \"access_token\": \"" + TOKEN_VALID + "\",\n" +
                "  \"refresh_token\": \"" + TOKEN_VALID + "\",\n" +
                "  \"token_type\": \"bearer\",\n" +
                "  \"expires_in\": 3600,\n" +
                "  \"logout_token\": \"r0ZZSZDVbUma9sTz0ZXduwvic1V7RdSIZMe\\/ysHTY8s=\"\n" +
                "}";

        server.enqueue(new MockResponse()
                .setResponseCode(HttpURLConnection.HTTP_OK)
                .setBody(mockResponse));

        // Request
        final Map<String, String> formData = new HashMap<>();
        formData.put("grant_type", "password");
        formData.put("client_id", "fake-id");
        formData.put("client_secret", "fake-secret");
        formData.put("username", "user EMAIL");
        formData.put("password", "user PASSWORD");

        final TokenResponse response = Service.getInstance(mockEndPoint)
                .acquireToken(formData)
                .toBlocking()
                .first();

        assertThat(response.getAccessToken()).isEqualTo("valid");
    }
}

Error:

19:44:09 <>Test > testAcquireToken STANDARD_ERROR
19:44:09     Jun 10, 2016 12:45:41 PM okhttp3.mockwebserver.MockWebServer$3 execute
19:44:09     INFO: MockWebServer[63326] starting to accept connections
19:44:09     Jun 10, 2016 12:45:41 PM okhttp3.mockwebserver.MockWebServer$4 processOneRequest
19:44:09     INFO: MockWebServer[63326] received request: POST /auth/v1/token HTTP/1.1 and responded: HTTP/1.1 200 OK
19:44:09     Jun 10, 2016 12:45:41 PM okhttp3.mockwebserver.MockWebServer$3 acceptConnections
19:44:09     INFO: MockWebServer[63326] done accepting connections: Socket closed
20:09:59 Build timed out (after 30 minutes). Marking the build as failed.

Most helpful comment

Awww, that鈥檚 awkward. We鈥檝e seen other problems with Robolectric, mostly around how OkHttp attempts to detect which platform it鈥檚 running on. Robolectric makes that very difficult because it looks like Android and the JVM simultaneously.

All 5 comments

After playing with the dependencies, it seems that rolling back Robolectric to 3.1-rc1 from 3.1 seems to work fine:

    testCompile "org.robolectric:shadows-play-services:3.1-rc1"                         // Slowing down tests
    testCompile "org.robolectric:shadows-support-v4:3.1-rc1"                            // Slowing down tests

Awww, that鈥檚 awkward. We鈥檝e seen other problems with Robolectric, mostly around how OkHttp attempts to detect which platform it鈥檚 running on. Robolectric makes that very difficult because it looks like Android and the JVM simultaneously.

If you can figure out what we need to do to interop better with Robolectric, please send a pull request. I鈥檓 not a Robolectric user so I find that to be difficult!

@swankjesse I understand. Thanks for reading the issue. I ended up rolling back robolectric. I am starting to look into separating my tests from robolectric and looking into ui tests as well.

@swankjesse i also face similar issue. i am not sure what happened. do you know how to solve that?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yschimke picture yschimke  路  3Comments

yschimke picture yschimke  路  3Comments

vanshg picture vanshg  路  3Comments

albka1986 picture albka1986  路  3Comments

SElab2019 picture SElab2019  路  3Comments