Skip to content

Commit

Permalink
Fix flaky URLConnectionTest.serverShutdownOutput test
Browse files Browse the repository at this point in the history
There are usually 2 requests sent to '/b' during the test:
square#1 on the busted connection, attempting reuse
square#2 on a new connection

On successful runs, the server recorded square#1 and then square#2 and added them to the queue in that order.

On failed runs, the server started reading request square#1 before square#2, but there was a context switch, and it finished reading square#2 before square#1, recording them in that order. Since square#1 was the last to be recorded, the test incorrectly used it to assert that the sequence number was 0. Since square#1 was the second to be received on the busted connection, it had a sequence number of 1 so the test failed.

The fix removes the assumption that square#2 is the last to be recorded.

Fixes square#4140.
  • Loading branch information
amirlivneh committed Dec 25, 2018
1 parent a36c315 commit f2006dc
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,14 @@ private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception
assertContent("This comes after a busted connection", connection2);

// Check that a fresh connection was created, either immediately or after attempting reuse.
// We know that a fresh connection was created if the server recorded a request with sequence
// number 0. Since the client may have attempted to reuse the broken connection just before
// creating a fresh connection, the server may have recorded 2 requests at this point. The order
// of recording is non-deterministic.
RecordedRequest requestAfter = server.takeRequest();
if (server.getRequestCount() == 3) {
requestAfter = server.takeRequest(); // The failure consumed a response.
}
// sequence number 0 means the HTTP socket connection was not reused
assertEquals(0, requestAfter.getSequenceNumber());
assertTrue(
requestAfter.getSequenceNumber() == 0
|| server.getRequestCount() == 3 && server.takeRequest().getSequenceNumber() == 0);
}

enum WriteKind {BYTE_BY_BYTE, SMALL_BUFFERS, LARGE_BUFFERS}
Expand Down

0 comments on commit f2006dc

Please sign in to comment.