Repository: curator Updated Branches: refs/heads/CURATOR-411 [created] 5407746c3
Added a method to Timing to take from a queue with timeouts and applied it to tests that needed it Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/872bfb02 Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/872bfb02 Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/872bfb02 Branch: refs/heads/CURATOR-411 Commit: 872bfb0285dc4807d873b9ee2707b0f6044747f6 Parents: f898959 Author: randgalt <[email protected]> Authored: Mon May 8 06:01:45 2017 +0200 Committer: randgalt <[email protected]> Committed: Mon May 8 06:01:45 2017 +0200 ---------------------------------------------------------------------- .../curator/framework/imps/TestFramework.java | 2 +- .../framework/imps/TestFrameworkEdges.java | 8 +++--- .../recipes/cache/TestEventOrdering.java | 2 +- .../recipes/leader/TestLeaderSelector.java | 2 +- .../java/org/apache/curator/test/Timing.java | 28 ++++++++++++++++++++ 5 files changed, 35 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/872bfb02/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java index 44f9486..5d0c5ed 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFramework.java @@ -261,7 +261,7 @@ public class TestFramework extends BaseClassForTests client.getChildren().usingWatcher(watcher).forPath("/base"); client.create().forPath("/base/child"); - String path = queue.take(); + String path = new Timing().takeFromQueue(queue); Assert.assertEquals(path, "/base"); } finally http://git-wip-us.apache.org/repos/asf/curator/blob/872bfb02/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkEdges.java ---------------------------------------------------------------------- diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkEdges.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkEdges.java index bf1c281..ce0bb26 100644 --- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkEdges.java +++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestFrameworkEdges.java @@ -180,8 +180,8 @@ public class TestFrameworkEdges extends BaseClassForTests final String TEST_PATH = "/a/b/c/test-"; client.create().withMode(mode).inBackground(callback).forPath(TEST_PATH); - String name1 = paths.take(); - String path1 = paths.take(); + String name1 = timing.takeFromQueue(paths); + String path1 = timing.takeFromQueue(paths); client.close(); @@ -196,8 +196,8 @@ public class TestFrameworkEdges extends BaseClassForTests createBuilder.debugForceFindProtectedNode = true; createBuilder.withMode(mode).inBackground(callback).forPath(TEST_PATH); - String name2 = paths.take(); - String path2 = paths.take(); + String name2 = timing.takeFromQueue(paths); + String path2 = timing.takeFromQueue(paths); Assert.assertEquals(ZKPaths.getPathAndNode(name1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath()); Assert.assertEquals(ZKPaths.getPathAndNode(name2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath()); http://git-wip-us.apache.org/repos/asf/curator/blob/872bfb02/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestEventOrdering.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestEventOrdering.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestEventOrdering.java index 216c07c..7b3a07e 100644 --- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestEventOrdering.java +++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/cache/TestEventOrdering.java @@ -143,7 +143,7 @@ public abstract class TestEventOrdering<T extends Closeable> extends BaseClassFo int eventSuggestedQty = 0; while ( events.size() > 0 ) { - Event event = events.take(); + Event event = timing.takeFromQueue(events); localEvents.add(event); eventSuggestedQty += (event.eventType == EventType.ADDED) ? 1 : -1; } http://git-wip-us.apache.org/repos/asf/curator/blob/872bfb02/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java index c1622ba..60619d0 100644 --- a/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java +++ b/curator-recipes/src/test/java/org/apache/curator/framework/recipes/leader/TestLeaderSelector.java @@ -193,7 +193,7 @@ public class TestLeaderSelector extends BaseClassForTests selector = new LeaderSelector(client, "/leader", listener); selector.start(); - Thread leaderThread = queue.take(); + Thread leaderThread = timing.takeFromQueue(queue); server.stop(); leaderThread.interrupt(); server.restart(); http://git-wip-us.apache.org/repos/asf/curator/blob/872bfb02/curator-test/src/main/java/org/apache/curator/test/Timing.java ---------------------------------------------------------------------- diff --git a/curator-test/src/main/java/org/apache/curator/test/Timing.java b/curator-test/src/main/java/org/apache/curator/test/Timing.java index 27e4e53..242aa50 100644 --- a/curator-test/src/main/java/org/apache/curator/test/Timing.java +++ b/curator-test/src/main/java/org/apache/curator/test/Timing.java @@ -19,9 +19,11 @@ package org.apache.curator.test; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * Utility to get various testing times @@ -128,6 +130,32 @@ public class Timing } /** + * Try to take an item from the given queue + * + * @param queue queue + * @return item + * @throws Exception interrupted or timed out + */ + public <T> T takeFromQueue(BlockingQueue<T> queue) throws Exception + { + Timing m = forWaiting(); + try + { + T value = queue.poll(m.value, m.unit); + if ( value == null ) + { + throw new TimeoutException("Timed out trying to take from queue"); + } + return value; + } + catch ( InterruptedException e ) + { + Thread.currentThread().interrupt(); + throw e; + } + } + + /** * Wait on the given semaphore * * @param semaphore the semaphore
