Repository: brooklyn-server Updated Branches: refs/heads/master d4d5966c1 -> 1cfc0ed5b
Rename EffectorConcatenateTest to MethodEffectorTest Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/8483359d Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/8483359d Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/8483359d Branch: refs/heads/master Commit: 8483359dff42f09e8117c87a588eb3b3eccd3abb Parents: 3c35a1e Author: Aled Sage <[email protected]> Authored: Fri May 19 10:30:53 2017 +0100 Committer: Aled Sage <[email protected]> Committed: Fri May 19 10:32:51 2017 +0100 ---------------------------------------------------------------------- .../core/effector/EffectorConcatenateTest.java | 264 ------------------- .../core/effector/MethodEffectorTest.java | 264 +++++++++++++++++++ 2 files changed, 264 insertions(+), 264 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/8483359d/core/src/test/java/org/apache/brooklyn/core/effector/EffectorConcatenateTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/effector/EffectorConcatenateTest.java b/core/src/test/java/org/apache/brooklyn/core/effector/EffectorConcatenateTest.java deleted file mode 100644 index a80839f..0000000 --- a/core/src/test/java/org/apache/brooklyn/core/effector/EffectorConcatenateTest.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.effector; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.fail; - -import java.util.concurrent.Callable; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.entity.ImplementedBy; -import org.apache.brooklyn.api.mgmt.ExecutionManager; -import org.apache.brooklyn.api.mgmt.Task; -import org.apache.brooklyn.core.annotation.Effector; -import org.apache.brooklyn.core.annotation.EffectorParam; -import org.apache.brooklyn.core.entity.AbstractEntity; -import org.apache.brooklyn.core.entity.EntityInternal; -import org.apache.brooklyn.core.mgmt.BrooklynTaskTags; -import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.util.core.task.BasicExecutionContext; -import org.apache.brooklyn.util.core.task.Tasks; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.base.Predicate; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; - -public class EffectorConcatenateTest extends BrooklynAppUnitTestSupport { - - private static final Logger log = LoggerFactory.getLogger(EffectorConcatenateTest.class); - private static final long TIMEOUT = 10*1000; - - @ImplementedBy(MyEntityImpl.class) - public static interface MyEntity extends Entity, EntityInternal { - public static MethodEffector<String> CONCATENATE = new MethodEffector<String>(MyEntityImpl.class, "concatenate"); - public static MethodEffector<Void> WAIT_A_BIT = new MethodEffector<Void>(MyEntityImpl.class, "waitabit"); - public static MethodEffector<Void> SPAWN_CHILD = new MethodEffector<Void>(MyEntityImpl.class, "spawnchild"); - - @Effector(description="sample effector concatenating strings") - String concatenate(@EffectorParam(name="first", description="first argument") String first, - @EffectorParam(name="second", description="2nd arg") String second) throws Exception; - - @Effector(description="sample effector doing some waiting") - void waitabit() throws Exception; - - @Effector(description="sample effector that spawns a child task that waits a bit") - void spawnchild() throws Exception; - - AtomicReference<Task<?>> getWaitingTask(); - - /** latch is .countDown'ed by the effector at the beginning of the "waiting" point */ - CountDownLatch getNowWaitingLatch(); - - /** latch is await'ed on by the effector when it is in the "waiting" point */ - CountDownLatch getContinueFromWaitingLatch(); - } - - public static class MyEntityImpl extends AbstractEntity implements MyEntity { - /** The "current task" representing the effector currently executing */ - AtomicReference<Task<?>> waitingTask = new AtomicReference<Task<?>>(); - - /** latch is .countDown'ed by the effector at the beginning of the "waiting" point */ - CountDownLatch nowWaitingLatch = new CountDownLatch(1); - - /** latch is await'ed on by the effector when it is in the "waiting" point */ - CountDownLatch continueFromWaitingLatch = new CountDownLatch(1); - - @Override - public AtomicReference<Task<?>> getWaitingTask() { - return waitingTask; - } - - @Override - public CountDownLatch getNowWaitingLatch() { - return nowWaitingLatch; - } - - @Override - public CountDownLatch getContinueFromWaitingLatch() { - return continueFromWaitingLatch; - } - - @Override - public String concatenate(String first, String second) throws Exception { - return first+second; - } - - @Override - public void waitabit() throws Exception { - waitingTask.set(Tasks.current()); - - Tasks.setExtraStatusDetails("waitabit extra status details"); - - Tasks.withBlockingDetails("waitabit.blocking", new Callable<Void>() { - @Override - public Void call() throws Exception { - nowWaitingLatch.countDown(); - if (!continueFromWaitingLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { - fail("took too long to be told to continue"); - } - return null; - }}); - } - - @Override - public void spawnchild() throws Exception { - // spawn a child, then wait - BasicExecutionContext.getCurrentExecutionContext().submit( - MutableMap.of("displayName", "SpawnedChildName"), - new Callable<Void>() { - @Override - public Void call() throws Exception { - log.info("beginning spawned child response "+Tasks.current()+", with tags "+Tasks.current().getTags()); - Tasks.setBlockingDetails("spawned child blocking details"); - nowWaitingLatch.countDown(); - if (!continueFromWaitingLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { - fail("took too long to be told to continue"); - } - return null; - }}); - } - } - - private MyEntity entity; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - super.setUp(); - entity = app.addChild(EntitySpec.create(MyEntity.class)); - } - - @Test - public void testCanInvokeEffector() throws Exception { - // invocation map syntax - Task<String> task = entity.invoke(MyEntity.CONCATENATE, ImmutableMap.of("first", "a", "second", "b")); - assertEquals(task.get(TIMEOUT, TimeUnit.MILLISECONDS), "ab"); - - // method syntax - assertEquals("xy", entity.concatenate("x", "y")); - } - - @Test - public void testReportsTaskDetails() throws Exception { - final AtomicReference<String> result = new AtomicReference<String>(); - - Thread bg = new Thread(new Runnable() { - @Override - public void run() { - try { - // Expect "wait a bit" to tell us it's blocking - if (!entity.getNowWaitingLatch().await(TIMEOUT, TimeUnit.MILLISECONDS)) { - result.set("took too long for waitabit to be waiting"); - return; - } - - // Expect "wait a bit" to have retrieved and set its task - try { - Task<?> t = entity.getWaitingTask().get(); - String status = t.getStatusDetail(true); - log.info("waitabit task says:\n"+status); - if (!status.contains("waitabit extra status details")) { - result.set("Status not in expected format: doesn't contain extra status details phrase 'My extra status details'\n"+status); - return; - } - if (!status.startsWith("waitabit.blocking")) { - result.set("Status not in expected format: doesn't start with blocking details 'waitabit.blocking'\n"+status); - return; - } - } finally { - entity.getContinueFromWaitingLatch().countDown(); - } - } catch (Throwable t) { - log.warn("Failure: "+t, t); - result.set("Failure: "+t); - } - }}); - bg.start(); - - entity.invoke(MyEntity.WAIT_A_BIT, ImmutableMap.<String,Object>of()) - .get(TIMEOUT, TimeUnit.MILLISECONDS); - - bg.join(TIMEOUT*2); - assertFalse(bg.isAlive()); - - String problem = result.get(); - if (problem!=null) fail(problem); - } - - @Test - public void testReportsSpawnedTaskDetails() throws Exception { - final AtomicReference<String> result = new AtomicReference<String>(); - - Thread bg = new Thread(new Runnable() { - @Override - public void run() { - try { - // Expect "spawned child" to tell us it's blocking - if (!entity.getNowWaitingLatch().await(TIMEOUT, TimeUnit.MILLISECONDS)) { - result.set("took too long for spawnchild's sub-task to be waiting"); - return; - } - - // Expect spawned task to be have been tagged with entity - ExecutionManager em = entity.getManagementContext().getExecutionManager(); - Task<?> subtask = Iterables.find(BrooklynTaskTags.getTasksInEntityContext(em, entity), new Predicate<Task<?>>() { - @Override - public boolean apply(Task<?> input) { - return "SpawnedChildName".equals(input.getDisplayName()); - } - }); - - // Expect spawned task to haev correct "blocking details" - try { - String status = subtask.getStatusDetail(true); - log.info("subtask task says:\n"+status); - if (!status.contains("spawned child blocking details")) { - result.set("Status not in expected format: doesn't contain blocking details phrase 'spawned child blocking details'\n"+status); - return; - } - } finally { - entity.getContinueFromWaitingLatch().countDown(); - } - } catch (Throwable t) { - log.warn("Failure: "+t, t); - result.set("Failure: "+t); - } - }}); - bg.start(); - - entity.invoke(MyEntity.SPAWN_CHILD, ImmutableMap.<String,Object>of()) - .get(TIMEOUT, TimeUnit.MILLISECONDS); - - bg.join(TIMEOUT*2); - assertFalse(bg.isAlive()); - - String problem = result.get(); - if (problem!=null) fail(problem); - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/8483359d/core/src/test/java/org/apache/brooklyn/core/effector/MethodEffectorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/effector/MethodEffectorTest.java b/core/src/test/java/org/apache/brooklyn/core/effector/MethodEffectorTest.java new file mode 100644 index 0000000..97c5b85 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/effector/MethodEffectorTest.java @@ -0,0 +1,264 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.core.effector; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.fail; + +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.entity.ImplementedBy; +import org.apache.brooklyn.api.mgmt.ExecutionManager; +import org.apache.brooklyn.api.mgmt.Task; +import org.apache.brooklyn.core.annotation.Effector; +import org.apache.brooklyn.core.annotation.EffectorParam; +import org.apache.brooklyn.core.entity.AbstractEntity; +import org.apache.brooklyn.core.entity.EntityInternal; +import org.apache.brooklyn.core.mgmt.BrooklynTaskTags; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.core.task.BasicExecutionContext; +import org.apache.brooklyn.util.core.task.Tasks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.base.Predicate; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; + +public class MethodEffectorTest extends BrooklynAppUnitTestSupport { + + private static final Logger log = LoggerFactory.getLogger(MethodEffectorTest.class); + private static final long TIMEOUT = 10*1000; + + @ImplementedBy(MyEntityImpl.class) + public static interface MyEntity extends Entity, EntityInternal { + public static MethodEffector<String> CONCATENATE = new MethodEffector<String>(MyEntityImpl.class, "concatenate"); + public static MethodEffector<Void> WAIT_A_BIT = new MethodEffector<Void>(MyEntityImpl.class, "waitabit"); + public static MethodEffector<Void> SPAWN_CHILD = new MethodEffector<Void>(MyEntityImpl.class, "spawnchild"); + + @Effector(description="sample effector concatenating strings") + String concatenate(@EffectorParam(name="first", description="first argument") String first, + @EffectorParam(name="second", description="2nd arg") String second) throws Exception; + + @Effector(description="sample effector doing some waiting") + void waitabit() throws Exception; + + @Effector(description="sample effector that spawns a child task that waits a bit") + void spawnchild() throws Exception; + + AtomicReference<Task<?>> getWaitingTask(); + + /** latch is .countDown'ed by the effector at the beginning of the "waiting" point */ + CountDownLatch getNowWaitingLatch(); + + /** latch is await'ed on by the effector when it is in the "waiting" point */ + CountDownLatch getContinueFromWaitingLatch(); + } + + public static class MyEntityImpl extends AbstractEntity implements MyEntity { + /** The "current task" representing the effector currently executing */ + AtomicReference<Task<?>> waitingTask = new AtomicReference<Task<?>>(); + + /** latch is .countDown'ed by the effector at the beginning of the "waiting" point */ + CountDownLatch nowWaitingLatch = new CountDownLatch(1); + + /** latch is await'ed on by the effector when it is in the "waiting" point */ + CountDownLatch continueFromWaitingLatch = new CountDownLatch(1); + + @Override + public AtomicReference<Task<?>> getWaitingTask() { + return waitingTask; + } + + @Override + public CountDownLatch getNowWaitingLatch() { + return nowWaitingLatch; + } + + @Override + public CountDownLatch getContinueFromWaitingLatch() { + return continueFromWaitingLatch; + } + + @Override + public String concatenate(String first, String second) throws Exception { + return first+second; + } + + @Override + public void waitabit() throws Exception { + waitingTask.set(Tasks.current()); + + Tasks.setExtraStatusDetails("waitabit extra status details"); + + Tasks.withBlockingDetails("waitabit.blocking", new Callable<Void>() { + @Override + public Void call() throws Exception { + nowWaitingLatch.countDown(); + if (!continueFromWaitingLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("took too long to be told to continue"); + } + return null; + }}); + } + + @Override + public void spawnchild() throws Exception { + // spawn a child, then wait + BasicExecutionContext.getCurrentExecutionContext().submit( + MutableMap.of("displayName", "SpawnedChildName"), + new Callable<Void>() { + @Override + public Void call() throws Exception { + log.info("beginning spawned child response "+Tasks.current()+", with tags "+Tasks.current().getTags()); + Tasks.setBlockingDetails("spawned child blocking details"); + nowWaitingLatch.countDown(); + if (!continueFromWaitingLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("took too long to be told to continue"); + } + return null; + }}); + } + } + + private MyEntity entity; + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + super.setUp(); + entity = app.addChild(EntitySpec.create(MyEntity.class)); + } + + @Test + public void testCanInvokeEffector() throws Exception { + // invocation map syntax + Task<String> task = entity.invoke(MyEntity.CONCATENATE, ImmutableMap.of("first", "a", "second", "b")); + assertEquals(task.get(TIMEOUT, TimeUnit.MILLISECONDS), "ab"); + + // method syntax + assertEquals("xy", entity.concatenate("x", "y")); + } + + @Test + public void testReportsTaskDetails() throws Exception { + final AtomicReference<String> result = new AtomicReference<String>(); + + Thread bg = new Thread(new Runnable() { + @Override + public void run() { + try { + // Expect "wait a bit" to tell us it's blocking + if (!entity.getNowWaitingLatch().await(TIMEOUT, TimeUnit.MILLISECONDS)) { + result.set("took too long for waitabit to be waiting"); + return; + } + + // Expect "wait a bit" to have retrieved and set its task + try { + Task<?> t = entity.getWaitingTask().get(); + String status = t.getStatusDetail(true); + log.info("waitabit task says:\n"+status); + if (!status.contains("waitabit extra status details")) { + result.set("Status not in expected format: doesn't contain extra status details phrase 'My extra status details'\n"+status); + return; + } + if (!status.startsWith("waitabit.blocking")) { + result.set("Status not in expected format: doesn't start with blocking details 'waitabit.blocking'\n"+status); + return; + } + } finally { + entity.getContinueFromWaitingLatch().countDown(); + } + } catch (Throwable t) { + log.warn("Failure: "+t, t); + result.set("Failure: "+t); + } + }}); + bg.start(); + + entity.invoke(MyEntity.WAIT_A_BIT, ImmutableMap.<String,Object>of()) + .get(TIMEOUT, TimeUnit.MILLISECONDS); + + bg.join(TIMEOUT*2); + assertFalse(bg.isAlive()); + + String problem = result.get(); + if (problem!=null) fail(problem); + } + + @Test + public void testReportsSpawnedTaskDetails() throws Exception { + final AtomicReference<String> result = new AtomicReference<String>(); + + Thread bg = new Thread(new Runnable() { + @Override + public void run() { + try { + // Expect "spawned child" to tell us it's blocking + if (!entity.getNowWaitingLatch().await(TIMEOUT, TimeUnit.MILLISECONDS)) { + result.set("took too long for spawnchild's sub-task to be waiting"); + return; + } + + // Expect spawned task to be have been tagged with entity + ExecutionManager em = entity.getManagementContext().getExecutionManager(); + Task<?> subtask = Iterables.find(BrooklynTaskTags.getTasksInEntityContext(em, entity), new Predicate<Task<?>>() { + @Override + public boolean apply(Task<?> input) { + return "SpawnedChildName".equals(input.getDisplayName()); + } + }); + + // Expect spawned task to haev correct "blocking details" + try { + String status = subtask.getStatusDetail(true); + log.info("subtask task says:\n"+status); + if (!status.contains("spawned child blocking details")) { + result.set("Status not in expected format: doesn't contain blocking details phrase 'spawned child blocking details'\n"+status); + return; + } + } finally { + entity.getContinueFromWaitingLatch().countDown(); + } + } catch (Throwable t) { + log.warn("Failure: "+t, t); + result.set("Failure: "+t); + } + }}); + bg.start(); + + entity.invoke(MyEntity.SPAWN_CHILD, ImmutableMap.<String,Object>of()) + .get(TIMEOUT, TimeUnit.MILLISECONDS); + + bg.join(TIMEOUT*2); + assertFalse(bg.isAlive()); + + String problem = result.get(); + if (problem!=null) fail(problem); + } +}
