Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/622#discussion_r110596123
--- Diff:
core/src/test/java/org/apache/brooklyn/core/entity/ApplicationLifecycleStateTest.java
---
@@ -201,6 +227,163 @@ public void
testStartsThenChildFailsButWithQuorumCausesAppToStayHealthy() throws
assertUpAndRunningEventually(app);
}
+ /**
+ * Sensor value does not match the order of events. For example events
are running, starting,
+ * but sensor value is running.
+ * Causes a problem with
ComputeServiceIndicatorsFromChildrenAndMembers which will flag the entity
+ * on-fire event though children are running.
+ */
+ @Test
+ public void testSettingSensorFromThreads() {
+ final TestApplication app =
mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class));
+ final AttributeSensor<String> TEST_SENSOR =
Sensors.newStringSensor("test.sensor");
+
+ final AtomicReference<String> lastSeenState = new
AtomicReference<>();
+ app.subscriptions().subscribe(app, TEST_SENSOR, new
SensorEventListener<String>() {
+ @Override
+ public void onEvent(SensorEvent<String> event) {
+ lastSeenState.set(event.getValue());
+ log.debug("seen event=" + event);
+ }
+ });
+
+ Task<?> first = mgmt.getExecutionManager().submit(new Runnable() {
+ @Override
+ public void run() {
+ app.sensors().set(TEST_SENSOR, "first");
+ log.debug("set first");
+ }
+ });
+ Task<?> second = mgmt.getExecutionManager().submit(new Runnable() {
+ @Override
+ public void run() {
+ app.sensors().set(TEST_SENSOR, "second");
+ log.debug("set second");
+ }
+ });
+ first.blockUntilEnded();
+ second.blockUntilEnded();
+
+ Asserts.succeedsEventually(new Runnable() {
+ @Override
+ public void run() {
+ EntityAsserts.assertAttributeEquals(app, TEST_SENSOR,
lastSeenState.get());
+ }
+ });
+ }
+
+ public static class RecodingChildSensorEnricher extends
AbstractMultipleSensorAggregator<Void> {
+ public static AttributeSensor<String> RECORDED_SENSOR =
Sensors.newStringSensor("recorded.sensor");
+ List<String> seenValues = new ArrayList<>();
+
+ @Override
+ protected Collection<Sensor<?>> getSourceSensors() {
+ return ImmutableList.<Sensor<?>>of(RECORDED_SENSOR);
+ }
+
+ @Override
+ protected Object compute() {
+ throw new UnsupportedOperationException("Not expected to be
called since onUpdated is overriden");
+ }
+
+ @Override
+ protected void setEntityLoadingTargetConfig() {
+ }
+
+ @Override
+ protected void onUpdated() {
+ Iterator<String> values =
getValues(RECORDED_SENSOR).values().iterator();
+ if (values.hasNext()) {
+ seenValues.add(values.next());
+ }
+ }
+ }
+
+ /**
+ * Enricher sees state in the wrong order -> running, starting, running
+ *
+ * Indeterministic, fails a couple of times per 100 invocations when
run with "mvn test" in the
+ * brooklyn-itest docker container.
+ */
+ @Test(groups="Broken")
+ public void testWrongSensorInitValue() {
+ TestApplication app =
mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)
+ .impl(TestApplicationNoEnrichersImpl.class)
+
.enricher(EnricherSpec.create(RecodingChildSensorEnricher.class))
+ .child(EntitySpec.create(TestEntity.class)
+ .impl(TestEntityNoEnrichersImpl.class)));
+
+ Entity child = Iterables.get(app.getChildren(), 0);
+ child.sensors().set(RecodingChildSensorEnricher.RECORDED_SENSOR,
"first");
+ child.sensors().set(RecodingChildSensorEnricher.RECORDED_SENSOR,
"second");
+
+ final RecodingChildSensorEnricher enricher = getFirstEnricher(app,
RecodingChildSensorEnricher.class);
+
+ // setEntity -> onUpdate
+ // CHILD_ADDED -> onUpdate
+ // set RECORDED_SENSOR=first -> onUpdate
+ // set RECORDED_SENSOR=second -> onUpdate
+ Asserts.eventually(Suppliers.ofInstance(enricher.seenValues),
CollectionFunctionals.sizeEquals(4));
+
+ boolean isOrdered = Ordering.explicit(MutableList.of("first",
"second"))
+ .nullsFirst()
+ .isOrdered(enricher.seenValues);
+ assertTrue(isOrdered, "Unexpected ordering for " +
enricher.seenValues);
+ }
+
+ public static class EmittingEnricher extends AbstractEnricher {
+ @Override
+ public void setEntity(@SuppressWarnings("deprecation")
org.apache.brooklyn.api.entity.EntityLocal entity) {
+ super.setEntity(entity);
+ this.suppressDuplicates = true;
+ }
+
+ @Override
+ public <T> void emit(Sensor<T> sensor, Object val) {
+ super.emit(sensor, val);
+ }
+ }
+
+ /**
+ * The deduplication logic in AbstractEnricher does not work for
parallel invocations.
--- End diff --
javadoc no longer correct, now that you've fixed it. How about:
```
* The deduplication logic in AbstractEnricher previously did not work
for parallel invocations.
* It used to do a get and then a compare, so another thread could
change the value between
* those two operations.
```
---
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.
---