From 93e1c00534b48a8de8d525ec1ceac9574d906787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Fri, 31 May 2024 17:36:45 +0200 Subject: [PATCH] try to get OpenFromClipboardTests.testMethod_11 stable * use concurrent data structure for inter-thread communication * fix wait for charsetsjobs instead of exclude for waiting for them * use monotonic nantotime https://github.com/eclipse-jdt/eclipse.jdt.debug/issues/442 --- .../org/eclipse/jdt/debug/tests/TestUtil.java | 24 +++++++++++-------- .../tests/ui/OpenFromClipboardTests.java | 8 +++---- 2 files changed, 18 insertions(+), 14 deletions(-) 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..2c16315c91 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 @@ -133,12 +133,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(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,17 +151,17 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, } } while (!Job.getJobManager().isIdle()) { + List jobs = getRunningOrWaitingJobs(jobFamily, excludedFamilies); + if (jobs.isEmpty()) { + // only uninteresting jobs running + break; + } runEventLoop(); try { Thread.sleep(10); } catch (InterruptedException e) { // Uninterruptable } - List jobs = getRunningOrWaitingJobs(null, 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 @@ -165,11 +169,11 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs, return true; } - if (System.currentTimeMillis() - start >= maxTimeMs) { + if (System.nanoTime() - start >= maxTimeMs * 1_000_000) { dumpRunningOrWaitingJobs(owner, jobs); return true; } - wakeUpSleepingJobs(null); + wakeUpSleepingJobs(jobFamily); } runningJobs.clear(); return false; 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; }