From a33b82d7c92ac99e15ec2e9014f2f7e415fa502a Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Thu, 10 Feb 2022 13:32:36 +0300 Subject: [PATCH] platform: improve leak detection in tests GitOrigin-RevId: ecd24f2e198eac9131c204ba044f8fc29bba92b6 --- .../impl/NonBlockingReadActionTest.java | 60 +++++++++++++------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/NonBlockingReadActionTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/NonBlockingReadActionTest.java index 21aa1792ed50..3255c1a7193b 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/NonBlockingReadActionTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/NonBlockingReadActionTest.java @@ -203,33 +203,59 @@ public class NonBlockingReadActionTest extends LightPlatformTestCase { } public void testDoNotLeakFirstCancelledCoalescedAction() { - Object leak = new Object() {}; + Object leak = new Object() { + }; Disposable disposable = Disposer.newDisposable(); Disposer.dispose(disposable); - CancellablePromise p = ReadAction - .nonBlocking(() -> "a") - .expireWith(disposable) - .coalesceBy(leak) - .submit(AppExecutorUtil.getAppExecutorService()); - assertTrue(p.isCancelled()); + try { + CancellablePromise p = ReadAction + .nonBlocking(() -> "a") + .expireWith(disposable) + .coalesceBy(leak) + .submit(AppExecutorUtil.getAppExecutorService()); + assertTrue(p.isCancelled()); - LeakHunter.checkLeak(NonBlockingReadActionImpl.getTasksByEquality(), leak.getClass()); + LeakHunter.checkLeak(NonBlockingReadActionImpl.getTasksByEquality(), leak.getClass()); + + Disposer.disposeChildren(disposable, (child) -> { + throw new IllegalStateException(child.toString()); + }); + } + finally { + Disposer.dispose(disposable); + } } public void testDoNotLeakSecondCancelledCoalescedAction() throws Exception { - Executor executor = AppExecutorUtil.createBoundedApplicationPoolExecutor("TestDoNotLeakSecondCancelledCoalescedAction", 10); + Disposable disposable = Disposer.newDisposable(); + try { + Executor executor = AppExecutorUtil.createBoundedApplicationPoolExecutor("TestDoNotLeakSecondCancelledCoalescedAction", 10); - Object leak = new Object(){}; - CancellablePromise p = ReadAction.nonBlocking(() -> "a").coalesceBy(leak).submit(executor); - WriteAction.run(() -> { - ReadAction.nonBlocking(() -> "b").coalesceBy(leak).submit(executor).cancel(); - }); - assertTrue(p.isDone()); + Object leak = new Object() { + }; + CancellablePromise p = ReadAction.nonBlocking(() -> "a").coalesceBy(leak).submit(executor); + WriteAction.run(() -> { + ReadAction.nonBlocking(() -> "b") + .coalesceBy(leak) + .expireWith(disposable) + .submit(executor) + .cancel(); + }); + assertTrue(p.isDone()); - ((BoundedTaskExecutor) executor).waitAllTasksExecuted(1, TimeUnit.SECONDS); + ((BoundedTaskExecutor)executor).waitAllTasksExecuted(1, TimeUnit.SECONDS); - LeakHunter.checkLeak(NonBlockingReadActionImpl.getTasksByEquality(), leak.getClass()); + LeakHunter.checkLeak(NonBlockingReadActionImpl.getTasksByEquality(), leak.getClass()); + + Disposer.disposeChildren(disposable, (child) -> { + throw new IllegalStateException(child.toString()); + }); + } + finally { + Disposer.dispose(disposable); + } } + public void testSyncExecutionHonorsConstraints() { setupUncommittedDocument();