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 2014/05/08 15:32:00 UTC

svn commit: r1593277 - in /sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl: BundleDeploymentTest.java ContentDeploymentTest.java helpers/Poller.java helpers/ServerAdapter.java

Author: rombert
Date: Thu May  8 13:31:59 2014
New Revision: 1593277

URL: http://svn.apache.org/r1593277
Log:
SLING-3544 - Integration tests running against a live Sling Launchpad
instance

Use a Poller for operations which are timing-sensitive.

Added:
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java   (with props)
Modified:
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/BundleDeploymentTest.java
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ContentDeploymentTest.java
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ServerAdapter.java

Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/BundleDeploymentTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/BundleDeploymentTest.java?rev=1593277&r1=1593276&r2=1593277&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/BundleDeploymentTest.java (original)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/BundleDeploymentTest.java Thu May  8 13:31:59 2014
@@ -18,7 +18,9 @@ package org.apache.sling.ide.test.impl;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.concurrent.Callable;
 
+import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.io.IOUtils;
 import org.apache.sling.ide.osgi.OsgiClientException;
 import org.apache.sling.ide.test.impl.helpers.DisableDebugStatusHandlers;
@@ -26,6 +28,7 @@ import org.apache.sling.ide.test.impl.he
 import org.apache.sling.ide.test.impl.helpers.LaunchpadConfig;
 import org.apache.sling.ide.test.impl.helpers.MavenDependency;
 import org.apache.sling.ide.test.impl.helpers.OsgiBundleManifest;
+import org.apache.sling.ide.test.impl.helpers.Poller;
 import org.apache.sling.ide.test.impl.helpers.ProjectAdapter;
 import org.apache.sling.ide.test.impl.helpers.ServerAdapter;
 import org.apache.sling.ide.test.impl.helpers.RepositoryAccessor;
@@ -36,6 +39,7 @@ import org.eclipse.core.resources.IProje
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.core.JavaCore;
+import org.hamcrest.CoreMatchers;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.RuleChain;
@@ -116,12 +120,17 @@ public class BundleDeploymentTest {
         ServerAdapter server = new ServerAdapter(wstServer.getServer());
         server.installModule(bundleProject);
 
-        Thread.sleep(1000); // for good measure, make sure the output is there - TODO replace with polling
+        final RepositoryAccessor repo = new RepositoryAccessor(config);
+        Poller poller = new Poller();
+        poller.pollUntil(new Callable<Void>() {
+            @Override
+            public Void call() throws HttpException, IOException {
+                repo.assertGetIsSuccessful("simple-servlet", "Version 1");
+                return null;
+            }
+        }, CoreMatchers.nullValue());
 
-        RepositoryAccessor repo = new RepositoryAccessor(config);
-        repo.assertGetIsSuccessful("simple-servlet", "Version 1");
-
-        // create DS component class
+        // update DS component class
         InputStream simpleServlet2 = null;
         try {
             simpleServlet2 = getClass().getResourceAsStream("SimpleServlet.java.v2.txt");
@@ -130,8 +139,12 @@ public class BundleDeploymentTest {
             IOUtils.closeQuietly(simpleServlet2);
         }
 
-        Thread.sleep(1000); // for good measure, make sure the output is there - TODO replace with polling
-
-        repo.assertGetIsSuccessful("simple-servlet", "Version 2");
+        poller.pollUntil(new Callable<Void>() {
+            @Override
+            public Void call() throws HttpException, IOException {
+                repo.assertGetIsSuccessful("simple-servlet", "Version 2");
+                return null;
+            }
+        }, CoreMatchers.nullValue());
     }
 }

Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ContentDeploymentTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ContentDeploymentTest.java?rev=1593277&r1=1593276&r2=1593277&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ContentDeploymentTest.java (original)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/ContentDeploymentTest.java Thu May  8 13:31:59 2014
@@ -18,12 +18,14 @@ package org.apache.sling.ide.test.impl;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.util.concurrent.Callable;
 
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.URIException;
 import org.apache.sling.ide.test.impl.helpers.DisableDebugStatusHandlers;
 import org.apache.sling.ide.test.impl.helpers.ExternalSlingLaunchpad;
 import org.apache.sling.ide.test.impl.helpers.LaunchpadConfig;
+import org.apache.sling.ide.test.impl.helpers.Poller;
 import org.apache.sling.ide.test.impl.helpers.ProjectAdapter;
 import org.apache.sling.ide.test.impl.helpers.ServerAdapter;
 import org.apache.sling.ide.test.impl.helpers.RepositoryAccessor;
@@ -33,6 +35,7 @@ import org.eclipse.core.resources.IProje
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.core.JavaCore;
+import org.hamcrest.CoreMatchers;
 import org.junit.After;
 import org.junit.Rule;
 import org.junit.Test;
@@ -79,26 +82,41 @@ public class ContentDeploymentTest {
         ServerAdapter server = new ServerAdapter(wstServer.getServer());
         server.installModule(contentProject);
 
-        Thread.sleep(1000); // for good measure, make sure the output is there - TODO replace with polling
-
         // verify that file is created
-        RepositoryAccessor repo = new RepositoryAccessor(config);
-        repo.assertGetIsSuccessful("hello.txt", "hello, world");
+        final RepositoryAccessor repo = new RepositoryAccessor(config);
+        Poller poller = new Poller();
+        poller.pollUntil(new Callable<Void>() {
+            @Override
+            public Void call() throws HttpException, IOException {
+                repo.assertGetIsSuccessful("hello.txt", "hello, world");
+                return null;
+            }
+        }, CoreMatchers.nullValue());
 
         project.createOrUpdateFile(Path.fromPortableString("jcr_root/hello.txt"), new ByteArrayInputStream(
                 "goodbye, world".getBytes()));
 
-        Thread.sleep(2000); // for good measure, make sure the output is there - TODO replace with polling
-
         // verify that file is updated
-        repo.assertGetIsSuccessful("hello.txt", "goodbye, world");
+        poller.pollUntil(new Callable<Void>() {
+            @Override
+            public Void call() throws HttpException, IOException {
+                repo.assertGetIsSuccessful("hello.txt", "goodbye, world");
+                return null;
+            }
+        }, CoreMatchers.nullValue());
+
 
         project.deleteMember(Path.fromPortableString("jcr_root/hello.txt"));
 
-        Thread.sleep(2000); // for good measure, make sure the output is there - TODO replace with polling
+        // verify that file is deleted
+        poller.pollUntil(new Callable<Void>() {
+            @Override
+            public Void call() throws HttpException, IOException {
+                repo.assertGetReturns404("hello.txt");
+                return null;
+            }
+        }, CoreMatchers.nullValue());
 
-        // verify that the file is deleted
-        repo.assertGetReturns404("hello.txt");
     }
 
     @After

Added: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java?rev=1593277&view=auto
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java (added)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java Thu May  8 13:31:59 2014
@@ -0,0 +1,86 @@
+/*
+ * 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.ide.test.impl.helpers;
+
+import static org.junit.Assert.assertThat;
+
+import java.util.concurrent.Callable;
+
+import org.hamcrest.Matcher;
+
+import junit.framework.AssertionFailedError;
+
+public class Poller {
+
+    private static final long DEFAULT_POLL_WAIT_MILLIS = 5000;
+    private static final long DEFAULT_DELAY_MILLIS = 100;
+
+    public void pollUntilSuccessful(Runnable r) throws InterruptedException {
+
+        long cutoff = System.currentTimeMillis() + DEFAULT_POLL_WAIT_MILLIS;
+
+        while (true) {
+
+            try {
+                r.run();
+                break;
+            } catch (RuntimeException e) {
+                // skip
+            } catch (AssertionFailedError e) {
+                // skip
+            } catch (AssertionError e) {
+                // skip
+            }
+
+            if (System.currentTimeMillis() >= cutoff) {
+                throw new AssertionFailedError("Runnable " + r + " did not succeed in the allocated "
+                        + DEFAULT_DELAY_MILLIS + " ms");
+            }
+
+            Thread.sleep(DEFAULT_DELAY_MILLIS);
+        }
+    }
+
+    public <V> V pollUntil(final Callable<V> callable, final Matcher<Object> matcher) throws InterruptedException {
+
+        final Object[] holder = new Object[1];
+
+        pollUntilSuccessful(new Runnable() {
+            @Override
+            public void run() {
+                V result;
+                try {
+                    result = callable.call();
+                    holder[0] = result;
+                } catch (RuntimeException e) {
+                    throw e;
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+                assertThat(result, matcher);
+            }
+
+            @Override
+            public String toString() {
+                return callable.toString();
+            }
+        });
+
+        // safe, since only we write in holder[0]
+        return (V) holder[0];
+    }
+}

Propchange: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/Poller.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ServerAdapter.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ServerAdapter.java?rev=1593277&r1=1593276&r2=1593277&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ServerAdapter.java (original)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ServerAdapter.java Thu May  8 13:31:59 2014
@@ -16,6 +16,10 @@
  */
 package org.apache.sling.ide.test.impl.helpers;
 
+import static org.hamcrest.CoreMatchers.notNullValue;
+
+import java.util.concurrent.Callable;
+
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.NullProgressMonitor;
@@ -36,23 +40,15 @@ public class ServerAdapter {
         this.server = server;
     }
 
-    public void installModule(IProject project) throws CoreException, InterruptedException {
+    public void installModule(final IProject project) throws CoreException, InterruptedException {
 
-        IModule bundleModule = ServerUtil.getModule(project);
-        // TODO - why do we need to poll?
-        if (bundleModule == null) {
-            for (int i = 0; i < 50; i++) {
-                Thread.sleep(100);
-                bundleModule = ServerUtil.getModule(project);
-                if (bundleModule != null) {
-                    break;
-                }
+        // not sure why we need to poll at all here ... there is some async operation that I'm not aware of
+        IModule bundleModule = new Poller().pollUntil(new Callable<IModule>() {
+            @Override
+            public IModule call() throws Exception {
+                return ServerUtil.getModule(project);
             }
-        }
-
-        if (bundleModule == null) {
-            throw new RuntimeException("Failed getting a module for " + project);
-        }
+        }, notNullValue());
 
         IServerWorkingCopy serverWorkingCopy = server.createWorkingCopy();
         serverWorkingCopy.modifyModules(new IModule[] { bundleModule }, new IModule[0], new NullProgressMonitor());