Skip to content

Commit

Permalink
Disable the automatic snapshots for TestBug297635
Browse files Browse the repository at this point in the history
Disable the automatic snapshots while running tests in TestBug297635 to
avoid random failures.

Fixes eclipse-platform#460
  • Loading branch information
fedejeanne committed Aug 8, 2023
1 parent 3e3f88d commit 1b35a80
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.internal.utils.Policy;
import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

/**
Expand All @@ -28,6 +31,7 @@ public class DelayedSnapshotJob extends Job {
private static final String MSG_SNAPSHOT = Messages.resources_snapshot;
private SaveManager saveManager;
private Workspace workspace;
private volatile boolean suspended;

public DelayedSnapshotJob(SaveManager manager, Workspace workspace) {
super(MSG_SNAPSHOT);
Expand All @@ -44,7 +48,7 @@ public DelayedSnapshotJob(SaveManager manager, Workspace workspace) {
public IStatus run(IProgressMonitor monitor) {
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
if (!workspace.isOpen()) {
if (!workspace.isOpen() || suspended) {
return Status.OK_STATUS;
}
try {
Expand All @@ -56,4 +60,22 @@ public IStatus run(IProgressMonitor monitor) {
saveManager.snapshotRequested = false;
}
}

/**
* Suspend automatic snapshots until {@link #resume()} is called.
*
* @see #resume()
*/
void suspend() {
suspended = true;
}

/**
* Resume automatic snapshots. This method does not schedule this Job.
*
* @see #suspend()
*/
void resume() {
suspended = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,22 @@ public void shutdown(final IProgressMonitor monitor) {
snapshotJob.cancel();
}

/**
* Tries to determine if the workspace has changes and calls
* {@link #snapshotIfNeeded(boolean)}. If an exception occurs while trying to
* determine whether or not the workspace has changes then the snapshot might be
* taken anyway, the workspace will be simply assumed unchanged.
*/
private void snapshotIfNeeded() {
boolean workspaceHasTreeChanges = false;
try {
workspaceHasTreeChanges = workspace.hasTreeChanges();
} catch (CoreException e) {
Policy.log(e);
}
snapshotIfNeeded(workspaceHasTreeChanges);
}

/**
* Performs a snapshot if one is deemed necessary.
* Encapsulates rules for determining when a snapshot is needed.
Expand Down Expand Up @@ -2205,4 +2221,13 @@ protected void writeWorkspaceFields(DataOutputStream output, IProgressMonitor mo
// save the registered sync partners in the synchronizer
((Synchronizer) workspace.getSynchronizer()).savePartners(output);
}

public void suspendSnapshotJob() {
snapshotJob.suspend();
}

public void resumeSnapshotJob() {
snapshotJob.resume();
snapshotIfNeeded();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1578,11 +1578,7 @@ public void endOperation(ISchedulingRule rule, boolean build) throws CoreExcepti
// build() and snapshot() should not fail if they are called.
workManager.rebalanceNestedOperations();

//find out if any operation has potentially modified the tree
hasTreeChanges = workManager.shouldBuild();
//double check if the tree has actually changed
if (hasTreeChanges)
hasTreeChanges = operationTree != null && ElementTree.hasChanges(tree, operationTree, ResourceComparator.getBuildComparator(), true);
hasTreeChanges = hasTreeChanges();
broadcastPostChange();
// Request a snapshot if we are sufficiently out of date.
saveManager.snapshotIfNeeded(hasTreeChanges);
Expand All @@ -1601,6 +1597,13 @@ public void endOperation(ISchedulingRule rule, boolean build) throws CoreExcepti
buildManager.endTopLevel(hasTreeChanges);
}

boolean hasTreeChanges() throws CoreException {
return getWorkManager().shouldBuild() // find out if any operation has potentially modified the tree
//double check if the tree has actually changed
&& operationTree != null
&& ElementTree.hasChanges(tree, operationTree, ResourceComparator.getBuildComparator(), true);
}

/**
* Flush the build order cache for the workspace. The buildOrder cache contains the total
* order of the build configurations in the workspace, including projects not mentioned in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ public BundleContext getContext() {
return Platform.getBundle(PI_RESOURCES_TESTS).getBundleContext();
}

@Override
protected void setUp() throws Exception {
super.setUp();
getSaveManager().suspendSnapshotJob();
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
getSaveManager().resumeSnapshotJob();
}

private SaveManager getSaveManager() {
return ((Workspace) getWorkspace()).getSaveManager();
}

public void testBug() throws Exception {
installBundle();

Expand Down Expand Up @@ -136,11 +152,11 @@ private Map<String, SavedState> getSavedStatesFromSaveManager()
// there
Field field = SaveManager.class.getDeclaredField("savedStates");
field.setAccessible(true);
return (Map<String, SavedState>) field.get(((Workspace) getWorkspace()).getSaveManager());
return (Map<String, SavedState>) field.get(getSaveManager());
}

private void saveSnapshot() throws CoreException {
((Workspace) getWorkspace()).getSaveManager().save(ISaveContext.SNAPSHOT, true, null, getMonitor());
getSaveManager().save(ISaveContext.SNAPSHOT, true, null, getMonitor());
}

private void assertStateTrees(SavedState savedState, boolean isNull)
Expand Down

0 comments on commit 1b35a80

Please sign in to comment.