changeset 2548:f198b30beb47

Fix time-sensitive test methods in NotificationsCommandTest This patch addresses Jenkins test failures in #3328 & #3334. Reviewed-by: aazores Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-December/021852.html
author Alex Macdonald <almacdon@redhat.com>
date Wed, 14 Dec 2016 10:29:05 -0500
parents ab494110bf67
children a4d9708db5f1
files vm-jmx/client-cli/src/test/java/com/redhat/thermostat/vm/jmx/client/cli/NotificationsCommandTest.java
diffstat 1 files changed, 18 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/vm-jmx/client-cli/src/test/java/com/redhat/thermostat/vm/jmx/client/cli/NotificationsCommandTest.java	Fri Dec 09 14:11:16 2016 -0500
+++ b/vm-jmx/client-cli/src/test/java/com/redhat/thermostat/vm/jmx/client/cli/NotificationsCommandTest.java	Wed Dec 14 10:29:05 2016 -0500
@@ -74,6 +74,7 @@
 import java.util.Collections;
 import java.util.Date;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
@@ -424,11 +425,7 @@
             }
         });
         helper.start();
-        try {
-            Thread.sleep(750);
-            timerFactory.getAction().run();
-        } catch (InterruptedException ignored) {
-        }
+        runTimerFactoryAction();
         when(inStream.available()).thenReturn(1);
         try {
             helper.join();
@@ -461,11 +458,7 @@
             }
         });
         helper.start();
-        try {
-            Thread.sleep(750);
-            timerFactory.getAction().run();
-        } catch (InterruptedException ignored) {
-        }
+        runTimerFactoryAction();
         jmxNotificationStatus.setEnabled(false);
         try {
             helper.join();
@@ -561,4 +554,19 @@
         when(args.getSubcommand()).thenReturn(subcommand);
     }
 
+    private void runTimerFactoryAction() {
+        long startTime = System.nanoTime();
+        while(!timerFactory.isActive()) {
+            long elapsedTime = System.nanoTime() - startTime;
+            if (elapsedTime > TimeUnit.NANOSECONDS.convert(5, TimeUnit.SECONDS)) {
+                fail("Timer took more than 1 second to be created.");
+            }
+            try {
+                Thread.sleep(100);
+            } catch (InterruptedException ignored) {
+            }
+        }
+        timerFactory.getAction().run();
+    }
+
 }