FELIX-3927 - Make tests more reliable on KF
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1454741 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-creation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java b/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-creation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
index 509721c..c10b973 100644
--- a/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-creation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
+++ b/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-creation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
@@ -18,8 +18,10 @@
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.ops4j.pax.tinybundles.core.TinyBundle;
import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
import org.ow2.chameleon.testing.helpers.IPOJOHelper;
import org.ow2.chameleon.testing.helpers.OSGiHelper;
import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
@@ -113,6 +115,8 @@
}
String version = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VERSION);
System.out.println("OSGi Framework : " + vendor + " - " + version);
+
+ waitForStability(bc);
}
@After
@@ -194,5 +198,75 @@
fail("Assertion failed : " + s);
}
+ /**
+ * Waits for stability:
+ * <ul>
+ * <li>all bundles are activated
+ * <li>service count is stable
+ * </ul>
+ * If the stability can't be reached after a specified time,
+ * the method throws a {@link IllegalStateException}.
+ * @param context the bundle context
+ * @throws IllegalStateException when the stability can't be reach after a several attempts.
+ */
+ private void waitForStability(BundleContext context) throws IllegalStateException {
+ // Wait for bundle initialization.
+ boolean bundleStability = getBundleStability(context);
+ int count = 0;
+ while (!bundleStability && count < 500) {
+ try {
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ // Interrupted
+ }
+ count++;
+ bundleStability = getBundleStability(context);
+ }
+
+ if (count == 500) {
+ System.err.println("Bundle stability isn't reached after 500 tries");
+ throw new IllegalStateException("Cannot reach the bundle stability");
+ }
+
+ boolean serviceStability = false;
+ count = 0;
+ int count1 = 0;
+ int count2 = 0;
+ while (! serviceStability && count < 500) {
+ try {
+ ServiceReference[] refs = context.getServiceReferences((String) null, null);
+ count1 = refs.length;
+ Thread.sleep(500);
+ refs = context.getServiceReferences((String) null, null);
+ count2 = refs.length;
+ serviceStability = count1 == count2;
+ } catch (Exception e) {
+ System.err.println(e);
+ serviceStability = false;
+ // Nothing to do, while recheck the condition
+ }
+ count++;
+ }
+
+ if (count == 500) {
+ System.err.println("Service stability isn't reached after 500 tries (" + count1 + " != " + count2);
+ throw new IllegalStateException("Cannot reach the service stability");
+ }
+ }
+
+ /**
+ * Are bundle stables.
+ * @param bc the bundle context
+ * @return <code>true</code> if every bundles are activated.
+ */
+ private boolean getBundleStability(BundleContext bc) {
+ boolean stability = true;
+ Bundle[] bundles = bc.getBundles();
+ for (int i = 0; i < bundles.length; i++) {
+ stability = stability && (bundles[i].getState() == Bundle.ACTIVE);
+ }
+ return stability;
+ }
+
}
diff --git a/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-metadata-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java b/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-metadata-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
index 39d6474..d4b9ec8 100644
--- a/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-metadata-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
+++ b/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-metadata-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
@@ -21,6 +21,7 @@
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
import org.ow2.chameleon.testing.helpers.IPOJOHelper;
import org.ow2.chameleon.testing.helpers.OSGiHelper;
import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
@@ -117,6 +118,8 @@
}
String version = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VERSION);
System.out.println("OSGi Framework : " + vendor + " - " + version);
+
+ waitForStability(bc);
}
@After
@@ -196,5 +199,75 @@
fail("Assertion failed : " + s);
}
+ /**
+ * Waits for stability:
+ * <ul>
+ * <li>all bundles are activated
+ * <li>service count is stable
+ * </ul>
+ * If the stability can't be reached after a specified time,
+ * the method throws a {@link IllegalStateException}.
+ * @param context the bundle context
+ * @throws IllegalStateException when the stability can't be reach after a several attempts.
+ */
+ private void waitForStability(BundleContext context) throws IllegalStateException {
+ // Wait for bundle initialization.
+ boolean bundleStability = getBundleStability(context);
+ int count = 0;
+ while (!bundleStability && count < 500) {
+ try {
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ // Interrupted
+ }
+ count++;
+ bundleStability = getBundleStability(context);
+ }
+
+ if (count == 500) {
+ System.err.println("Bundle stability isn't reached after 500 tries");
+ throw new IllegalStateException("Cannot reach the bundle stability");
+ }
+
+ boolean serviceStability = false;
+ count = 0;
+ int count1 = 0;
+ int count2 = 0;
+ while (! serviceStability && count < 500) {
+ try {
+ ServiceReference[] refs = context.getServiceReferences((String) null, null);
+ count1 = refs.length;
+ Thread.sleep(500);
+ refs = context.getServiceReferences((String) null, null);
+ count2 = refs.length;
+ serviceStability = count1 == count2;
+ } catch (Exception e) {
+ System.err.println(e);
+ serviceStability = false;
+ // Nothing to do, while recheck the condition
+ }
+ count++;
+ }
+
+ if (count == 500) {
+ System.err.println("Service stability isn't reached after 500 tries (" + count1 + " != " + count2);
+ throw new IllegalStateException("Cannot reach the service stability");
+ }
+ }
+
+ /**
+ * Are bundle stables.
+ * @param bc the bundle context
+ * @return <code>true</code> if every bundles are activated.
+ */
+ private boolean getBundleStability(BundleContext bc) {
+ boolean stability = true;
+ Bundle[] bundles = bc.getBundles();
+ for (int i = 0; i < bundles.length; i++) {
+ stability = stability && (bundles[i].getState() == Bundle.ACTIVE);
+ }
+ return stability;
+ }
+
}
diff --git a/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java b/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
index 509721c..c10b973 100644
--- a/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
+++ b/ipojo/manipulator/manipulator-it/src/it/ipojo-manipulator-manipulation-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
@@ -18,8 +18,10 @@
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.ops4j.pax.tinybundles.core.TinyBundle;
import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
import org.ow2.chameleon.testing.helpers.IPOJOHelper;
import org.ow2.chameleon.testing.helpers.OSGiHelper;
import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
@@ -113,6 +115,8 @@
}
String version = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VERSION);
System.out.println("OSGi Framework : " + vendor + " - " + version);
+
+ waitForStability(bc);
}
@After
@@ -194,5 +198,75 @@
fail("Assertion failed : " + s);
}
+ /**
+ * Waits for stability:
+ * <ul>
+ * <li>all bundles are activated
+ * <li>service count is stable
+ * </ul>
+ * If the stability can't be reached after a specified time,
+ * the method throws a {@link IllegalStateException}.
+ * @param context the bundle context
+ * @throws IllegalStateException when the stability can't be reach after a several attempts.
+ */
+ private void waitForStability(BundleContext context) throws IllegalStateException {
+ // Wait for bundle initialization.
+ boolean bundleStability = getBundleStability(context);
+ int count = 0;
+ while (!bundleStability && count < 500) {
+ try {
+ Thread.sleep(5);
+ } catch (InterruptedException e) {
+ // Interrupted
+ }
+ count++;
+ bundleStability = getBundleStability(context);
+ }
+
+ if (count == 500) {
+ System.err.println("Bundle stability isn't reached after 500 tries");
+ throw new IllegalStateException("Cannot reach the bundle stability");
+ }
+
+ boolean serviceStability = false;
+ count = 0;
+ int count1 = 0;
+ int count2 = 0;
+ while (! serviceStability && count < 500) {
+ try {
+ ServiceReference[] refs = context.getServiceReferences((String) null, null);
+ count1 = refs.length;
+ Thread.sleep(500);
+ refs = context.getServiceReferences((String) null, null);
+ count2 = refs.length;
+ serviceStability = count1 == count2;
+ } catch (Exception e) {
+ System.err.println(e);
+ serviceStability = false;
+ // Nothing to do, while recheck the condition
+ }
+ count++;
+ }
+
+ if (count == 500) {
+ System.err.println("Service stability isn't reached after 500 tries (" + count1 + " != " + count2);
+ throw new IllegalStateException("Cannot reach the service stability");
+ }
+ }
+
+ /**
+ * Are bundle stables.
+ * @param bc the bundle context
+ * @return <code>true</code> if every bundles are activated.
+ */
+ private boolean getBundleStability(BundleContext bc) {
+ boolean stability = true;
+ Bundle[] bundles = bc.getBundles();
+ for (int i = 0; i < bundles.length; i++) {
+ stability = stability && (bundles[i].getState() == Bundle.ACTIVE);
+ }
+ return stability;
+ }
+
}