Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform element changes always in a non UI thread #1916

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -29,7 +31,10 @@
import org.eclipse.jdt.testplugin.JavaProjectHelper;
import org.eclipse.jdt.testplugin.JavaTestPlugin;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.Job;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
Expand All @@ -54,6 +59,7 @@
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;

import org.eclipse.jdt.internal.ui.packageview.PackageExplorerContentProvider;
import org.eclipse.jdt.internal.ui.util.CoreUtility;


Expand Down Expand Up @@ -212,9 +218,25 @@ public void testDeleteBottomLevelFragmentFolding() throws Exception {
protected void sendEvent(IJavaElementDelta delta) {
IElementChangedListener listener= (IElementChangedListener) fProvider;
listener.elementChanged(new ElementChangedEvent(delta, ElementChangedEvent.POST_CHANGE));

//force events from dispaly
while(fMyPart.getTreeViewer().getControl().getDisplay().readAndDispatch()) {
CountDownLatch latch = new CountDownLatch(1);
new Thread(new Runnable() {

@Override
public void run() {
try {
Job.getJobManager().join(PackageExplorerContentProvider.class, new NullProgressMonitor());
} catch (OperationCanceledException | InterruptedException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not ignore unexpected exceptions but at least log them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anything goes wrong here the test will simply fail and one has to investigate that, I don't think that any logmessage is useful here.

}
latch.countDown();
}
}).start();
//force events from display
try {
while(!latch.await(10, TimeUnit.MILLISECONDS)) {
while(fMyPart.getTreeViewer().getControl().getDisplay().readAndDispatch()) {
}
}
} catch (InterruptedException e) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
Expand All @@ -28,6 +30,7 @@
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;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -93,6 +96,8 @@ public class PackageExplorerContentProvider extends StandardJavaElementContentPr

private UIJob fUpdateJob;

private final DeltaJob fdeltaJob;

/**
* We use a cache to know whether a package has a single child for the hierarchical representation.
* This avoids looping over all packages for each call to
Expand All @@ -107,6 +112,7 @@ public class PackageExplorerContentProvider extends StandardJavaElementContentPr
*/
public PackageExplorerContentProvider(boolean provideMembers) {
super(provideMembers);
fdeltaJob= new DeltaJob();
fShowLibrariesNode= false;
fIsFlatLayout= false;
fFoldPackages= arePackagesFoldedInHierarchicalLayout();
Expand All @@ -127,6 +133,12 @@ protected Object getViewerInput() {

@Override
public void elementChanged(final ElementChangedEvent event) {
IJavaElementDelta delta= event.getDelta();
fdeltaJob.queue.add(delta);
fdeltaJob.schedule();
}

protected void processDelta(IJavaElementDelta delta) {
final ArrayList<Runnable> runnables= new ArrayList<>();
try {
clearPackageCache();
Expand All @@ -136,7 +148,7 @@ public void elementChanged(final ElementChangedEvent event) {
if (inputDeleted(runnables))
return;

processDelta(event.getDelta(), runnables);
processDelta(delta, runnables);
} catch (JavaModelException e) {
JavaPlugin.log(e);
} finally {
Expand Down Expand Up @@ -1006,4 +1018,29 @@ public void propertyChange(PropertyChangeEvent event) {
}
}
}

private final class DeltaJob extends Job {
private Queue<IJavaElementDelta> queue= new ConcurrentLinkedQueue<>();

DeltaJob() {
super(PackagesMessages.PackageExplorerContentProvider_update_job_description);
setSystem(true);
}

@Override
public boolean belongsTo(Object family) {
return family == PackageExplorerContentProvider.class;
}

@Override
protected IStatus run(IProgressMonitor monitor) {

IJavaElementDelta delta;
while ((delta= queue.poll()) != null) {
processDelta(delta);
}
return Status.OK_STATUS;
}

}
}
Loading