/** * Asserts that the entity's value for this attribute changes, by registering a subscription and checking the value. * * @param entity The entity whose attribute will be checked. * @param attribute The attribute to check on the entity. * * @throws AssertionError if the assertion fails. */ public static void assertAttributeChangesEventually(final Entity entity, final AttributeSensor<?> attribute) { final Object origValue = entity.getAttribute(attribute); final AtomicBoolean changed = new AtomicBoolean(); SubscriptionHandle handle = entity.subscriptions().subscribe(entity, attribute, new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { if (!Objects.equal(origValue, event.getValue())) { changed.set(true); } }}); try { Asserts.succeedsEventually(new Runnable() { @Override public void run() { Asserts.assertTrue(changed.get(), entity + " -> " + attribute + " not changed from "+origValue); }}); } finally { entity.subscriptions().unsubscribe(entity, handle); } }
protected <T> SubscriptionHandle recordEvents(Entity entity, AttributeSensor<T> sensor, final List<SensorEvent<T>> events) { SensorEventListener<T> listener = new SensorEventListener<T>() { @Override public void onEvent(SensorEvent<T> event) { log.info("onEvent: {}", event); events.add(event); } }; return entity.subscriptions().subscribe(entity, sensor, listener); }
protected static RecordingSensorEventListener<Object> subscribeToHaSensors(Entity entity) { RecordingSensorEventListener<Object> listener = new RecordingSensorEventListener<>(); entity.subscriptions().subscribe(entity, HASensors.ENTITY_RECOVERED, listener); entity.subscriptions().subscribe(entity, HASensors.ENTITY_FAILED, listener); return listener; } }
subscription = entity.subscriptions().subscribe(source, sensor, new SensorEventListener<T>() { @Override public void onEvent(SensorEvent<T> event) { synchronized (publishedValues) { publishedValues.add(event.getValue()); } }}); for (final AttributeAndSensorCondition abortCondition : abortSensorConditions) { abortSubscriptions.add(entity.subscriptions().subscribe(abortCondition.source, abortCondition.sensor, new SensorEventListener<Object>() { @Override public void onEvent(SensorEvent<Object> event) { if (abortCondition.predicate.apply(event.getValue())) { } finally { if (subscription != null) { entity.subscriptions().unsubscribe(subscription); entity.subscriptions().unsubscribe(handle);
@Test(groups="Integration") @SuppressWarnings("deprecation") public void testHttpSensorSuppressingDuplicates() throws Exception { RecordingSensorEventListener<String> listener = new RecordingSensorEventListener<>(); entity.subscriptions().subscribe(entity, SENSOR_STRING, listener); HttpRequestSensor<Integer> sensor = new HttpRequestSensor<Integer>(ConfigBag.newInstance() .configure(HttpRequestSensor.SUPPRESS_DUPLICATES, true) .configure(HttpRequestSensor.SENSOR_PERIOD, Duration.millis(1)) .configure(HttpRequestSensor.SENSOR_NAME, SENSOR_STRING.getName()) .configure(HttpRequestSensor.SENSOR_TYPE, STRING_TARGET_TYPE) .configure(HttpRequestSensor.JSON_PATH, "$.myKey") .configure(HttpRequestSensor.SENSOR_URI, serverUrl + "/myKey/myValue")); sensor.apply((org.apache.brooklyn.api.entity.EntityLocal)entity); entity.sensors().set(Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_STRING, "myValue"); listener.assertHasEventEventually(Predicates.alwaysTrue()); Asserts.succeedsContinually(new Runnable() { @Override public void run() { listener.assertEventCount(1); }}); }