You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2020/03/20 14:23:09 UTC

[sling-org-apache-sling-scripting-bundle-tracker-it] branch master updated: made tests more resilient

This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-bundle-tracker-it.git


The following commit(s) were added to refs/heads/master by this push:
     new 718abb2  made tests more resilient
718abb2 is described below

commit 718abb2b1249d7c72677f14ed6b6df3c40715881
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Fri Mar 20 15:22:59 2020 +0100

    made tests more resilient
---
 it/pom.xml                                         |   8 +-
 .../junit/teleporter/customizers/ITCustomizer.java |   2 +-
 .../tracker/it/AbstractEndpointTestBase.java       |  21 +++-
 .../bundle/tracker/it/BundledScriptTrackerIT.java  |   5 +
 .../bundle/tracker/it/LaunchpadReadyRule.java      | 121 +++++++++++++++++++++
 .../scripting/bundle/tracker/it/ScriptMatchIT.java |   6 +-
 6 files changed, 150 insertions(+), 13 deletions(-)

diff --git a/it/pom.xml b/it/pom.xml
index fb5030c..1b3227c 100644
--- a/it/pom.xml
+++ b/it/pom.xml
@@ -92,6 +92,8 @@
                 </executions>
                 <configuration>
                     <systemPropertyVariables>
+                        <launchpad.http.server.host>${http.host}</launchpad.http.server.host>
+                        <launchpad.http.server.port>${http.port}</launchpad.http.server.port>
                         <launchpad.http.server.url>http://${http.host}:${http.port}/</launchpad.http.server.url>
                     </systemPropertyVariables>
                 </configuration>
@@ -138,7 +140,7 @@
                         <server>
                             <id>bundle-tracker-it-instance</id>
                             <port>${http.port}</port>
-                            <vmOpts>${sling.vm.options} ${jacoco.agent}</vmOpts>
+                            <vmOpts>${sling.vm.options} ${jacoco.agent} -Dlaunchpad.http.server.port=${http.port}</vmOpts>
                         </server>
                     </servers>
                 </configuration>
@@ -261,13 +263,13 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.junit.teleporter</artifactId>
-            <version>1.0.16</version>
+            <version>1.0.18</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.clients</artifactId>
-            <version>1.1.12</version>
+            <version>1.2.6</version>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/it/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java b/it/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java
index e14e36f..c45abe3 100644
--- a/it/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java
+++ b/it/src/test/java/org/apache/sling/junit/teleporter/customizers/ITCustomizer.java
@@ -28,7 +28,7 @@ public class ITCustomizer implements TeleporterRule.Customizer {
         final ClientSideTeleporter cst = (ClientSideTeleporter)t;
         cst.setBaseUrl(System.getProperty(BASE_URL_PROP, BASE_URL_PROP + "_IS_NOT_SET"));
         cst.setServerCredentials("admin", "admin");
-        cst.includeDependencyPrefix("org.apache.sling.scripting.resolver.internal");
+        cst.includeDependencyPrefix("org.apache.sling.scripting.bundle.tracker.it");
         cst.includeDependencyPrefix("org.apache.sling.testing");
         cst.setTestReadyTimeoutSeconds(20);
     }
diff --git a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/AbstractEndpointTestBase.java b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/AbstractEndpointTestBase.java
index 33092d0..852c205 100644
--- a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/AbstractEndpointTestBase.java
+++ b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/AbstractEndpointTestBase.java
@@ -22,7 +22,10 @@ import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.http.HttpResponse;
@@ -60,7 +63,12 @@ public abstract class AbstractEndpointTestBase {
     @ClassRule
     public static final SlingInstanceRule SLING_INSTANCE_RULE = new SlingInstanceRule();
 
+    @ClassRule
+    public static final LaunchpadReadyRule LAUNCHPAD_READY_RULE = new LaunchpadReadyRule(Integer.getInteger("launchpad.http.server.port",
+            8080));
+
     private Map<String, Document> documentMap = new ConcurrentHashMap<>();
+    private Set<String> resourceAlreadyPresent = Collections.synchronizedSet(new HashSet<>());
 
     @BeforeClass
     public static void setUp() {
@@ -90,7 +98,7 @@ public abstract class AbstractEndpointTestBase {
         URI uri = uriBuilder.build();
         Document document = documentMap.get(httpMethod + ":" + uri.toString());
         if (document == null) {
-            HttpResponse response = getResponse(httpMethod, url, 200);
+            HttpResponse response = getResponse(httpMethod, url);
             document = Jsoup.parse(response.getEntity().getContent(), StandardCharsets.UTF_8.name(),
                     System.getProperty(ITCustomizer.BASE_URL_PROP, ITCustomizer.BASE_URL_PROP +
                             "_IS_NOT_SET"));
@@ -99,15 +107,16 @@ public abstract class AbstractEndpointTestBase {
         return document;
     }
 
-    protected HttpResponse getResponse(String method, String url, int statusCode, NameValuePair... parameters) throws Exception {
+    protected HttpResponse getResponse(String method, String url, NameValuePair... parameters) throws Exception {
         String resourcePath = url.substring(0, url.indexOf('.'));
-        SLING_INSTANCE_RULE.getAdminClient().waitExists(resourcePath, contentFindTimeout, contentFindRetryDelay);
+        if (!resourceAlreadyPresent.contains(resourcePath)) {
+            SLING_INSTANCE_RULE.getAdminClient().waitExists(resourcePath, contentFindTimeout, contentFindRetryDelay);
+            resourceAlreadyPresent.add(resourcePath);
+        }
         HttpUriRequest request = prepareRequest(method, url, parameters);
         HttpResponse response = httpClient.execute(request);
         Assert.assertNotNull(response);
-        Assert.assertEquals("URL " + url + " did not return a " + statusCode + " status code.", statusCode,
-                response.getStatusLine().getStatusCode
-                        ());
+        Assert.assertEquals("URL " + url + " did not return a 200 status code.", 200, response.getStatusLine().getStatusCode());
         return response;
     }
 
diff --git a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/BundledScriptTrackerIT.java b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/BundledScriptTrackerIT.java
index 99756f9..40d5a2e 100644
--- a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/BundledScriptTrackerIT.java
+++ b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/BundledScriptTrackerIT.java
@@ -20,11 +20,16 @@ import javax.servlet.Servlet;
 
 import org.apache.sling.junit.rules.TeleporterRule;
 import org.apache.sling.testing.clients.util.poller.Polling;
+import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 
 public class BundledScriptTrackerIT {
 
+    @ClassRule
+    public static final LaunchpadReadyRule LAUNCHPAD_READY_RULE = new LaunchpadReadyRule(Integer.getInteger("launchpad.http.server.port",
+            8080));
+
     @Rule
     public TeleporterRule teleporter = TeleporterRule.forClass(getClass(), "IT");
 
diff --git a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/LaunchpadReadyRule.java b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/LaunchpadReadyRule.java
new file mode 100644
index 0000000..3473d7e
--- /dev/null
+++ b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/LaunchpadReadyRule.java
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.sling.scripting.bundle.tracker.it;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.ConnectException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.junit.rules.ExternalResource;
+
+public class LaunchpadReadyRule extends ExternalResource {
+
+    private static final int TRIES = 60;
+    private static final int WAIT_BETWEEN_TRIES_MILLIS = 1000;
+
+    private final List<Check> checks = new ArrayList<>();
+
+    public LaunchpadReadyRule(int launchpadPort) {
+
+        checks.add(new Check("http://localhost:" + launchpadPort + "/server/default/jcr:root/content"));
+        checks.add(new Check("http://localhost:" + launchpadPort + "/content/starter.html") {
+            @Override
+            public String runCheck(HttpResponse response) throws Exception {
+                try (InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
+                        BufferedReader reader = new BufferedReader(isr)) {
+
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        if (line.contains("Do not remove this comment, used for Starter integration tests")) {
+                            return null;
+                        }
+                    }
+                }
+
+                return "Did not find 'ready' marker in the response body";
+            }
+        });
+    }
+
+    @Override
+    protected void before() throws Throwable {
+
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            for (Check check : checks) {
+                runCheck(client, check);
+            }
+        }
+    }
+
+    private void runCheck(CloseableHttpClient client, Check check) throws Exception {
+
+        String lastFailure = null;
+        HttpGet get = new HttpGet(check.getUrl());
+        
+        for (int i = 0; i < TRIES; i++) {
+            try (CloseableHttpResponse response = client.execute(get)) {
+
+                if (response.getStatusLine().getStatusCode() != 200) {
+                    lastFailure = "Status code is " + response.getStatusLine();
+                    Thread.sleep(WAIT_BETWEEN_TRIES_MILLIS);
+                    continue;
+                }
+
+                lastFailure = check.runCheck(response);
+                if (lastFailure == null) {
+                    return;
+                }
+            } catch ( ConnectException e ) {
+                lastFailure = e.getClass().getName() + " : " + e.getMessage();
+            }
+
+            Thread.sleep(WAIT_BETWEEN_TRIES_MILLIS);
+        }
+        
+        throw new RuntimeException(String.format("Launchpad not ready. Failed check for URL %s with message '%s'",
+                check.getUrl(), lastFailure));
+    }
+
+    static class Check {
+        private String url;
+
+        public Check(String url) {
+            this.url = url;
+        }
+
+        public String getUrl() {
+            return url;
+        }
+
+        /**
+         * @param response the HttpResponse
+         * @return null if check check was successful, an error description otherwise
+         * @throws Exception
+         */
+        public String runCheck(HttpResponse response) throws Exception {
+            return null;
+        }
+    }
+
+}
diff --git a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/ScriptMatchIT.java b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/ScriptMatchIT.java
index 863ed2a..43e9a0a 100644
--- a/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/ScriptMatchIT.java
+++ b/it/src/test/java/org/apache/sling/scripting/bundle/tracker/it/ScriptMatchIT.java
@@ -49,7 +49,7 @@ public class ScriptMatchIT extends AbstractEndpointTestBase {
 
     @Test
     public void testHEADMethodMatching() throws Exception {
-        HttpResponse response = getResponse(HttpHead.METHOD_NAME, "/content/srr/examples/script-matching.html", 200);
+        HttpResponse response = getResponse(HttpHead.METHOD_NAME, "/content/srr/examples/script-matching.html");
         Header[] header = response.getHeaders("X-Script-Name");
         assertEquals("Expected to find one X-Script-Name header.", 1, header.length);
         assertEquals("/javax.script/org.apache.sling.scripting.examplebundle.scriptmatching/1.0.0/HEAD.html", header[0].getValue());
@@ -57,7 +57,7 @@ public class ScriptMatchIT extends AbstractEndpointTestBase {
 
     @Test
     public void testHEADMethodSelectorMatching() throws Exception {
-        HttpResponse response = getResponse(HttpHead.METHOD_NAME, "/content/srr/examples/script-matching.selector-1.html", 200);
+        HttpResponse response = getResponse(HttpHead.METHOD_NAME, "/content/srr/examples/script-matching.selector-1.html");
         Header[] header = response.getHeaders("X-Script-Name");
         assertEquals("Expected to find one X-Script-Name header.", 1, header.length);
         assertEquals("/javax.script/org.apache.sling.scripting.examplebundle.scriptmatching/1.0.0/selector-1.HEAD.html",
@@ -136,7 +136,7 @@ public class ScriptMatchIT extends AbstractEndpointTestBase {
         String path = url.substring(url.lastIndexOf('/'));
         String[] parts = path.split("\\.");
         String selectorString = null;
-        String extension = null;
+        String extension;
         if (parts.length == 3) {
             selectorString = parts[1];
             extension = parts[2];