LANG-1091: Shutdown thread pools in test cases. This fixes #58 from github. Thanks to Fabian Lange.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1669310 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2015-03-26 10:21:24 +00:00
parent 0b7ef7f53f
commit c8e96c0c73
7 changed files with 43 additions and 15 deletions

View File

@ -22,6 +22,7 @@
<body> <body>
<release version="3.4" date="tba" description="tba"> <release version="3.4" date="tba" description="tba">
<action issue="LANG-1091" type="update" dev="britter" due-to="Fabian Lange">Shutdown thread pools in test cases</action>
<action issue="LANG-1101" type="update" dev="chas">FastDateParser and FastDatePrinter support 'X' format</action> <action issue="LANG-1101" type="update" dev="chas">FastDateParser and FastDatePrinter support 'X' format</action>
<action issue="LANG-1100" type="update" dev="chas" due-to="mbracher">Avoid memory allocation when using date formating to StringBuffer</action> <action issue="LANG-1100" type="update" dev="chas" due-to="mbracher">Avoid memory allocation when using date formating to StringBuffer</action>
<action issue="LANG-935" type="update" dev="britter" due-to="Fabian Lange, Thomas Neidhart">Possible performance improvement on string escape functions</action> <action issue="LANG-935" type="update" dev="britter" due-to="Fabian Lange, Thomas Neidhart">Possible performance improvement on string escape functions</action>

View File

@ -27,9 +27,9 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -116,5 +116,7 @@ public class ReflectionToStringBuilderConcurrencyTest {
for (final Future<Integer> future : futures) { for (final Future<Integer> future : futures) {
Assert.assertEquals(REPEAT, future.get().intValue()); Assert.assertEquals(REPEAT, future.get().intValue());
} }
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
} }
} }

View File

@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Test; import org.junit.Test;
@ -106,5 +107,7 @@ public class ToStringStyleConcurrencyTest {
for (final Future<Integer> future : futures) { for (final Future<Integer> future : futures) {
future.get(); future.get();
} }
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
} }
} }

View File

@ -17,7 +17,9 @@
package org.apache.commons.lang3.concurrent; package org.apache.commons.lang3.concurrent;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -66,7 +68,7 @@ public class BackgroundInitializerTest {
* Tests whether an external executor is correctly detected. * Tests whether an external executor is correctly detected.
*/ */
@Test @Test
public void testGetActiveExecutorExternal() { public void testGetActiveExecutorExternal() throws InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor(); final ExecutorService exec = Executors.newSingleThreadExecutor();
try { try {
final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl( final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl(
@ -76,6 +78,7 @@ public class BackgroundInitializerTest {
checkInitialize(init); checkInitialize(init);
} finally { } finally {
exec.shutdown(); exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
} }
} }
@ -130,14 +133,18 @@ public class BackgroundInitializerTest {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException because the test implementation may throw it * @throws org.apache.commons.lang3.concurrent.ConcurrentException because the test implementation may throw it
*/ */
@Test @Test
public void testSetExternalExecutorAfterStart() throws ConcurrentException { public void testSetExternalExecutorAfterStart() throws ConcurrentException, InterruptedException {
final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl(); final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl();
init.start(); init.start();
ExecutorService exec = Executors.newSingleThreadExecutor();
try { try {
init.setExternalExecutor(Executors.newSingleThreadExecutor()); init.setExternalExecutor(exec);
fail("Could set executor after start()!"); fail("Could set executor after start()!");
} catch (final IllegalStateException istex) { } catch (final IllegalStateException istex) {
init.get(); init.get();
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
} }
} }
@ -235,7 +242,7 @@ public class BackgroundInitializerTest {
getThread.interrupt(); getThread.interrupt();
latch1.await(); latch1.await();
exec.shutdownNow(); exec.shutdownNow();
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); exec.awaitTermination(1, TimeUnit.SECONDS);
assertNotNull("No interrupted exception", iex.get()); assertNotNull("No interrupted exception", iex.get());
} }

View File

@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test; import org.junit.Test;
@ -47,11 +48,13 @@ public class CallableBackgroundInitializerTest {
* class. * class.
*/ */
@Test @Test
public void testInitExecutor() { public void testInitExecutor() throws InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor(); final ExecutorService exec = Executors.newSingleThreadExecutor();
final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<Integer>( final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<Integer>(
new TestCallable(), exec); new TestCallable(), exec);
assertEquals("Executor not set", exec, init.getExternalExecutor()); assertEquals("Executor not set", exec, init.getExternalExecutor());
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
} }
/** /**
@ -59,9 +62,15 @@ public class CallableBackgroundInitializerTest {
* This should cause an exception. * This should cause an exception.
*/ */
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
public void testInitExecutorNullCallable() { public void testInitExecutorNullCallable() throws InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor(); final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
new CallableBackgroundInitializer<Integer>(null, exec); new CallableBackgroundInitializer<Integer>(null, exec);
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}
} }
/** /**

View File

@ -26,6 +26,7 @@ import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -152,7 +153,7 @@ public class MultiBackgroundInitializerTest {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
*/ */
@Test @Test
public void testInitializeExternalExec() throws ConcurrentException { public void testInitializeExternalExec() throws ConcurrentException, InterruptedException {
final ExecutorService exec = Executors.newCachedThreadPool(); final ExecutorService exec = Executors.newCachedThreadPool();
try { try {
initializer = new MultiBackgroundInitializer(exec); initializer = new MultiBackgroundInitializer(exec);
@ -162,6 +163,7 @@ public class MultiBackgroundInitializerTest {
assertFalse("Executor was shutdown", exec.isShutdown()); assertFalse("Executor was shutdown", exec.isShutdown());
} finally { } finally {
exec.shutdown(); exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
} }
} }
@ -172,7 +174,7 @@ public class MultiBackgroundInitializerTest {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
*/ */
@Test @Test
public void testInitializeChildWithExecutor() throws ConcurrentException { public void testInitializeChildWithExecutor() throws ConcurrentException, InterruptedException {
final String initExec = "childInitializerWithExecutor"; final String initExec = "childInitializerWithExecutor";
final ExecutorService exec = Executors.newSingleThreadExecutor(); final ExecutorService exec = Executors.newSingleThreadExecutor();
try { try {
@ -187,6 +189,7 @@ public class MultiBackgroundInitializerTest {
checkChild(c2, exec); checkChild(c2, exec);
} finally { } finally {
exec.shutdown(); exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
} }
} }

View File

@ -312,7 +312,10 @@ public class FastDateFormatTest {
}); });
} }
pool.shutdown(); pool.shutdown();
if(!pool.awaitTermination(20, TimeUnit.SECONDS)) { // depending on the performance of the machine used to run the parsing,
// the tests can run for a while. It should however complete within
// 30 seconds. Might need increase on very slow machines.
if(!pool.awaitTermination(30, TimeUnit.SECONDS)) {
pool.shutdownNow(); pool.shutdownNow();
fail("did not complete tasks"); fail("did not complete tasks");
} }