azkaban-aplcache

Fix flaky test ExecutorServiceUtilsTest (#1767) * Fix flaky

5/26/2018 4:01:46 PM

Details

diff --git a/az-core/src/test/java/azkaban/utils/ExecutorServiceUtilsTest.java b/az-core/src/test/java/azkaban/utils/ExecutorServiceUtilsTest.java
index f95e536..aa95ba2 100644
--- a/az-core/src/test/java/azkaban/utils/ExecutorServiceUtilsTest.java
+++ b/az-core/src/test/java/azkaban/utils/ExecutorServiceUtilsTest.java
@@ -30,6 +30,8 @@ public class ExecutorServiceUtilsTest {
 
   private final ExecutorServiceUtils executorServiceUtils = new ExecutorServiceUtils();
 
+  private boolean isTestThreadInterrupted = false;
+
   @Test
   public void gracefulShutdown() throws InterruptedException {
     // given
@@ -55,9 +57,12 @@ public class ExecutorServiceUtilsTest {
 
     // then
     assertThat(service.isShutdown()).isTrue();
+    assertThat(this.isTestThreadInterrupted).isTrue();
     final long shutdownDuration = endShutdownTime - beginShutdownTime;
-    // Give some buffer for overhead to reduce false positives.
-    assertThat(shutdownDuration).isLessThan(10);
+
+    // Test that the wait time parameter is effective.
+    // Give some buffer to account for overhead to reduce false positives.
+    assertThat(shutdownDuration).isLessThan(1000);
   }
 
   @Test
@@ -69,9 +74,7 @@ public class ExecutorServiceUtilsTest {
 
     // when
     this.executorServiceUtils.gracefulShutdown(service, Duration.ofMillis(1));
-    final Throwable thrown = catchThrowable(() -> {
-      service.submit(this::sleep);
-    });
+    final Throwable thrown = catchThrowable(() -> service.submit(this::sleep));
 
     // then
     assertThat(thrown).isInstanceOf(RejectedExecutionException.class);
@@ -79,8 +82,9 @@ public class ExecutorServiceUtilsTest {
 
   private void sleep() {
     try {
-      Thread.sleep(1000);
+      Thread.sleep(5000);
     } catch (final InterruptedException ex) {
+      this.isTestThreadInterrupted = true;
     }
   }
 }