From 9972634409b4d963f5988bd1fbd5acceb3d9358e Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 5 Oct 2015 16:00:15 +0300 Subject: [PATCH] shutdown thread executors to avoid thread leaks in tests --- .../util/lang/UrlClassLoaderTest.java | 96 ++++++++++--------- .../util/io/BaseOutputReaderTest.java | 7 ++ 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java b/platform/platform-tests/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java index 407d930e35d6..aa545c678cd2 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/lang/UrlClassLoaderTest.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Random; import java.util.concurrent.Future; import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -119,60 +120,65 @@ public class UrlClassLoaderTest extends TestCase { final int resourceCount = 20; ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(threadCount, ConcurrencyUtil.newNamedThreadFactory("conc loading")); - final Random random = new Random(); - UrlClassLoader.CachePool pool = UrlClassLoader.createCachePool(); - for (int attempt = 0; attempt < attemptCount; attempt++) { - final UrlClassLoader loader = UrlClassLoader.build().urls(urls).parent(null). - useCache(pool, new UrlClassLoader.CachingCondition() { - @Override - public boolean shouldCacheData(@NotNull URL url) { - return true; // fails also without cache pool (but with cache enabled), but takes much longer - } - }).get(); - //if (attempt % 10 == 0) System.out.println("Attempt " + attempt); + try { + final Random random = new Random(); + UrlClassLoader.CachePool pool = UrlClassLoader.createCachePool(); + for (int attempt = 0; attempt < attemptCount; attempt++) { + final UrlClassLoader loader = UrlClassLoader.build().urls(urls).parent(null). + useCache(pool, new UrlClassLoader.CachingCondition() { + @Override + public boolean shouldCacheData(@NotNull URL url) { + return true; // fails also without cache pool (but with cache enabled), but takes much longer + } + }).get(); + //if (attempt % 10 == 0) System.out.println("Attempt " + attempt); - final List namesToLoad = ContainerUtil.newArrayList(); - for (int j = 0; j < resourceCount; j++) { - namesToLoad.add(resourceNames.get(random.nextInt(resourceNames.size()))); - } + final List namesToLoad = ContainerUtil.newArrayList(); + for (int j = 0; j < resourceCount; j++) { + namesToLoad.add(resourceNames.get(random.nextInt(resourceNames.size()))); + } - List futures = ContainerUtil.newArrayList(); - for (int i = 0; i < threadCount; i++) { - futures.add(executor.submit(new Runnable() { - @Override - public void run() { - for (String name : namesToLoad) { - try { - assertNotNull(findResource(name)); - } - catch (Throwable e) { - System.out.println("Failed loading " + name); - throw new RuntimeException(e); + List futures = ContainerUtil.newArrayList(); + for (int i = 0; i < threadCount; i++) { + futures.add(executor.submit(new Runnable() { + @Override + public void run() { + for (String name : namesToLoad) { + try { + assertNotNull(findResource(name)); + } + catch (Throwable e) { + System.out.println("Failed loading " + name); + throw new RuntimeException(e); + } } } - } - private final Random findResourceOrFindResourcesChooser = new Random(); - private URL findResource(String name) { - if (findResourceOrFindResourcesChooser.nextBoolean()) { - try { - Enumeration resources = loader.getResources(name); - assertTrue(resources.hasMoreElements()); - return resources.nextElement(); - } - catch (IOException e) { - throw new RuntimeException(e); + private final Random findResourceOrFindResourcesChooser = new Random(); + private URL findResource(String name) { + if (findResourceOrFindResourcesChooser.nextBoolean()) { + try { + Enumeration resources = loader.getResources(name); + assertTrue(resources.hasMoreElements()); + return resources.nextElement(); + } + catch (IOException e) { + throw new RuntimeException(e); + } } + return loader.findResource(name); } - return loader.findResource(name); - } - })); - } + })); + } - for (Future future : futures) { - future.get(); + for (Future future : futures) { + future.get(); + } } } - + finally { + executor.shutdownNow(); + executor.awaitTermination(1000, TimeUnit.SECONDS); + } } } diff --git a/platform/util/testSrc/com/intellij/util/io/BaseOutputReaderTest.java b/platform/util/testSrc/com/intellij/util/io/BaseOutputReaderTest.java index 71f268928625..2c696b50fde6 100644 --- a/platform/util/testSrc/com/intellij/util/io/BaseOutputReaderTest.java +++ b/platform/util/testSrc/com/intellij/util/io/BaseOutputReaderTest.java @@ -34,6 +34,7 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -71,6 +72,12 @@ public class BaseOutputReaderTest { @AfterClass public static void tearDown() { ourExecutor.shutdown(); + try { + ourExecutor.awaitTermination(1000, TimeUnit.SECONDS); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } ourExecutor = null; }