Github user aledsage commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/1120#discussion_r48564756
--- Diff:
brooklyn-server/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/DslAndRebindYamlTest.java
---
@@ -115,34 +125,207 @@ public T call() throws Exception {
}
}).build()).getUnchecked();
}
-
+
@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());
+
+ 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
+ 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
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");
--- End diff --
This seems to be confusing modifying the entities from before rebind, but
doing so after rebind. The call to `rebind()` is to simulate us having
restarted the Brooklyn server. It's configurable whether we actually terminate
the original management context. I guess this test is passing because the
original management context was not terminated.
I'm looking at these tests in more detail. I'll make a few changes and
submit a pull request against your branch @googlielmo (either this evening or
tomorrow).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---