Repository: knox
Updated Branches:
  refs/heads/master 3098fadba -> 2868f4a16


KNOX-1548 - Improve ZooKeeperConfigurationMonitorTest to not use Thread.sleep()

Signed-off-by: Kevin Risden <kris...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/2868f4a1
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/2868f4a1
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/2868f4a1

Branch: refs/heads/master
Commit: 2868f4a160e9b4285cebbbd3e3aa0f41dd9ddafd
Parents: 3098fad
Author: Kevin Risden <kris...@apache.org>
Authored: Thu Nov 1 14:18:52 2018 -0400
Committer: Kevin Risden <kris...@apache.org>
Committed: Fri Nov 2 09:04:47 2018 -0400

----------------------------------------------------------------------
 .../ZooKeeperConfigurationMonitorTest.java      | 62 ++++++++++----------
 .../java/org/apache/knox/test/TestUtils.java    | 20 +++++++
 2 files changed, 52 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/2868f4a1/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/ZooKeeperConfigurationMonitorTest.java
----------------------------------------------------------------------
diff --git 
a/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/ZooKeeperConfigurationMonitorTest.java
 
b/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/ZooKeeperConfigurationMonitorTest.java
index 4ec368f..92eaee9 100644
--- 
a/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/ZooKeeperConfigurationMonitorTest.java
+++ 
b/gateway-server/src/test/java/org/apache/knox/gateway/topology/monitor/ZooKeeperConfigurationMonitorTest.java
@@ -198,27 +198,27 @@ public class ZooKeeperConfigurationMonitorTest {
             final File pc_two         = new File(providersDir, 
"providers-config2.xml");
 
             
client.create().withMode(CreateMode.PERSISTENT).forPath(pc_one_znode, 
TEST_PROVIDERS_CONFIG_1.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(pc_one.exists());
+
+            assertTrue(TestUtils.waitUntil(pc_one::exists, true,1000));
             assertEquals(TEST_PROVIDERS_CONFIG_1, 
FileUtils.readFileToString(pc_one, StandardCharsets.UTF_8));
 
             
client.create().withMode(CreateMode.PERSISTENT).forPath(getProviderPath("providers-config2.xml"),
 TEST_PROVIDERS_CONFIG_2.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(pc_two.exists());
-            assertEquals(TEST_PROVIDERS_CONFIG_2, 
FileUtils.readFileToString(pc_two, StandardCharsets.UTF_8));
+            assertTrue(TestUtils.waitUntil(pc_two::exists, true,1000));
+            assertTrue(TestUtils.waitUntil(
+                () -> 
TEST_PROVIDERS_CONFIG_2.equals(FileUtils.readFileToString(pc_two, 
StandardCharsets.UTF_8)),
+                true, 1000));
 
             client.setData().forPath(pc_two_znode, 
TEST_PROVIDERS_CONFIG_1.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(pc_two.exists());
-            assertEquals(TEST_PROVIDERS_CONFIG_1, 
FileUtils.readFileToString(pc_two, StandardCharsets.UTF_8));
+            assertTrue(TestUtils.waitUntil(pc_two::exists, true,1000));
+            assertTrue(TestUtils.waitUntil(
+                () -> 
TEST_PROVIDERS_CONFIG_1.equals(FileUtils.readFileToString(pc_two, 
StandardCharsets.UTF_8)),
+                true, 1000));
 
             client.delete().forPath(pc_two_znode);
-            Thread.sleep(100);
-            assertFalse(pc_two.exists());
+            assertFalse(TestUtils.waitUntil(pc_two::exists, false,1000));
 
             client.delete().forPath(pc_one_znode);
-            Thread.sleep(100);
-            assertFalse(pc_one.exists());
+            assertFalse(TestUtils.waitUntil(pc_one::exists, false,1000));
 
             final String desc_one_znode   = getDescriptorPath("test1.json");
             final String desc_two_znode   = getDescriptorPath("test2.json");
@@ -228,36 +228,38 @@ public class ZooKeeperConfigurationMonitorTest {
             final File desc_three         = new File(descriptorsDir, 
"test3.json");
 
             
client.create().withMode(CreateMode.PERSISTENT).forPath(desc_one_znode, 
TEST_DESCRIPTOR_1.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(desc_one.exists());
-            assertEquals(TEST_DESCRIPTOR_1, 
FileUtils.readFileToString(desc_one, StandardCharsets.UTF_8));
+            assertTrue(TestUtils.waitUntil(desc_one::exists, true,1000));
+            assertTrue(TestUtils.waitUntil(
+                () -> 
TEST_DESCRIPTOR_1.equals(FileUtils.readFileToString(desc_one, 
StandardCharsets.UTF_8)),
+                true, 1000));
 
             
client.create().withMode(CreateMode.PERSISTENT).forPath(desc_two_znode, 
TEST_DESCRIPTOR_1.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(desc_two.exists());
-            assertEquals(TEST_DESCRIPTOR_1, 
FileUtils.readFileToString(desc_two, StandardCharsets.UTF_8));
+            assertTrue(TestUtils.waitUntil(desc_two::exists, true,1000));
+            assertTrue(TestUtils.waitUntil(
+                () -> 
TEST_DESCRIPTOR_1.equals(FileUtils.readFileToString(desc_two, 
StandardCharsets.UTF_8)),
+                true, 1000));
 
             client.setData().forPath(desc_two_znode, 
TEST_DESCRIPTOR_2.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(desc_two.exists());
-            assertEquals(TEST_DESCRIPTOR_2, 
FileUtils.readFileToString(desc_two, StandardCharsets.UTF_8));
+            assertTrue(TestUtils.waitUntil(desc_two::exists, true,1000));
+            assertTrue(TestUtils.waitUntil(
+                () -> 
TEST_DESCRIPTOR_2.equals(FileUtils.readFileToString(desc_two, 
StandardCharsets.UTF_8)),
+                true, 1000));
 
             
client.create().withMode(CreateMode.PERSISTENT).forPath(desc_three_znode, 
TEST_DESCRIPTOR_1.getBytes(StandardCharsets.UTF_8));
-            Thread.sleep(100);
-            assertTrue(desc_three.exists());
-            assertEquals(TEST_DESCRIPTOR_1, 
FileUtils.readFileToString(desc_three, StandardCharsets.UTF_8));
+            assertTrue(TestUtils.waitUntil(desc_three::exists, true,1000));
+            assertTrue(TestUtils.waitUntil(
+                () -> 
TEST_DESCRIPTOR_1.equals(FileUtils.readFileToString(desc_three, 
StandardCharsets.UTF_8)),
+                true, 1000));
 
             client.delete().forPath(desc_two_znode);
-            Thread.sleep(100);
-            assertFalse("Expected test2.json to have been deleted.", 
desc_two.exists());
+            assertFalse("Expected test2.json to have been deleted.",
+                TestUtils.waitUntil(desc_two::exists, false,1000));
 
             client.delete().forPath(desc_three_znode);
-            Thread.sleep(100);
-            assertFalse(desc_three.exists());
+            assertFalse(TestUtils.waitUntil(desc_three::exists, false,1000));
 
             client.delete().forPath(desc_one_znode);
-            Thread.sleep(100);
-            assertFalse(desc_one.exists());
+            assertFalse(TestUtils.waitUntil(desc_one::exists, false,1000));
         } finally {
             clientService.stop();
             cm.stop();

http://git-wip-us.apache.org/repos/asf/knox/blob/2868f4a1/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
----------------------------------------------------------------------
diff --git 
a/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java 
b/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
index 9374b46..5a28238 100644
--- a/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
+++ b/gateway-test-utils/src/main/java/org/apache/knox/test/TestUtils.java
@@ -46,6 +46,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Locale;
 import java.util.Properties;
 import java.util.UUID;
+import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 
 public class TestUtils {
@@ -192,6 +193,25 @@ public class TestUtils {
     return port;
   }
 
+  /**
+   * Waits until a given function meets a given condition
+   * @param function function to check before the timeout
+   * @param expected boolean expected value to check
+   * @param timeout Timeout in milliseconds to wait for condition to be met
+   * @return expected based on condition
+   * @throws Exception on any error
+   */
+  public static boolean waitUntil(Callable<Boolean> function, boolean 
expected, long timeout) throws Exception {
+    long before = System.currentTimeMillis();
+    while((System.currentTimeMillis() - before) < timeout) {
+      if(function.call() == expected) {
+        return expected;
+      }
+      Thread.sleep(100);
+    }
+    return false;
+  }
+
   public static void waitUntilNextSecond() {
     long before = System.currentTimeMillis();
     long wait;

Reply via email to