Refactor BrooklynShutdownHooks to make testable Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/a972f00e Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/a972f00e Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/a972f00e
Branch: refs/pull/1406/head Commit: a972f00e6dced2431b672716cb1620c8e92fb4cf Parents: 49d190c Author: Aled Sage <aled.s...@gmail.com> Authored: Thu May 22 09:24:57 2014 +0100 Committer: Aled Sage <aled.s...@gmail.com> Committed: Thu May 22 09:24:57 2014 +0100 ---------------------------------------------------------------------- .../entity/basic/BrooklynShutdownHooks.java | 53 +++++++++++--------- .../entity/basic/BrooklynShutdownHooksTest.java | 37 ++++++++++++++ 2 files changed, 66 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a972f00e/core/src/main/java/brooklyn/entity/basic/BrooklynShutdownHooks.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/brooklyn/entity/basic/BrooklynShutdownHooks.java b/core/src/main/java/brooklyn/entity/basic/BrooklynShutdownHooks.java index 3f0e49a..979422b 100644 --- a/core/src/main/java/brooklyn/entity/basic/BrooklynShutdownHooks.java +++ b/core/src/main/java/brooklyn/entity/basic/BrooklynShutdownHooks.java @@ -13,6 +13,7 @@ import brooklyn.management.Task; import brooklyn.util.collections.MutableMap; import brooklyn.util.javalang.Threads; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; public class BrooklynShutdownHooks { @@ -23,33 +24,37 @@ public class BrooklynShutdownHooks { private static final List<Entity> entitiesToStopOnShutdown = Lists.newArrayList(); public static void invokeStopOnShutdown(Entity entity) { + synchronized (entitiesToStopOnShutdown) { + entitiesToStopOnShutdown.add(entity); + } if (isShutdownHookRegistered.compareAndSet(false, true)) { - Threads.addShutdownHook(new Runnable() { - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void run() { - synchronized (entitiesToStopOnShutdown) { - log.info("Brooklyn stopOnShutdown shutdown-hook invoked: stopping "+entitiesToStopOnShutdown); - List<Task> stops = new ArrayList<Task>(); - for (Entity entity: entitiesToStopOnShutdown) { - try { - stops.add(entity.invoke(Startable.STOP, new MutableMap())); - } catch (Exception exc) { - log.debug("stopOnShutdown of "+entity+" returned error: "+exc, exc); - } - } - for (Task t: stops) { - try { - log.debug("stopOnShutdown of {} completed: {}", t, t.get()); - } catch (Exception exc) { - log.debug("stopOnShutdown of "+t+" returned error: "+exc, exc); - } - } + Threads.addShutdownHook(new BrooklynShutdownHookJob()); + } + } + + @VisibleForTesting + public static class BrooklynShutdownHookJob implements Runnable { + @SuppressWarnings({ "unchecked", "rawtypes" }) + @Override + public void run() { + synchronized (entitiesToStopOnShutdown) { + log.info("Brooklyn stopOnShutdown shutdown-hook invoked: stopping "+entitiesToStopOnShutdown); + List<Task> stops = new ArrayList<Task>(); + for (Entity entity: entitiesToStopOnShutdown) { + try { + stops.add(entity.invoke(Startable.STOP, new MutableMap())); + } catch (Exception exc) { + log.debug("stopOnShutdown of "+entity+" returned error: "+exc, exc); } } - }); - } - synchronized (entitiesToStopOnShutdown) { - entitiesToStopOnShutdown.add(entity); + for (Task t: stops) { + try { + log.debug("stopOnShutdown of {} completed: {}", t, t.get()); + } catch (Exception exc) { + log.debug("stopOnShutdown of "+t+" returned error: "+exc, exc); + } + } + } } } } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/a972f00e/core/src/test/java/brooklyn/entity/basic/BrooklynShutdownHooksTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/brooklyn/entity/basic/BrooklynShutdownHooksTest.java b/core/src/test/java/brooklyn/entity/basic/BrooklynShutdownHooksTest.java new file mode 100644 index 0000000..ba65f46 --- /dev/null +++ b/core/src/test/java/brooklyn/entity/basic/BrooklynShutdownHooksTest.java @@ -0,0 +1,37 @@ +package brooklyn.entity.basic; + +import static org.testng.Assert.assertTrue; + +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import brooklyn.entity.proxying.EntitySpec; +import brooklyn.test.entity.TestApplication; +import brooklyn.test.entity.TestEntity; + +public class BrooklynShutdownHooksTest { + + private TestApplication app; + private TestEntity entity; + + @BeforeMethod(alwaysRun=true) + public void setUp() { + app = ApplicationBuilder.newManagedApp(TestApplication.class); + entity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + } + + @AfterMethod(alwaysRun=true) + public void tearDown() { + if (app != null) Entities.destroyAll(app.getManagementContext()); + } + + @Test + public void testInvokeStopEntityOnShutdown() throws Exception { + BrooklynShutdownHooks.invokeStopOnShutdown(entity); + BrooklynShutdownHooks.BrooklynShutdownHookJob job = new BrooklynShutdownHooks.BrooklynShutdownHookJob(); + job.run(); + + assertTrue(entity.getCallHistory().contains("stop")); + } +}