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));
}
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);
}
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);
}
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));
}
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);
}
Aggregations