Search in sources :

Example 81 with Disposable

use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.

the class BoundedTaskExecutorTest method testStressForHorribleABAProblemWhenFirstThreadFinishesTaskAndIsAboutToDecrementCountAndSecondThreadIncrementsCounterToTwoThenSkipsExecutionThenDecrementsItBackAndTheFirstThreadFinishedDecrementingSuccessfully.

public void testStressForHorribleABAProblemWhenFirstThreadFinishesTaskAndIsAboutToDecrementCountAndSecondThreadIncrementsCounterToTwoThenSkipsExecutionThenDecrementsItBackAndTheFirstThreadFinishedDecrementingSuccessfully() throws Exception {
    ExecutorService backendExecutor = Executors.newCachedThreadPool(ConcurrencyUtil.newNamedThreadFactory(getName()));
    int maxSimultaneousTasks = 1;
    final Disposable myDisposable = Disposer.newDisposable();
    BoundedTaskExecutor executor = new BoundedTaskExecutor(getName(), backendExecutor, maxSimultaneousTasks, myDisposable);
    AtomicInteger running = new AtomicInteger();
    AtomicInteger maxThreads = new AtomicInteger();
    try {
        int N = 100000;
        for (int i = 0; i < N; i++) {
            final int finalI = i;
            Future<?> future = executor.submit(new Runnable() {

                @Override
                public void run() {
                    maxThreads.accumulateAndGet(running.incrementAndGet(), Math::max);
                    try {
                        if (finalI % 100 == 0) {
                            TimeoutUtil.sleep(1);
                        }
                    } finally {
                        running.decrementAndGet();
                    }
                }

                @Override
                public String toString() {
                    return "iter " + finalI;
                }
            });
            CountDownLatch waitCompleted = new CountDownLatch(1);
            CountDownLatch waitStarted = new CountDownLatch(1);
            UIUtil.invokeLaterIfNeeded(() -> {
                try {
                    waitStarted.countDown();
                    executor.waitAllTasksExecuted(1, TimeUnit.MINUTES);
                    waitCompleted.countDown();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
            waitStarted.await();
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    maxThreads.accumulateAndGet(running.incrementAndGet(), Math::max);
                    try {
                        Thread.yield();
                    } finally {
                        running.decrementAndGet();
                    }
                }

                @Override
                public String toString() {
                    return "check for " + finalI;
                }
            });
            //TimeoutUtil.sleep(1);
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    maxThreads.accumulateAndGet(running.incrementAndGet(), Math::max);
                    try {
                        Thread.yield();
                    } finally {
                        running.decrementAndGet();
                    }
                }

                @Override
                public String toString() {
                    return "check 2 for " + finalI;
                }
            });
            assertTrue(waitCompleted.await(1, TimeUnit.MINUTES));
            assertTrue(future.isDone());
        }
        UIUtil.invokeAndWaitIfNeeded((Runnable) () -> {
            try {
                executor.waitAllTasksExecuted(1, TimeUnit.MINUTES);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    } finally {
        Disposer.dispose(myDisposable);
        assertTrue(executor.isShutdown());
    }
    assertTrue("Max threads was: " + maxThreads + " but bound was 1", maxThreads.get() == 1);
    backendExecutor.shutdownNow();
    assertTrue(backendExecutor.awaitTermination(1, TimeUnit.MINUTES));
}
Also used : Disposable(com.intellij.openapi.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable)

Example 82 with Disposable

use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.

the class InvokerTest method testThreadChangingOnEDTfromBgPool.

@Test
public void testThreadChangingOnEDTfromBgPool() {
    Disposable parent = InvokerTest::dispose;
    testThreadChanging(parent, new Invoker.EDT(parent), new Invoker.BackgroundPool(parent), false);
}
Also used : Disposable(com.intellij.openapi.Disposable) Test(org.junit.Test)

Example 83 with Disposable

use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.

the class InvokerTest method testThreadChangingOnEDTfromEDT.

@Test
public void testThreadChangingOnEDTfromEDT() {
    Disposable parent = InvokerTest::dispose;
    testThreadChanging(parent, new Invoker.EDT(parent), new Invoker.EDT(parent), true);
}
Also used : Disposable(com.intellij.openapi.Disposable) Test(org.junit.Test)

Example 84 with Disposable

use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.

the class InvokerTest method testInvokeLaterIfNeededOnBgThread.

@Test
public void testInvokeLaterIfNeededOnBgThread() {
    Disposable parent = InvokerTest::dispose;
    testInvokeLaterIfNeeded(parent, new Invoker.BackgroundThread(parent));
}
Also used : Disposable(com.intellij.openapi.Disposable) Test(org.junit.Test)

Example 85 with Disposable

use of com.intellij.openapi.Disposable in project intellij-community by JetBrains.

the class InvokerTest method testThreadChangingOnBgPoolFromBgPool.

@Test
public void testThreadChangingOnBgPoolFromBgPool() {
    Disposable parent = InvokerTest::dispose;
    testThreadChanging(parent, new Invoker.BackgroundPool(parent), new Invoker.BackgroundPool(parent), true);
}
Also used : Disposable(com.intellij.openapi.Disposable) Test(org.junit.Test)

Aggregations

Disposable (com.intellij.openapi.Disposable)282 NotNull (org.jetbrains.annotations.NotNull)52 Test (org.junit.Test)25 Project (com.intellij.openapi.project.Project)18 TestOnly (org.jetbrains.annotations.TestOnly)17 ArrayList (java.util.ArrayList)13 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 RelativePoint (com.intellij.ui.awt.RelativePoint)11 Nullable (org.jetbrains.annotations.Nullable)10 Document (com.intellij.openapi.editor.Document)9 File (java.io.File)9 ProcessHandler (com.intellij.execution.process.ProcessHandler)6 Editor (com.intellij.openapi.editor.Editor)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)6 VirtualFilePointer (com.intellij.openapi.vfs.pointers.VirtualFilePointer)6 Content (com.intellij.ui.content.Content)6 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)5 Alarm (com.intellij.util.Alarm)5 ActionEvent (java.awt.event.ActionEvent)5 ListSelectionEvent (javax.swing.event.ListSelectionEvent)5