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:13 UTC

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

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