From 57bcdfca1de248b39e857d3e08021b00c7a3c80f Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Mon, 5 Aug 2024 16:31:05 +0200 Subject: [PATCH] Wait for FAMILY_SNAPSHOT jobs after creating projects/teardown org.eclipse.core.internal.resources.DelayedSnapshotJob.run(IProgressMonitor) is triggered both periodically and on few special cases. Special cases are project deletion and creation, options change... This job may create change events coming to JDT from non-UI thread, see org.eclipse.jdt.internal.core.DeltaProcessor.resourceChanged(IResourceChangeEvent). To avoid sporadic test fails we should also wait for that job. Note: we can't unconditionally call waitForSnapShot(), it should be always done outside of the code that holds workspace rule, otherwise we will run in deadlocks with other jobs... See https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2716 --- .../tests/model/AbstractJavaModelTests.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java index 0c3051832fd..a1e34b692b9 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/AbstractJavaModelTests.java @@ -4107,6 +4107,7 @@ public void waitForManualRefresh() { try { Job.getJobManager().wakeUp(ResourcesPlugin.FAMILY_MANUAL_REFRESH); Job.getJobManager().join(ResourcesPlugin.FAMILY_MANUAL_REFRESH, null); + waitForSnapShot(); JavaModelManager.getIndexManager().waitForIndex(isIndexDisabledForTest(), null); wasInterrupted = false; } catch (OperationCanceledException e) { @@ -4127,6 +4128,27 @@ public static boolean isTouchJobRunning() { return false; } + public void waitForSnapShot() { + if (isWorkspaceRuleAlreadyInUse(getWorkspaceRoot())){ + // Don't wait holding workspace lock on FAMILY_SNAPSHOT, because + // we might deadlock with DelayedSnapshotJob + System.out.println("\n\nAborted waitForSnapShot() because running with the workspace rule\n\n"); + return; + } + boolean wasInterrupted = false; + do { + try { + Job.getJobManager().wakeUp(ResourcesPlugin.FAMILY_SNAPSHOT); + Job.getJobManager().join(ResourcesPlugin.FAMILY_SNAPSHOT, null); + wasInterrupted = false; + } catch (OperationCanceledException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + wasInterrupted = true; + } + } while (wasInterrupted); + } + public void waitUntilIndexesReady() { // dummy query for waiting until the indexes are ready SearchEngine engine = new SearchEngine();