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/06 16:48:58 UTC
svn commit: r1689419 - in
/sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup:
AsyncInstaller.java IncrementalStartupIT.java WaitFor.java
Author: bdelacretaz
Date: Mon Jul 6 14:48:58 2015
New Revision: 1689419
URL: http://svn.apache.org/r1689419
Log:
SLING-4851 - test with bundles at various start levels, but no slow Activators so far
Added:
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
sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/IncrementalStartupIT.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=1689419&r1=1689418&r2=1689419&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 Mon Jul 6 14:48:58 2015
@@ -21,8 +21,12 @@ package org.apache.sling.launchpad.it.st
import static org.junit.Assert.assertTrue;
import java.io.InputStream;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Dictionary;
import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
@@ -50,11 +54,13 @@ class AsyncInstaller implements Framewor
private final StartLevel startLevelService;
private final OsgiInstaller installer;
private final Set<String> installedBundles = new HashSet<String>();
+ private final List<InstallableResource> resources = new ArrayList<InstallableResource>();
private final String bundleNamePrefix = "AsyncInstaller-" + UUID.randomUUID().toString() + ".";
private final Random random = new Random(42);
private final AtomicInteger counter = new AtomicInteger();
+ private int lastStartLevel = -1;
- public static final int MAX_BUNDLES_PER_LEVEL = 7;
+ public static final int MAX_BUNDLES_PER_LEVEL = 13;
AsyncInstaller(BundleContext bc, OsgiInstaller inst, StartLevel s) {
bundleContext = bc;
@@ -69,26 +75,39 @@ class AsyncInstaller implements Framewor
.build(TinyBundles.withBnd());
}
- void installBundles() throws Exception {
+ void installMoreBundles() throws Exception {
final int n = (int)(random.nextFloat() * MAX_BUNDLES_PER_LEVEL);
final int startLevel = startLevelService.getStartLevel();
log.info("Installing {} test bundles at start level {}", n, startLevel);
- final InstallableResource [] toInstall = new InstallableResource[n];
for(int i=0; i < n; i++) {
- final String bsn = bundleNamePrefix + counter.incrementAndGet() + "." + startLevel;
+ final String bsn = bundleNamePrefix + startLevel + "." + counter.incrementAndGet();
final InputStream is = getTestBundleStream(bsn);
- toInstall[i] = new InstallableResource(bsn, is, null, bsn, "bundle", 100);
+ final Dictionary<String, Object> dict = new Hashtable<String, Object>();
+ dict.put(InstallableResource.BUNDLE_START_LEVEL, startLevel);
+ resources.add(new InstallableResource(bsn, is, dict, bsn, "bundle", 100));
installedBundles.add(bsn);
}
- installer.registerResources(getClass().getSimpleName(), toInstall);
+ // Simulate some resource discovery latency
+ WaitFor.randomWait(156);
+ installer.registerResources(getClass().getSimpleName(), resources.toArray(new InstallableResource[]{}));
+ log.info("A total of {} InstallableResources are now registered", resources.size());
}
boolean isTestBundle(Bundle b) {
return b.getSymbolicName().startsWith(bundleNamePrefix);
}
+ private int startLevelFromBsn(String bsn) {
+ int result = -1;
+ final String [] parts = bsn.split("\\.");
+ if(parts.length == 3) {
+ result = Integer.valueOf(parts[1]);
+ }
+ return result;
+ }
+
Collection<String> getBundleIssues(Bundle [] toCheck, boolean checkActiveState) {
final Set<String> issues = new HashSet<String>(installedBundles);
assertTrue("Expecting some installed bundles", issues.size() > 0);
@@ -97,19 +116,32 @@ class AsyncInstaller implements Framewor
if(b.getState() != Bundle.ACTIVE) {
issues.add(b.getSymbolicName() + " (not active)");
}
+ 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);
+ }
}
}
return issues ;
}
+
+ public int getLastStartLevel() {
+ return lastStartLevel;
+ }
@Override
public void frameworkEvent(FrameworkEvent event) {
if(event.getType() == FrameworkEvent.STARTLEVEL_CHANGED) {
+ final int level = startLevelService.getStartLevel();
try {
- installBundles();
+ // Simulate some resource discovery latency
+ WaitFor.randomWait(56);
+ installMoreBundles();
} catch(Exception e) {
log.error("Installing bundles failed", e);
}
+ lastStartLevel = level;
}
}
}
\ No newline at end of file
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=1689419&r1=1689418&r2=1689419&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 Mon Jul 6 14:48:58 2015
@@ -18,9 +18,6 @@
*/
package org.apache.sling.launchpad.it.startup;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-
import java.util.Collection;
import javax.inject.Inject;
@@ -40,6 +37,7 @@ import org.slf4j.LoggerFactory;
public class IncrementalStartupIT {
private final Logger log = LoggerFactory.getLogger(getClass());
+ public static final int DEFAULT_TIMEOUT = 5;
@Inject
protected BundleContext bundleContext;
@@ -55,36 +53,47 @@ public class IncrementalStartupIT {
return P.paxConfig();
}
+ /** TODO this should be driven by the start level sequencer */
+ private void setStartLevel(final int to) {
+ log.info("Changing start level to {}", to);
+ startLevel.setStartLevel(to);
+ WaitFor.randomWait(200);
+
+ new WaitFor(DEFAULT_TIMEOUT) {
+ protected boolean condition() {
+ setInfo("Expecting start level to reach " + to);
+ return startLevel.getStartLevel() == to;
+ }
+ };
+
+ }
+
@Test
public void allBundlesActive() throws Exception {
final AsyncInstaller ai = new AsyncInstaller(bundleContext, installer, startLevel);
- ai.installBundles();
+ ai.installMoreBundles();
- final int to = startLevel.getStartLevel();
-
- /*
final int from = startLevel.getStartLevel();
final int to = from + 10;
- log.info("Changing start level from {} to {}", from, to);
- startLevel.setStartLevel(to);
- */
+ for(int i=from; i <= to; i++) {
+ setStartLevel(i);
+ }
- final long timeoutMsec = 10000;
- final long endtime = System.currentTimeMillis() + timeoutMsec;
- Collection<String> issues = null;
- while(System.currentTimeMillis() < endtime) {
- if(startLevel.getStartLevel() != to) {
- continue;
+ new WaitFor(DEFAULT_TIMEOUT) {
+ protected boolean condition() {
+ return ai.getLastStartLevel() == to;
}
- issues = ai.getBundleIssues(bundleContext.getBundles(), true);
- if(issues.isEmpty()) {
- return;
+ };
+
+ new WaitFor(DEFAULT_TIMEOUT) {
+ protected boolean condition() {
+ final Collection<String> issues = ai.getBundleIssues(bundleContext.getBundles(), true);
+ if(issues.isEmpty()) {
+ return true;
+ }
+ setInfo(issues.toString());
+ return false;
}
- Thread.sleep(50);
- }
- assertEquals("Expecting start level change to be done", to, startLevel.getStartLevel());
- if(!issues.isEmpty()) {
- fail("Missing or inactive bundles:" + issues);
- }
+ };
}
}
\ No newline at end of file
Added: 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=1689419&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java (added)
+++ sling/whiteboard/bdelacretaz/it-startup/src/test/java/org/apache/sling/launchpad/it/startup/WaitFor.java Mon Jul 6 14:48:58 2015
@@ -0,0 +1,58 @@
+/*
+ * 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 static org.junit.Assert.fail;
+
+import java.util.Random;
+
+public abstract class WaitFor {
+
+ private String info = "NO INFO??";
+
+ private static final Random random = new Random(42);
+
+ public WaitFor(int timeoutSeconds) {
+ final long endtime = System.currentTimeMillis() + timeoutSeconds * 1000L;
+ while(System.currentTimeMillis() < endtime) {
+ if(condition()) {
+ return;
+ }
+ sleep(50);
+ }
+ fail(info);
+ }
+
+ protected void setInfo(String info) {
+ this.info = info;
+ }
+
+ protected abstract boolean condition();
+
+ public static void sleep(long msec) {
+ try {
+ Thread.sleep(msec);
+ } catch(InterruptedException ignore) {
+ }
+ }
+
+ public static void randomWait(long msec) {
+ sleep((long)(random.nextFloat() * msec));
+ }
+}
\ No newline at end of file