Author: cziegeler
Date: Sun Oct 16 13:14:53 2016
New Revision: 1765152

URL: http://svn.apache.org/viewvc?rev=1765152&view=rev
Log:
SLING-4867 : EntityResourceList should be thread-safe

Modified:
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/installer/InstallManyBundlesTest.java

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/installer/InstallManyBundlesTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/installer/InstallManyBundlesTest.java?rev=1765152&r1=1765151&r2=1765152&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/installer/InstallManyBundlesTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/installer/InstallManyBundlesTest.java
 Sun Oct 16 13:14:53 2016
@@ -20,9 +20,11 @@ import static org.junit.Assert.fail;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.UUID;
 
-import junit.framework.AssertionFailedError;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
@@ -36,56 +38,58 @@ import org.osgi.framework.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/** Test the installation and update of several (optionally many) 
- *  bundles via the JCR installer 
+import junit.framework.AssertionFailedError;
+
+/** Test the installation and update of several (optionally many)
+ *  bundles via the JCR installer
  */
 public class InstallManyBundlesTest {
     /** How many test cycles to run - can be set to a higher
      *  value manually for heavier testing.
      */
     private static final int HOW_MANY = 
Integer.getInteger("InstallManyBundlesTest.howMany", 10);
-    
+
     private static final long WAIT_ACTIVE_TIMEOUT_MSEC = 10000;
-    
+
     private static final String TEST_ID = UUID.randomUUID().toString();
-    
-    private static final String BASE_BSN = "org.apache.sling.testbundle." 
-        + InstallManyBundlesTest.class.getSimpleName() 
-        + "." + TEST_ID; 
-    
-    private static final String INSTALL_PATH = "/apps/" + 
InstallManyBundlesTest.class.getSimpleName() + "/" + TEST_ID + "/install"; 
-    
+
+    private static final String BASE_BSN = "org.apache.sling.testbundle."
+        + InstallManyBundlesTest.class.getSimpleName()
+        + "." + TEST_ID;
+
+    private static final String INSTALL_PATH = "/apps/" + 
InstallManyBundlesTest.class.getSimpleName() + "/" + TEST_ID + "/install";
+
     private static HttpTest H = new HttpTest();
-    
+
     private final Logger log = LoggerFactory.getLogger(getClass());
-    
+
     private String toDelete;
-    
+
     @Before
     public void setup() throws Exception {
         H.setUp();
         H.getTestClient().mkdirs(HttpTest.HTTP_BASE_URL, INSTALL_PATH);
         toDelete = HttpTest.HTTP_BASE_URL + INSTALL_PATH;
     }
-    
+
     @After
     public void cleanup() throws IOException {
         H.getTestClient().delete(toDelete);
     }
-    
+
     private InputStream getBundleStream(String bsn, String version) {
         return TinyBundles.bundle()
                 .set(Constants.BUNDLE_VERSION, version)
                 .set(Constants.BUNDLE_SYMBOLICNAME, bsn)
                 .build();
     }
-    
+
     private void assertActiveBundle(String bsn, String expectedState, String 
expectedVersion) throws IOException, JSONException {
         final String consoleUrl = HttpTest.HTTP_BASE_URL + 
"/system/console/bundles/" + bsn + ".json";
-        
+
         String state = null;
         String version = null;
-        
+
         final long timeoutMsec = 
TimeoutsProvider.getInstance().getTimeout(WAIT_ACTIVE_TIMEOUT_MSEC);
         final long endTime = System.currentTimeMillis() + timeoutMsec;
         while(System.currentTimeMillis() < endTime) {
@@ -104,25 +108,46 @@ public class InstallManyBundlesTest {
             } catch(InterruptedException ignore) {
             }
         }
-        
-        fail("Did not get state=" 
-                + expectedState + " and version=" + expectedVersion 
+
+        fail("Did not get state="
+                + expectedState + " and version=" + expectedVersion
                 + " within " + timeoutMsec + " msec"
                 + ", got " + state + " / " + version);
     }
-    
+
+    private boolean waitNoBundles(String bsn) throws IOException {
+        final String consoleUrl = HttpTest.HTTP_BASE_URL + 
"/system/console/bundles/" + bsn + ".json";
+
+        final long timeoutMsec = 
TimeoutsProvider.getInstance().getTimeout(WAIT_ACTIVE_TIMEOUT_MSEC);
+        final long endTime = System.currentTimeMillis() + timeoutMsec;
+        while(System.currentTimeMillis() < endTime) {
+            try {
+                H.getContent(consoleUrl, HttpTest.CONTENT_TYPE_DONTCARE, null, 
HttpServletResponse.SC_NOT_FOUND );
+                return true;
+            } catch(AssertionFailedError dontCare) {
+                // Thrown by getContent - might happen if the bundle is still 
installed
+            }
+            try {
+                Thread.sleep(100);
+            } catch(InterruptedException ignore) {
+            }
+        }
+
+        return false;
+    }
+
     private void installAndCheckBundle(String bsn, int filenameVariant, String 
version) throws IOException, JSONException {
         final String filename = bsn + "." + filenameVariant + ".jar";
-        final String url = HttpTest.HTTP_BASE_URL + INSTALL_PATH + "/" + 
filename; 
+        final String url = HttpTest.HTTP_BASE_URL + INSTALL_PATH + "/" + 
filename;
         H.getTestClient().upload(url, getBundleStream(bsn, version));
         assertActiveBundle(bsn, "Active", version);
     }
-    
+
     @Test
     public void installAndUpgradeBundleManyTimes() throws IOException, 
JSONException {
+        final String bsn = BASE_BSN + "_upgradetest";
         int i = 0;
         try {
-            final String bsn = BASE_BSN + "_upgradetest";
             for(i=0; i < HOW_MANY; i++) {
                 final String version = "42.0." + i;
                 installAndCheckBundle(bsn, i, version);
@@ -130,21 +155,36 @@ public class InstallManyBundlesTest {
             log.info("Test bundle successfully installed, upgraded and started 
{} times", HOW_MANY);
         } finally {
             log.info("installAndUpgradeBundleManyTimes exiting with i={}", i);
+            // we should wait until the OSGi installer has removed everything
+            H.getTestClient().delete(toDelete);
+            waitNoBundles(bsn);
         }
     }
-    
+
     @Test
     public void installManyBundles() throws IOException, JSONException {
+        final List<String> bsns = new ArrayList<String>();
         int i = 0;
         try {
             final String version = "42.42.42";
             for(i=0; i < HOW_MANY; i++) {
                 final String bsn = BASE_BSN + "_manybundles_" + i;
+                bsns.add(bsn);
                 installAndCheckBundle(bsn, 0, version);
             }
             log.info("{} different bundles successfully installed and 
started", HOW_MANY);
         } finally {
             log.info("installManyBundles exiting with i={}", i);
+            // we should wait until the OSGi installer has removed everything
+            H.getTestClient().delete(toDelete);
+            while ( !bsns.isEmpty() ) {
+                if ( waitNoBundles(bsns.get(0)) ) {
+                    bsns.remove(0);
+                } else {
+                    final String bsn = bsns.remove(0);
+                    bsns.add(bsn);
+                }
+            }
         }
     }
 }
\ No newline at end of file


Reply via email to