You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2015/07/07 16:32:40 UTC

svn commit: r1689692 - in /sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup: AsyncInstaller.java IncrementalStartupIT.java P.java SlowActivator.java StartLevelReport.java StartLevelReportImpl.java WaitFor.java

Author: bdelacretaz
Date: Tue Jul  7 14:32:39 2015
New Revision: 1689692

URL: http://svn.apache.org/r1689692
Log:
SLING-4851 - SlowActivator added but reports don't work yet

Added:
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/SlowActivator.java
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReport.java
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReportImpl.java
Modified:
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java
    sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java

Modified: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java?rev=1689692&r1=1689691&r2=1689692&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java (original)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/AsyncInstaller.java Tue Jul  7 14:32:39 2015
@@ -27,6 +27,7 @@ import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
@@ -60,7 +61,29 @@ class AsyncInstaller implements Framewor
     private final AtomicInteger counter = new AtomicInteger();
     private int lastStartLevel = -1;
     
-    public static final int MAX_BUNDLES_PER_LEVEL = 13;
+    /** TinyBundles.add(...) has trouble with classloaders in our environment, so
+     *  add classes via the classpath resources mechanism. 
+     */
+    static class EmbeddedClass {
+        private final String path;
+        
+        EmbeddedClass(Class<?> c) {
+            path = c.getName().replaceAll("\\.", "/") + ".class";
+        }
+        
+        String getPath() {
+            return path; 
+        }
+        
+        InputStream getStream() {
+            final String fullPath = "/" + getPath();
+            final InputStream result = getClass().getResourceAsStream(fullPath);
+            if(result == null) {
+                throw new IllegalStateException("Null stream for " + fullPath);
+            }
+            return result;
+        }
+    }
 
     AsyncInstaller(BundleContext bc, OsgiInstaller inst, StartLevel s) {
         bundleContext = bc;
@@ -70,13 +93,20 @@ class AsyncInstaller implements Framewor
     }
     
     private InputStream getTestBundleStream(String bundleSymbolicName) throws Exception {
+        final EmbeddedClass activator = new EmbeddedClass(SlowActivator.class);
+        final EmbeddedClass waitFor = new EmbeddedClass(WaitFor.class);
+        final EmbeddedClass report = new EmbeddedClass(StartLevelReport.class);
         return TinyBundles.bundle()
             .set(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName)
+            .set(Constants.BUNDLE_ACTIVATOR, SlowActivator.class.getName())
+            .add(activator.getPath(), activator.getStream())
+            .add(waitFor.getPath(), waitFor.getStream())
+            .add(report.getPath(), report.getStream())
             .build(TinyBundles.withBnd());
     }
     
     void installMoreBundles() throws Exception {
-        final int n = (int)(random.nextFloat() * MAX_BUNDLES_PER_LEVEL);
+        final int n = (int)(random.nextFloat() * P.MAX_BUNDLES_PER_LEVEL);
         final int startLevel = startLevelService.getStartLevel();
         log.info("Installing {} test bundles at start level {}", n, startLevel);
         
@@ -111,6 +141,8 @@ class AsyncInstaller implements Framewor
     Collection<String> getBundleIssues(Bundle [] toCheck, boolean checkActiveState) {
         final Set<String> issues = new HashSet<String>(installedBundles);
         assertTrue("Expecting some installed bundles", issues.size() > 0);
+        
+        // Check that all our bundles are installed at the right start level, and active 
         for(Bundle b : toCheck) {
             if(issues .remove(b.getSymbolicName()) && checkActiveState) {
                 if(b.getState() != Bundle.ACTIVE) {
@@ -119,11 +151,27 @@ class AsyncInstaller implements Framewor
                 final int expected = startLevelFromBsn(b.getSymbolicName());
                 final int level = startLevelService.getBundleStartLevel(b);
                 if(level != expected) {
-                    issues.add(b.getSymbolicName() + ": expecting start level " + expected + ", got " + level);
+                    issues.add(b.getSymbolicName() + ": expecting installed start level " + expected + ", got " + level);
                 }
             }
         }
-        return issues ;
+        
+        return issues;
+    }
+    
+    /** Check that the test bundle activators finished at the right start level */
+    Collection<String> getActivatorIssues(StartLevelReport reports) {
+        final Map<String, Integer> levels = reports.getBsnToStartLevelMap();
+        final Set<String> issues = new HashSet<String>(installedBundles);
+        assertTrue("Expecting some activator reports", levels.size() > 0);
+        for(String bsn : levels.keySet()) {
+            final int expected = startLevelFromBsn(bsn);
+            final int actual = levels.get(bsn);
+            if(expected != actual) {
+                issues.add(bsn + ": expected activation start level " + expected + ", got " + actual);
+            }
+        }
+        return issues;
     }
     
     public int getLastStartLevel() {
@@ -134,6 +182,7 @@ class AsyncInstaller implements Framewor
     public void frameworkEvent(FrameworkEvent event) {
         if(event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) {
             final int level = startLevelService.getStartLevel();
+            log.info("Start level is now {}", level);
             try {
                 // Simulate some resource discovery latency 
                 WaitFor.randomWait(56);

Modified: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java?rev=1689692&r1=1689691&r2=1689692&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java (original)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.java Tue Jul  7 14:32:39 2015
@@ -28,6 +28,7 @@ import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.junit.PaxExam;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.startlevel.StartLevel;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -38,6 +39,8 @@ public class IncrementalStartupIT {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
     public static final int DEFAULT_TIMEOUT = 5;
+    private StartLevelReport startLevelReport;
+    private ServiceRegistration startLevelReportReg;
     
     @Inject
     protected BundleContext bundleContext;
@@ -68,32 +71,62 @@ public class IncrementalStartupIT {
         
     }
     
+    private void setupReports() {
+        startLevelReport = new StartLevelReportImpl(startLevel);
+        startLevelReportReg = bundleContext.registerService(StartLevelReport.class.getName(), startLevelReport, null);
+    }
+    
+    private void cleanup() {
+        if(startLevelReportReg != null) {
+            startLevelReportReg.unregister();
+            startLevelReportReg = null;
+        }
+    }
+    
     @Test
     public void allBundlesActive() throws Exception {
-        final AsyncInstaller ai = new AsyncInstaller(bundleContext, installer, startLevel);
-        ai.installMoreBundles();
-        
-        final int from = startLevel.getStartLevel();
-        final int to = from + 10;
-        for(int i=from; i <= to; i++) {
-            setStartLevel(i);
-        }
-        
-        new WaitFor(DEFAULT_TIMEOUT) {
-            protected boolean condition() {
-                return ai.getLastStartLevel() == to;
+        setupReports();
+        try {
+            final AsyncInstaller ai = new AsyncInstaller(bundleContext, installer, startLevel);
+            ai.installMoreBundles();
+            
+            final int from = startLevel.getStartLevel();
+            final int to = from + P.START_LEVEL_CHANGE;
+            for(int i=from; i <= to; i++) {
+                setStartLevel(i);
             }
-        };
-        
-        new WaitFor(DEFAULT_TIMEOUT) {
-            protected boolean condition() {
-                final Collection<String> issues = ai.getBundleIssues(bundleContext.getBundles(), true);
-                if(issues.isEmpty()) {
-                    return true;
+            
+            new WaitFor(DEFAULT_TIMEOUT) {
+                protected boolean condition() {
+                    return ai.getLastStartLevel() == to;
                 }
-                setInfo(issues.toString());
-                return false;
-            }
-        };
+            };
+            
+            new WaitFor(DEFAULT_TIMEOUT) {
+                protected boolean condition() {
+                    final Collection<String> issues = ai.getBundleIssues(bundleContext.getBundles(), true);
+                    if(issues.isEmpty()) {
+                        return true;
+                    }
+                    setInfo("Bundle issues: " + issues.toString());
+                    return false;
+                }
+            };
+
+            // No need for a WaitFor here, if the previous test passes all bundles
+            // are started
+            new WaitFor(DEFAULT_TIMEOUT) {
+                protected boolean condition() {
+                    final Collection<String> issues = ai.getActivatorIssues(startLevelReport);
+                    if(issues.isEmpty()) {
+                        return true;
+                    }
+                    setInfo("Activator issues: " + issues.toString());
+                    return false;
+                }
+            };
+        } finally {
+            cleanup();
+        }
     }
 }
\ No newline at end of file

Modified: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java?rev=1689692&r1=1689691&r2=1689692&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java (original)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/P.java Tue Jul  7 14:32:39 2015
@@ -28,8 +28,12 @@ import java.io.File;
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.options.DefaultCompositeOption;
 
-/** Utilities for Pax Exam testing */
+/** Test parameters and configuration */
 public class P {
+    
+    public static final int START_LEVEL_CHANGE = 3; 
+    public static final int MAX_BUNDLES_PER_LEVEL = 3;
+    
     public static Option[] paxConfig() {
         final File thisProjectsBundle = new File(System.getProperty( "bundle.file.name", "BUNDLE_FILE_NOT_SET" ));
         return new DefaultCompositeOption(

Added: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/SlowActivator.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/SlowActivator.java?rev=1689692&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/SlowActivator.java (added)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/SlowActivator.java Tue Jul  7 14:32:39 2015
@@ -0,0 +1,59 @@
+/*
+ * 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.launchpad.it.startup;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Asynchronously installs test bundles when start levels change,
+ *  to verify the Sling incremental startup mechanism. 
+ */
+public class SlowActivator implements BundleActivator {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    public static final int MAX_WAIT_MSEC = 23;
+    
+    @Override
+    public void start(BundleContext context) throws Exception {
+        final String bsn = context.getBundle().getSymbolicName();
+        final long msec = WaitFor.randomLong(MAX_WAIT_MSEC);
+        log.info("Activating with a delay of {} msec: {}", msec, bsn);
+        WaitFor.sleep(msec);
+
+        /* does not work yet
+        final ServiceReference ref = context.getServiceReference(StartLevelReport.class.getName());
+        if(ref == null) {
+            throw new IllegalStateException("StartLevelReport service not found - already gone?");
+        }
+        final StartLevelReport slr = (StartLevelReport)context.getService(ref);
+        try {
+            slr.activationDone(bsn);
+        } finally {
+            context.ungetService(ref);
+        }
+        */
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+    }
+}
\ No newline at end of file

Added: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReport.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReport.java?rev=1689692&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReport.java (added)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReport.java Tue Jul  7 14:32:39 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.launchpad.it.startup;
+
+import java.util.Map;
+
+/** Records when each activator finishes, in terms of start levels.
+ *  The goal of the incremental startup is that all bundles are activated
+ *  before switching to the next start level, even if they are installed
+ *  asynchronously via the OSGi installer. 
+ */
+public interface StartLevelReport {
+    void activationDone(String bsn); 
+    Map<String, Integer> getBsnToStartLevelMap();
+}
\ No newline at end of file

Added: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReportImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReportImpl.java?rev=1689692&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReportImpl.java (added)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/StartLevelReportImpl.java Tue Jul  7 14:32:39 2015
@@ -0,0 +1,47 @@
+/*
+ * 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.launchpad.it.startup;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.service.startlevel.StartLevel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class StartLevelReportImpl implements StartLevelReport {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final Map<String, Integer> bsnToStartLevel = new HashMap<String, Integer>();
+    private final StartLevel startLevelService;
+    
+    StartLevelReportImpl(StartLevel startLevelService) {
+        this.startLevelService = startLevelService;
+    }
+    
+    public void activationDone(String bsn) {
+        final int level = startLevelService.getStartLevel();
+        log.info("Activation done: {} at start level {}", bsn, level);
+        bsnToStartLevel.put(bsn, level);
+    }
+    
+    public Map<String, Integer> getBsnToStartLevelMap() {
+        return Collections.unmodifiableMap(bsnToStartLevel);
+    }
+}
\ No newline at end of file

Modified: sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java?rev=1689692&r1=1689691&r2=1689692&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java (original)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java Tue Jul  7 14:32:39 2015
@@ -52,7 +52,11 @@ public abstract class WaitFor {
         }
     }
     
+    public static long randomLong(long upTo) {
+        return (long)(random.nextFloat() * upTo);
+    }
+    
     public static void randomWait(long msec) {
-        sleep((long)(random.nextFloat() * msec));
+        sleep(randomLong(msec));
     }
 }
\ No newline at end of file