Bug 563372 - Be sure to handle async update properly

testBug258209_1 has a timing issue because the Framework.update
operation is async.  This allows the test to try to waitForStop but the
framework may have already be re-activated. Update this test to do what
other tests that update the framework do by calling waitForStop in
another thread before invoking Framework.update.

Change-Id: Iee4de68f05b7cb151be2f61fcad353a625eb455c
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/AbstractBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/AbstractBundleTests.java
index ab849d1..75b74e3 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/AbstractBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/AbstractBundleTests.java
@@ -308,6 +308,44 @@
 		return stop(equinox, true, 10000);
 	}
 
+	static public FrameworkEvent update(final Framework equinox) {
+		final Exception[] failureException = new BundleException[1];
+		final FrameworkEvent[] success = new FrameworkEvent[] { null };
+		final String uuid = getUUID(equinox);
+		Thread waitForUpdate = new Thread(new Runnable() {
+			@Override
+			public void run() {
+				success[0] = waitForStop(equinox, uuid, false, 10000);
+			}
+		}, "test waitForStop thread"); //$NON-NLS-1$
+		waitForUpdate.start();
+
+		try {
+			// delay hack to allow waitForUpdate thread to block on waitForStop before we
+			// update.
+			Thread.sleep(200);
+		} catch (InterruptedException e) {
+			Thread.currentThread().interrupt();
+			fail("unexpected interuption", e);
+		}
+
+		try {
+			equinox.update();
+		} catch (BundleException e) {
+			fail("Failed to update the framework", e); //$NON-NLS-1$
+		}
+		try {
+			waitForUpdate.join();
+		} catch (InterruptedException e) {
+			Thread.currentThread().interrupt();
+			fail("unexpected interuption", e); //$NON-NLS-1$
+		}
+		if (failureException[0] != null) {
+			fail("Error occurred while waiting", failureException[0]); //$NON-NLS-1$
+		}
+		return success[0];
+	}
+
 	static public FrameworkEvent stop(Framework equinox, boolean quietly, long timeout) {
 		if (equinox == null)
 			return null;
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
index 83b9f34..b67f9f6 100755
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
@@ -549,39 +549,9 @@
 			fail("Failed to start the framework", e); //$NON-NLS-1$
 		}
 		assertEquals("Wrong state for SystemBundle", Bundle.ACTIVE, equinox.getState()); //$NON-NLS-1$
-		final Exception[] failureException = new BundleException[1];
-		final FrameworkEvent[] success = new FrameworkEvent[] {null};
-		Thread t = new Thread(new Runnable() {
-			@Override
-			public void run() {
-				try {
-					success[0] = equinox.waitForStop(10000);
-				} catch (InterruptedException e) {
-					failureException[0] = e;
-				}
-			}
-		}, "test waitForStop thread"); //$NON-NLS-1$
-		t.start();
-		try {
-			// delay hack to allow t thread to block on waitForStop before we update.
-			Thread.sleep(500);
-		} catch (InterruptedException e) {
-			fail("unexpected interuption", e);
-		}
-		try {
-			equinox.update();
-		} catch (BundleException e) {
-			fail("Failed to update the framework", e); //$NON-NLS-1$
-		}
-		try {
-			t.join();
-		} catch (InterruptedException e) {
-			fail("unexpected interuption", e); //$NON-NLS-1$
-		}
-		if (failureException[0] != null)
-			fail("Error occurred while waiting", failureException[0]); //$NON-NLS-1$
-		assertNotNull("Wait for stop event is null", success[0]); //$NON-NLS-1$
-		assertEquals("Wait for stop event type is wrong", FrameworkEvent.STOPPED_UPDATE, success[0].getType()); //$NON-NLS-1$
+
+		FrameworkEvent success = update(equinox);
+		assertEquals("Wait for stop event type is wrong", FrameworkEvent.STOPPED_UPDATE, success.getType()); //$NON-NLS-1$
 		// TODO delay hack to allow the framework to get started again
 		for (int i = 0; i < 5 && Bundle.ACTIVE != equinox.getState(); i++)
 			try {
@@ -939,37 +909,7 @@
 
 		openAllBundleFiles(equinox.getBundleContext());
 
-		final Exception[] failureException = new BundleException[1];
-		final FrameworkEvent[] success = new FrameworkEvent[] {null};
-		Thread waitForUpdate = new Thread(new Runnable() {
-			@Override
-			public void run() {
-				try {
-					success[0] = equinox.waitForStop(10000);
-				} catch (InterruptedException e) {
-					failureException[0] = e;
-				}
-			}
-		}, "test waitForStop thread"); //$NON-NLS-1$
-		waitForUpdate.start();
-		try {
-			// delay hack to allow waitForUpdate thread to block on waitForStop before we update.
-			Thread.sleep(100);
-		} catch (InterruptedException e) {
-			fail("unexpected interuption", e);
-		}
-		try {
-			equinox.update();
-		} catch (BundleException e) {
-			fail("Failed to update the framework", e); //$NON-NLS-1$
-		}
-		try {
-			waitForUpdate.join();
-		} catch (InterruptedException e) {
-			fail("unexpected interuption", e); //$NON-NLS-1$
-		}
-		if (failureException[0] != null)
-			fail("Error occurred while waiting", failureException[0]); //$NON-NLS-1$
+		update(equinox);
 
 		// we can either have a hack here that waits until the system bundle is active
 		// or we can just try to start it and race with the update() call above
@@ -1623,14 +1563,7 @@
 		equinox.start();
 		assertEquals("Unexpected state", Bundle.ACTIVE, testTCCL.getState()); //$NON-NLS-1$
 
-		String uuid = getUUID(equinox);
-		// test that the correct tccl is used for framework update
-		try {
-			equinox.update();
-		} catch (Exception e) {
-			fail("Unexpected exception", e); //$NON-NLS-1$
-		}
-		waitForStop(equinox, uuid, false, 10000);
+		update(equinox);
 
 		checkActive(testTCCL);