Skip to content

Commit

Permalink
Fix a flaky test by waiting until the Okio watchdog is caught up.
Browse files Browse the repository at this point in the history
We had tests that flake because they expected the watchdog to have
completed closing a connection, but we weren't blocking until that
work was done.

Closes square#1328
  • Loading branch information
squarejesse committed Mar 15, 2015
1 parent 57b265b commit 7576c10
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import okio.AsyncTimeout;
import okio.Buffer;
import okio.BufferedSink;
import okio.ByteString;
Expand Down Expand Up @@ -884,6 +886,7 @@ public final class Spdy3ConnectionTest {
} catch (InterruptedIOException expected) {
}
long elapsedNanos = System.nanoTime() - startNanos;
awaitWatchdogIdle();
assertEquals(500d, TimeUnit.NANOSECONDS.toMillis(elapsedNanos), 200d /* 200ms delta */);
assertEquals(0, connection.openStreamCount());

Expand Down Expand Up @@ -911,6 +914,7 @@ public final class Spdy3ConnectionTest {
} catch (InterruptedIOException expected) {
}
long elapsedNanos = System.nanoTime() - startNanos;
awaitWatchdogIdle();
assertEquals(500d, TimeUnit.NANOSECONDS.toMillis(elapsedNanos), 200d /* 200ms delta */);
assertEquals(0, connection.openStreamCount());

Expand Down Expand Up @@ -948,6 +952,7 @@ public final class Spdy3ConnectionTest {
} catch (InterruptedIOException expected) {
}
long elapsedNanos = System.nanoTime() - startNanos;
awaitWatchdogIdle();
assertEquals(500d, TimeUnit.NANOSECONDS.toMillis(elapsedNanos), 200d /* 200ms delta */);
assertEquals(0, connection.openStreamCount());

Expand Down Expand Up @@ -1301,6 +1306,23 @@ private void interruptAfterDelay(final long delayMillis) {
}.start();
}

/**
* Returns true when all work currently in progress by the watchdog have completed. This method
* creates more work for the watchdog and waits for that work to be executed. When it is, we know
* work that preceded this call is complete.
*/
private void awaitWatchdogIdle() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
AsyncTimeout watchdogJob = new AsyncTimeout() {
@Override protected void timedOut() {
latch.countDown();
}
};
watchdogJob.deadlineNanoTime(System.nanoTime()); // Due immediately!
watchdogJob.enter();
latch.await();
}

static int roundUp(int num, int divisor) {
return (num + divisor - 1) / divisor;
}
Expand Down

0 comments on commit 7576c10

Please sign in to comment.