brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [03/10] brooklyn-server git commit: sample effector for use in UI tests
Date Sat, 22 Jul 2017 02:14:42 GMT
sample effector for use in UI tests


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

Branch: refs/heads/master
Commit: fd99a2e56b030cfd95b5c802a4b12e48cd737d6a
Parents: 4f9564f
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Wed Jul 5 14:45:37 2017 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Wed Jul 5 14:45:37 2017 +0100

----------------------------------------------------------------------
 .../core/effector/SampleManyTasksEffector.java  | 87 ++++++++++++++++++++
 1 file changed, 87 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/fd99a2e5/core/src/test/java/org/apache/brooklyn/core/effector/SampleManyTasksEffector.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/effector/SampleManyTasksEffector.java
b/core/src/test/java/org/apache/brooklyn/core/effector/SampleManyTasksEffector.java
new file mode 100644
index 0000000..6cf4e2c
--- /dev/null
+++ b/core/src/test/java/org/apache/brooklyn/core/effector/SampleManyTasksEffector.java
@@ -0,0 +1,87 @@
+package org.apache.brooklyn.core.effector;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.api.mgmt.TaskAdaptable;
+import org.apache.brooklyn.core.effector.EffectorTasks.EffectorTaskFactory;
+import org.apache.brooklyn.util.collections.MutableList;
+import org.apache.brooklyn.util.core.config.ConfigBag;
+import org.apache.brooklyn.util.core.task.DynamicTasks;
+import org.apache.brooklyn.util.core.task.TaskBuilder;
+import org.apache.brooklyn.util.core.task.Tasks;
+import org.apache.brooklyn.util.time.Duration;
+import org.apache.brooklyn.util.time.Time;
+
+/** Effector which can be used to create a lot of tasks with delays.
+ * Mainly used for manual UI testing, with a blueprint such as the following:
+
+<pre>
+
+name: Test with many tasks
+location: localhost
+services:
+- type: org.apache.brooklyn.entity.software.base.VanillaSoftwareProcess
+  brooklyn.initializers:
+  - type: org.apache.brooklyn.core.effector.SampleManyTasksEffector
+  launch.command: |
+    echo hello | nc -l 4321 &
+    echo $! > $PID_FILE
+    ## to experiment with errors or sleeping
+    # sleep 10
+    # exit 3
+
+</pre>
+
+ */
+public class SampleManyTasksEffector extends AddEffector {
+
+    public SampleManyTasksEffector(ConfigBag params) {
+        super(Effectors.effector(String.class, params.get(EFFECTOR_NAME)).name("eatand").impl(body(params)).build());
+    }
+
+    private static EffectorTaskFactory<String> body(ConfigBag params) {
+        return new EffectorTaskFactory<String>() {
+            @Override
+            public TaskAdaptable<String> newTask(Entity entity, Effector<String>
effector, ConfigBag parameters) {
+                return Tasks.<String>builder().displayName("eat-sleep-rave-repeat").addAll(tasks(0)).build();
+            }
+            List<Task<Object>> tasks(final int depth) {
+                List<Task<Object>> result = MutableList.of();
+                do {
+                    TaskBuilder<Object> t = Tasks.builder();
+                    double x = Math.random();
+                    if (depth>4) x *= Math.random();
+                    if (depth>6) x *= Math.random();
+                    if (x<0.3) {
+                        t.displayName("eat").body(new Callable<Object>() { public Object
call() { return "eat"; }});
+                    } else if (x<0.6) {
+                        final Duration time = Duration.millis(Math.round(10*1000*Math.random()*Math.random()*Math.random()*Math.random()*Math.random()));
+                        t.displayName("sleep").description("Sleeping "+time).body(new Callable<Object>()
{ public Object call() {
+                            Tasks.setBlockingDetails("sleeping "+time);
+                            Time.sleep(time);
+                            return "slept "+time;
+                        }});
+                    } else if (x<0.8) {
+                        t.displayName("rave").body(new Callable<Object>() { public
Object call() {
+                            List<Task<Object>> ts = tasks(depth+1);
+                            for (Task<Object> tt: ts) {
+                                DynamicTasks.queue(tt);
+                            }
+                            return "raved with "+ts.size()+" tasks";
+                        }});
+                    } else {
+                        t.displayName("repeat").addAll(tasks(depth+1));
+                    }
+                    result.add(t.build());
+                    
+                } while (Math.random()<0.8);
+                return result;
+            }
+        };
+    }
+
+}


Mime
View raw message