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

[sling-whiteboard] 01/08: Some work on integration 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 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