diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java index e34d7aae77..57d374ea33 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java @@ -23,6 +23,7 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; +import org.eclipse.core.internal.jobs.JobManager; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; @@ -133,12 +134,16 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs) * @return true if the method timed out, false if all the jobs terminated before the timeout */ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, Object... excludedFamilies) { + return waitForJobs(owner, null, minTimeMs, maxTimeMs, excludedFamilies); + } + + public static boolean waitForJobs(@SuppressWarnings("unused") String owner, Object jobFamily, long minTimeMs, long maxTimeMs, Object... excludedFamilies) { if (maxTimeMs < minTimeMs) { throw new IllegalArgumentException("Max time is smaller as min time!"); } - wakeUpSleepingJobs(null); - final long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start < minTimeMs) { + wakeUpSleepingJobs(jobFamily); + final long start = System.nanoTime(); + while (System.nanoTime() - start < minTimeMs * 1_000_000) { runEventLoop(); try { Thread.sleep(Math.min(10, minTimeMs)); @@ -147,29 +152,28 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, } } while (!Job.getJobManager().isIdle()) { - runEventLoop(); - try { - Thread.sleep(10); - } catch (InterruptedException e) { - // Uninterruptable - } - List jobs = getRunningOrWaitingJobs(null, excludedFamilies); + List jobs = getRunningOrWaitingJobs(jobFamily, excludedFamilies); if (jobs.isEmpty()) { // only uninteresting jobs running break; } - if (!Collections.disjoint(runningJobs, jobs)) { // There is a job which runs already quite some time, don't wait for it to avoid test timeouts - dumpRunningOrWaitingJobs(owner, jobs); + dumpRunningOrWaitingJobs(jobs); return true; } - if (System.currentTimeMillis() - start >= maxTimeMs) { - dumpRunningOrWaitingJobs(owner, jobs); + if (System.nanoTime() - start >= maxTimeMs * 1_000_000) { + dumpRunningOrWaitingJobs(jobs); return true; } - wakeUpSleepingJobs(null); + wakeUpSleepingJobs(jobFamily); + runEventLoop(); + try { + Thread.sleep(10); + } catch (InterruptedException e) { + // Uninterruptable + } } runningJobs.clear(); return false; @@ -184,12 +188,12 @@ private static void wakeUpSleepingJobs(Object family) { static Set runningJobs = new LinkedHashSet<>(); - private static void dumpRunningOrWaitingJobs(String owner, List jobs) { - String message = "Some job is still running or waiting to run: " + dumpRunningOrWaitingJobs(jobs); - log(IStatus.ERROR, owner, message); + private static void dumpRunningOrWaitingJobs(List jobs) { + new IllegalStateException("Some job is still running or waiting to run.").printStackTrace(); + System.err.println("JobList:" + getDump(jobs)); } - private static String dumpRunningOrWaitingJobs(List jobs) { + private static String getDump(List jobs) { if (jobs.isEmpty()) { return ""; } @@ -200,6 +204,7 @@ private static String dumpRunningOrWaitingJobs(List jobs) { runningJobs.add(job); sb.append("\n'").append(job.toString()).append("'/"); sb.append(job.getClass().getName()); + sb.append(":").append(JobManager.printState(job)); Thread thread = job.getThread(); if (thread != null) { ThreadInfo[] threadInfos = ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { thread.getId() }, true, true); @@ -212,7 +217,7 @@ private static String dumpRunningOrWaitingJobs(List jobs) { Thread thread = Display.getDefault().getThread(); ThreadInfo[] threadInfos = ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { thread.getId() }, true, true); - if (threadInfos[0] != null) { + if (!Thread.currentThread().equals(thread) && threadInfos[0] != null) { sb.append("\n").append("UI thread info: ").append(threadInfos[0]); } return sb.toString(); diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/OpenFromClipboardTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/OpenFromClipboardTests.java index 37f2537aa1..26a7dc2f2c 100644 --- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/OpenFromClipboardTests.java +++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/OpenFromClipboardTests.java @@ -15,8 +15,8 @@ import static org.junit.Assert.assertEquals; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import org.eclipse.core.internal.resources.CharsetDeltaJob; import org.eclipse.core.internal.resources.ValidateProjectEncoding; @@ -97,8 +97,8 @@ public static void tearDownClass() throws CoreException { } private static void waitForEncodingRelatedJobs() { - TestUtil.waitForJobs("OpenFromClipboardTests", 10, 5_000, ValidateProjectEncoding.class); - TestUtil.waitForJobs("OpenFromClipboardTests", 10, 5_000, CharsetDeltaJob.FAMILY_CHARSET_DELTA); + TestUtil.waitForJobs("OpenFromClipboardTests", ValidateProjectEncoding.class, 0, 5_000); + TestUtil.waitForJobs("OpenFromClipboardTests", CharsetDeltaJob.FAMILY_CHARSET_DELTA, 0, 5_000); } private static IJavaProject createProject(String name) throws CoreException { @@ -136,7 +136,7 @@ private int getMatachingPattern(String s) throws Exception { private List getJavaElementMatches(final String textData) throws Exception { JavaModelManager.getIndexManager().waitForIndex(false, null); - final List matches = new ArrayList<>(); + final List matches = new CopyOnWriteArrayList<>(); Display.getDefault().syncCall(() -> OpenFromClipboardAction.getJavaElementMatches(textData, matches)); return matches; }