Skip to content

Commit

Permalink
Closes apache#1011
Browse files Browse the repository at this point in the history
Fix requiredUnless config key constraint, with attributeWhenReady

Previously this caused the entity’s startup to hang.
  • Loading branch information
Duncan Grant committed Oct 25, 2018
2 parents 04c3283 + 2ff53c7 commit 96768de
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,46 @@ public void testConfigParameterConstraintObject() throws Exception {
assertKeyEquals(newEntity, "testRequired", null, String.class, null, "myprefix-myVal");
}

@Test
public void testConfigParameterConstraintOnOtherKeyWithAttributeWhenReady() throws Exception {
addCatalogItems(
"brooklyn.catalog:",
" itemType: entity",
" items:",
" - id: entity-with-keys",
" item:",
" type: "+TestEntity.class.getName(),
" brooklyn.parameters:",
" - name: key1",
" type: String",
" constraints:",
" - requiredUnless: key2",
" - name: key2",
" type: String",
" constraints:",
" - requiredUnless: key1");

String yaml = Joiner.on("\n").join(
"services:",
"- type: entity-with-keys",
" brooklyn.config:",
" key1: $brooklyn:root().attributeWhenReady(\"myattribute\")");

Entity app = createStartWaitAndLogApplication(yaml);
TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren());

Predicate<?> constraint = entity.getEntityType().getConfigKey("key1").getConstraint();
assertEquals(constraint.toString(), "requiredUnless(\"key2\")");

// Rebind, and then check again that the config key is listed
Entity newApp = rebind();
TestEntity newEntity = (TestEntity) Iterables.getOnlyElement(newApp.getChildren());

Predicate<?> newConstraint = newEntity.getEntityType().getConfigKey("key1").getConstraint();
assertEquals(newConstraint.toString(), "requiredUnless(\"key2\")");
}


public static class PredicateRegexPojo implements Predicate<Object> {
private String regex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,17 @@ public boolean apply(Object input, BrooklynObject context) {
// would be nice to offer an explanation, but that will need a richer API or a thread local
List<Object> vals = new ArrayList<>();
for (String otherKeyName : otherKeyNames) {
vals.add(context.config().get(ConfigKeys.newConfigKey(Object.class, otherKeyName)));
// Use getNonBlocking, in case the value is an `attributeWhenReady` and the
// attribute is not yet available - don't want to hang.
ConfigKey<Object> otherKey = ConfigKeys.newConfigKey(Object.class, otherKeyName);
BrooklynObjectInternal.ConfigurationSupportInternal configInternal = ((BrooklynObjectInternal) context).config();

Maybe<?> maybeValue = configInternal.getNonBlocking(otherKey);
if (maybeValue.isPresent()) {
vals.add(maybeValue.get());
} else {
return true; // abort; assume valid
}
}
return test(input, vals);
}
Expand Down

0 comments on commit 96768de

Please sign in to comment.