Test Transformer reliability when resolving DSL
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/670f83d1 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/670f83d1 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/670f83d1 Branch: refs/heads/master Commit: 670f83d19ce90ab94c5446506ebeb81c23cd2a56 Parents: 05622be Author: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com> Authored: Sun Oct 2 15:19:46 2016 +0200 Committer: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com> Committed: Tue Oct 11 09:20:19 2016 +0300 ---------------------------------------------------------------------- .../TransformerEnricherWithDslTest.java | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/670f83d1/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/enricher/TransformerEnricherWithDslTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/enricher/TransformerEnricherWithDslTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/enricher/TransformerEnricherWithDslTest.java new file mode 100644 index 0000000..2f4465a --- /dev/null +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/enricher/TransformerEnricherWithDslTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.brooklyn.camp.brooklyn.enricher; + +import static org.testng.Assert.assertEquals; + +import org.apache.brooklyn.api.sensor.AttributeSensor; +import org.apache.brooklyn.api.sensor.EnricherSpec; +import org.apache.brooklyn.camp.brooklyn.spi.dsl.methods.BrooklynDslCommon; +import org.apache.brooklyn.core.entity.EntityAsserts; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.enricher.stock.Transformer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class TransformerEnricherWithDslTest extends BrooklynAppUnitTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(TransformerEnricherWithDslTest.class); + + int START_PORT = 10000; + + @Test(groups="Broken") + // See https://issues.apache.org/jira/browse/BROOKLYN-356 + public void testTransformerResolvesResolvableValues() { + testTransformerResolvesResolvableValues(START_PORT, 200); + } + + @Test(groups={"Integration", "Broken"}, invocationCount=10) + // See https://issues.apache.org/jira/browse/BROOKLYN-356 + public void testTransformerResolvesResolvableValuesIntegration() { + LOG.info("Starting 1000 iterations"); + testTransformerResolvesResolvableValues(START_PORT, 1000); + } + + private void testTransformerResolvesResolvableValues(int portStart, int portCount) { + // Note: The test gets progressively slower with iterations, probably due to the GC triggering much more frequently. + // There's no memory leak, but doesn't seem right to be putting so much pressure on the GC with such a simple test. + AttributeSensor<Integer> sourceSensor = Sensors.newIntegerSensor("port"); + AttributeSensor<String> targetSensor = Sensors.newStringSensor("port.transformed"); + app.enrichers().add(EnricherSpec.create(Transformer.class) + .configure(Transformer.SOURCE_SENSOR, sourceSensor) + .configure(Transformer.TARGET_SENSOR, targetSensor) + .configure(Transformer.TARGET_VALUE, + // Can only use the inner-most sensor, but including the + // wrapping formatStrings amplifies the resolving effort, making + // a bug more probable to manifest. + BrooklynDslCommon.formatString("%s", + BrooklynDslCommon.formatString("%d", + BrooklynDslCommon.attributeWhenReady("port"))))); + + int failures = 0; + for (int port = portStart; port < portStart + portCount; port++) { + app.sensors().set(sourceSensor, port); + try { + EntityAsserts.assertAttributeEqualsEventually(app, targetSensor, Integer.toString(port)); + } catch (Exception e) { + failures++; + LOG.warn("Assertion failed, port=" + port + ", transformed sensor is " + app.sensors().get(targetSensor), e); + } + } + + assertEquals(failures, 0, failures + " assertion failures while transforming sensor; see logs for detailed errors"); + } + +}