You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ge...@apache.org on 2017/03/08 15:12:07 UTC
[2/4] brooklyn-server git commit: Add generalised
Entities.waitFor(entity, condition, timeout)
Add generalised Entities.waitFor(entity, condition, timeout)
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/71727885
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/71727885
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/71727885
Branch: refs/heads/master
Commit: 71727885f174b17b27ef64d1090930084daf3430
Parents: 24f4e0b
Author: Alasdair Hodge <gi...@alasdairhodge.co.uk>
Authored: Wed Mar 8 13:21:37 2017 +0000
Committer: Alasdair Hodge <gi...@alasdairhodge.co.uk>
Committed: Wed Mar 8 13:21:37 2017 +0000
----------------------------------------------------------------------
.../apache/brooklyn/core/entity/Entities.java | 39 ++++++++++++--------
.../brooklyn/core/entity/EntitiesTest.java | 20 ++++++++--
2 files changed, 41 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/71727885/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
index 9df6e93..6d68694 100644
--- a/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
+++ b/core/src/main/java/org/apache/brooklyn/core/entity/Entities.java
@@ -18,6 +18,9 @@
*/
package org.apache.brooklyn.core.entity;
+import static org.apache.brooklyn.core.entity.EntityPredicates.attributeEqualTo;
+import static org.apache.brooklyn.util.guava.Functionals.isSatisfied;
+
import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
@@ -32,7 +35,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
-import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@@ -1237,25 +1239,32 @@ public class Entities {
log.warn("Ignoring "+key+" set on "+entity+" ("+entity.getConfig(key)+")");
}
- /** Waits until {@link Startable#SERVICE_UP} returns true. */
- public static void waitForServiceUp(final Entity entity, Duration timeout) {
- String description = "Waiting for SERVICE_UP on "+entity;
- Tasks.setBlockingDetails(description);
+ /** Waits until the passed entity satisfies the supplied predicate. */
+ public static void waitFor(Entity entity, Predicate<Entity> condition, Duration timeout) {
try {
- if (!Repeater.create(description).limitTimeTo(timeout)
- .rethrowException().backoffTo(Duration.ONE_SECOND)
- .until(new Callable<Boolean>() {
- @Override
- public Boolean call() {
- return Boolean.TRUE.equals(entity.getAttribute(Startable.SERVICE_UP));
- }})
- .run()) {
- throw new IllegalStateException("Timeout waiting for SERVICE_UP from "+entity);
+ String description = "Waiting for " + condition + " on " + entity;
+ Tasks.setBlockingDetails(description);
+
+ Repeater repeater = Repeater.create(description)
+ .until(isSatisfied(entity, condition))
+ .limitTimeTo(timeout)
+ .backoffTo(Duration.ONE_SECOND)
+ .rethrowException();
+
+ if (!repeater.run()) {
+ throw new IllegalStateException("Timeout waiting for " + condition + " on " + entity);
}
+
} finally {
Tasks.resetBlockingDetails();
}
- log.debug("Detected SERVICE_UP for software {}", entity);
+
+ log.debug("Detected {} for {}", condition, entity);
+ }
+
+ /** Waits until {@link Startable#SERVICE_UP} returns true. */
+ public static void waitForServiceUp(final Entity entity, Duration timeout) {
+ waitFor(entity, attributeEqualTo(Startable.SERVICE_UP, true), timeout);
}
public static void waitForServiceUp(final Entity entity, long duration, TimeUnit units) {
waitForServiceUp(entity, Duration.of(duration, units));
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/71727885/core/src/test/java/org/apache/brooklyn/core/entity/EntitiesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/EntitiesTest.java b/core/src/test/java/org/apache/brooklyn/core/entity/EntitiesTest.java
index 43f9870..80ae563 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/EntitiesTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/EntitiesTest.java
@@ -18,6 +18,7 @@
*/
package org.apache.brooklyn.core.entity;
+import static org.apache.brooklyn.core.entity.EntityPredicates.applicationIdEqualTo;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
@@ -26,14 +27,12 @@ import java.util.concurrent.atomic.AtomicReference;
import org.apache.brooklyn.api.entity.EntitySpec;
import org.apache.brooklyn.api.location.LocationSpec;
-import org.apache.brooklyn.core.entity.Entities;
-import org.apache.brooklyn.core.entity.EntityAndAttribute;
-import org.apache.brooklyn.core.entity.EntityInitializers;
import org.apache.brooklyn.core.location.SimulatedLocation;
import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
import org.apache.brooklyn.core.test.entity.TestEntity;
import org.apache.brooklyn.test.Asserts;
import org.apache.brooklyn.util.collections.MutableSet;
+import org.apache.brooklyn.util.time.Duration;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@@ -170,4 +169,19 @@ public class EntitiesTest extends BrooklynAppUnitTestSupport {
Assert.assertEquals(entity.tags().getTags(), MutableSet.of(app));
}
+ @Test
+ public void testWaitFor() throws Exception {
+ entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+ Duration timeout = Duration.ONE_MILLISECOND;
+
+ Entities.waitFor(entity, applicationIdEqualTo(app.getApplicationId()), timeout);
+
+ try {
+ Entities.waitFor(entity, applicationIdEqualTo(app.getApplicationId() + "-wrong"), timeout);
+ Asserts.shouldHaveFailedPreviously("Entities.waitFor() should have timed out");
+ } catch (Exception e) {
+ Asserts.expectedFailureContains(e, "Timeout waiting for ");
+ }
+ }
+
}