Skip to content

Commit

Permalink
Merge pull request #3033 from square/jwilson.1211.flaky_ws_test
Browse files Browse the repository at this point in the history
Fix flakiness in RealWebSocketTest.clientAndServerCloseClosesConnection
  • Loading branch information
JakeWharton authored Dec 12, 2016
2 parents d073ffa + 48ee90c commit dc5cbfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public final class RealWebSocketTest {
server.listener.assertClosed(1000, "Hello!");
}

@Test public void clientAndServerCloseClosesConnection() throws IOException {
@Test public void clientAndServerCloseClosesConnection() throws Exception {
// Send close from both sides at the same time.
server.webSocket.close(1000, "Hello!");
client.processNextFrame(); // Read close, close connection close.
Expand All @@ -175,6 +175,7 @@ public final class RealWebSocketTest {
server.listener.assertClosing(1000, "Hi!");
client.listener.assertClosed(1000, "Hello!");
server.listener.assertClosed(1000, "Hi!");
client.webSocket.awaitTermination(5, TimeUnit.SECONDS);
assertTrue(client.closed);

server.listener.assertExhausted(); // Client should not have sent second close.
Expand Down
8 changes: 8 additions & 0 deletions okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -276,6 +277,13 @@ boolean processNextFrame() throws IOException {
}
}

/**
* For testing: wait until the web socket's executor has terminated.
*/
void awaitTermination(int timeout, TimeUnit timeUnit) throws InterruptedException {
executor.awaitTermination(timeout, timeUnit);
}

synchronized int pingCount() {
return pingCount;
}
Expand Down

0 comments on commit dc5cbfd

Please sign in to comment.