You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2019/06/05 12:33:09 UTC

[sling-whiteboard] branch feature/url-connection-agent-testing created (now d6d4966)

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

rombert pushed a change to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git.


      at d6d4966  Stubbed out a wait to launch external processes

This branch includes the following new commits:

     new ed6e122  Some work on integration tests
     new a5fea2d  Migrate to Junit 5 for more fine-grained timeouts and exception checks.
     new caaf5a1  Provisional integration tests, currently setting the timeouts manually
     new 29ab7c6  Cleanups in tests
     new 5d76add  Allow accessing server as an instance field and stop hardcoding the local port.
     new 126660d  Improved test support classes naming and documentation
     new 1875d91  Removed unused dependencies
     new d6d4966  Stubbed out a wait to launch external processes

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-whiteboard] 08/08: Stubbed out a wait to launch external processes

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit d6d4966bd55a47cdd0c0d0a02c8edf7c12342ae8
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 14:32:29 2019 +0200

    Stubbed out a wait to launch external processes
---
 .../java/org/apache/sling/uca/impl/IntegrationTest.java  | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index 9dec971..0a3af13 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -27,6 +27,8 @@ import java.net.MalformedURLException;
 import java.net.SocketTimeoutException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.jupiter.api.Test;
@@ -74,7 +76,19 @@ public class IntegrationTest {
     }
     
 
-    private void runTest(String urlSpec) throws MalformedURLException, IOException {
+    private void runTest(String urlSpec) throws MalformedURLException, IOException, InterruptedException {
+        
+        String javaHome = System.getProperty("java.home");
+        Path javaExe = Paths.get(javaHome, "bin", "java");
+        ProcessBuilder pb = new ProcessBuilder(javaExe.toString(), "-version");
+        pb.inheritIO();
+        Process process = pb.start();
+        
+        process.getInputStream();
+        
+        int exitCode = process.waitFor();
+        
+        LOG.info("Exited with code {}", exitCode);
         
         URL url = new URL(urlSpec);
         LOG.info("connecting to {}", url);


[sling-whiteboard] 06/08: Improved test support classes naming and documentation

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 126660db83c510e9dbb3d2be92aeb034381c7239
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 14:20:44 2019 +0200

    Improved test support classes naming and documentation
---
 .../org/apache/sling/uca/impl/IntegrationTest.java |  5 ++--
 .../sling/uca/impl/MisbehavingServerControl.java   | 31 ++++++++++++++++++++++
 ...erRule.java => MisbehavingServerExtension.java} | 23 ++++++++--------
 .../org/apache/sling/uca/impl/ServerControl.java   |  6 -----
 4 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index aa02dd1..9dec971 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -29,13 +29,12 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.sling.uca.impl.ServerRule.MisbehavingServer;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ExtendWith(ServerRule.class)
+@ExtendWith(MisbehavingServerExtension.class)
 public class IntegrationTest {
     
     private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
@@ -66,7 +65,7 @@ public class IntegrationTest {
      * @throws IOException various I/O problems
      */
     @Test
-    public void readTimeout(@MisbehavingServer ServerControl server) throws IOException {
+    public void readTimeout(MisbehavingServerControl server) throws IOException {
         
         SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
             () -> assertTimeout(ofSeconds(10),  () -> runTest("http://localhost:" + server.getLocalPort()))
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerControl.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerControl.java
new file mode 100644
index 0000000..9b0bf93
--- /dev/null
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerControl.java
@@ -0,0 +1,31 @@
+/*
+ * 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.uca.impl;
+
+/**
+ * Allows control of a local server
+ *
+ */
+public interface MisbehavingServerControl {
+
+    /**
+     * Returns the port on which the local server is bound
+     * 
+     * @return the port
+     */
+    int getLocalPort();
+}
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerExtension.java
similarity index 85%
rename from url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
rename to url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerExtension.java
index 80d4fc4..702b82f 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerExtension.java
@@ -17,10 +17,6 @@
 package org.apache.sling.uca.impl;
 
 import java.io.IOException;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
 import java.util.concurrent.TimeUnit;
 
 import javax.servlet.ServletException;
@@ -41,11 +37,14 @@ import org.junit.jupiter.api.extension.ParameterResolver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolver, ServerControl {
-    
-    @Retention(RetentionPolicy.RUNTIME)
-    @Target(ElementType.PARAMETER)
-    public @interface MisbehavingServer { }
+/**
+ * Provides an Jetty-based local server that can be configured to timeout
+ * 
+ * <p>After extending a JUnit Jupiter test with this extension, any parameter of type {@link MisbehavingServerControl}
+ * will be resolved.</p>
+ *
+ */
+class MisbehavingServerExtension implements BeforeAllCallback, AfterAllCallback, ParameterResolver, MisbehavingServerControl {
     
     private final Logger logger = LoggerFactory.getLogger(getClass());
     
@@ -58,16 +57,16 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolv
     @Override
     public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
             throws ParameterResolutionException {
-        return parameterContext.isAnnotated(MisbehavingServer.class);
+        return parameterContext.getParameter().getType() == MisbehavingServerControl.class;
     }
     
     @Override
     public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
             throws ParameterResolutionException {
-        if ( parameterContext.getParameter().getType() == ServerControl.class )
+        if ( parameterContext.getParameter().getType() == MisbehavingServerControl.class )
             return this;
         
-        throw new ParameterResolutionException("Unable to get a Server instance for " + parameterContext);
+        throw new ParameterResolutionException("Unable to get a " + MisbehavingServerControl.class.getSimpleName() + " instance for " + parameterContext);
     }
     
     @Override
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java
deleted file mode 100644
index e73d682..0000000
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package org.apache.sling.uca.impl;
-
-public interface ServerControl {
-
-    int getLocalPort();
-}


[sling-whiteboard] 01/08: Some work on integration tests

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit ed6e1225912b48c4eeebe5f71c88eba6af1525be
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 11:03:49 2019 +0200

    Some work on integration tests
---
 url-connection-agent/pom.xml                       |  11 ++
 .../org/apache/sling/uca/impl/IntegrationTest.java |  71 ++++++++++++
 .../java/org/apache/sling/uca/impl/ServerRule.java | 121 +++++++++++++++++++++
 3 files changed, 203 insertions(+)

diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml
index 01af5b3..e44e7ba 100644
--- a/url-connection-agent/pom.xml
+++ b/url-connection-agent/pom.xml
@@ -70,5 +70,16 @@
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-server</artifactId>
+            <version>9.4.18.v20190429</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
new file mode 100644
index 0000000..b3d6816
--- /dev/null
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.uca.impl;
+
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class IntegrationTest {
+    
+    private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
+    
+    @Rule
+    public ServerRule server = new ServerRule();
+
+    @Test(expected = IOException.class, timeout = 5000)
+    public void connectTimeout() throws IOException {
+
+        runTest(false);
+    }
+
+    @Test(expected = IOException.class, timeout = 15000)
+    public void readTimeout() throws IOException {
+        
+        runTest(true);
+    }
+    
+
+    private void runTest(boolean shouldConnect) throws MalformedURLException, IOException {
+        URL url = new URL("http://localhost:" + server.getLocalPort());
+        LOG.info("connecting");
+        URLConnection connection = url.openConnection();
+        // TODO - remove when running through the harness
+        connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(3));
+        connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(3));
+        connection.connect();
+        /*
+         * if ( !shouldConnect ) fail("Connection should not be succesful");
+         */        
+        LOG.info("connected");
+        try ( InputStream is = connection.getInputStream()) {
+            while ( is.read() != -1)
+                ;
+        }
+        LOG.info("read");
+    }
+}
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
new file mode 100644
index 0000000..6bcad85
--- /dev/null
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.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.uca.impl;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.io.Connection;
+import org.eclipse.jetty.io.EndPoint;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.HttpConnectionFactory;
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.junit.rules.ExternalResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class ServerRule extends ExternalResource {
+    
+    private static final Logger LOG = LoggerFactory.getLogger(ServerRule.class);
+    
+    private Server server;
+    
+    private int localPort = 12312;
+
+    @Override
+    protected void before() throws Throwable {
+        server = new Server(localPort);
+        ServerConnector connector = new ServerConnector(server) {
+            @Override
+            public void accept(int acceptorID) throws IOException {
+                LOG.info("Waiting before accepting");
+                try {
+                    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    return;
+                }
+                super.accept(acceptorID);
+                LOG.info("Accepted");
+            }
+        };
+        connector.setPort(localPort);
+        connector.setConnectionFactories(Collections.singleton(new HttpConnectionFactory() {
+            @Override
+            public Connection newConnection(Connector connector, EndPoint endPoint) {
+                LOG.info("Waiting before creating connection");
+                try {
+                    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    throw new RuntimeException("Interrupted");
+                }
+                
+                Connection connection = super.newConnection(connector, endPoint);
+                LOG.info("Connection created");
+                return connection;
+            }
+        }));
+        server.setConnectors(new Connector[] { 
+            connector
+        });
+        server.setHandler(new AbstractHandler() {
+            
+            @Override
+            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
+                    throws IOException, ServletException {
+                
+                LOG.info("Waiting before handling");
+                
+                try {
+                    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    return;
+                }
+                
+                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
+                baseRequest.setHandled(true);
+                LOG.info("Handled");
+            }
+        });
+        
+        server.start();
+    }
+    
+    @Override
+    protected void after() {
+        if ( server != null )
+            try {
+                server.stop();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+    }
+
+    public int getLocalPort() {
+        return localPort;
+    }
+}
\ No newline at end of file


[sling-whiteboard] 07/08: Removed unused dependencies

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 1875d9147cadf096dd47c02b6f5b5bfa8ee1a2aa
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 14:21:15 2019 +0200

    Removed unused dependencies
---
 url-connection-agent/pom.xml | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml
index 86da21d..bc5a25f 100644
--- a/url-connection-agent/pom.xml
+++ b/url-connection-agent/pom.xml
@@ -83,11 +83,5 @@
             <version>5.4.2</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>commons-io</groupId>
-            <artifactId>commons-io</artifactId>
-            <version>2.6</version>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 </project>
\ No newline at end of file


[sling-whiteboard] 02/08: Migrate to Junit 5 for more fine-grained timeouts and exception checks.

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit a5fea2d2aa812c891e3001ad95ccbfdf9f807a52
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 11:34:02 2019 +0200

    Migrate to Junit 5 for more fine-grained timeouts and exception checks.
---
 url-connection-agent/pom.xml                       | 10 ++++---
 .../org/apache/sling/uca/impl/IntegrationTest.java | 31 ++++++++++++++--------
 .../java/org/apache/sling/uca/impl/ServerRule.java | 29 +++++++++++---------
 .../sling/uca/impl/UrlTimeoutTransformerTest.java  | 16 ++++++-----
 4 files changed, 52 insertions(+), 34 deletions(-)

diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml
index e44e7ba..bc5a25f 100644
--- a/url-connection-agent/pom.xml
+++ b/url-connection-agent/pom.xml
@@ -67,10 +67,6 @@
             <version>3.24.0-GA</version>
         </dependency>
         <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-        </dependency>
-        <dependency>
             <groupId>org.eclipse.jetty</groupId>
             <artifactId>jetty-server</artifactId>
             <version>9.4.18.v20190429</version>
@@ -81,5 +77,11 @@
             <artifactId>slf4j-simple</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>5.4.2</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index b3d6816..d9a1d7c 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -16,42 +16,51 @@
  */
 package org.apache.sling.uca.impl;
 
-import static org.junit.Assert.fail;
+import static java.time.Duration.ofSeconds;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTimeout;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
+import java.net.SocketTimeoutException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.concurrent.TimeUnit;
 
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@ExtendWith(ServerRule.class)
 public class IntegrationTest {
     
     private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
-    
-    @Rule
-    public ServerRule server = new ServerRule();
 
-    @Test(expected = IOException.class, timeout = 5000)
+    @Test
     public void connectTimeout() throws IOException {
 
-        runTest(false);
+        SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
+            () -> assertTimeout(ofSeconds(5),  () -> runTest(false))
+        );
+        assertEquals("Connect timed out", exception.getMessage());
     }
 
-    @Test(expected = IOException.class, timeout = 15000)
+    @Test
     public void readTimeout() throws IOException {
         
-        runTest(true);
+        SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
+            () -> assertTimeout(ofSeconds(10),  () -> runTest(false))
+        );
+        assertEquals("Read timed out", exception.getMessage());
     }
     
 
     private void runTest(boolean shouldConnect) throws MalformedURLException, IOException {
-        URL url = new URL("http://localhost:" + server.getLocalPort());
+        
+        URL url = new URL("http://localhost:" + ServerRule.getLocalPort());
         LOG.info("connecting");
         URLConnection connection = url.openConnection();
         // TODO - remove when running through the harness
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
index 6bcad85..c047afd 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
@@ -32,21 +32,28 @@ import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
 import org.eclipse.jetty.server.handler.AbstractHandler;
-import org.junit.rules.ExternalResource;
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class ServerRule extends ExternalResource {
+class ServerRule implements BeforeAllCallback, AfterAllCallback {
     
     private static final Logger LOG = LoggerFactory.getLogger(ServerRule.class);
     
-    private Server server;
+    private static final int LOCAL_PORT = 12312;
     
-    private int localPort = 12312;
 
+    public static int getLocalPort() {
+        return LOCAL_PORT;
+    }
+    
+    private Server server;
+    
     @Override
-    protected void before() throws Throwable {
-        server = new Server(localPort);
+    public void beforeAll(ExtensionContext context) throws Exception {
+        server = new Server(LOCAL_PORT);
         ServerConnector connector = new ServerConnector(server) {
             @Override
             public void accept(int acceptorID) throws IOException {
@@ -61,7 +68,7 @@ class ServerRule extends ExternalResource {
                 LOG.info("Accepted");
             }
         };
-        connector.setPort(localPort);
+        connector.setPort(LOCAL_PORT);
         connector.setConnectionFactories(Collections.singleton(new HttpConnectionFactory() {
             @Override
             public Connection newConnection(Connector connector, EndPoint endPoint) {
@@ -104,9 +111,9 @@ class ServerRule extends ExternalResource {
         
         server.start();
     }
-    
+
     @Override
-    protected void after() {
+    public void afterAll(ExtensionContext context) throws Exception {
         if ( server != null )
             try {
                 server.stop();
@@ -114,8 +121,4 @@ class ServerRule extends ExternalResource {
                 e.printStackTrace();
             }
     }
-
-    public int getLocalPort() {
-        return localPort;
-    }
 }
\ No newline at end of file
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java
index 774b420..3847ef2 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java
@@ -16,10 +16,12 @@
  */
 package org.apache.sling.uca.impl;
 
-import static org.junit.Assert.assertNotNull;
 
-import org.junit.Before;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 
 import javassist.NotFoundException;
 
@@ -27,7 +29,7 @@ public class UrlTimeoutTransformerTest {
     
     private URLTimeoutTransformer transformer;
 
-    @Before
+    @BeforeEach
     public void initFields() {
         transformer = new URLTimeoutTransformer(1, 1);
     }
@@ -37,11 +39,13 @@ public class UrlTimeoutTransformerTest {
         assertNotNull(transformer.findConnectMethod("sun/net/www/protocol/http/HttpURLConnection"));
     }
 
-    @Test(expected = NotFoundException.class)
+    @Test
     public void findInheritedConnectMethod() throws NotFoundException {
         // do NOT look for inherited methods, as we can only rewrite the precise classes the
         // retransform was triggered for
-        transformer.findConnectMethod("sun/net/www/protocol/https/DelegateHttpsURLConnection");
+        assertThrows( NotFoundException.class,
+            () -> transformer.findConnectMethod("sun/net/www/protocol/https/DelegateHttpsURLConnection")
+        );
     }
     
 }


[sling-whiteboard] 03/08: Provisional integration tests, currently setting the timeouts manually

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit caaf5a1917e58ce96c7ff840be958024ba679bf8
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 13:51:45 2019 +0200

    Provisional integration tests, currently setting the timeouts manually
---
 url-connection-agent/pom.xml                              |  6 ++++++
 .../java/org/apache/sling/uca/impl/IntegrationTest.java   | 15 ++++++---------
 .../test/java/org/apache/sling/uca/impl/ServerRule.java   |  2 +-
 .../src/test/resources/simplelogger.properties            |  1 +
 4 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml
index bc5a25f..86da21d 100644
--- a/url-connection-agent/pom.xml
+++ b/url-connection-agent/pom.xml
@@ -83,5 +83,11 @@
             <version>5.4.2</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.6</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index d9a1d7c..31ba3bf 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -43,33 +43,30 @@ public class IntegrationTest {
     public void connectTimeout() throws IOException {
 
         SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
-            () -> assertTimeout(ofSeconds(5),  () -> runTest(false))
+            () -> assertTimeout(ofSeconds(5),  () -> runTest("http://sling.apache.org:81"))
         );
-        assertEquals("Connect timed out", exception.getMessage());
+        assertEquals("connect timed out", exception.getMessage());
     }
 
     @Test
     public void readTimeout() throws IOException {
         
         SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
-            () -> assertTimeout(ofSeconds(10),  () -> runTest(false))
+            () -> assertTimeout(ofSeconds(10),  () -> runTest("http://localhost:" + ServerRule.getLocalPort()))
         );
         assertEquals("Read timed out", exception.getMessage());
     }
     
 
-    private void runTest(boolean shouldConnect) throws MalformedURLException, IOException {
+    private void runTest(String urlSpec) throws MalformedURLException, IOException {
         
-        URL url = new URL("http://localhost:" + ServerRule.getLocalPort());
-        LOG.info("connecting");
+        URL url = new URL(urlSpec);
+        LOG.info("connecting to {}", url);
         URLConnection connection = url.openConnection();
         // TODO - remove when running through the harness
         connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(3));
         connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(3));
         connection.connect();
-        /*
-         * if ( !shouldConnect ) fail("Connection should not be succesful");
-         */        
         LOG.info("connected");
         try ( InputStream is = connection.getInputStream()) {
             while ( is.read() != -1)
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
index c047afd..a799b55 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
@@ -44,7 +44,6 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback {
     
     private static final int LOCAL_PORT = 12312;
     
-
     public static int getLocalPort() {
         return LOCAL_PORT;
     }
@@ -53,6 +52,7 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback {
     
     @Override
     public void beforeAll(ExtensionContext context) throws Exception {
+        
         server = new Server(LOCAL_PORT);
         ServerConnector connector = new ServerConnector(server) {
             @Override
diff --git a/url-connection-agent/src/test/resources/simplelogger.properties b/url-connection-agent/src/test/resources/simplelogger.properties
new file mode 100644
index 0000000..6dfc7aa
--- /dev/null
+++ b/url-connection-agent/src/test/resources/simplelogger.properties
@@ -0,0 +1 @@
+org.slf4j.simpleLogger.showDateTime=true
\ No newline at end of file


[sling-whiteboard] 04/08: Cleanups in tests

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 29ab7c65914811f302f7256d40da3b548f715a71
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 13:59:43 2019 +0200

    Cleanups in tests
---
 .../org/apache/sling/uca/impl/IntegrationTest.java | 16 +++++++
 .../java/org/apache/sling/uca/impl/ServerRule.java | 50 ++++------------------
 2 files changed, 25 insertions(+), 41 deletions(-)

diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index 31ba3bf..d952181 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -39,6 +39,17 @@ public class IntegrationTest {
     
     private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
 
+    /**
+     * Validates that connecting to a unaccessible port on an existing port fails with a connect 
+     * timeout exception
+     * 
+     * <p>It is surprisingly hard to simulate a connnection timeout. The most reliable way seems to
+     * be to get a firewall to drop packets, but this is very hard to do portably and safely
+     * in a unit test. The least bad possible solution is to access an URL that we know will timeout
+     * and that is related to us - the Sling website.</p>
+     * 
+     * @throws IOException various I/O problems 
+     */
     @Test
     public void connectTimeout() throws IOException {
 
@@ -48,6 +59,11 @@ public class IntegrationTest {
         assertEquals("connect timed out", exception.getMessage());
     }
 
+    /**
+     * Validates that connecting to a host that delays the response fails with a read timeout
+     * 
+     * @throws IOException various I/O problems
+     */
     @Test
     public void readTimeout() throws IOException {
         
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
index a799b55..62a6380 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
@@ -17,17 +17,13 @@
 package org.apache.sling.uca.impl;
 
 import java.io.IOException;
-import java.util.Collections;
 import java.util.concurrent.TimeUnit;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.eclipse.jetty.io.Connection;
-import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.server.Connector;
-import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.ServerConnector;
@@ -40,7 +36,7 @@ import org.slf4j.LoggerFactory;
 
 class ServerRule implements BeforeAllCallback, AfterAllCallback {
     
-    private static final Logger LOG = LoggerFactory.getLogger(ServerRule.class);
+    private final Logger logger = LoggerFactory.getLogger(getClass());
     
     private static final int LOCAL_PORT = 12312;
     
@@ -69,43 +65,14 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback {
             }
         };
         connector.setPort(LOCAL_PORT);
-        connector.setConnectionFactories(Collections.singleton(new HttpConnectionFactory() {
-            @Override
-            public Connection newConnection(Connector connector, EndPoint endPoint) {
-                LOG.info("Waiting before creating connection");
-                try {
-                    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
-                } catch (InterruptedException e) {
-                    Thread.currentThread().interrupt();
-                    throw new RuntimeException("Interrupted");
-                }
-                
-                Connection connection = super.newConnection(connector, endPoint);
-                LOG.info("Connection created");
-                return connection;
-            }
-        }));
-        server.setConnectors(new Connector[] { 
-            connector
-        });
+        server.setConnectors(new Connector[] { connector });
         server.setHandler(new AbstractHandler() {
             
             @Override
             public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
                     throws IOException, ServletException {
-                
-                LOG.info("Waiting before handling");
-                
-                try {
-                    Thread.sleep(TimeUnit.SECONDS.toMillis(10));
-                } catch (InterruptedException e) {
-                    Thread.currentThread().interrupt();
-                    return;
-                }
-                
                 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                 baseRequest.setHandled(true);
-                LOG.info("Handled");
             }
         });
         
@@ -114,11 +81,12 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback {
 
     @Override
     public void afterAll(ExtensionContext context) throws Exception {
-        if ( server != null )
-            try {
-                server.stop();
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
+        if ( server == null )
+            return;
+        try {
+            server.stop();
+        } catch (Exception e) {
+            logger.info("Failed shutting down server", e);
+        }
     }
 }
\ No newline at end of file


[sling-whiteboard] 05/08: Allow accessing server as an instance field and stop hardcoding the local port.

Posted by ro...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 5d76add543658946bd23a9d6134c54dfd111926c
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 14:13:00 2019 +0200

    Allow accessing server as an instance field and stop hardcoding the local port.
---
 .../org/apache/sling/uca/impl/IntegrationTest.java |  5 +--
 .../org/apache/sling/uca/impl/ServerControl.java   |  6 ++++
 .../java/org/apache/sling/uca/impl/ServerRule.java | 37 ++++++++++++++++++----
 3 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index d952181..aa02dd1 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -29,6 +29,7 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.sling.uca.impl.ServerRule.MisbehavingServer;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.slf4j.Logger;
@@ -65,10 +66,10 @@ public class IntegrationTest {
      * @throws IOException various I/O problems
      */
     @Test
-    public void readTimeout() throws IOException {
+    public void readTimeout(@MisbehavingServer ServerControl server) throws IOException {
         
         SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
-            () -> assertTimeout(ofSeconds(10),  () -> runTest("http://localhost:" + ServerRule.getLocalPort()))
+            () -> assertTimeout(ofSeconds(10),  () -> runTest("http://localhost:" + server.getLocalPort()))
         );
         assertEquals("Read timed out", exception.getMessage());
     }
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java
new file mode 100644
index 0000000..e73d682
--- /dev/null
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java
@@ -0,0 +1,6 @@
+package org.apache.sling.uca.impl;
+
+public interface ServerControl {
+
+    int getLocalPort();
+}
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
index 62a6380..80d4fc4 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
@@ -17,6 +17,10 @@
 package org.apache.sling.uca.impl;
 
 import java.io.IOException;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 import java.util.concurrent.TimeUnit;
 
 import javax.servlet.ServletException;
@@ -31,25 +35,45 @@ import org.eclipse.jetty.server.handler.AbstractHandler;
 import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class ServerRule implements BeforeAllCallback, AfterAllCallback {
+class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolver, ServerControl {
     
-    private final Logger logger = LoggerFactory.getLogger(getClass());
+    @Retention(RetentionPolicy.RUNTIME)
+    @Target(ElementType.PARAMETER)
+    public @interface MisbehavingServer { }
     
-    private static final int LOCAL_PORT = 12312;
+    private final Logger logger = LoggerFactory.getLogger(getClass());
     
-    public static int getLocalPort() {
-        return LOCAL_PORT;
+    public int getLocalPort() {
+        return ((ServerConnector) server.getConnectors()[0]).getLocalPort();
     }
     
     private Server server;
     
     @Override
+    public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
+            throws ParameterResolutionException {
+        return parameterContext.isAnnotated(MisbehavingServer.class);
+    }
+    
+    @Override
+    public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
+            throws ParameterResolutionException {
+        if ( parameterContext.getParameter().getType() == ServerControl.class )
+            return this;
+        
+        throw new ParameterResolutionException("Unable to get a Server instance for " + parameterContext);
+    }
+    
+    @Override
     public void beforeAll(ExtensionContext context) throws Exception {
         
-        server = new Server(LOCAL_PORT);
+        server = new Server();
         ServerConnector connector = new ServerConnector(server) {
             @Override
             public void accept(int acceptorID) throws IOException {
@@ -64,7 +88,6 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback {
                 LOG.info("Accepted");
             }
         };
-        connector.setPort(LOCAL_PORT);
         server.setConnectors(new Connector[] { connector });
         server.setHandler(new AbstractHandler() {