brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/5] brooklyn-server git commit: TestHttpCall: support “maxAttempts” config
Date Fri, 10 Mar 2017 13:51:14 GMT
TestHttpCall: support “maxAttempts” config

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

Branch: refs/heads/master
Commit: 59624f52cbb2fc89cf5fcbba400a221db72c8028
Parents: b2fd0e8
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Mar 8 20:49:19 2017 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Mar 8 23:45:40 2017 +0000

----------------------------------------------------------------------
 .../brooklyn/test/framework/TestHttpCall.java   |  6 +++++
 .../test/framework/TestHttpCallImpl.java        |  7 +++---
 .../test/framework/TestHttpCallTest.java        | 26 ++++++++++++++++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/59624f52/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
index 31dac5d..3e4867b 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java
@@ -24,6 +24,7 @@ import org.apache.brooklyn.api.entity.ImplementedBy;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.config.ConfigKeys;
 import org.apache.brooklyn.util.core.flags.SetFromFlag;
+import org.apache.brooklyn.util.time.Duration;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpHead;
@@ -63,6 +64,11 @@ public interface TestHttpCall extends BaseTest {
     ConfigKey<HttpAssertionTarget> ASSERTION_TARGET = ConfigKeys.newConfigKey(HttpAssertionTarget.class,
"applyAssertionTo",
         "The HTTP field to apply the assertion to [body,status]", HttpAssertionTarget.body);
 
+    /**
+     * The duration to wait for an assertion to succeed or fail before throwing an exception.
+     */
+    ConfigKey<Integer> MAX_ATTEMPTS = ConfigKeys.newIntegerConfigKey("maxAttempts",
"Maximum number of attempts");
+
     enum HttpMethod {
         GET(HttpGet.class),
         POST(HttpPost.class),

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/59624f52/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
----------------------------------------------------------------------
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
index 9f51c0e..69bed81 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java
@@ -29,6 +29,7 @@ import org.apache.brooklyn.api.location.Location;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
 import org.apache.brooklyn.test.framework.TestFrameworkAssertions.AssertionOptions;
+import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.http.HttpTool;
@@ -39,7 +40,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Supplier;
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Lists;
 
 /**
@@ -65,6 +65,7 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
             final String body = config().get(TARGET_BODY);
             final List<Map<String, Object>> assertions = getAssertions(this,
ASSERTIONS);
             final Duration timeout = getConfig(TIMEOUT);
+            final Integer maxAttempts = getConfig(MAX_ATTEMPTS);
             final Duration backoffToPeriod = getConfig(BACKOFF_TO_PERIOD);
             final HttpAssertionTarget target = getRequiredConfig(ASSERTION_TARGET);
             final boolean trustAll = getRequiredConfig(TRUST_ALL);
@@ -72,7 +73,7 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
                 throw new RuntimeException(String.format("The entity [%s] cannot have child
entities", getClass().getName()));
             }
             
-            doRequestAndCheckAssertions(ImmutableMap.of("timeout", timeout, "backoffToPeriod",
backoffToPeriod), 
+            doRequestAndCheckAssertions(MutableMap.of("timeout", timeout, "backoffToPeriod",
backoffToPeriod, "maxAttempts", maxAttempts), 
                     assertions, target, method, url, headers, trustAll, body);
             setUpAndRunState(true, Lifecycle.RUNNING);
 
@@ -87,7 +88,7 @@ public class TestHttpCallImpl extends TargetableTestComponentImpl implements
Tes
         }
     }
 
-    private void doRequestAndCheckAssertions(Map<String, Duration> flags, List<Map<String,
Object>> assertions,
+    private void doRequestAndCheckAssertions(Map<String, ?> flags, List<Map<String,
Object>> assertions,
                                              HttpAssertionTarget target, final HttpMethod
method, final String url, final Map<String, String> headers, final boolean trustAll,
final String body) {
         switch (target) {
             case body:

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/59624f52/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
----------------------------------------------------------------------
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
index 99638a4..9c58e4b 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestHttpCallTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.test.framework;
 
+import static org.testng.Assert.assertTrue;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
@@ -28,8 +30,10 @@ import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.LocationSpec;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation;
+import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.test.http.TestHttpRequestHandler;
 import org.apache.brooklyn.test.http.TestHttpServer;
+import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Identifiers;
 import org.apache.brooklyn.util.time.Duration;
 import org.apache.http.HttpException;
@@ -41,6 +45,7 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Stopwatch;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 
@@ -172,6 +177,27 @@ public class TestHttpCallTest extends BrooklynAppUnitTestSupport {
         app.start(ImmutableList.of(loc));
     }
 
+    @Test(groups = "Integration")
+    public void testMaxAttempts() {
+        app.addChild(EntitySpec.create(TestHttpCall.class)
+                .configure(TestHttpCall.TARGET_URL, server.getUrl() + "/201")
+                .configure(TestHttpCall.TIMEOUT, Duration.minutes(1))
+                .configure(TestHttpCall.MAX_ATTEMPTS, 1)
+                .configure(TestSensor.ASSERTIONS, newAssertion("isEqualTo", "Wrong")));
+        Stopwatch stopwatch = Stopwatch.createStarted();
+        try {
+            app.start(ImmutableList.of(loc));
+            Asserts.shouldHaveFailedPreviously();
+        } catch (Exception e) {
+            AssertionError ae = Exceptions.getFirstThrowableOfType(e, AssertionError.class);
+            if (ae == null || !ae.toString().contains("body expected isEqualTo Wrong")) {
+                throw e;
+            }
+        }
+        Duration duration = Duration.of(stopwatch);
+        assertTrue(duration.isShorterThan(Asserts.DEFAULT_LONG_TIMEOUT), "duration="+duration);
+    }
+
     private List<Map<String, Object>> newAssertion(final String assertionKey,
final Object assertionValue) {
         final List<Map<String, Object>> result = new ArrayList<>();
         result.add(ImmutableMap.of(assertionKey, assertionValue));


Mime
View raw message