public static <T> void assertAttributeEqualsContinually(final Entity entity, final AttributeSensor<T> attribute, final T expected) { assertAttributeEqualsContinually(ImmutableMap.of(), entity, attribute, expected); }
@Override public void run() { EntityAsserts.assertAttributeEqualsContinually(entity, TestEntity.NAME, myName); } });
private void assertHealthContinually(Entity entity, Lifecycle expectedState, Boolean expectedUp) { // short wait, so unit tests don't take ages Map<String, ?> flags = ImmutableMap.of("timeout", ValueResolver.REAL_QUICK_WAIT); EntityAsserts.assertAttributeEqualsContinually(flags, entity, Attributes.SERVICE_STATE_ACTUAL, expectedState); EntityAsserts.assertAttributeEqualsContinually(flags, entity, Attributes.SERVICE_UP, expectedUp); }
@Test public void shouldAssertAttributeEqualsContinually() { final String myName = "myname"; entity.sensors().set(TestEntity.NAME, myName); EntityAsserts.assertAttributeEqualsContinually( ImmutableMap.of("timeout", "100ms"), entity, TestEntity.NAME, myName); }
private static <T> void assertAttributeEqualsContinually(Entity x, AttributeSensor<T> sensor, T value) { try { EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", Duration.millis(25)), x, sensor, value); } catch (Throwable e) { log.warn("Expected "+x+" continually to have "+sensor+" = "+value+"; instead:"); Entities.dumpInfo(x); throw Exceptions.propagate(e); } } private static <T> void assertAttributeEquals(Entity x, AttributeSensor<T> sensor, T value) {
@Test public void testNoWinrm() throws Exception { Entity app = createAndStartApplication( "location: byon:(hosts=\"1.2.3.4\",osFamily=windows)", "services:", "- type: "+EmptyWindowsProcess.class.getName(), " brooklyn.config:", " winrmMonitoring.enabled: false", " onbox.base.dir.skipResolution: true"); waitForApplicationTasks(app); EmptyWindowsProcess entity = Iterables.getOnlyElement(Entities.descendantsAndSelf(app, EmptyWindowsProcess.class)); EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_UP, true); EntityAsserts.assertAttributeEqualsContinually(entity, Attributes.SERVICE_UP, true); Iterables.find(entity.getLocations(), Predicates.instanceOf(WinRmMachineLocation.class)); } }
@Test(dataProvider = "specProvider", groups={"Broken"}) public void testFeedContinuesWhenPollerThrows(EntitySpec<FeedExceptionEntity> spec) { Map<?, ?> timeoutFlags = ImmutableMap.of("timeout", "100ms"); FeedExceptionEntity fee = app.createAndManageChild(spec); app.start(ImmutableList.of(app.newSimulatedLocation())); EntityAsserts.assertAttributeEqualsEventually(timeoutFlags, fee, FeedExceptionEntity.FLAG, true); fee.startThrowingPollExceptions(); EntityAsserts.assertAttributeEqualsEventually(timeoutFlags, fee, FeedExceptionEntity.FLAG, false); EntityAsserts.assertAttributeEqualsContinually(timeoutFlags, fee, FeedExceptionEntity.FLAG, false); fee.stopThrowingPollExceptions(); EntityAsserts.assertAttributeEqualsEventually(timeoutFlags, fee, FeedExceptionEntity.FLAG, true); EntityAsserts.assertAttributeEqualsContinually(timeoutFlags, fee, FeedExceptionEntity.FLAG, true); }
@Test public void testIgnoresWhenNoSubnetAddress() throws Exception { entity.sensors().set(Attributes.HTTP_PORT, 1234); entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class) .configure(OnPublicNetworkEnricher.SENSORS, ImmutableList.of(Attributes.HTTP_PORT))); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT), entity, Sensors.newStringSensor(Attributes.HTTP_PORT.getName()+".mapped.subnet"), null); }
@Test(dataProvider = "invalidVariants") public <T> void testIgnoresWhenInvalidAttribute(AttributeSensor<T> sensor, T sensorVal) throws Exception { entity.sensors().set(Attributes.SUBNET_ADDRESS, "127.0.0.1"); entity.sensors().set(sensor, sensorVal); entity.addLocations(ImmutableList.of(machine)); entity.enrichers().add(EnricherSpec.create(OnSubnetNetworkEnricher.class) .configure(OnSubnetNetworkEnricher.SENSORS, ImmutableList.of(sensor))); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", VERY_SHORT_WAIT), entity, Sensors.newStringSensor(sensor.getName()+".mapped.subnet"), null); }
@Test public void testCanDisableFilterForRunningEntities() throws Exception { geoDns.config().set(AbstractGeoDnsService.FILTER_FOR_RUNNING, false); app.start(ImmutableList.of(westChildWithLocation, eastChildWithLocationAndWithPrivateHostname)); publishSensors(2, true, true, true); assertAttributeEventually(geoDns, AbstractGeoDnsService.TARGETS, CollectionFunctionals.<String>mapSizeEquals(2)); final Map<String, String> targets = ImmutableMap.copyOf(geoDns.sensors().get(AbstractGeoDnsService.TARGETS)); TestEntity problemChild = Iterables.get(Entities.descendantsAndSelf(app, TestEntity.class), 0); problemChild.sensors().set(Attributes.SERVICE_STATE_ACTUAL, Lifecycle.ON_FIRE); assertAttributeEqualsContinually(geoDns, AbstractGeoDnsService.TARGETS, targets); }
@Test public void testSummingEnricherWhenNoSensorValuesYetDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testSummingEnricherWithNoValues() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testTransformerAvoidsInfiniteLoopWhereSourceAndTargetSame() throws Exception { producer.enrichers().add(EnricherSpec.create(Transformer.class) .configure(Transformer.SOURCE_SENSOR, intSensorA) .configure(Transformer.TARGET_SENSOR, intSensorA) .configure(Transformer.PRODUCER, producer) .configure(Transformer.TRANSFORMATION_FROM_VALUE, MathFunctions.times(2))); // TODO doesn't match strongly typed int->long // Short wait; expect us to never re-publish the source-sensor as that would cause infinite loop. producer.sensors().set(intSensorA, 1); EntityAsserts.assertAttributeEqualsContinually(ImmutableMap.of("timeout", SHORT_WAIT), producer, intSensorA, 1); }
@Test public void testAveragingEnricherWhenNoAndNullSensorValues() { TestEntity producer1 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromHardcodedProducers(ImmutableList.of(producer1)) .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); producer1.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); }
@Test public void testSummingEnricherWhenNullSensorValue() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromHardcodedProducers(ImmutableList.of(entity)) .build()); entity.sensors().set(intSensor, null); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testAveragingEnricherWhenNoProducersDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(doubleSensor) .computingAverage() .fromChildren() .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, doubleSensor, null); }
@Test public void testSummingEnricherWithNoProducersDefaultsToNull() { entity.enrichers().add(Enrichers.builder() .aggregating(intSensor) .publishing(target) .computingSum() .fromChildren() .build()); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 50), entity, target, null); }
@Test public void testReducingBuilderWithConcatenator() { entity.enrichers().add(Enrichers.builder() .reducing(Reducer.class, ImmutableList.<AttributeSensor<?>>of(STR1, STR2)) .from(entity) .computing(new Concatenator()) .publishing(STR3) .build() ); EntityAsserts.assertAttributeEquals(entity, STR3, null); entity.sensors().set(STR1, "foo"); EntityAsserts.assertAttributeEqualsContinually(entity, STR3, null); entity.sensors().set(STR2, "bar"); EntityAsserts.assertAttributeEqualsEventually(entity, STR3, "foobar"); }
@Test public void testPropagatesSpecificSensor() { app.enrichers().add(Enrichers.builder() .propagating(TestEntity.NAME) .from(entity) .build()); // name propagated entity.sensors().set(TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); // sequence not propagated entity.sensors().set(TestEntity.SEQUENCE, 2); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); }
@Test public void testPropagatesAllBut() { app.enrichers().add(Enrichers.builder() .propagatingAllBut(TestEntity.SEQUENCE) .from(entity) .build()); // name propagated entity.sensors().set(TestEntity.NAME, "foo"); EntityAsserts.assertAttributeEqualsEventually(app, TestEntity.NAME, "foo"); // sequence not propagated entity.sensors().set(TestEntity.SEQUENCE, 2); EntityAsserts.assertAttributeEqualsContinually(MutableMap.of("timeout", 100), app, TestEntity.SEQUENCE, null); }