Skip to content

Commit

Permalink
Increase test timeouts to reduce likelihood of false negatives (#228)
Browse files Browse the repository at this point in the history
* Increase time-sensitive test error factor

Decrease likelihood of false negatives due to system load pressure and other external factors

* Add thread sleep hack to "fix" sporadic test
  • Loading branch information
andrewazores authored Oct 13, 2020
1 parent 766b90f commit 0f5dc1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ void clientReaderShouldPropagateClose() throws IOException {
void clientReaderShouldBlockUntilConnected() {
String expectedText = "hello world";
long expectedDelta = TimeUnit.SECONDS.toNanos(1);
int maxErrorFactor = 10;
assertTimeoutPreemptively(
Duration.ofNanos(expectedDelta * 3),
Duration.ofNanos(expectedDelta * maxErrorFactor),
() -> {
when(crw2.readLine()).thenReturn(expectedText);
Executors.newSingleThreadScheduledExecutor()
Expand All @@ -133,8 +134,12 @@ void clientReaderShouldBlockUntilConnected() {
MatcherAssert.assertThat(
delta,
Matchers.allOf(
Matchers.greaterThan((long) (expectedDelta * 0.75)),
Matchers.lessThan((long) (expectedDelta * 1.25))));
// actual should never be less than expected, but since this is
// relying on a real wall-clock timer, allow for some error in
// that direction. Allow much more error in the greater-than
// direction to account for system scheduling etc.
Matchers.greaterThan((long) (expectedDelta * 0.9)),
Matchers.lessThan((long) (expectedDelta * maxErrorFactor))));
});
}

Expand Down Expand Up @@ -162,7 +167,7 @@ void webSocketCloseHandlerShouldRemoveConnection()
}

@Test
void shouldHandleRemovedConnections() {
void shouldHandleRemovedConnections() throws Exception {
String expectedText = "hello world";
when(crw2.readLine()).thenReturn(expectedText);

Expand All @@ -186,6 +191,9 @@ void shouldHandleRemovedConnections() {
String newText = "another message";
when(crw1.readLine()).thenReturn(newText);

// FIXME this is a dirty hack. See https://github.com/rh-jmc-team/container-jfr/issues/132
Thread.sleep(500);

MatcherAssert.assertThat(server.getClientReader().readLine(), Matchers.equalTo(newText));
verify(crw1, Mockito.atLeastOnce()).readLine();
verifyNoMoreInteractions(crw2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ void setup() {
@Test
void readLineShouldBlockUntilClosed() {
long expectedDelta = TimeUnit.SECONDS.toNanos(1);
int maxErrorFactor = 10;
assertTimeoutPreemptively(
Duration.ofNanos(expectedDelta * 3),
Duration.ofNanos(expectedDelta * maxErrorFactor),
() -> {
Executors.newSingleThreadScheduledExecutor()
.schedule(crw::close, expectedDelta, TimeUnit.NANOSECONDS);
Expand All @@ -94,8 +95,12 @@ void readLineShouldBlockUntilClosed() {
MatcherAssert.assertThat(
delta,
Matchers.allOf(
Matchers.greaterThan((long) (expectedDelta * 0.75)),
Matchers.lessThan((long) (expectedDelta * 1.25))));
// actual should never be less than expected, but since this is
// relying on a real wall-clock timer, allow for some error in
// that direction. Allow much more error in the greater-than
// direction to account for system scheduling etc.
Matchers.greaterThan((long) (expectedDelta * 0.9)),
Matchers.lessThan((long) (expectedDelta * maxErrorFactor))));
});
}

Expand Down

0 comments on commit 0f5dc1b

Please sign in to comment.