Bug 511607: Fix race condition in LaunchGroupTests

LaunchGroup's .launch blocks until the awaited output is written. There
is a chance that the thread is unable to set the 'finished' boolean to
true before the test thread checks it. This can easily be reproduced
with a breakpoint at 'finished.set' in the second thread.

Change-Id: Iee056180a25e8078a6479bfef09de0359c78d298
Signed-off-by: Markus Duft <markus.duft@ssi-schaefer.com>
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java
index c17d130..8799b6f 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java
@@ -273,8 +273,10 @@
 				try {
 					// wait some time before causing the group to continue
 					Thread.sleep(2000);
-					attachListener.getStream().write("TestOutput"); //$NON-NLS-1$
-					finished.set(true);
+					synchronized (finished) {
+						attachListener.getStream().write("TestOutput"); //$NON-NLS-1$
+						finished.set(true);
+					}
 				} catch (Exception e) {
 					e.printStackTrace();
 				}
@@ -286,7 +288,12 @@
 
 		// launching the group must block until the output is produced
 		grp.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor());
-		getLaunchManager().removeLaunchListener(attachListener);
+
+		synchronized (finished) {
+			// if the output appeared we have to wait for the thread to finish
+			// setting state.
+			getLaunchManager().removeLaunchListener(attachListener);
+		}
 
 		assertTrue("thread did not finish", finished.get()); //$NON-NLS-1$
 		assertTrue("output was not awaited", (System.currentTimeMillis() - start) >= 2000); //$NON-NLS-1$