brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From drigod...@apache.org
Subject [2/3] brooklyn-server git commit: Adds TaskPredicates methods
Date Mon, 24 Jul 2017 12:01:22 GMT
Adds TaskPredicates methods

* hasTag(Object)
* isEffector()
* isTransient()
* isInessential()

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/36260656
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/36260656
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/36260656

Branch: refs/heads/master
Commit: 36260656af706350e2e187b769e371cac1afa0ca
Parents: 759c14f
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Jul 19 11:56:38 2017 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Jul 19 11:56:38 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/util/core/task/TaskPredicates.java | 78 +++++++++++++++++++-
 .../util/core/task/TaskPredicatesTest.java      | 70 ++++++++++++++++++
 2 files changed, 147 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36260656/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java b/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java
index 9c2f7b0..ee34fb7 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/task/TaskPredicates.java
@@ -21,6 +21,7 @@ package org.apache.brooklyn.util.core.task;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
 
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
@@ -69,7 +70,7 @@ public class TaskPredicates {
     private static class IsDone implements Predicate<Task<?>> {
         @Override
         public boolean apply(Task<?> input) {
-            return input.isDone();
+            return (input != null) && input.isDone();
         }
         @Override
         public String toString() {
@@ -77,4 +78,79 @@ public class TaskPredicates {
         }
     }
     
+    /**
+     * @since 0.12.0
+     */
+    public static Predicate<Task<?>> hasTag(Object tag) {
+        return new HasTag(tag);
+    }
+
+    private static class HasTag implements Predicate<Task<?>> {
+        private final Object tag;
+        public HasTag(Object tag) {
+            this.tag = checkNotNull(tag, "tag");
+        }
+        @Override
+        public boolean apply(Task<?> input) {
+            return (input != null) && input.getTags().contains(tag);
+        }
+        @Override
+        public String toString() {
+            return "hasTag("+tag+")";
+        }
+    }
+    
+    /**
+     * @since 0.12.0
+     */
+    public static Predicate<Task<?>> isEffector() {
+        return new IsEffector();
+    }
+    
+    private static class IsEffector implements Predicate<Task<?>> {
+        @Override
+        public boolean apply(Task<?> input) {
+            return (input != null) && BrooklynTaskTags.isEffectorTask(input);
+        }
+        @Override
+        public String toString() {
+            return "isEffector()";
+        }
+    }
+    
+    /**
+     * @since 0.12.0
+     */
+    public static Predicate<Task<?>> isTransient() {
+        return new IsTransient();
+    }
+    
+    private static class IsTransient implements Predicate<Task<?>> {
+        @Override
+        public boolean apply(Task<?> input) {
+            return (input != null) && BrooklynTaskTags.isTransient(input);
+        }
+        @Override
+        public String toString() {
+            return "isTransient()";
+        }
+    }
+    
+    /**
+     * @since 0.12.0
+     */
+    public static Predicate<Task<?>> isInessential() {
+        return new IsInessential();
+    }
+    
+    private static class IsInessential implements Predicate<Task<?>> {
+        @Override
+        public boolean apply(Task<?> input) {
+            return (input != null) && BrooklynTaskTags.isInessential(input);
+        }
+        @Override
+        public String toString() {
+            return "isTransient()";
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/36260656/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java
b/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java
index 8a25361..90d6b06 100644
--- a/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java
+++ b/core/src/test/java/org/apache/brooklyn/util/core/task/TaskPredicatesTest.java
@@ -21,13 +21,21 @@ package org.apache.brooklyn.util.core.task;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
+import java.util.concurrent.CountDownLatch;
+
+import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.api.mgmt.ExecutionManager;
 import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
+import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.Callables;
 
 public class TaskPredicatesTest extends BrooklynAppUnitTestSupport {
@@ -70,4 +78,66 @@ public class TaskPredicatesTest extends BrooklynAppUnitTestSupport {
         assertTrue(TaskPredicates.displayNameSatisfies(Predicates.equalTo("myname")).apply(task));
         assertFalse(TaskPredicates.displayNameSatisfies(Predicates.equalTo("wrong")).apply(task));
     }
+    
+    @Test
+    public void testIsDone() throws Exception {
+        CountDownLatch latch = new CountDownLatch(1);
+        Task<?> task = app.getExecutionContext().submit(new Runnable() {
+            public void run() {
+                try {
+                    latch.await();
+                } catch (InterruptedException e) {
+                    throw Exceptions.propagate(e);
+                }
+            }});
+        
+        assertFalse(TaskPredicates.isDone().apply(task));
+        
+        latch.countDown();
+        task.get();
+        assertTrue(TaskPredicates.isDone().apply(task));
+    }
+    
+    @Test
+    public void testHasTag() throws Exception {
+        Task<?> task = execManager.submit(TaskBuilder.<Object>builder()
+                .body(Callables.<Object>returning("val"))
+                .tag("mytag")
+                .build());
+        assertTrue(TaskPredicates.hasTag("mytag").apply(task));
+        assertFalse(TaskPredicates.hasTag("wrongtag").apply(task));
+    }
+    
+    @Test
+    public void testIsEffector() throws Exception {
+        Task<?> task = app.invoke(TestApplication.START, ImmutableMap.of("locations",
ImmutableList.<Location>of()));
+        Task<?> otherTask = execManager.submit(TaskBuilder.<Object>builder()
+                .body(Callables.<Object>returning("val"))
+                .build());
+        assertTrue(TaskPredicates.isEffector().apply(task));
+        assertFalse(TaskPredicates.isEffector().apply(otherTask));
+        
+    }
+    
+    @Test
+    public void testIsTransient() throws Exception {
+        Task<?> task = execManager.submit(TaskBuilder.<Object>builder()
+                .body(Callables.<Object>returning("val"))
+                .build());
+        assertFalse(TaskPredicates.isTransient().apply(task));
+        
+        BrooklynTaskTags.setTransient(task);
+        assertTrue(TaskPredicates.isTransient().apply(task));
+    }
+    
+    @Test
+    public void testIsInessential() throws Exception {
+        Task<?> task = execManager.submit(TaskBuilder.<Object>builder()
+                .body(Callables.<Object>returning("val"))
+                .build());
+        assertFalse(TaskPredicates.isInessential().apply(task));
+        
+        BrooklynTaskTags.setInessential(task);
+        assertTrue(TaskPredicates.isInessential().apply(task));
+    }
 }


Mime
View raw message