Bug 516348 - [tests] testSuspendLongRunningCondition workaround

Fix test by increasing wait time before suspend and by moving sleep()
call into the for loop in the evaluation loop. Additionally make sure
that ASTEvaluationEngine.EvalRunnable.run() calls
evaluationFinished(IEvaluationResult) also on "unexpected" errors.

This is the point 1 and 3 from bug 516348 comment 8.

Change-Id: Ic9aa0662a983be617b139c1498fe151fcaeaab29
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java
index dd843de..3281e6d 100755
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/breakpoints/ConditionalBreakpointsTests.java
@@ -195,7 +195,7 @@
 	public void testSuspendLongRunningCondition() throws Exception {
 		String typeName = "MethodLoop";
 		IJavaLineBreakpoint first = createLineBreakpoint(19, typeName);
-		createConditionalLineBreakpoint(29, typeName, "for (int x = 0; x < 1000; x++) { System.out.println(x);} Thread.sleep(200); return true;", true);
+		createConditionalLineBreakpoint(29, typeName, "for (int x = 0; x < 1000; x++) { System.out.println(x); Thread.sleep(30); } return true;", true);
 
 		IJavaThread thread= null;
 		try {
@@ -203,7 +203,7 @@
 			IStackFrame top = thread.getTopStackFrame();
 			assertNotNull("Missing top frame", top);
 			thread.resume();
-			Thread.sleep(100);
+			Thread.sleep(3000);
 			thread.suspend();
 			assertTrue("Thread should be suspended", thread.isSuspended());
 			IJavaStackFrame frame = (IJavaStackFrame) thread.getTopStackFrame();
diff --git a/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java b/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java
index 4c1cae6..8198fa0 100644
--- a/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java
+++ b/org.eclipse.jdt.debug/eval/org/eclipse/jdt/internal/debug/eval/ast/engine/ASTEvaluationEngine.java
@@ -750,58 +750,66 @@
 
 			EvaluationRunnable er = new EvaluationRunnable();
 			CoreException exception = null;
-			long start = System.currentTimeMillis();
 			try {
-				fThread.runEvaluation(er, null, fEvaluationDetail,
-						fHitBreakpoints);
-			} catch (DebugException e) {
-				exception = e;
-			}
-			long end = System.currentTimeMillis();
-
-			IJavaValue value = interpreter.getResult();
-
-			if (exception == null) {
-				exception = er.getException();
-			}
-
-			result.setTerminated(er.fTerminated);
-			if (exception != null) {
-				if (JDIDebugOptions.DEBUG_AST_EVAL) {
-					StringBuffer buf = new StringBuffer();
-					buf.append("\tException: "); //$NON-NLS-1$
-					buf.append(exception.toString());
-					JDIDebugOptions.trace(buf.toString());
+				long start = System.currentTimeMillis();
+				try {
+					fThread.runEvaluation(er, null, fEvaluationDetail,
+							fHitBreakpoints);
+				} catch (DebugException e) {
+					exception = e;
+				} catch (Exception e) {
+					IStatus status = new Status(IStatus.ERROR,
+							JDIDebugPlugin.getUniqueIdentifier(),
+							JDIDebugPlugin.ERROR,
+							EvaluationEngineMessages.ASTEvaluationEngine_An_unknown_error_occurred_during_evaluation, e);
+					exception = new DebugException(status);
 				}
-				if (exception instanceof DebugException) {
-					result.setException((DebugException) exception);
-				} else {
-					result.setException(new DebugException(exception
-							.getStatus()));
+				long end = System.currentTimeMillis();
+
+				IJavaValue value = interpreter.getResult();
+
+				if (exception == null) {
+					exception = er.getException();
 				}
-			} else {
-				if (value != null) {
-					result.setValue(value);
+
+				result.setTerminated(er.fTerminated);
+				if (exception != null) {
 					if (JDIDebugOptions.DEBUG_AST_EVAL) {
 						StringBuffer buf = new StringBuffer();
-						buf.append("\tResult: "); //$NON-NLS-1$
-						buf.append(value);
+						buf.append("\tException: "); //$NON-NLS-1$
+						buf.append(exception.toString());
 						JDIDebugOptions.trace(buf.toString());
 					}
+					if (exception instanceof DebugException) {
+						result.setException((DebugException) exception);
+					} else {
+						result.setException(new DebugException(exception
+								.getStatus()));
+					}
 				} else {
-					result.addError(EvaluationEngineMessages.ASTEvaluationEngine_An_unknown_error_occurred_during_evaluation);
+					if (value != null) {
+						result.setValue(value);
+						if (JDIDebugOptions.DEBUG_AST_EVAL) {
+							StringBuffer buf = new StringBuffer();
+							buf.append("\tResult: "); //$NON-NLS-1$
+							buf.append(value);
+							JDIDebugOptions.trace(buf.toString());
+						}
+					} else {
+						result.addError(EvaluationEngineMessages.ASTEvaluationEngine_An_unknown_error_occurred_during_evaluation);
+					}
 				}
-			}
 
-			if (JDIDebugOptions.DEBUG_AST_EVAL) {
-				StringBuffer buf = new StringBuffer();
-				buf.append("\tDuration: "); //$NON-NLS-1$
-				buf.append(end - start);
-				buf.append("ms"); //$NON-NLS-1$
-				JDIDebugOptions.trace(buf.toString());
+				if (JDIDebugOptions.DEBUG_AST_EVAL) {
+					StringBuffer buf = new StringBuffer();
+					buf.append("\tDuration: "); //$NON-NLS-1$
+					buf.append(end - start);
+					buf.append("ms"); //$NON-NLS-1$
+					JDIDebugOptions.trace(buf.toString());
+				}
+			} finally {
+				evaluationFinished(result);
 			}
-
-			evaluationFinished(result);
 		}
 
 		private void evaluationFinished(IEvaluationResult result) {