Skip to content

Commit

Permalink
try to get OpenFromClipboardTests.testMethod_11 stable
Browse files Browse the repository at this point in the history
* use concurrent data structure for inter-thread communication
* fix wait for charsetsjobs instead of exclude for waiting for them
* use monotonic nantotime

eclipse-jdt#442
  • Loading branch information
EcljpseB0T committed Jun 5, 2024
1 parent d0be170 commit 93e1c00
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -147,29 +151,29 @@ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs,
}
}
while (!Job.getJobManager().isIdle()) {
List<Job> jobs = getRunningOrWaitingJobs(jobFamily, excludedFamilies);
if (jobs.isEmpty()) {
// only uninteresting jobs running
break;
}
runEventLoop();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// Uninterruptable
}
List<Job> 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
dumpRunningOrWaitingJobs(owner, jobs);
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Object> matches = new ArrayList<>();
final List<Object> matches = new CopyOnWriteArrayList<>();
Display.getDefault().syncCall(() -> OpenFromClipboardAction.getJavaElementMatches(textData, matches));
return matches;
}
Expand Down

0 comments on commit 93e1c00

Please sign in to comment.