brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [1/5] git commit: NginxUrlMapping test: remove some incorrect asserts
Date Thu, 21 Aug 2014 16:02:44 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master cc1915aaa -> 8257a4fff


NginxUrlMapping test: remove some incorrect asserts


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/2f279231
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/2f279231
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/2f279231

Branch: refs/heads/master
Commit: 2f2792318a8f44b86d4a31da74495acd66951a19
Parents: 42ba635
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Aug 6 13:24:09 2014 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Thu Aug 21 16:49:43 2014 +0100

----------------------------------------------------------------------
 .../nginx/NginxUrlMappingIntegrationTest.java   |  10 +-
 .../main/java/brooklyn/test/HttpTestUtils.java  | 108 +++++++++++++++----
 2 files changed, 92 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2f279231/software/webapp/src/test/java/brooklyn/entity/proxy/nginx/NginxUrlMappingIntegrationTest.java
----------------------------------------------------------------------
diff --git a/software/webapp/src/test/java/brooklyn/entity/proxy/nginx/NginxUrlMappingIntegrationTest.java
b/software/webapp/src/test/java/brooklyn/entity/proxy/nginx/NginxUrlMappingIntegrationTest.java
index b8103d9..d6918a5 100644
--- a/software/webapp/src/test/java/brooklyn/entity/proxy/nginx/NginxUrlMappingIntegrationTest.java
+++ b/software/webapp/src/test/java/brooklyn/entity/proxy/nginx/NginxUrlMappingIntegrationTest.java
@@ -196,10 +196,12 @@ public class NginxUrlMappingIntegrationTest extends BrooklynAppLiveTestSupport
{
         HttpTestUtils.assertHttpStatusCodeEquals("http://localhost2:"+port+"/hello-world/notexists",
404);
         HttpTestUtils.assertHttpStatusCodeEquals("http://localhost3:"+port+"/", 404);
         
-        //make sure nginx default welcome page isn't displayed
-        HttpTestUtils.assertContentNotContainsText("http://localhost:"+port+"/", "ginx");
-        HttpTestUtils.assertContentNotContainsText("http://localhost2:"+port+"/", "ginx");
-        HttpTestUtils.assertContentNotContainsText("http://localhost3:"+port+"/", "ginx");
+        // TODO previously said "make sure nginx default welcome page isn't displayed",
+        // but the assertion only worked because threw exception on 404 trying to read
+        // stdin of http connection. If reading stderr of http connection, we do see
+        // "ginx" in the output. Why were we asserting this? Can we just delete it?
+        // Previous code was:
+        //     Asserts.assertFails { HttpTestUtils.assertContentContainsText([timeout:1],
"http://localhost:${port}/", "ginx"); }
     }
 
     @Test(groups = "Integration")

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/2f279231/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
----------------------------------------------------------------------
diff --git a/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java b/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
index bc238bf..8f96dcf 100644
--- a/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
+++ b/usage/test-support/src/main/java/brooklyn/test/HttpTestUtils.java
@@ -18,19 +18,16 @@
  */
 package brooklyn.test;
 
-import static com.google.common.base.Predicates.instanceOf;
-import static com.google.common.base.Throwables.getCausalChain;
-import static com.google.common.collect.Iterables.find;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.List;
 import java.util.Map;
-import java.util.NoSuchElementException;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -48,6 +45,9 @@ import org.slf4j.LoggerFactory;
 import org.testng.Assert;
 
 import brooklyn.util.collections.MutableMap;
+import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.stream.Streams;
+import brooklyn.util.time.Time;
 
 import com.google.common.base.Throwables;
 import com.google.common.collect.Lists;
@@ -132,11 +132,7 @@ public class HttpTestUtils {
         int status = ((HttpURLConnection) connection).getResponseCode();
         
         // read fully if possible, then close everything, trying to prevent cached threads
at server
-        try { DefaultGroovyMethods.getText( connection.getInputStream() ); } catch (Exception
e) {}
-        try { ((HttpURLConnection) connection).disconnect(); } catch (Exception e) {}
-        try { connection.getInputStream().close(); } catch (Exception e) {}
-        try { connection.getOutputStream().close(); } catch (Exception e) {}
-        try { ((HttpURLConnection) connection).getErrorStream().close(); } catch (Exception
e) {}
+        consumeAndCloseQuietly((HttpURLConnection) connection);
         
         if (LOG.isDebugEnabled())
             LOG.debug("connection to {} ({}ms) gives {}", new Object[] { url, (System.currentTimeMillis()-startTime),
status });
@@ -166,7 +162,7 @@ public class HttpTestUtils {
             Thread.currentThread().interrupt();
             throw new RuntimeException("Interrupted for "+url+" (in assertion that unreachable)",
e);
         } catch (Exception e) {
-            IOException cause = getFirstThrowableOfType(e, IOException.class);
+            IOException cause = Exceptions.getFirstThrowableOfType(e, IOException.class);
             if (cause != null) {
                 // success; clean shutdown transitioning from 400 to error
             } else {
@@ -180,7 +176,7 @@ public class HttpTestUtils {
     }
     
     public static void assertUrlUnreachableEventually(Map flags, final String url) {
-        TestUtils.executeUntilSucceeds(flags, new Runnable() {
+        Asserts.succeedsEventually(flags, new Runnable() {
             public void run() {
                 assertUrlUnreachable(url);
             }
@@ -209,7 +205,7 @@ public class HttpTestUtils {
     }
 
     public static void assertHttpStatusCodeEventuallyEquals(Map flags, final String url,
final int expectedCode) {
-        TestUtils.executeUntilSucceeds(flags, new Runnable() {
+        Asserts.succeedsEventually(flags, new Runnable() {
             public void run() {
                 assertHttpStatusCodeEquals(url, expectedCode);
             }
@@ -219,7 +215,7 @@ public class HttpTestUtils {
     public static void assertContentContainsText(final String url, final String phrase, final
String ...additionalPhrases) {
         try {
             String contents = getContent(url);
-            Assert.assertTrue(contents!=null && contents.length()>0);
+            Assert.assertTrue(contents != null && contents.length() > 0);
             for (String text: Lists.asList(phrase, additionalPhrases)) {
                 if (!contents.contains(text)) {
                     LOG.warn("CONTENTS OF URL "+url+" MISSING TEXT: "+text+"\n"+contents);
@@ -246,12 +242,43 @@ public class HttpTestUtils {
         }
     }
 
+    public static void assertErrorContentContainsText(final String url, final String phrase,
final String ...additionalPhrases) {
+        try {
+            String contents = getErrorContent(url);
+            Assert.assertTrue(contents != null && contents.length() > 0);
+            for (String text: Lists.asList(phrase, additionalPhrases)) {
+                if (!contents.contains(text)) {
+                    LOG.warn("CONTENTS OF URL "+url+" MISSING TEXT: "+text+"\n"+contents);
+                    Assert.fail("URL "+url+" does not contain text: "+text);
+                }
+            }
+        } catch (Exception e) {
+            throw Throwables.propagate(e);
+        }
+    }
+
+
+    public static void assertErrorContentNotContainsText(final String url, final String phrase,
final String ...additionalPhrases) {
+        try {
+            String err = getErrorContent(url);
+            Assert.assertTrue(err != null);
+            for (String text: Lists.asList(phrase, additionalPhrases)) {
+                if (err.contains(text)) {
+                    LOG.warn("CONTENTS OF URL "+url+" HAS TEXT: "+text+"\n"+err);
+                    Assert.fail("URL "+url+" contain text: "+text);
+                }
+            }
+        } catch (Exception e) {
+            throw Throwables.propagate(e);
+        }
+    }
+    
     public static void assertContentEventuallyContainsText(final String url, final String
phrase, final String ...additionalPhrases) {
         assertContentEventuallyContainsText(MutableMap.of(), url, phrase, additionalPhrases);
     }
     
     public static void assertContentEventuallyContainsText(Map flags, final String url, final
String phrase, final String ...additionalPhrases) {
-        TestUtils.executeUntilSucceeds(new Runnable() {
+        Asserts.succeedsEventually(flags, new Runnable() {
             public void run() {
                 assertContentContainsText(url, phrase, additionalPhrases);
             }
@@ -273,6 +300,30 @@ public class HttpTestUtils {
         });
     }
     
+    public static String getErrorContent(String url) {
+        try {
+            HttpURLConnection connection = (HttpURLConnection) connectToUrl(url);
+            long startTime = System.currentTimeMillis();
+            
+            String err;
+            int status;
+            try {
+                InputStream errStream = connection.getErrorStream();
+                err = Streams.readFullyString(errStream);
+                status = connection.getResponseCode();
+            } finally {
+                closeQuietly(connection);
+            }
+            
+            if (LOG.isDebugEnabled())
+                LOG.debug("read of err {} ({}ms) complete; http code {}", new Object[] {
url, Time.makeTimeStringRounded(System.currentTimeMillis()-startTime), status});
+            return err;
+
+        } catch (Exception e) {
+            throw Exceptions.propagate(e);
+        }
+    }
+    
     public static String getContent(String url) {
         try {
             return DefaultGroovyMethods.getText(new URL(url).openStream());
@@ -315,13 +366,26 @@ public class HttpTestUtils {
         });
     }
     
-    // TODO Part-duplicated from jclouds Throwables2
-    @SuppressWarnings("unchecked")
-    private static <T extends Throwable> T getFirstThrowableOfType(Throwable from,
Class<T> clazz) {
-        try {
-            return (T) find(getCausalChain(from), instanceOf(clazz));
-        } catch (NoSuchElementException e) {
-            return null;
-        }
+    /**
+     * Consumes the input stream entirely and then cleanly closes the connection.
+     * Ignores all exceptions completely, not even logging them!
+     * 
+     * Consuming the stream fully is useful for preventing idle TCP connections. 
+     * See {@linkplain http://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html}.
+     */
+    public static void consumeAndCloseQuietly(HttpURLConnection connection) {
+        try { Streams.readFully(connection.getInputStream()); } catch (Exception e) {}
+        closeQuietly(connection);
+    }
+    
+    /**
+     * Closes all streams of the connection, and disconnects it. Ignores all exceptions completely,
+     * not even logging them!
+     */
+    public static void closeQuietly(HttpURLConnection connection) {
+        try { connection.disconnect(); } catch (Exception e) {}
+        try { connection.getInputStream().close(); } catch (Exception e) {}
+        try { connection.getOutputStream().close(); } catch (Exception e) {}
+        try { connection.getErrorStream().close(); } catch (Exception e) {}
     }
 }


Mime
View raw message