brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From drigod...@apache.org
Subject [1/4] brooklyn-server git commit: TestEffector: fix assertion timeouts
Date Mon, 10 Apr 2017 11:43:49 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master 61dfee320 -> b539b110e


TestEffector: fix assertion timeouts

Timeout applies to the effector call.
Assertion of the effector result done exactly once

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/23392d4e
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/23392d4e
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/23392d4e

Branch: refs/heads/master
Commit: 23392d4e43e85855668b935a049ff157852c0cc3
Parents: 1599d7a
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Apr 5 17:22:01 2017 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Apr 5 17:22:01 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/test/framework/TestEffector.java   | 18 +++++----
 .../test/framework/TestEffectorImpl.java        |  2 +-
 .../test/framework/TestFrameworkAssertions.java |  4 ++
 .../test/framework/TestEffectorTest.java        | 40 ++++++++++++++++++++
 .../test/framework/entity/TestEntity.java       | 29 ++++++++++----
 .../test/framework/entity/TestEntityImpl.java   |  7 ++++
 6 files changed, 85 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/23392d4e/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffector.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffector.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffector.java
index 1101741..5c996d6 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffector.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffector.java
@@ -18,15 +18,16 @@
  */
 package org.apache.brooklyn.test.framework;
 
-import com.google.common.collect.ImmutableMap;
-import com.google.common.reflect.TypeToken;
+import java.util.Map;
+
 import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
 
-import java.util.Map;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.reflect.TypeToken;
 
 /**
  * Entity that invokes an effector on another entity
@@ -37,9 +38,12 @@ public interface TestEffector extends BaseTest {
     @SetFromFlag(nullable = false)
     ConfigKey<String> EFFECTOR_NAME = ConfigKeys.newConfigKey(String.class, "effector",
"The name of the effector to invoke");
 
-    ConfigKey<Map<String, ?>> EFFECTOR_PARAMS = ConfigKeys.newConfigKey(new TypeToken<Map<String,
?>>() {
-    }, "params", "The parameters to pass to the effector", ImmutableMap.<String, Object>of());
+    @SuppressWarnings("serial")
+    ConfigKey<Map<String, ?>> EFFECTOR_PARAMS = ConfigKeys.newConfigKey(
+            new TypeToken<Map<String, ?>>() {}, 
+            "params", 
+            "The parameters to pass to the effector", 
+            ImmutableMap.<String, Object>of());
 
     AttributeSensorAndConfigKey<Object, Object> EFFECTOR_RESULT = ConfigKeys.newSensorAndConfigKey(Object.class,
"result", "The result of invoking the effector");
-
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/23392d4e/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffectorImpl.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffectorImpl.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffectorImpl.java
index 7afe134..8a08086 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffectorImpl.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestEffectorImpl.java
@@ -88,8 +88,8 @@ public class TestEffectorImpl extends TargetableTestComponentImpl implements
Tes
             if(assertions != null && !assertions.isEmpty()){
                 Supplier<?> supplier = Suppliers.ofInstance(effectorResult);
                 TestFrameworkAssertions.checkAssertionsEventually(new AssertionOptions(effectorName,
supplier)
+                        .maxAttempts(1)
                         .timeout(timeout)
-                        .backoffToPeriod(backoffToPeriod)
                         .assertions(assertions));
             }
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/23392d4e/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
index 9484ab5..ff9c1c5 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestFrameworkAssertions.java
@@ -84,6 +84,10 @@ public class TestFrameworkAssertions {
             this.flags.put("timeout", val);
             return this;
         }
+        public AssertionOptions maxAttempts(Integer val) {
+            this.flags.put("maxAttempts", val);
+            return this;
+        }
         public AssertionOptions backoffToPeriod(Duration val) {
             this.flags.put("backoffToPeriod", val);
             return this;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/23392d4e/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestEffectorTest.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestEffectorTest.java
b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestEffectorTest.java
index ad1b6da..022effd 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestEffectorTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestEffectorTest.java
@@ -21,6 +21,8 @@ package org.apache.brooklyn.test.framework;
 
 import static org.apache.brooklyn.core.entity.trait.Startable.SERVICE_UP;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
 
 import java.util.List;
 import java.util.Map;
@@ -44,6 +46,7 @@ import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Predicates;
+import com.google.common.base.Stopwatch;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
@@ -191,6 +194,43 @@ public class TestEffectorTest extends BrooklynAppUnitTestSupport {
     }
 
     @Test
+    public void testEffectorTimeoutAppliesOnlyToCallAndNotToAssertionCheck() throws Exception
{
+        String stringToReturn = "Goodbye World!";
+
+        Map<String, String> effectorParams = ImmutableMap.of("stringToReturn", stringToReturn);
+
+        List<Map<String, Object>> assertions = ImmutableList.<Map<String,
Object>>of(
+                ImmutableMap.<String, Object>of(TestFrameworkAssertions.EQUAL_TO, "Not
the string I expected")
+        );
+
+        testCase.addChild(EntitySpec.create(TestEffector.class)
+                .configure(TestEffector.TIMEOUT, Duration.minutes(1))
+                .configure(TestEffector.TARGET_ENTITY, testEntity)
+                .configure(TestEffector.EFFECTOR_NAME, "effectorReturnsString")
+                .configure(TestEffector.EFFECTOR_PARAMS, effectorParams)
+                .configure(TestEffector.ASSERTIONS, assertions));
+
+        Stopwatch stopwatch = Stopwatch.createStarted();
+
+        assertStartFails(app, AssertionError.class, Asserts.DEFAULT_LONG_TIMEOUT);
+        
+        Duration duration = Duration.of(stopwatch);
+        assertTrue(duration.isShorterThan(Asserts.DEFAULT_LONG_TIMEOUT), "duration="+duration);
+    }
+
+    @Test
+    public void testEffectorFailureNotRetried() throws Exception {
+        testCase.addChild(EntitySpec.create(TestEffector.class)
+                .configure(TestEffector.TIMEOUT, Duration.minutes(1))
+                .configure(TestEffector.TARGET_ENTITY, testEntity)
+                .configure(TestEffector.EFFECTOR_NAME, "effectorFails"));
+
+        assertStartFails(app, TestEntity.EffectorFailureException.class, Asserts.DEFAULT_LONG_TIMEOUT);
+        
+        assertEquals(testEntity.sensors().get(TestEntity.FAILING_EFFECTOR_INVOCATION_COUNT),
Integer.valueOf(1));
+    }
+
+    @Test
     public void testFailFastIfNoTargetEntity() throws Exception {
         testCase.addChild(EntitySpec.create(TestEffector.class)
                 .configure(TestEffector.EFFECTOR_NAME, "simpleEffector"));

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/23392d4e/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntity.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntity.java
b/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntity.java
index 83b0052..88bed45 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntity.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntity.java
@@ -21,20 +21,21 @@ package org.apache.brooklyn.test.framework.entity;
 
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.ImplementedBy;
+import org.apache.brooklyn.api.sensor.AttributeSensor;
 import org.apache.brooklyn.core.annotation.Effector;
 import org.apache.brooklyn.core.annotation.EffectorParam;
-import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.core.entity.trait.Startable;
-import org.apache.brooklyn.core.sensor.AttributeSensorAndConfigKey;
+import org.apache.brooklyn.core.sensor.Sensors;
 
 @ImplementedBy(TestEntityImpl.class)
 public interface TestEntity extends Entity, Startable {
 
-    AttributeSensorAndConfigKey<Boolean, Boolean> SIMPLE_EFFECTOR_INVOKED = ConfigKeys.newSensorAndConfigKey(Boolean.class,
"simple-effector-invoked", "");
-    AttributeSensorAndConfigKey<Boolean, Boolean> COMPLEX_EFFECTOR_INVOKED = ConfigKeys.newSensorAndConfigKey(Boolean.class,
"complex-effector-invoked", "");
-    AttributeSensorAndConfigKey<String, String> COMPLEX_EFFECTOR_STRING = ConfigKeys.newSensorAndConfigKey(String.class,
"complex-effector-string", "");
-    AttributeSensorAndConfigKey<Boolean, Boolean> COMPLEX_EFFECTOR_BOOLEAN = ConfigKeys.newSensorAndConfigKey(Boolean.class,
"complex-effector-boolean", "");
-    AttributeSensorAndConfigKey<Long, Long> COMPLEX_EFFECTOR_LONG = ConfigKeys.newSensorAndConfigKey(Long.class,
"complex-effector-long", "");
+    AttributeSensor<Boolean> SIMPLE_EFFECTOR_INVOKED = Sensors.newBooleanSensor("simple-effector-invoked");
+    AttributeSensor<Boolean> COMPLEX_EFFECTOR_INVOKED = Sensors.newBooleanSensor("complex-effector-invoked");
+    AttributeSensor<String> COMPLEX_EFFECTOR_STRING = Sensors.newStringSensor("complex-effector-string");
+    AttributeSensor<Boolean> COMPLEX_EFFECTOR_BOOLEAN = Sensors.newBooleanSensor("complex-effector-boolean");
+    AttributeSensor<Long> COMPLEX_EFFECTOR_LONG = Sensors.newLongSensor("complex-effector-long");
+    AttributeSensor<Integer> FAILING_EFFECTOR_INVOCATION_COUNT = Sensors.newIntegerSensor("failing-effector-count");
 
     @Effector
     void simpleEffector();
@@ -53,6 +54,20 @@ public interface TestEntity extends Entity, Startable {
     @Effector
     void effectorHangs();
 
+    @Effector
+    void effectorFails() throws EffectorFailureException;
+    
+    class EffectorFailureException extends Exception {
+        private static final long serialVersionUID = -8996475930661355402L;
+        
+        public EffectorFailureException(String msg) {
+            super(msg);
+        }
+        public EffectorFailureException(String msg, Throwable cause) {
+            super(msg, cause);
+        }
+    }
+    
     class TestPojo {
         private final String stringValue;
         private final Boolean booleanValue;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/23392d4e/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntityImpl.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntityImpl.java
b/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntityImpl.java
index a6c41a4..439c0ce 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntityImpl.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/entity/TestEntityImpl.java
@@ -68,4 +68,11 @@ public class TestEntityImpl extends AbstractEntity implements TestEntity
{
     public void effectorHangs() {
         Time.sleep(Duration.minutes(5));
     }
+    
+    @Override
+    public void effectorFails() throws EffectorFailureException {
+        Integer count = sensors().get(FAILING_EFFECTOR_INVOCATION_COUNT);
+        sensors().set(FAILING_EFFECTOR_INVOCATION_COUNT, (count == null ? 0 : count) + 1);
+        throw new EffectorFailureException("Simulating effector failure");
+    }
 }


Mime
View raw message