From 2b999838c8be3fd8e59c4f58966e55734dc0ffd3 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 12 Nov 2015 18:28:49 +0300 Subject: [PATCH] join thread in tests to avoid thread leaks --- .../com/intellij/find/FindManagerTest.java | 5 +- .../psi/PsiConcurrencyStressTest.java | 12 +- .../application/impl/LaterInvocatorTest.java | 1 + .../progress/impl/ProgressIndicatorTest.java | 262 +++++++----------- .../util/messages/MessageBusTest.java | 33 ++- .../util/net/ssl/CertificateTest.java | 18 ++ .../sm/OutputLineSplitterTest.java | 23 +- .../maven/indices/MavenIndicesStressTest.java | 4 + .../jetbrains/idea/svn/SvnBusyOnAddTest.java | 35 ++- .../svn/SvnCachingRepositoryPoolTest.java | 11 +- .../jetbrains/idea/svn/SvnLockingTest.java | 7 + .../idea/svn/VcsWaitForUpdateForTest.java | 1 + .../idea/svn16/VcsWaitForUpdateForTest.java | 1 + .../env/python/console/PyConsoleTask.java | 3 +- .../env/python/debug/PyBaseDebuggerTask.java | 3 +- .../util/xml/DomConcurrencyStressTest.java | 68 ++--- 16 files changed, 218 insertions(+), 269 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/find/FindManagerTest.java b/java/java-tests/testSrc/com/intellij/find/FindManagerTest.java index a894d32cef8f..0d169020bcaf 100644 --- a/java/java-tests/testSrc/com/intellij/find/FindManagerTest.java +++ b/java/java-tests/testSrc/com/intellij/find/FindManagerTest.java @@ -99,7 +99,7 @@ public class FindManagerTest extends DaemonAnalyzerTestCase { super.tearDown(); } - public void testFindString() { + public void testFindString() throws InterruptedException { FindModel findModel = FindManagerTestUtils.configureFindModel("done"); String text = "public static class MyClass{\n/*done*/\npublic static void main(){}}"; @@ -149,7 +149,7 @@ public class FindManagerTest extends DaemonAnalyzerTestCase { findModel.setProjectScope(true); final FindResult[] findResultArr = new FindResult[1]; - findInNewThread(findModel, myFindManager, text, 0, findResultArr); + Thread thread = findInNewThread(findModel, myFindManager, text, 0, findResultArr); new WaitFor(30 *1000){ @Override protected boolean condition() { @@ -158,6 +158,7 @@ public class FindManagerTest extends DaemonAnalyzerTestCase { }.assertCompleted(); assertFalse(findResultArr[0].isStringFound()); + thread.join(); } private static Thread findInNewThread(final FindModel model, diff --git a/java/java-tests/testSrc/com/intellij/psi/PsiConcurrencyStressTest.java b/java/java-tests/testSrc/com/intellij/psi/PsiConcurrencyStressTest.java index 5e2f89a10f3c..94d71a667e7a 100644 --- a/java/java-tests/testSrc/com/intellij/psi/PsiConcurrencyStressTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/PsiConcurrencyStressTest.java @@ -110,15 +110,9 @@ public class PsiConcurrencyStressTest extends DaemonAnalyzerTestCase { } assertTrue("Timed out", reads.await(5, TimeUnit.MINUTES)); - ContainerUtil.process(threads, thread -> { - try { - thread.join(); - return true; - } - catch (InterruptedException e) { - throw new RuntimeException(e); - } - }); + for (Thread thread : threads) { + thread.join(); + } } private static void mark(final String s) { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/LaterInvocatorTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/LaterInvocatorTest.java index 500b5844ffaf..68cd2845e690 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/LaterInvocatorTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/application/impl/LaterInvocatorTest.java @@ -417,6 +417,7 @@ public class LaterInvocatorTest extends PlatformTestCase { flushSwingQueue(); checkOrder(1); }); + thread.join(); } private static void flushSwingQueue() { diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/progress/impl/ProgressIndicatorTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/progress/impl/ProgressIndicatorTest.java index 064868eb170d..ecb6a45b3b9b 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/progress/impl/ProgressIndicatorTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/progress/impl/ProgressIndicatorTest.java @@ -28,9 +28,10 @@ import com.intellij.openapi.util.EmptyRunnable; import com.intellij.openapi.wm.ex.ProgressIndicatorEx; import com.intellij.testFramework.BombedProgressIndicator; import com.intellij.testFramework.LightPlatformTestCase; -import com.intellij.testFramework.PlatformTestCase; import com.intellij.testFramework.PlatformTestUtil; -import com.intellij.util.*; +import com.intellij.util.Alarm; +import com.intellij.util.ArrayUtil; +import com.intellij.util.TimeoutUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.DoubleArrayList; import com.intellij.util.containers.Stack; @@ -64,17 +65,14 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { public void testProgressManagerCheckCanceledWorksRightAfterIndicatorBeenCanceled() { for (int i=0; i<1000;i++) { final ProgressIndicatorBase indicator = new ProgressIndicatorBase(); - ProgressManager.getInstance().runProcess(new Runnable() { - @Override - public void run() { + ProgressManager.getInstance().runProcess(() -> { + ProgressManager.checkCanceled(); + try { + indicator.cancel(); ProgressManager.checkCanceled(); - try { - indicator.cancel(); - ProgressManager.checkCanceled(); - fail("checkCanceled() must have caught just canceled indicator"); - } - catch (ProcessCanceledException ignored) { - } + fail("checkCanceled() must have caught just canceled indicator"); + } + catch (ProcessCanceledException ignored) { } }, indicator); } @@ -88,29 +86,26 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { final TLongArrayList times = new TLongArrayList(); final long end = warmupEnd + 1000; - ApplicationManagerEx.getApplicationEx().runProcessWithProgressSynchronously(new Runnable() { - @Override - public void run() { - final Alarm alarm = new Alarm(Alarm.ThreadToUse.OWN_THREAD, getTestRootDisposable()); - ProgressIndicatorEx indicator = (ProgressIndicatorEx)ProgressIndicatorProvider.getGlobalProgressIndicator(); - prevTime = System.currentTimeMillis(); - assert indicator != null; - indicator.addStateDelegate(new ProgressIndicatorStub() { - @Override - public void checkCanceled() throws ProcessCanceledException { - now = System.currentTimeMillis(); - if (now > warmupEnd) { - int delta = (int)(now - prevTime); - times.add(delta); - } - prevTime = now; + ApplicationManagerEx.getApplicationEx().runProcessWithProgressSynchronously(() -> { + final Alarm alarm = new Alarm(Alarm.ThreadToUse.OWN_THREAD, getTestRootDisposable()); + ProgressIndicatorEx indicator = (ProgressIndicatorEx)ProgressIndicatorProvider.getGlobalProgressIndicator(); + prevTime = System.currentTimeMillis(); + assert indicator != null; + indicator.addStateDelegate(new ProgressIndicatorStub() { + @Override + public void checkCanceled() throws ProcessCanceledException { + now = System.currentTimeMillis(); + if (now > warmupEnd) { + int delta = (int)(now - prevTime); + times.add(delta); } - }); - while (System.currentTimeMillis() < end) { - ProgressManager.checkCanceled(); + prevTime = now; } - alarm.cancelAllRequests(); + }); + while (System.currentTimeMillis() < end) { + ProgressManager.checkCanceled(); } + alarm.cancelAllRequests(); }, "", false, getProject(), null, ""); long averageDelay = ArrayUtil.averageAmongMedians(times.toNativeArray(), 5); System.out.println("averageDelay = " + averageDelay); @@ -137,11 +132,8 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { while (!insideReadAction.get()) { } - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - assertTrue(indicator.isCanceled()); - } + ApplicationManager.getApplication().runWriteAction(() -> { + assertTrue(indicator.isCanceled()); }); assertTrue(indicator.isCanceled()); } @@ -149,44 +141,24 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { public void testThereIsNoDelayBetweenIndicatorCancelAndProgressManagerCheckCanceled() throws Throwable { for (int i=0; i<100;i++) { final ProgressIndicatorBase indicator = new ProgressIndicatorBase(); - List threads = ContainerUtil.map(Collections.nCopies(10, ""), new Function() { - @Override - public Thread fun(String s) { - return new Thread(new Runnable() { - @Override - public void run() { - ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(new Random().nextInt(100)); - indicator.cancel(); - ProgressManager.checkCanceled(); - fail("checkCanceled() must know about canceled indicator even from different thread"); - } - catch (ProcessCanceledException ignored) { - } - catch (Throwable e) { - exception = e; - } - } - }, indicator); - } - },"indicator test"){{start();}}; - } - }); - ContainerUtil.process(threads, new Processor() { - @Override - public boolean process(Thread thread) { - try { - thread.join(); - } - catch (InterruptedException e) { - throw new RuntimeException(e); - } - return true; - } - }); + List threads = ContainerUtil.map(Collections.nCopies(10, ""), + s -> new Thread(() -> ProgressManager.getInstance().executeProcessUnderProgress(() -> { + try { + Thread.sleep(new Random().nextInt(100)); + indicator.cancel(); + ProgressManager.checkCanceled(); + fail("checkCanceled() must know about canceled indicator even from different thread"); + } + catch (ProcessCanceledException ignored) { + } + catch (Throwable e) { + exception = e; + } + }, indicator), "indicator test")); + threads.forEach(Thread::start); + for (Thread thread : threads) { + thread.join(); + } } if (exception != null) throw exception; } @@ -203,7 +175,7 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { taskCanceled = taskSucceeded = false; exception = null; Future future = ((ProgressManagerImpl)ProgressManager.getInstance()).runProcessWithProgressAsynchronously( - new Task.Backgroundable(getProject(), "xxx") { + new Task.Backgroundable(getProject(), "Xxx") { @Override public void run(@NotNull ProgressIndicator indicator) { try { @@ -218,11 +190,7 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { checkCanceledCalled = true; throw e; } - catch (RuntimeException e) { - exception = e; - throw e; - } - catch (Error e) { + catch (RuntimeException | Error e) { exception = e; throw e; } @@ -274,20 +242,12 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { private void ensureCheckCanceledCalled(@NotNull ProgressIndicator indicator) { myFlag = false; Alarm alarm = new Alarm(Alarm.ThreadToUse.POOLED_THREAD, myTestRootDisposable); - alarm.addRequest(new Runnable() { - @Override - public void run() { - myFlag = true; - } - }, 100); + alarm.addRequest(() -> myFlag = true, 100); final long start = System.currentTimeMillis(); try { - ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() { - @Override - public void run() { - while (System.currentTimeMillis() - start < 10000) { - ProgressManager.checkCanceled(); - } + ProgressManager.getInstance().executeProcessUnderProgress(() -> { + while (System.currentTimeMillis() - start < 10000) { + ProgressManager.checkCanceled(); } }, indicator); fail("must have thrown PCE"); @@ -315,38 +275,32 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { public void testNestedIndicatorsAreCanceledRight() { checkCanceledCalled = false; - ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() { - @Override - public void run() { + ProgressManager.getInstance().executeProcessUnderProgress(() -> { + assertFalse(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); + ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator(); + assertTrue(indicator != null && !indicator.isCanceled()); + indicator.cancel(); + assertTrue(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); + assertTrue(indicator.isCanceled()); + final ProgressIndicatorEx nested = new ProgressIndicatorBase(); + nested.addStateDelegate(new ProgressIndicatorStub() { + @Override + public void checkCanceled() throws ProcessCanceledException { + checkCanceledCalled = true; + } + }); + ProgressManager.getInstance().executeProcessUnderProgress(() -> { assertFalse(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); - ProgressIndicator indicator = ProgressIndicatorProvider.getGlobalProgressIndicator(); - assertTrue(indicator != null && !indicator.isCanceled()); - indicator.cancel(); - assertTrue(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); - assertTrue(indicator.isCanceled()); - final ProgressIndicatorEx nested = new ProgressIndicatorBase(); - nested.addStateDelegate(new ProgressIndicatorStub() { - @Override - public void checkCanceled() throws ProcessCanceledException { - checkCanceledCalled = true; - } - }); - ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() { - @Override - public void run() { - assertFalse(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); - ProgressIndicator indicator2 = ProgressIndicatorProvider.getGlobalProgressIndicator(); - assertTrue(indicator2 != null && !indicator2.isCanceled()); - assertSame(indicator2, nested); - ProgressManager.checkCanceled(); - } - }, nested); + ProgressIndicator indicator2 = ProgressIndicatorProvider.getGlobalProgressIndicator(); + assertTrue(indicator2 != null && !indicator2.isCanceled()); + assertSame(indicator2, nested); + ProgressManager.checkCanceled(); + }, nested); - ProgressIndicator indicator3 = ProgressIndicatorProvider.getGlobalProgressIndicator(); - assertSame(indicator, indicator3); + ProgressIndicator indicator3 = ProgressIndicatorProvider.getGlobalProgressIndicator(); + assertSame(indicator, indicator3); - assertTrue(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); - } + assertTrue(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); }, new EmptyProgressIndicator()); assertFalse(checkCanceledCalled); } @@ -355,24 +309,18 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { EmptyProgressIndicator indicator1 = new EmptyProgressIndicator(); DelegatingProgressIndicator indicator2 = new DelegatingProgressIndicator(indicator1); final DelegatingProgressIndicator indicator3 = new DelegatingProgressIndicator(indicator2); - ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() { - @Override - public void run() { - ProgressIndicator current = ProgressIndicatorProvider.getGlobalProgressIndicator(); - assertSame(indicator3, current); - } + ProgressManager.getInstance().executeProcessUnderProgress(() -> { + ProgressIndicator current = ProgressIndicatorProvider.getGlobalProgressIndicator(); + assertSame(indicator3, current); }, indicator3); assertFalse(checkCanceledCalled); } public void testProgressPerformance() { - PlatformTestUtil.startPerformanceTest("progress", 100, new ThrowableRunnable() { - @Override - public void run() throws Throwable { - EmptyProgressIndicator indicator = new EmptyProgressIndicator(); - for (int i=0;i<100000;i++) { - ProgressManager.getInstance().executeProcessUnderProgress(EmptyRunnable.getInstance(), indicator); - } + PlatformTestUtil.startPerformanceTest("progress", 100, () -> { + EmptyProgressIndicator indicator = new EmptyProgressIndicator(); + for (int i=0;i<100000;i++) { + ProgressManager.getInstance().executeProcessUnderProgress(EmptyRunnable.getInstance(), indicator); } }).assertTiming(); } @@ -385,17 +333,14 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { } }; try { - ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() { - @Override - public void run() { - assertFalse(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); - assertTrue(!progress.isCanceled()); - progress.cancel(); - assertTrue(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); - assertTrue(progress.isCanceled()); - while (true) { // wait for PCE - ProgressManager.checkCanceled(); - } + ProgressManager.getInstance().executeProcessUnderProgress(() -> { + assertFalse(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); + assertTrue(!progress.isCanceled()); + progress.cancel(); + assertTrue(CoreProgressManager.threadsUnderCanceledIndicator.contains(Thread.currentThread())); + assertTrue(progress.isCanceled()); + while (true) { // wait for PCE + ProgressManager.checkCanceled(); } }, ProgressWrapper.wrap(progress)); fail("PCE must have been thrown"); @@ -407,22 +352,19 @@ public class ProgressIndicatorTest extends LightPlatformTestCase { public void testBombedIndicator() { final int count = 10; - new BombedProgressIndicator(count).runBombed(new Runnable() { - @Override - public void run() { - for (int i = 0; i < count * 2; i++) { - TimeoutUtil.sleep(10); - try { + new BombedProgressIndicator(count).runBombed(() -> { + for (int i = 0; i < count * 2; i++) { + TimeoutUtil.sleep(10); + try { + ProgressManager.checkCanceled(); + if (i >= count) { ProgressManager.checkCanceled(); - if (i >= count) { - ProgressManager.checkCanceled(); - fail("PCE expected on " + i + "th check"); - } + fail("PCE expected on " + i + "th check"); } - catch (ProcessCanceledException e) { - if (i < count) { - fail("Too early PCE"); - } + } + catch (ProcessCanceledException e) { + if (i < count) { + fail("Too early PCE"); } } } diff --git a/platform/platform-tests/testSrc/com/intellij/util/messages/MessageBusTest.java b/platform/platform-tests/testSrc/com/intellij/util/messages/MessageBusTest.java index 22fc07145132..ae4546fe2f87 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/messages/MessageBusTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/messages/MessageBusTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,8 +43,8 @@ public class MessageBusTest extends TestCase { void t22(); } - private static final Topic TOPIC1 = new Topic("T1", T1Listener.class); - private static final Topic TOPIC2 = new Topic("T2", T2Listener.class); + private static final Topic TOPIC1 = new Topic<>("T1", T1Listener.class); + private static final Topic TOPIC2 = new Topic<>("T2", T2Listener.class); private class T1Handler implements T1Listener { private final String id; @@ -86,7 +86,7 @@ public class MessageBusTest extends TestCase { protected void setUp() throws Exception { super.setUp(); myBus = MessageBusFactory.newMessageBus(this); - myLog = new ArrayList(); + myLog = new ArrayList<>(); } public void testNoListenersSubscribed() { @@ -217,13 +217,10 @@ public class MessageBusTest extends TestCase { new MessageBusImpl(this, childBus); } - PlatformTestUtil.assertTiming("Too long", 3000, new Runnable() { - @Override - public void run() { - T1Listener publisher = myBus.syncPublisher(TOPIC1); - for (int i = 0; i < 1000000; i++) { - publisher.t11(); - } + PlatformTestUtil.assertTiming("Too long", 3000, () -> { + T1Listener publisher = myBus.syncPublisher(TOPIC1); + for (int i = 0; i < 1000000; i++) { + publisher.t11(); } }); } @@ -231,15 +228,16 @@ public class MessageBusTest extends TestCase { public void testStress() throws Throwable { final int threadsNumber = 10; final int iterationsNumber = 100; - final AtomicReference exception = new AtomicReference(); + final AtomicReference exception = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(threadsNumber); final MessageBus parentBus = MessageBusFactory.newMessageBus("parent"); + List threads = new ArrayList<>(); for (int i = 0; i < threadsNumber; i++) { - new Thread(String.valueOf(i)) { + Thread thread = new Thread(String.valueOf(i)) { @Override public void run() { - int remains = iterationsNumber; try { + int remains = iterationsNumber; while (remains-- > 0) { //noinspection ThrowableResultOfMethodCallIgnored if (exception.get() != null) { @@ -255,13 +253,18 @@ public class MessageBusTest extends TestCase { latch.countDown(); } } - }.start(); + }; + thread.start(); + threads.add(thread); } latch.await(); final Throwable e = exception.get(); if (e != null) { throw e; } + for (Thread thread : threads) { + thread.join(); + } } diff --git a/platform/platform-tests/testSrc/com/intellij/util/net/ssl/CertificateTest.java b/platform/platform-tests/testSrc/com/intellij/util/net/ssl/CertificateTest.java index 40038839ec70..4f6d87ab64a9 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/net/ssl/CertificateTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/net/ssl/CertificateTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.util.net.ssl; import com.intellij.openapi.application.ApplicationManager; @@ -112,6 +127,7 @@ public class CertificateTest extends LightPlatformTestCase { final long interruptionTimeout = CertificateManager.DIALOG_VISIBILITY_TIMEOUT + 1000; // Will be interrupted after at most interruptionTimeout (6 seconds originally) + Thread[] t = {null}; ApplicationManager.getApplication().invokeAndWait(new Runnable() { @Override public void run() { @@ -147,11 +163,13 @@ public class CertificateTest extends LightPlatformTestCase { fail("Deadlock was not detected in time"); } } + t[0] = thread; } }, ModalityState.any()); if (!throwableRef.isNull()) { throw new AssertionError(throwableRef.get()); } + t[0].join(); } @Override diff --git a/platform/smRunner/testSrc/com/intellij/execution/testframework/sm/OutputLineSplitterTest.java b/platform/smRunner/testSrc/com/intellij/execution/testframework/sm/OutputLineSplitterTest.java index 6fd0ca976b40..35bc91d2837e 100644 --- a/platform/smRunner/testSrc/com/intellij/execution/testframework/sm/OutputLineSplitterTest.java +++ b/platform/smRunner/testSrc/com/intellij/execution/testframework/sm/OutputLineSplitterTest.java @@ -17,10 +17,10 @@ package com.intellij.execution.testframework.sm; import com.intellij.execution.process.ProcessOutputTypes; import com.intellij.execution.testframework.sm.runner.OutputLineSplitter; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.testFramework.UsefulTestCase; -import com.intellij.util.concurrency.FutureResult; +import com.intellij.testFramework.PlatformTestCase; import gnu.trove.THashMap; import org.jetbrains.annotations.NotNull; @@ -28,7 +28,7 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; -public class OutputLineSplitterTest extends UsefulTestCase { +public class OutputLineSplitterTest extends PlatformTestCase { private static final Key RED = Key.create(OutputLineSplitterTest.class + ".RED"); private static final Key GREEN = Key.create(OutputLineSplitterTest.class + ".GREEN"); private static final Key BLUE = Key.create(OutputLineSplitterTest.class + ".BLUE"); @@ -183,7 +183,7 @@ public class OutputLineSplitterTest extends UsefulTestCase { isFinished.set(true); for (Future each : futures) { - each.get(10, TimeUnit.SECONDS); + each.get(); } } catch (Exception e) { @@ -193,19 +193,6 @@ public class OutputLineSplitterTest extends UsefulTestCase { } private Future execute(final Runnable runnable) { - final FutureResult result = new FutureResult(); - new Thread(new Runnable() { - @Override - public void run() { - try { - runnable.run(); - result.set(null); - } - catch (Throwable e) { - result.setException(e); - } - } - },"line split").start(); - return result; + return ApplicationManager.getApplication().executeOnPooledThread(runnable); } } diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/indices/MavenIndicesStressTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/indices/MavenIndicesStressTest.java index 580d849bf7e9..6003adc09cac 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/indices/MavenIndicesStressTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/indices/MavenIndicesStressTest.java @@ -86,6 +86,8 @@ public abstract class MavenIndicesStressTest extends MavenIndicesTestCase implem t2.join(100); indices.close(); + t1.join(); + t2.join(); } public void test2() throws Exception { @@ -130,6 +132,8 @@ public abstract class MavenIndicesStressTest extends MavenIndicesTestCase implem indices1.close(); indices2.close(); + t1.join(); + t2.join(); } private static Thread createThread(final MavenIndex index, final AtomicInteger finishedCount) { diff --git a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnBusyOnAddTest.java b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnBusyOnAddTest.java index 4f9b1dfa46d0..c99138a785da 100644 --- a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnBusyOnAddTest.java +++ b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnBusyOnAddTest.java @@ -124,12 +124,9 @@ public class SvnBusyOnAddTest extends TestCase { final Semaphore semaphoreWokeUp = new Semaphore(); final AtomicReference wasUp = new AtomicReference(false); - final ISVNStatusHandler handler = new ISVNStatusHandler() { - @Override - public void handleStatus(SVNStatus status) throws SVNException { - semaphore.waitFor(); - wasUp.set(true); - } + final ISVNStatusHandler handler = status -> { + semaphore.waitFor(); + wasUp.set(true); }; semaphore.down(); @@ -137,19 +134,17 @@ public class SvnBusyOnAddTest extends TestCase { semaphoreWokeUp.down(); final SVNException[] exception = new SVNException[1]; - new Thread(new Runnable() { - @Override - public void run() { - try { - semaphoreMain.up(); - readClient.doStatus(myWorkingCopyRoot, true, false, true, false, handler); - semaphoreWokeUp.up(); - } - catch (SVNException e) { - exception[0] = e; - } + Thread thread = new Thread(() -> { + try { + semaphoreMain.up(); + readClient.doStatus(myWorkingCopyRoot, true, false, true, false, handler); + semaphoreWokeUp.up(); } - },"svn test").start(); + catch (SVNException e) { + exception[0] = e; + } + }, "svn test"); + thread.start(); semaphoreMain.waitFor(); TimeoutUtil.sleep(5); @@ -162,7 +157,9 @@ public class SvnBusyOnAddTest extends TestCase { if (exception[0] != null) { throw exception[0]; } - } finally { + thread.join(); + } + finally { ioFile.delete(); } } diff --git a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnCachingRepositoryPoolTest.java b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnCachingRepositoryPoolTest.java index c9ca1afc96df..f97d3baf0348 100644 --- a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnCachingRepositoryPoolTest.java +++ b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnCachingRepositoryPoolTest.java @@ -32,6 +32,8 @@ import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.io.ISVNSession; import org.tmatesoft.svn.core.io.SVNRepository; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Random; @@ -127,6 +129,7 @@ public class SvnCachingRepositoryPoolTest extends FileBasedTest { } } Assert.assertTrue(!thread.isAlive()); + thread.join(); Assert.assertNotNull(exc[0]); //repository1.fireConnectionClosed(); // also test that used are also closed.. in dispose @@ -151,7 +154,7 @@ public class SvnCachingRepositoryPoolTest extends FileBasedTest { Assert.assertEquals(0, group.getInactiveSize()); } - private void testBigFlow(final SvnIdeaRepositoryPoolManager poolManager, boolean disposeAfter) throws SVNException { + private void testBigFlow(final SvnIdeaRepositoryPoolManager poolManager, boolean disposeAfter) throws SVNException, InterruptedException { poolManager.setCreator(new ThrowableConvertor() { @Override public SVNRepository convert(SVNURL svnurl) throws SVNException { @@ -163,6 +166,7 @@ public class SvnCachingRepositoryPoolTest extends FileBasedTest { final int[] cnt = new int[1]; cnt[0] = 25; final SVNException[] exc = new SVNException[1]; + List threads = new ArrayList<>(); for (int i = 0; i < 25; i++) { Runnable target = new Runnable() { @Override @@ -186,6 +190,7 @@ public class SvnCachingRepositoryPoolTest extends FileBasedTest { }; Thread thread = new Thread(target, "svn cache"); thread.start(); + threads.add(thread); } final long start = System.currentTimeMillis(); @@ -217,6 +222,10 @@ public class SvnCachingRepositoryPoolTest extends FileBasedTest { Assert.assertEquals(0, group.getUsedSize()); Assert.assertEquals(0, group.getInactiveSize()); } + + for (Thread thread : threads) { + thread.join(); + } } private boolean timeout(long start) { diff --git a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnLockingTest.java b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnLockingTest.java index 7e4dc310d7e6..b3402bed1980 100644 --- a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnLockingTest.java +++ b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnLockingTest.java @@ -108,6 +108,8 @@ public class SvnLockingTest extends TestCase { thread1.interrupt(); thread2.interrupt(); + thread1.join(); + thread2.join(); } } @@ -138,6 +140,8 @@ public class SvnLockingTest extends TestCase { thread1.interrupt(); thread2.interrupt(); + thread1.join(); + thread2.join(); } } @@ -225,6 +229,9 @@ public class SvnLockingTest extends TestCase { thread1.interrupt(); thread2.interrupt(); threadRead.interrupt(); + thread1.join(); + thread2.join(); + threadRead.join(); } } diff --git a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/VcsWaitForUpdateForTest.java b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/VcsWaitForUpdateForTest.java index 9477478950cb..7c95d06505f4 100644 --- a/plugins/svn4idea/testSource/org/jetbrains/idea/svn/VcsWaitForUpdateForTest.java +++ b/plugins/svn4idea/testSource/org/jetbrains/idea/svn/VcsWaitForUpdateForTest.java @@ -57,5 +57,6 @@ public class VcsWaitForUpdateForTest extends Svn17TestCase { } assert Boolean.TRUE.equals(done.get()); + thread.join(); } } diff --git a/plugins/svn4idea/testSource/org/jetbrains/idea/svn16/VcsWaitForUpdateForTest.java b/plugins/svn4idea/testSource/org/jetbrains/idea/svn16/VcsWaitForUpdateForTest.java index 99d4a89be958..f65393ae9133 100644 --- a/plugins/svn4idea/testSource/org/jetbrains/idea/svn16/VcsWaitForUpdateForTest.java +++ b/plugins/svn4idea/testSource/org/jetbrains/idea/svn16/VcsWaitForUpdateForTest.java @@ -57,5 +57,6 @@ public class VcsWaitForUpdateForTest extends Svn16TestCase { } assert Boolean.TRUE.equals(done.get()); + thread.join(); } } diff --git a/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java b/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java index ee81d63311f4..97b0e74a0ff9 100644 --- a/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java +++ b/python/testSrc/com/jetbrains/env/python/console/PyConsoleTask.java @@ -324,9 +324,10 @@ public class PyConsoleTask extends PyExecutionFixtureTestTask { myLen = s.length(); } - public void stop() { + public void stop() throws InterruptedException { printToConsole(); myThread.interrupt(); + myThread.join(); } } diff --git a/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java b/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java index 337a33dd75fc..881e366306f1 100644 --- a/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java +++ b/python/testSrc/com/jetbrains/env/python/debug/PyBaseDebuggerTask.java @@ -382,8 +382,9 @@ public abstract class PyBaseDebuggerTask extends PyExecutionFixtureTestTask { } } - public void stop() { + public void stop() throws InterruptedException { myThread.interrupt(); + myThread.join(); } } } diff --git a/xml/dom-tests/tests/com/intellij/util/xml/DomConcurrencyStressTest.java b/xml/dom-tests/tests/com/intellij/util/xml/DomConcurrencyStressTest.java index 82f2f804c5c8..88faf95ea656 100644 --- a/xml/dom-tests/tests/com/intellij/util/xml/DomConcurrencyStressTest.java +++ b/xml/dom-tests/tests/com/intellij/util/xml/DomConcurrencyStressTest.java @@ -28,7 +28,6 @@ import com.intellij.semantic.SemService; import com.intellij.testFramework.PlatformTestUtil; import com.intellij.testFramework.Timings; import com.intellij.util.Function; -import com.intellij.util.Processor; import com.intellij.util.TimeoutUtil; import com.intellij.util.containers.ContainerUtil; import com.intellij.util.xml.impl.DomFileElementImpl; @@ -121,33 +120,23 @@ public class DomConcurrencyStressTest extends DomTestCase { int N = 8; final CountDownLatch reads = new CountDownLatch(N); - List threads = ContainerUtil.map(Collections.nCopies(N, ""), new Function() { + List threads = ContainerUtil.map(Collections.nCopies(N, ""), (Function)s -> new Thread("dom concurrency") { @Override - public Thread fun(String s) { - return new Thread("dom concurrency") { - @Override - public void run() { - try { - runnable.run(); - } - catch (Throwable e) { - exc.set(e); - } - finally { - reads.countDown(); - } - } - }; - } - }); - ContainerUtil.process(threads, new Processor() { - @Override - public boolean process(Thread thread) { - thread.start(); - return true; + public void run() { + try { + runnable.run(); + } + catch (Throwable e) { + exc.set(e); + } + finally { + reads.countDown(); + } } }); + threads.forEach(Thread::start); + reads.await(); if (!exc.isNull()) { throw exc.get(); @@ -207,26 +196,19 @@ public class DomConcurrencyStressTest extends DomTestCase { assert bigXml != null; final XmlFile file = (XmlFile)PsiManager.getInstance(ourProject).findFile(bigXml); - runThreads(42, new Runnable() { + runThreads(42, () -> { + final Random random = new Random(); + for (int i = 0; i < ITERATIONS; i++) { + ApplicationManager.getApplication().runReadAction(() -> { + int offset = random.nextInt(file.getTextLength() - 10); + XmlTag tag = PsiTreeUtil.findElementOfClassAtOffset(file, offset, XmlTag.class, false); + assert tag != null : offset; + DomElement element = DomUtil.getDomElement(tag); + assert element instanceof MyAllCustomElement : element; + }); - @Override - public void run() { - final Random random = new Random(); - for (int i = 0; i < ITERATIONS; i++) { - ApplicationManager.getApplication().runReadAction(new Runnable() { - @Override - public void run() { - int offset = random.nextInt(file.getTextLength() - 10); - XmlTag tag = PsiTreeUtil.findElementOfClassAtOffset(file, offset, XmlTag.class, false); - assert tag != null : offset; - DomElement element = DomUtil.getDomElement(tag); - assert element instanceof MyAllCustomElement : element; - } - }); - - if (random.nextInt(50) == 0) { - SemService.getSemService(getProject()).clearCache(); - } + if (random.nextInt(50) == 0) { + SemService.getSemService(getProject()).clearCache(); } } });