From 0f5dc1be941fb2bbf6e41d18c0248261c1f88127 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Tue, 13 Oct 2020 18:05:02 -0400 Subject: [PATCH] Increase test timeouts to reduce likelihood of false negatives (#228) * 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 --- .../containerjfr/tui/ws/MessagingServerTest.java | 16 ++++++++++++---- .../tui/ws/WsClientReaderWriterTest.java | 11 ++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/MessagingServerTest.java b/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/MessagingServerTest.java index 5fd16b83ac..8a9d209d96 100644 --- a/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/MessagingServerTest.java +++ b/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/MessagingServerTest.java @@ -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() @@ -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)))); }); } @@ -162,7 +167,7 @@ void webSocketCloseHandlerShouldRemoveConnection() } @Test - void shouldHandleRemovedConnections() { + void shouldHandleRemovedConnections() throws Exception { String expectedText = "hello world"; when(crw2.readLine()).thenReturn(expectedText); @@ -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); diff --git a/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/WsClientReaderWriterTest.java b/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/WsClientReaderWriterTest.java index a25e33ced0..7837b29984 100644 --- a/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/WsClientReaderWriterTest.java +++ b/src/test/java/com/redhat/rhjmc/containerjfr/tui/ws/WsClientReaderWriterTest.java @@ -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); @@ -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)))); }); }