Bug 574883 - wait a bit longer before teardown to avoid test errors

If the test fails (it will fail randomly) and we catch that, scheduled
jobs are still running and trigger monitor access like in stack below:

java.util.ConcurrentModificationException
at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1043)
at java.base/java.util.ArrayList$Itr.next(ArrayList.java:997)
at org.eclipse.core.tests.runtime.jobs.FussyProgressProvider.sanityCheck(FussyProgressProvider.java:41)
at org.eclipse.core.tests.runtime.jobs.AbstractJobManagerTest.tearDown(AbstractJobManagerTest.java:47)

Change-Id: I83308a48cfae76c11c718d73a0511ce634ffa0e2
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.runtime/+/183169
Tested-by: Platform Bot <platform-bot@eclipse.org>
diff --git a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/Bug_574883.java b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/Bug_574883.java
index 72e1938..f65cad4 100644
--- a/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/Bug_574883.java
+++ b/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/Bug_574883.java
@@ -54,11 +54,11 @@
 		protected IStatus run(IProgressMonitor monitor) {
 			Runnable action = queue.remove(0);
 			try {
-				if (action != null) {
+				if (action != null && !monitor.isCanceled()) {
 					action.run();
 				}
 			} finally {
-				if (!queue.isEmpty()) {
+				if (!queue.isEmpty() && !monitor.isCanceled()) {
 					// this call confuses JobManager and causes bug 574883 if the action above
 					// runs *too fast*
 					schedule();
@@ -108,6 +108,9 @@
 			// TODO: print the fail only, as long as bug 574883 is not fixed yet
 			t.printStackTrace(System.out);
 			t.printStackTrace(System.err);
+			Job.getJobManager().cancel(this);
+			Thread.sleep(1000);
+			Job.getJobManager().join(this, null);
 		}
 	}
 
@@ -140,6 +143,9 @@
 			// TODO: print the fail only, as long as bug 574883 is not fixed yet
 			t.printStackTrace(System.out);
 			t.printStackTrace(System.err);
+			Job.getJobManager().cancel(this);
+			Thread.sleep(1000);
+			Job.getJobManager().join(this, null);
 		}
 	}
 
@@ -182,6 +188,9 @@
 			// TODO: print the fail only, as long as bug 574883 is not fixed yet
 			t.printStackTrace(System.out);
 			t.printStackTrace(System.err);
+			Job.getJobManager().cancel(this);
+			Thread.sleep(1000);
+			Job.getJobManager().join(this, null);
 		}
 	}