Search in sources :

Example 16 with ProgressIndicatorBase

use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.

the class ProgressIndicatorTest method testReadTaskCanceledShouldNotHappenAfterEdtContinuation.

public void testReadTaskCanceledShouldNotHappenAfterEdtContinuation() {
    for (int i = 0; i < 1000; i++) {
        final AtomicBoolean afterContinuation = new AtomicBoolean();
        final ProgressIndicatorBase indicator = new ProgressIndicatorBase();
        ProgressIndicatorUtils.scheduleWithWriteActionPriority(indicator, new ReadTask() {

            @Nullable
            @Override
            public Continuation performInReadAction(@NotNull ProgressIndicator indicator) throws ProcessCanceledException {
                return new Continuation(() -> afterContinuation.set(true));
            }

            @Override
            public void onCanceled(@NotNull ProgressIndicator indicator) {
                assertFalse(afterContinuation.get());
            }
        });
        UIUtil.dispatchAllInvocationEvents();
        ApplicationManager.getApplication().runWriteAction(() -> {
            if (!afterContinuation.get()) {
                assertTrue(indicator.isCanceled());
            }
        });
        UIUtil.dispatchAllInvocationEvents();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) DelegatingProgressIndicator(com.intellij.ide.util.DelegatingProgressIndicator) BombedProgressIndicator(com.intellij.testFramework.BombedProgressIndicator) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) ReadTask(com.intellij.openapi.progress.util.ReadTask)

Example 17 with ProgressIndicatorBase

use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.

the class JobUtilTest method testUnbalancedTaskJobUtilPerformance.

public void testUnbalancedTaskJobUtilPerformance() {
    List<Integer> things = new ArrayList<>(Collections.nCopies(10000, null));
    int sum = 0;
    for (int i = 0; i < things.size(); i++) {
        int v = i < 9950 ? 1 : 1000;
        things.set(i, v);
        sum += things.get(i);
    }
    assertEquals(59950, sum);
    long start = System.currentTimeMillis();
    boolean b = JobLauncher.getInstance().invokeConcurrentlyUnderProgress(things, new ProgressIndicatorBase(), false, false, o -> {
        busySleep(o);
        return true;
    });
    assertTrue(b);
    long elapsed = System.currentTimeMillis() - start;
    int expected = 2 * (9950 + 50 * 1000) / JobSchedulerImpl.CORES_COUNT;
    String message = "Elapsed: " + elapsed + "; expected: " + expected;
    System.out.println(message);
    assertTrue(message, elapsed < expected);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) AbstractProgressIndicatorBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorBase)

Example 18 with ProgressIndicatorBase

use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.

the class BombedProgressIndicator method runBombed.

/**
   * @return whether the indicator was canceled during runnable execution.
   */
public boolean runBombed(final Runnable runnable) {
    myThread = Thread.currentThread();
    final Semaphore canStart = new Semaphore();
    canStart.down();
    final Semaphore finished = new Semaphore();
    finished.down();
    // ProgressManager invokes indicator.checkCanceled only when there's at least one canceled indicator. So we have to create a mock one
    // on an unrelated thread and cancel it immediately.
    Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(() -> {
        final ProgressIndicatorBase mockIndicator = new ProgressIndicatorBase();
        ProgressManager.getInstance().runProcess(() -> {
            mockIndicator.cancel();
            canStart.up();
            finished.waitFor();
            try {
                ProgressManager.checkCanceled();
                TestCase.fail();
            } catch (ProcessCanceledException ignored) {
            }
        }, mockIndicator);
    });
    ProgressManager.getInstance().runProcess(() -> {
        canStart.waitFor();
        try {
            runnable.run();
        } catch (ProcessCanceledException ignore) {
        } finally {
            finished.up();
        }
    }, this);
    try {
        future.get();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    return isCanceled();
}
Also used : ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) AbstractProgressIndicatorBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorBase) Semaphore(com.intellij.util.concurrency.Semaphore) ExecutionException(java.util.concurrent.ExecutionException) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 19 with ProgressIndicatorBase

use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.

the class FindManagerTest method runAsyncTest.

private void runAsyncTest(String text, FindModel findModel) throws InterruptedException {
    final Ref<FindResult> result = new Ref<>();
    final CountDownLatch progressStarted = new CountDownLatch(1);
    final ProgressIndicatorBase progressIndicatorBase = new ProgressIndicatorBase();
    final Thread thread = new Thread(() -> ProgressManager.getInstance().runProcess(() -> {
        try {
            progressStarted.countDown();
            result.set(myFindManager.findString(text, 0, findModel, new LightVirtualFile("foo.java")));
        } catch (ProcessCanceledException ex) {
            result.set(new FindResultImpl());
        }
    }, progressIndicatorBase), "runAsyncTest");
    thread.start();
    progressStarted.await();
    thread.join(100);
    progressIndicatorBase.cancel();
    thread.join(500);
    assertNotNull(result.get());
    assertTrue(!result.get().isStringFound());
    thread.join();
}
Also used : Ref(com.intellij.openapi.util.Ref) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 20 with ProgressIndicatorBase

use of com.intellij.openapi.progress.util.ProgressIndicatorBase in project intellij-community by JetBrains.

the class AsyncEditorLoader method scheduleLoading.

private Future<Runnable> scheduleLoading() {
    PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
    long startStamp = myEditor.getDocument().getModificationStamp();
    return ourExecutor.submit(() -> {
        Ref<Runnable> ref = new Ref<>();
        try {
            while (!myEditorComponent.isDisposed()) {
                ProgressIndicatorUtils.runWithWriteActionPriority(() -> ref.set(psiDocumentManager.commitAndRunReadAction(() -> myProject.isDisposed() ? EmptyRunnable.INSTANCE : myTextEditor.loadEditorInBackground())), new ProgressIndicatorBase());
                Runnable continuation = ref.get();
                if (continuation != null) {
                    invokeLater(() -> {
                        if (startStamp == myEditor.getDocument().getModificationStamp())
                            loadingFinished(continuation);
                        else if (!myProject.isDisposed() && !myEditorComponent.isDisposed())
                            scheduleLoading();
                    });
                    return continuation;
                }
                ProgressIndicatorUtils.yieldToPendingWriteActions();
            }
        } finally {
            if (ref.isNull())
                invokeLater(() -> loadingFinished(null));
        }
        return null;
    });
}
Also used : Ref(com.intellij.openapi.util.Ref) ProgressIndicatorBase(com.intellij.openapi.progress.util.ProgressIndicatorBase) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Aggregations

ProgressIndicatorBase (com.intellij.openapi.progress.util.ProgressIndicatorBase)25 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)8 DaemonProgressIndicator (com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator)6 DelegatingProgressIndicator (com.intellij.ide.util.DelegatingProgressIndicator)6 ReadTask (com.intellij.openapi.progress.util.ReadTask)6 BombedProgressIndicator (com.intellij.testFramework.BombedProgressIndicator)6 NotNull (org.jetbrains.annotations.NotNull)5 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ApplicationManager (com.intellij.openapi.application.ApplicationManager)3 ModalityState (com.intellij.openapi.application.ModalityState)3 Logger (com.intellij.openapi.diagnostic.Logger)3 Project (com.intellij.openapi.project.Project)3 Ref (com.intellij.openapi.util.Ref)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 CommonBundle (com.intellij.CommonBundle)2 com.intellij.find (com.intellij.find)2 ShowUsagesAction (com.intellij.find.actions.ShowUsagesAction)2 Disposable (com.intellij.openapi.Disposable)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2