You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by al...@apache.org on 2016/01/07 17:10:47 UTC

[1/8] incubator-brooklyn git commit: Test correct persistence of attributeWhenReady

Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master e300cf33c -> 7bcb3920d


Test correct persistence of attributeWhenReady


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

Branch: refs/heads/master
Commit: 68d8018ae7c71df69006271a2a4c31e0f5350461
Parents: cd504e2
Author: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Authored: Tue Dec 22 16:43:22 2015 +0100
Committer: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Committed: Tue Dec 29 12:23:48 2015 +0100

----------------------------------------------------------------------
 .../brooklyn/camp/brooklyn/DslAndRebindYamlTest.java | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/68d8018a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 31a7951..8f0f560 100644
--- a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -27,6 +27,7 @@ import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.Sensor;
+import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.Attributes;
@@ -39,6 +40,7 @@ import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.test.EntityTestUtils;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.task.Tasks;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
@@ -124,6 +126,19 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     }
 
     @Test
+    public void testDslAttributeWhenReadyPersisted() throws Exception {
+        Entity testEntity = entityWithAttributeWhenReady();
+        Application app2 = rebind(testEntity.getApplication());
+        Entity e2 = Iterables.getOnlyElement( app2.getChildren() );
+
+        Maybe<Object> maybe = ((EntityInternal)e2).config().getLocalRaw(TestEntity.CONF_NAME);
+        Assert.assertTrue(maybe.isPresentAndNonNull());
+        Assert.assertTrue(BrooklynDslDeferredSupplier.class.isInstance(maybe.get()));
+        BrooklynDslDeferredSupplier deferredSupplier = (BrooklynDslDeferredSupplier) maybe.get();
+        Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"x\").attributeWhenReady(\"foo\")");
+    }
+
+    @Test
     public void testDslAttributeWhenReadyRebind() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
         ((EntityInternal)testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");


[8/8] incubator-brooklyn git commit: This closes #1120

Posted by al...@apache.org.
This closes #1120


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

Branch: refs/heads/master
Commit: 7bcb3920d4d1770e031800addd902a2c3c507894
Parents: e300cf3 ca6f665
Author: Aled Sage <al...@gmail.com>
Authored: Thu Jan 7 16:10:27 2016 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Jan 7 16:10:27 2016 +0000

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 304 +++++++++++++++----
 1 file changed, 252 insertions(+), 52 deletions(-)
----------------------------------------------------------------------



[2/8] incubator-brooklyn git commit: Address comments

Posted by al...@apache.org.
Address comments

- sanity check on serialized value
- set up test for attributeWhenReady in entity spec (not working ATM see
  FIXME comment)


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

Branch: refs/heads/master
Commit: 3e57289e481314aa12011b5fdc704370331cc852
Parents: 68d8018
Author: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Authored: Tue Dec 22 22:38:32 2015 +0100
Committer: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Committed: Tue Dec 29 12:27:22 2015 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 65 +++++++++++++++++---
 1 file changed, 56 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3e57289e/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 8f0f560..2fda4a0 100644
--- a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -21,10 +21,14 @@ package org.apache.brooklyn.camp.brooklyn;
 import java.io.File;
 import java.util.Set;
 import java.util.concurrent.Callable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.brooklyn.api.entity.Application;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.api.mgmt.ha.MementoCopyMode;
+import org.apache.brooklyn.api.mgmt.rebind.mementos.BrooklynMementoRawData;
 import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.api.sensor.Sensor;
 import org.apache.brooklyn.camp.brooklyn.spi.dsl.BrooklynDslDeferredSupplier;
@@ -34,6 +38,7 @@ import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
+import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
 import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.entity.TestEntity;
@@ -68,7 +73,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         mgmtContexts.add(mgmt);
         return mgmt;
     }
-    
+
     @AfterMethod(alwaysRun = true)
     @Override
     public void tearDown() {
@@ -101,11 +106,11 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
 
         log.info("App started:");
         Entities.dumpInfo(app);
-        
+
         Assert.assertTrue(app.getChildren().iterator().hasNext(), "Expected app to have child entity");
         Entity entity = app.getChildren().iterator().next();
         Assert.assertTrue(entity instanceof TestEntity, "Expected TestEntity, found " + entity.getClass());
-        
+
         return entity;
     }
 
@@ -117,7 +122,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
             }
         }).build()).getUnchecked();
     }
-    
+
     @Test
     public void testDslAttributeWhenReady() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
@@ -136,6 +141,48 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         Assert.assertTrue(BrooklynDslDeferredSupplier.class.isInstance(maybe.get()));
         BrooklynDslDeferredSupplier deferredSupplier = (BrooklynDslDeferredSupplier) maybe.get();
         Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"x\").attributeWhenReady(\"foo\")");
+
+        // assert the persisted state itself is as expected, and not too big
+        BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(app2.getManagementContext(), MementoCopyMode.LOCAL);
+        String persistedStateForE2 = raw.getEntities().get(e2.getId());
+        Matcher matcher = Pattern.compile(".*\\<test.confName\\>(.*)\\<\\/test.confName\\>.*", Pattern.DOTALL)
+                .matcher(persistedStateForE2);
+        Assert.assertTrue(matcher.find());
+        String testConfNamePersistedState = matcher.group(1);
+
+        Assert.assertNotNull(testConfNamePersistedState);
+        // should be about 200 chars long, something like:
+        //
+        //      <test.confName>
+        //        <org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent_-AttributeWhenReady>
+        //          <component>
+        //            <componentId>x</componentId>
+        //            <scope>GLOBAL</scope>
+        //          </component>
+        //          <sensorName>foo</sensorName>
+        //        </org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent_-AttributeWhenReady>
+        //      </test.confName>
+
+        Assert.assertTrue(testConfNamePersistedState.length() < 400, "persisted state too long: "+testConfNamePersistedState);
+    }
+
+    @Test public void testDslAttributeWhenReadyPersistedInEntitySpec() throws Exception {
+        String yaml =   "location: localhost\n"+
+                        "name: Test Cluster\n"+
+                        "services:\n"+
+                        "- type: org.apache.brooklyn.entity.group.DynamicCluster\n"+
+                        "  id: test-cluster\n"+
+                        "  initialSize: 1\n"+
+                        "  memberSpec:\n"+
+                        "    $brooklyn:entitySpec:\n"+
+                        "      type: org.apache.brooklyn.core.test.entity.TestEntity\n"+
+                        "      brooklyn.config:\n"+
+                        "        test.confName: $brooklyn:component(\"test-cluster\").attributeWhenReady(\"sensor\")";
+
+        Entity testEntity = createAndStartApplication(yaml);
+
+        // FIXME java.io.NotSerializableException: org.apache.brooklyn.entity.group.DynamicClusterImpl$NextClusterMemberIdSupplier
+        Application app2 = rebind(testEntity.getApplication());
     }
 
     @Test
@@ -144,12 +191,12 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         ((EntityInternal)testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
         Application app2 = rebind(testEntity.getApplication());
         Entity e2 = Iterables.getOnlyElement( app2.getChildren() );
-        
+
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
     }
 
     private Entity entityWithAttributeWhenReady() throws Exception {
-        return setupAndCheckTestEntityInBasicYamlWith( 
+        return setupAndCheckTestEntityInBasicYamlWith(
             "  id: x",
             "  brooklyn.config:",
             "    test.confName: $brooklyn:component(\"x\").attributeWhenReady(\"foo\")");
@@ -169,7 +216,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         s = inTask ? getConfigInTask(te2, configKey) : te2.getConfig(configKey);
         Assert.assertEquals(s, expectedSensor);
     }
-    
+
     @Test
     public void testDslSensorFromClass() throws Exception {
         doTestOnEntityWithSensor(entityWithSensorFromClass(), Attributes.SERVICE_UP);
@@ -179,7 +226,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     @Test
     public void testDslSensorLocal() throws Exception {
         doTestOnEntityWithSensor(entityWithSensorLocal(), TestEntity.SEQUENCE);
-        // here without context it makes one up, so type info (and description etc) not present; 
+        // here without context it makes one up, so type info (and description etc) not present;
         // but context is needed to submit the DslDeferredSupplier object, so this would fail
 //        doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, TestEntity.SEQUENCE.getName()), false);
     }
@@ -189,7 +236,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         // here context has no impact, but it is needed to submit the DslDeferredSupplier object so this would fail
 //        doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, "sensor.foo"), false);
     }
-    
+
     private Entity entityWithSensorFromClass() throws Exception {
         return setupAndCheckTestEntityInBasicYamlWith( 
             "  id: x",


[3/8] incubator-brooklyn git commit: Add new test for attributeWhenReady in EntitySpec

Posted by al...@apache.org.
Add new test for attributeWhenReady in EntitySpec

 - tests ATW is persisted correctly while a task is waiting for its value
 - rebase to master


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

Branch: refs/heads/master
Commit: 9d65a61ee71dd7eb94efe707615c03c3325b8ca6
Parents: 3e57289
Author: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Authored: Mon Dec 28 16:59:47 2015 +0100
Committer: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Committed: Tue Dec 29 16:12:48 2015 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 183 +++++++++++++------
 1 file changed, 123 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/9d65a61e/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 2fda4a0..522d609 100644
--- a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -21,6 +21,10 @@ package org.apache.brooklyn.camp.brooklyn;
 import java.io.File;
 import java.util.Set;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -42,6 +46,7 @@ import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
 import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.apache.brooklyn.entity.group.DynamicCluster;
 import org.apache.brooklyn.test.EntityTestUtils;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.task.Tasks;
@@ -57,16 +62,16 @@ import com.google.common.io.Files;
 
 @Test
 public class DslAndRebindYamlTest extends AbstractYamlTest {
-    
+
     private static final Logger log = LoggerFactory.getLogger(DslAndRebindYamlTest.class);
-    
+
     protected ClassLoader classLoader = getClass().getClassLoader();
     protected File mementoDir;
     protected Set<ManagementContext> mgmtContexts = MutableSet.of();
 
     @Override
     protected LocalManagementContext newTestManagementContext() {
-        if (mementoDir!=null) throw new IllegalStateException("already created mgmt context");
+        if (mementoDir != null) throw new IllegalStateException("already created mgmt context");
         mementoDir = Files.createTempDir();
         mementoDir.deleteOnExit();
         LocalManagementContext mgmt = RebindTestUtils.newPersistingManagementContext(mementoDir, classLoader, 1);
@@ -77,7 +82,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     @AfterMethod(alwaysRun = true)
     @Override
     public void tearDown() {
-        for (ManagementContext mgmt: mgmtContexts) Entities.destroyAll(mgmt);
+        for (ManagementContext mgmt : mgmtContexts) Entities.destroyAll(mgmt);
         super.tearDown();
         mementoDir = null;
         mgmtContexts.clear();
@@ -90,15 +95,14 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
 
     public Application rebind(Application app) throws Exception {
         RebindTestUtils.waitForPersisted(app);
-        // not strictly needed, but for good measure:
-        RebindTestUtils.checkCurrentMementoSerializable(app);
+        // Removed because of issues in some tests: // RebindTestUtils.checkCurrentMementoSerializable(app);
         Application result = RebindTestUtils.rebind(mementoDir, getClass().getClassLoader());
         mgmtContexts.add(result.getManagementContext());
         return result;
     }
 
 
-    protected Entity setupAndCheckTestEntityInBasicYamlWith(String ...extras) throws Exception {
+    protected Entity setupAndCheckTestEntityInBasicYamlWith(String... extras) throws Exception {
         Entity app = createAndStartApplication(loadYaml("test-entity-basic-template.yaml", extras));
         waitForApplicationTasks(app);
 
@@ -126,23 +130,25 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     @Test
     public void testDslAttributeWhenReady() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
-        ((EntityInternal)testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
+        ((EntityInternal) testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
         Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "bar");
     }
 
     @Test
     public void testDslAttributeWhenReadyPersisted() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
+
+        // Persist and rebind
         Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement( app2.getChildren() );
+        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
 
-        Maybe<Object> maybe = ((EntityInternal)e2).config().getLocalRaw(TestEntity.CONF_NAME);
+        Maybe<Object> maybe = ((EntityInternal) e2).config().getLocalRaw(TestEntity.CONF_NAME);
         Assert.assertTrue(maybe.isPresentAndNonNull());
         Assert.assertTrue(BrooklynDslDeferredSupplier.class.isInstance(maybe.get()));
         BrooklynDslDeferredSupplier deferredSupplier = (BrooklynDslDeferredSupplier) maybe.get();
         Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"x\").attributeWhenReady(\"foo\")");
 
-        // assert the persisted state itself is as expected, and not too big
+        // Assert the persisted state itself is as expected, and not too big
         BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(app2.getManagementContext(), MementoCopyMode.LOCAL);
         String persistedStateForE2 = raw.getEntities().get(e2.getId());
         Matcher matcher = Pattern.compile(".*\\<test.confName\\>(.*)\\<\\/test.confName\\>.*", Pattern.DOTALL)
@@ -163,48 +169,103 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         //        </org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent_-AttributeWhenReady>
         //      </test.confName>
 
-        Assert.assertTrue(testConfNamePersistedState.length() < 400, "persisted state too long: "+testConfNamePersistedState);
+        Assert.assertTrue(testConfNamePersistedState.length() < 400, "persisted state too long: " + testConfNamePersistedState);
     }
 
-    @Test public void testDslAttributeWhenReadyPersistedInEntitySpec() throws Exception {
-        String yaml =   "location: localhost\n"+
-                        "name: Test Cluster\n"+
-                        "services:\n"+
-                        "- type: org.apache.brooklyn.entity.group.DynamicCluster\n"+
-                        "  id: test-cluster\n"+
-                        "  initialSize: 1\n"+
-                        "  memberSpec:\n"+
-                        "    $brooklyn:entitySpec:\n"+
-                        "      type: org.apache.brooklyn.core.test.entity.TestEntity\n"+
-                        "      brooklyn.config:\n"+
-                        "        test.confName: $brooklyn:component(\"test-cluster\").attributeWhenReady(\"sensor\")";
-
-        Entity testEntity = createAndStartApplication(yaml);
-
-        // FIXME java.io.NotSerializableException: org.apache.brooklyn.entity.group.DynamicClusterImpl$NextClusterMemberIdSupplier
+    @Test
+    public void testDslAttributeWhenReadyPersistedInEntitySpecWhileTaskIsWaiting() throws Exception {
+        String yaml = "location: localhost\n" +
+                "name: Test Cluster\n" +
+                "services:\n" +
+                "- type: org.apache.brooklyn.entity.group.DynamicCluster\n" +
+                "  id: test-cluster\n" +
+                "  initialSize: 1\n" +
+                "  memberSpec:\n" +
+                "    $brooklyn:entitySpec:\n" +
+                "      type: org.apache.brooklyn.core.test.entity.TestEntity\n" +
+                "      brooklyn.config:\n" +
+                "        test.confName: $brooklyn:component(\"test-cluster\").attributeWhenReady(\"sensor\")";
+
+        final Entity testEntity = createAndStartApplication(yaml);
+
+        DynamicCluster clusterEntity1 = (DynamicCluster) Iterables.getOnlyElement(testEntity.getApplication().getChildren());
+
+        TestEntity testEntity1 = null;
+        for (Entity entity : clusterEntity1.getChildren()) {
+            if (entity instanceof TestEntity) {
+                testEntity1 = (TestEntity) entity;
+                break;
+            }
+        }
+        Assert.assertNotNull(testEntity1, "TestEntity not found in DynamicCluster");
+
+        final TestEntity childTestEntity = testEntity1;
+
+        // Wait for the attribute to be ready in a new Task
+        Callable<String> configGetter = new Callable<String>() {
+            @Override
+            public String call() throws Exception {
+                String s = getConfigInTask(childTestEntity, TestEntity.CONF_NAME);
+                getLogger().info("getConfig {}={}", TestEntity.CONF_NAME, s);
+                return s;
+            }
+        };
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+        Future<String> stringFuture = executorService.submit(configGetter);
+
+        // Persist and rebind
         Application app2 = rebind(testEntity.getApplication());
+
+        DynamicCluster clusterEntity2 = (DynamicCluster) Iterables.getOnlyElement(app2.getApplication().getChildren());
+
+        TestEntity testEntity2 = null;
+        for (Entity entity : clusterEntity2.getChildren()) {
+            if (entity instanceof TestEntity) {
+                testEntity2 = (TestEntity) entity;
+                break;
+            }
+        }
+        Assert.assertNotNull(testEntity2, "TestEntity not found in DynamicCluster");
+
+        Maybe<Object> maybe = testEntity2.config().getLocalRaw(TestEntity.CONF_NAME);
+
+        Assert.assertTrue(maybe.isPresentAndNonNull());
+        Assert.assertTrue(BrooklynDslDeferredSupplier.class.isInstance(maybe.get()));
+        BrooklynDslDeferredSupplier deferredSupplier = (BrooklynDslDeferredSupplier) maybe.get();
+        Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"test-cluster\").attributeWhenReady(\"sensor\")");
+
+        // Check that the Task is still waiting for attribute to be ready
+        Assert.assertFalse(stringFuture.isDone());
+
+        // Now set sensor value
+        ((EntityInternal) clusterEntity1).sensors().set(Sensors.newStringSensor("sensor"), "bar");
+
+        String s = stringFuture.get(10, TimeUnit.SECONDS); // Timeout just for sanity
+
+        Assert.assertEquals(s, "bar");
     }
 
     @Test
     public void testDslAttributeWhenReadyRebind() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
-        ((EntityInternal)testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
+        ((EntityInternal) testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
         Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement( app2.getChildren() );
+        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
 
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
     }
 
     private Entity entityWithAttributeWhenReady() throws Exception {
         return setupAndCheckTestEntityInBasicYamlWith(
-            "  id: x",
-            "  brooklyn.config:",
-            "    test.confName: $brooklyn:component(\"x\").attributeWhenReady(\"foo\")");
+                "  id: x",
+                "  brooklyn.config:",
+                "    test.confName: $brooklyn:component(\"x\").attributeWhenReady(\"foo\")");
     }
 
     private void doTestOnEntityWithSensor(Entity testEntity, Sensor<?> expectedSensor) throws Exception {
         doTestOnEntityWithSensor(testEntity, expectedSensor, true);
     }
+
     private void doTestOnEntityWithSensor(Entity testEntity, Sensor<?> expectedSensor, boolean inTask) throws Exception {
         @SuppressWarnings("rawtypes")
         ConfigKey<Sensor> configKey = ConfigKeys.newConfigKey(Sensor.class, "test.sensor");
@@ -212,7 +273,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         s = inTask ? getConfigInTask(testEntity, configKey) : testEntity.getConfig(configKey);
         Assert.assertEquals(s, expectedSensor);
         Application app2 = rebind(testEntity.getApplication());
-        Entity te2 = Iterables.getOnlyElement( app2.getChildren() );
+        Entity te2 = Iterables.getOnlyElement(app2.getChildren());
         s = inTask ? getConfigInTask(te2, configKey) : te2.getConfig(configKey);
         Assert.assertEquals(s, expectedSensor);
     }
@@ -223,6 +284,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         // without context it can still find it
         doTestOnEntityWithSensor(entityWithSensorFromClass(), Attributes.SERVICE_UP, false);
     }
+
     @Test
     public void testDslSensorLocal() throws Exception {
         doTestOnEntityWithSensor(entityWithSensorLocal(), TestEntity.SEQUENCE);
@@ -230,6 +292,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         // but context is needed to submit the DslDeferredSupplier object, so this would fail
 //        doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, TestEntity.SEQUENCE.getName()), false);
     }
+
     @Test
     public void testDslSensorAdHoc() throws Exception {
         doTestOnEntityWithSensor(entityWithSensorAdHoc(), Sensors.newSensor(Object.class, "sensor.foo"));
@@ -238,24 +301,24 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     }
 
     private Entity entityWithSensorFromClass() throws Exception {
-        return setupAndCheckTestEntityInBasicYamlWith( 
-            "  id: x",
-            "  brooklyn.config:",
-            "    test.sensor: $brooklyn:sensor(\""+Attributes.class.getName()+"\", \""+Attributes.SERVICE_UP.getName()+"\")");
+        return setupAndCheckTestEntityInBasicYamlWith(
+                "  id: x",
+                "  brooklyn.config:",
+                "    test.sensor: $brooklyn:sensor(\"" + Attributes.class.getName() + "\", \"" + Attributes.SERVICE_UP.getName() + "\")");
     }
 
     private Entity entityWithSensorLocal() throws Exception {
-        return setupAndCheckTestEntityInBasicYamlWith( 
-            "  id: x",
-            "  brooklyn.config:",
-            "    test.sensor: $brooklyn:sensor(\""+TestEntity.SEQUENCE.getName()+"\")");
+        return setupAndCheckTestEntityInBasicYamlWith(
+                "  id: x",
+                "  brooklyn.config:",
+                "    test.sensor: $brooklyn:sensor(\"" + TestEntity.SEQUENCE.getName() + "\")");
     }
 
     private Entity entityWithSensorAdHoc() throws Exception {
-        return setupAndCheckTestEntityInBasicYamlWith( 
-            "  id: x",
-            "  brooklyn.config:",
-            "    test.sensor: $brooklyn:sensor(\"sensor.foo\")");
+        return setupAndCheckTestEntityInBasicYamlWith(
+                "  id: x",
+                "  brooklyn.config:",
+                "    test.sensor: $brooklyn:sensor(\"sensor.foo\")");
     }
 
 
@@ -269,18 +332,18 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     public void testDslConfigFromRootRebind() throws Exception {
         Entity testEntity = entityWithConfigFromRoot();
         Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement( app2.getChildren() );
-        
+        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
     }
 
     private Entity entityWithConfigFromRoot() throws Exception {
-        return setupAndCheckTestEntityInBasicYamlWith( 
-            "  id: x",
-            "  brooklyn.config:",
-            "    test.confName: $brooklyn:component(\"x\").config(\"foo\")",
-            "brooklyn.config:",
-            "  foo: bar");
+        return setupAndCheckTestEntityInBasicYamlWith(
+                "  id: x",
+                "  brooklyn.config:",
+                "    test.confName: $brooklyn:component(\"x\").config(\"foo\")",
+                "brooklyn.config:",
+                "  foo: bar");
     }
 
 
@@ -294,16 +357,16 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     public void testDslFormatStringRebind() throws Exception {
         Entity testEntity = entityWithFormatString();
         Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement( app2.getChildren() );
-        
+        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+
         Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "hello world");
     }
 
     private Entity entityWithFormatString() throws Exception {
-        return setupAndCheckTestEntityInBasicYamlWith( 
-            "  id: x",
-            "  brooklyn.config:",
-            "    test.confName: $brooklyn:formatString(\"hello %s\", \"world\")");
+        return setupAndCheckTestEntityInBasicYamlWith(
+                "  id: x",
+                "  brooklyn.config:",
+                "    test.confName: $brooklyn:formatString(\"hello %s\", \"world\")");
     }
 
 


[7/8] incubator-brooklyn git commit: Merge pull request #2 from aledsage/googlielmo/feature/deferred-supplier-persistence-aled

Posted by al...@apache.org.
Merge pull request #2 from aledsage/googlielmo/feature/deferred-supplier-persistence-aled

More yaml AttributeWhenReady tests

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

Branch: refs/heads/master
Commit: ca6f6658293a502ae32517a42277a4709e5cfc3d
Parents: d09147f 9504022
Author: Guglielmo Nigri <go...@gmail.com>
Authored: Wed Dec 30 19:31:23 2015 +0100
Committer: Guglielmo Nigri <go...@gmail.com>
Committed: Wed Dec 30 19:31:23 2015 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 261 ++++++++++---------
 1 file changed, 138 insertions(+), 123 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/ca6f6658/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------


[4/8] incubator-brooklyn git commit: Test AWR value is not leaked in the persistent state

Posted by al...@apache.org.
Test AWR value is not leaked in the persistent state

- set test-cluster sensor `sensor` e.g. to `foo` and then ensure that
  `foo` is not being written


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

Branch: refs/heads/master
Commit: 3746503c292c307ce28b56389fa508966503d1b4
Parents: 9d65a61
Author: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Authored: Tue Dec 29 16:34:26 2015 +0100
Committer: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Committed: Tue Dec 29 16:34:26 2015 +0100

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 61 ++++++++++++++++++++
 1 file changed, 61 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/3746503c/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 522d609..0a9f778 100644
--- a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -246,6 +246,67 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     }
 
     @Test
+    public void testDslAttributeWhenReadyPersistedWithoutLeaks() throws Exception {
+        String yaml = "location: localhost\n" +
+                "name: Test Cluster\n" +
+                "services:\n" +
+                "- type: org.apache.brooklyn.entity.group.DynamicCluster\n" +
+                "  id: test-cluster\n" +
+                "  initialSize: 1\n" +
+                "  memberSpec:\n" +
+                "    $brooklyn:entitySpec:\n" +
+                "      type: org.apache.brooklyn.core.test.entity.TestEntity\n" +
+                "      brooklyn.config:\n" +
+                "        test.confName: $brooklyn:component(\"test-cluster\").attributeWhenReady(\"sensor\")";
+
+        final Entity testEntity = createAndStartApplication(yaml);
+
+        DynamicCluster clusterEntity1 = (DynamicCluster) Iterables.getOnlyElement(testEntity.getApplication().getChildren());
+
+        TestEntity testEntity1 = null;
+        for (Entity entity : clusterEntity1.getChildren()) {
+            if (entity instanceof TestEntity) {
+                testEntity1 = (TestEntity) entity;
+                break;
+            }
+        }
+        Assert.assertNotNull(testEntity1, "TestEntity not found in DynamicCluster");
+
+        final TestEntity childTestEntity = testEntity1;
+
+        // Now set sensor value
+        ((EntityInternal) clusterEntity1).sensors().set(Sensors.newStringSensor("sensor"), "bar");
+
+        String s1 = getConfigInTask(childTestEntity, TestEntity.CONF_NAME);
+        Assert.assertEquals(s1, "bar");
+
+        // Persist and rebind
+        Application app2 = rebind(testEntity.getApplication());
+
+        DynamicCluster clusterEntity2 = (DynamicCluster) Iterables.getOnlyElement(app2.getApplication().getChildren());
+
+        TestEntity testEntity2 = null;
+        for (Entity entity : clusterEntity2.getChildren()) {
+            if (entity instanceof TestEntity) {
+                testEntity2 = (TestEntity) entity;
+                break;
+            }
+        }
+        Assert.assertNotNull(testEntity2, "TestEntity not found in DynamicCluster");
+
+        // Assert the persisted state itself is as expected, and does not contain the value "bar"
+        BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(app2.getManagementContext(), MementoCopyMode.LOCAL);
+        String persistedState = raw.getEntities().get(testEntity2.getId());
+        Matcher matcher = Pattern.compile(".*\\<test.confName\\>(.*)\\<\\/test.confName\\>.*", Pattern.DOTALL)
+                .matcher(persistedState);
+        Assert.assertTrue(matcher.find());
+        String testConfNamePersistedState = matcher.group(1);
+
+        Assert.assertNotNull(testConfNamePersistedState);
+        Assert.assertFalse(testConfNamePersistedState.contains("bar"), "value leaked in persisted state");
+    }
+
+    @Test
     public void testDslAttributeWhenReadyRebind() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
         ((EntityInternal) testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");


[6/8] incubator-brooklyn git commit: More yaml AttributeWhenReady tests

Posted by al...@apache.org.
More yaml AttributeWhenReady tests

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

Branch: refs/heads/master
Commit: 95040227dff9b370b101c6366ffe3c7b95fd9e1f
Parents: 3746503
Author: Aled Sage <al...@gmail.com>
Authored: Tue Dec 29 23:27:31 2015 +0000
Committer: Aled Sage <al...@gmail.com>
Committed: Tue Dec 29 23:27:31 2015 +0000

----------------------------------------------------------------------
 .../camp/brooklyn/DslAndRebindYamlTest.java     | 261 ++++++++++---------
 1 file changed, 138 insertions(+), 123 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/95040227/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 0a9f778..1417f71 100644
--- a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -19,6 +19,7 @@
 package org.apache.brooklyn.camp.brooklyn;
 
 import java.io.File;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
@@ -40,6 +41,7 @@ import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.Entities;
+import org.apache.brooklyn.core.entity.EntityAsserts;
 import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
@@ -47,7 +49,6 @@ import org.apache.brooklyn.core.mgmt.rebind.RebindTestUtils;
 import org.apache.brooklyn.core.sensor.Sensors;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.entity.group.DynamicCluster;
-import org.apache.brooklyn.test.EntityTestUtils;
 import org.apache.brooklyn.util.collections.MutableSet;
 import org.apache.brooklyn.util.core.task.Tasks;
 import org.apache.brooklyn.util.guava.Maybe;
@@ -55,9 +56,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import com.google.common.io.Files;
 
 @Test
@@ -68,6 +71,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     protected ClassLoader classLoader = getClass().getClassLoader();
     protected File mementoDir;
     protected Set<ManagementContext> mgmtContexts = MutableSet.of();
+    protected ExecutorService executor;
 
     @Override
     protected LocalManagementContext newTestManagementContext() {
@@ -79,9 +83,17 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         return mgmt;
     }
 
+    @BeforeMethod(alwaysRun = true)
+    @Override
+    public void setUp() {
+    	super.setUp();
+        executor = Executors.newSingleThreadExecutor();
+    }
+    
     @AfterMethod(alwaysRun = true)
     @Override
     public void tearDown() {
+    	if (executor != null) executor.shutdownNow();
         for (ManagementContext mgmt : mgmtContexts) Entities.destroyAll(mgmt);
         super.tearDown();
         mementoDir = null;
@@ -127,6 +139,19 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         }).build()).getUnchecked();
     }
 
+    protected <T> Future<T> getConfigInTaskAsync(final Entity entity, final ConfigKey<T> key) {
+	    // Wait for the attribute to be ready in a new Task
+	    Callable<T> configGetter = new Callable<T>() {
+	        @Override
+	        public T call() throws Exception {
+	            T s = getConfigInTask(entity, key);
+	            getLogger().info("getConfig {}={}", key, s);
+	            return s;
+	        }
+	    };
+	    return executor.submit(configGetter);
+    }
+
     @Test
     public void testDslAttributeWhenReady() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
@@ -135,9 +160,53 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
     }
 
     @Test
-    public void testDslAttributeWhenReadyPersisted() throws Exception {
+    public void testDslAttributeWhenReadyRebindWhenResolved() throws Exception {
         Entity testEntity = entityWithAttributeWhenReady();
+        ((EntityInternal) testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
+        
+        Application app2 = rebind(testEntity.getApplication());
+        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
 
+        Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
+    }
+
+    @Test
+    public void testDslAttributeWhenReadyWhenNotYetResolved() throws Exception {
+        Entity testEntity = entityWithAttributeWhenReady();
+        
+        Application app2 = rebind(testEntity.getApplication());
+        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
+
+        // Wait for the attribute to be ready in a new Task
+        Future<String> stringFuture = getConfigInTaskAsync(e2, TestEntity.CONF_NAME);
+
+        // Check that the Task is still waiting for attribute to be ready
+        Assert.assertFalse(stringFuture.isDone());
+
+        // Set the sensor; expect that to complete
+        e2.sensors().set(Sensors.newStringSensor("foo"), "bar");
+        String s = stringFuture.get(10, TimeUnit.SECONDS); // Timeout just for sanity
+        Assert.assertEquals(s, "bar");
+    }
+
+    @Test
+    public void testDslAttributeWhenReadyPersistedAsDeferredSupplier() throws Exception {
+    	doDslAttributeWhenReadyPersistedAsDeferredSupplier(false);
+    }
+    
+    @Test
+    public void testDslAttributeWhenReadyPersistedWithoutLeakingResolvedValue() throws Exception {
+    	doDslAttributeWhenReadyPersistedAsDeferredSupplier(true);
+    }
+    
+    protected void doDslAttributeWhenReadyPersistedAsDeferredSupplier(boolean resolvedBeforeRebind) throws Exception {
+        Entity testEntity = entityWithAttributeWhenReady();
+        
+        if (resolvedBeforeRebind) {
+        	testEntity.sensors().set(Sensors.newStringSensor("foo"), "bar");
+        	Assert.assertEquals(getConfigInTask(testEntity, TestEntity.CONF_NAME), "bar");
+        }
+        
         // Persist and rebind
         Application app2 = rebind(testEntity.getApplication());
         Entity e2 = Iterables.getOnlyElement(app2.getChildren());
@@ -145,7 +214,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         Maybe<Object> maybe = ((EntityInternal) e2).config().getLocalRaw(TestEntity.CONF_NAME);
         Assert.assertTrue(maybe.isPresentAndNonNull());
         Assert.assertTrue(BrooklynDslDeferredSupplier.class.isInstance(maybe.get()));
-        BrooklynDslDeferredSupplier deferredSupplier = (BrooklynDslDeferredSupplier) maybe.get();
+        BrooklynDslDeferredSupplier<?> deferredSupplier = (BrooklynDslDeferredSupplier<?>) maybe.get();
         Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"x\").attributeWhenReady(\"foo\")");
 
         // Assert the persisted state itself is as expected, and not too big
@@ -170,89 +239,27 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         //      </test.confName>
 
         Assert.assertTrue(testConfNamePersistedState.length() < 400, "persisted state too long: " + testConfNamePersistedState);
+        
+        Assert.assertFalse(testConfNamePersistedState.contains("bar"), "value 'bar' leaked in persisted state");
     }
 
     @Test
-    public void testDslAttributeWhenReadyPersistedInEntitySpecWhileTaskIsWaiting() throws Exception {
-        String yaml = "location: localhost\n" +
-                "name: Test Cluster\n" +
-                "services:\n" +
-                "- type: org.apache.brooklyn.entity.group.DynamicCluster\n" +
-                "  id: test-cluster\n" +
-                "  initialSize: 1\n" +
-                "  memberSpec:\n" +
-                "    $brooklyn:entitySpec:\n" +
-                "      type: org.apache.brooklyn.core.test.entity.TestEntity\n" +
-                "      brooklyn.config:\n" +
-                "        test.confName: $brooklyn:component(\"test-cluster\").attributeWhenReady(\"sensor\")";
-
-        final Entity testEntity = createAndStartApplication(yaml);
-
-        DynamicCluster clusterEntity1 = (DynamicCluster) Iterables.getOnlyElement(testEntity.getApplication().getChildren());
-
-        TestEntity testEntity1 = null;
-        for (Entity entity : clusterEntity1.getChildren()) {
-            if (entity instanceof TestEntity) {
-                testEntity1 = (TestEntity) entity;
-                break;
-            }
-        }
-        Assert.assertNotNull(testEntity1, "TestEntity not found in DynamicCluster");
-
-        final TestEntity childTestEntity = testEntity1;
-
-        // Wait for the attribute to be ready in a new Task
-        Callable<String> configGetter = new Callable<String>() {
-            @Override
-            public String call() throws Exception {
-                String s = getConfigInTask(childTestEntity, TestEntity.CONF_NAME);
-                getLogger().info("getConfig {}={}", TestEntity.CONF_NAME, s);
-                return s;
-            }
-        };
-        ExecutorService executorService = Executors.newSingleThreadExecutor();
-        Future<String> stringFuture = executorService.submit(configGetter);
-
-        // Persist and rebind
-        Application app2 = rebind(testEntity.getApplication());
-
-        DynamicCluster clusterEntity2 = (DynamicCluster) Iterables.getOnlyElement(app2.getApplication().getChildren());
-
-        TestEntity testEntity2 = null;
-        for (Entity entity : clusterEntity2.getChildren()) {
-            if (entity instanceof TestEntity) {
-                testEntity2 = (TestEntity) entity;
-                break;
-            }
-        }
-        Assert.assertNotNull(testEntity2, "TestEntity not found in DynamicCluster");
-
-        Maybe<Object> maybe = testEntity2.config().getLocalRaw(TestEntity.CONF_NAME);
-
-        Assert.assertTrue(maybe.isPresentAndNonNull());
-        Assert.assertTrue(BrooklynDslDeferredSupplier.class.isInstance(maybe.get()));
-        BrooklynDslDeferredSupplier deferredSupplier = (BrooklynDslDeferredSupplier) maybe.get();
-        Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"test-cluster\").attributeWhenReady(\"sensor\")");
-
-        // Check that the Task is still waiting for attribute to be ready
-        Assert.assertFalse(stringFuture.isDone());
-
-        // Now set sensor value
-        ((EntityInternal) clusterEntity1).sensors().set(Sensors.newStringSensor("sensor"), "bar");
-
-        String s = stringFuture.get(10, TimeUnit.SECONDS); // Timeout just for sanity
-
-        Assert.assertEquals(s, "bar");
+    public void testDslAttributeWhenReadyInEntitySpecWhenNotYetResolved() throws Exception {
+    	doDslAttributeWhenReadyInEntitySpec(false);
     }
-
+    
     @Test
-    public void testDslAttributeWhenReadyPersistedWithoutLeaks() throws Exception {
+    public void testDslAttributeWhenReadyInEntitySpecWhenAlreadyResolved() throws Exception {
+    	doDslAttributeWhenReadyInEntitySpec(true);
+    }
+    
+    protected void doDslAttributeWhenReadyInEntitySpec(boolean resolvedBeforeRebind) throws Exception {
         String yaml = "location: localhost\n" +
                 "name: Test Cluster\n" +
                 "services:\n" +
                 "- type: org.apache.brooklyn.entity.group.DynamicCluster\n" +
                 "  id: test-cluster\n" +
-                "  initialSize: 1\n" +
+                "  initialSize: 0\n" +
                 "  memberSpec:\n" +
                 "    $brooklyn:entitySpec:\n" +
                 "      type: org.apache.brooklyn.core.test.entity.TestEntity\n" +
@@ -260,60 +267,68 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
                 "        test.confName: $brooklyn:component(\"test-cluster\").attributeWhenReady(\"sensor\")";
 
         final Entity testEntity = createAndStartApplication(yaml);
+        DynamicCluster cluster = (DynamicCluster) Iterables.getOnlyElement(testEntity.getApplication().getChildren());
+        cluster.resize(1);
+        Assert.assertEquals(cluster.getMembers().size(), 1);
 
-        DynamicCluster clusterEntity1 = (DynamicCluster) Iterables.getOnlyElement(testEntity.getApplication().getChildren());
-
-        TestEntity testEntity1 = null;
-        for (Entity entity : clusterEntity1.getChildren()) {
-            if (entity instanceof TestEntity) {
-                testEntity1 = (TestEntity) entity;
-                break;
-            }
+        if (resolvedBeforeRebind) {
+            cluster.sensors().set(Sensors.newStringSensor("sensor"), "bar");
         }
-        Assert.assertNotNull(testEntity1, "TestEntity not found in DynamicCluster");
-
-        final TestEntity childTestEntity = testEntity1;
-
-        // Now set sensor value
-        ((EntityInternal) clusterEntity1).sensors().set(Sensors.newStringSensor("sensor"), "bar");
-
-        String s1 = getConfigInTask(childTestEntity, TestEntity.CONF_NAME);
-        Assert.assertEquals(s1, "bar");
 
         // Persist and rebind
-        Application app2 = rebind(testEntity.getApplication());
-
-        DynamicCluster clusterEntity2 = (DynamicCluster) Iterables.getOnlyElement(app2.getApplication().getChildren());
-
-        TestEntity testEntity2 = null;
-        for (Entity entity : clusterEntity2.getChildren()) {
-            if (entity instanceof TestEntity) {
-                testEntity2 = (TestEntity) entity;
-                break;
-            }
-        }
-        Assert.assertNotNull(testEntity2, "TestEntity not found in DynamicCluster");
+        Application app2 = rebind(cluster.getApplication());
+        DynamicCluster cluster2 = (DynamicCluster) Iterables.getOnlyElement(app2.getApplication().getChildren());
 
-        // Assert the persisted state itself is as expected, and does not contain the value "bar"
+        // Assert the persisted state itself is as expected, and not too big
         BrooklynMementoRawData raw = BrooklynPersistenceUtils.newStateMemento(app2.getManagementContext(), MementoCopyMode.LOCAL);
-        String persistedState = raw.getEntities().get(testEntity2.getId());
-        Matcher matcher = Pattern.compile(".*\\<test.confName\\>(.*)\\<\\/test.confName\\>.*", Pattern.DOTALL)
-                .matcher(persistedState);
-        Assert.assertTrue(matcher.find());
+        String persistedStateForE2 = raw.getEntities().get(cluster2.getId());
+        String expectedTag = "org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.DslComponent_-AttributeWhenReady";
+        Matcher matcher = Pattern.compile(".*\\<"+expectedTag+"\\>(.*)\\<\\/"+expectedTag+"\\>.*", Pattern.DOTALL)
+                .matcher(persistedStateForE2);
+        Assert.assertTrue(matcher.find(), persistedStateForE2);
         String testConfNamePersistedState = matcher.group(1);
-
         Assert.assertNotNull(testConfNamePersistedState);
-        Assert.assertFalse(testConfNamePersistedState.contains("bar"), "value leaked in persisted state");
-    }
 
-    @Test
-    public void testDslAttributeWhenReadyRebind() throws Exception {
-        Entity testEntity = entityWithAttributeWhenReady();
-        ((EntityInternal) testEntity).sensors().set(Sensors.newStringSensor("foo"), "bar");
-        Application app2 = rebind(testEntity.getApplication());
-        Entity e2 = Iterables.getOnlyElement(app2.getChildren());
-
-        Assert.assertEquals(getConfigInTask(e2, TestEntity.CONF_NAME), "bar");
+        // Can re-size to create a new member entity
+        cluster2.resize(2);
+        Assert.assertEquals(cluster2.getMembers().size(), 2);
+        
+        // Both the existing and the new member should have the DeferredSupplier config
+        for (Entity member : Iterables.filter(cluster2.getChildren(), TestEntity.class)) {
+	        Maybe<Object> maybe = ((EntityInternal)member).config().getLocalRaw(TestEntity.CONF_NAME);
+	        Assert.assertTrue(maybe.isPresentAndNonNull());
+	        BrooklynDslDeferredSupplier<?> deferredSupplier = (BrooklynDslDeferredSupplier<?>) maybe.get();
+	        Assert.assertEquals(deferredSupplier.toString(), "$brooklyn:entity(\"test-cluster\").attributeWhenReady(\"sensor\")");
+        }
+        
+        if (resolvedBeforeRebind) {
+            // All members should resolve their config
+            for (Entity member : Iterables.filter(cluster2.getChildren(), TestEntity.class)) {
+		        String val = getConfigInTask(member, TestEntity.CONF_NAME);
+		        Assert.assertEquals(val, "bar");
+            }
+        } else {
+        	List<Future<String>> futures = Lists.newArrayList();
+        	
+            // All members should have unresolved values
+            for (Entity member : Iterables.filter(cluster2.getChildren(), TestEntity.class)) {
+		        // Wait for the attribute to be ready in a new Task
+		        Future<String> stringFuture = getConfigInTaskAsync(member, TestEntity.CONF_NAME);
+		        futures.add(stringFuture);
+		        
+		        // Check that the Task is still waiting for attribute to be ready
+		        Thread.sleep(100);
+		        Assert.assertFalse(stringFuture.isDone());
+            }
+            
+            // After setting the sensor, all those values should now resolve
+	        cluster2.sensors().set(Sensors.newStringSensor("sensor"), "bar");
+	        
+	        for (Future<String> future : futures) {
+		        String s = future.get(10, TimeUnit.SECONDS); // Timeout just for sanity
+		        Assert.assertEquals(s, "bar");
+            }
+        }
     }
 
     private Entity entityWithAttributeWhenReady() throws Exception {
@@ -478,7 +493,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         );
         testEntity.sensors().set(TestEntity.NAME, "somefooname");
         AttributeSensor<String> transformedSensor = Sensors.newStringSensor("test.name.transformed");
-        EntityTestUtils.assertAttributeEqualsEventually(testEntity, transformedSensor, "somebarname");
+        EntityAsserts.assertAttributeEqualsEventually(testEntity, transformedSensor, "somebarname");
     }
 
     @Test
@@ -495,7 +510,7 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
         testEntity.sensors().set(Sensors.newStringSensor("test.replacement"), "bar");
         testEntity.sensors().set(TestEntity.NAME, "somefooname");
         AttributeSensor<String> transformedSensor = Sensors.newStringSensor("test.name.transformed");
-        EntityTestUtils.assertAttributeEqualsEventually(testEntity, transformedSensor, "somebarname");
+        EntityAsserts.assertAttributeEqualsEventually(testEntity, transformedSensor, "somebarname");
     }
 
 }


[5/8] incubator-brooklyn git commit: Address comments

Posted by al...@apache.org.
Address comments


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

Branch: refs/heads/master
Commit: d09147f2d07eea33d30701173f7f639157c09b54
Parents: 3746503
Author: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Authored: Tue Dec 29 18:09:08 2015 +0100
Committer: Guglielmo Nigri <gu...@cloudsoftcorp.com>
Committed: Tue Dec 29 18:09:08 2015 +0100

----------------------------------------------------------------------
 .../org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java     | 1 -
 1 file changed, 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/d09147f2/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
index 0a9f778..32ff1db 100644
--- a/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
+++ b/brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
@@ -95,7 +95,6 @@ public class DslAndRebindYamlTest extends AbstractYamlTest {
 
     public Application rebind(Application app) throws Exception {
         RebindTestUtils.waitForPersisted(app);
-        // Removed because of issues in some tests: // RebindTestUtils.checkCurrentMementoSerializable(app);
         Application result = RebindTestUtils.rebind(mementoDir, getClass().getClassLoader());
         mgmtContexts.add(result.getManagementContext());
         return result;