diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 28857631b..69ace0d4d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Shutdown thread pools in test cases FastDateParser and FastDatePrinter support 'X' format Avoid memory allocation when using date formating to StringBuffer Possible performance improvement on string escape functions diff --git a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java index 5fc03fd0a..1ce1cd9de 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java @@ -27,9 +27,9 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import org.junit.Assert; - import org.junit.Ignore; import org.junit.Test; @@ -116,5 +116,7 @@ public class ReflectionToStringBuilderConcurrencyTest { for (final Future future : futures) { Assert.assertEquals(REPEAT, future.get().intValue()); } + threadPool.shutdown(); + threadPool.awaitTermination(1, TimeUnit.SECONDS); } } diff --git a/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java b/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java index 16659bfcb..db590e923 100644 --- a/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java @@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import org.junit.Test; @@ -106,5 +107,7 @@ public class ToStringStyleConcurrencyTest { for (final Future future : futures) { future.get(); } + threadPool.shutdown(); + threadPool.awaitTermination(1, TimeUnit.SECONDS); } } diff --git a/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java index 492df4aa3..c4805998a 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java @@ -17,7 +17,9 @@ package org.apache.commons.lang3.concurrent; import org.junit.Test; + import static org.junit.Assert.*; + import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -66,7 +68,7 @@ public class BackgroundInitializerTest { * Tests whether an external executor is correctly detected. */ @Test - public void testGetActiveExecutorExternal() { + public void testGetActiveExecutorExternal() throws InterruptedException { final ExecutorService exec = Executors.newSingleThreadExecutor(); try { final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl( @@ -76,6 +78,7 @@ public class BackgroundInitializerTest { checkInitialize(init); } finally { 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 */ @Test - public void testSetExternalExecutorAfterStart() throws ConcurrentException { + public void testSetExternalExecutorAfterStart() throws ConcurrentException, InterruptedException { final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl(); init.start(); + ExecutorService exec = Executors.newSingleThreadExecutor(); try { - init.setExternalExecutor(Executors.newSingleThreadExecutor()); + init.setExternalExecutor(exec); fail("Could set executor after start()!"); } catch (final IllegalStateException istex) { init.get(); + } finally { + exec.shutdown(); + exec.awaitTermination(1, TimeUnit.SECONDS); } } @@ -235,7 +242,7 @@ public class BackgroundInitializerTest { getThread.interrupt(); latch1.await(); exec.shutdownNow(); - exec.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); + exec.awaitTermination(1, TimeUnit.SECONDS); assertNotNull("No interrupted exception", iex.get()); } diff --git a/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java index b9028db66..ac78126f4 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.junit.Test; @@ -47,11 +48,13 @@ public class CallableBackgroundInitializerTest { * class. */ @Test - public void testInitExecutor() { + public void testInitExecutor() throws InterruptedException { final ExecutorService exec = Executors.newSingleThreadExecutor(); final CallableBackgroundInitializer init = new CallableBackgroundInitializer( new TestCallable(), exec); 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. */ @Test(expected=IllegalArgumentException.class) - public void testInitExecutorNullCallable() { + public void testInitExecutorNullCallable() throws InterruptedException { final ExecutorService exec = Executors.newSingleThreadExecutor(); - new CallableBackgroundInitializer(null, exec); + try { + new CallableBackgroundInitializer(null, exec); + } finally { + exec.shutdown(); + exec.awaitTermination(1, TimeUnit.SECONDS); + } + } /** diff --git a/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java b/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java index c22f9f783..72183c12e 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java @@ -26,6 +26,7 @@ import java.util.Iterator; import java.util.NoSuchElementException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import org.junit.Before; 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 */ @Test - public void testInitializeExternalExec() throws ConcurrentException { + public void testInitializeExternalExec() throws ConcurrentException, InterruptedException { final ExecutorService exec = Executors.newCachedThreadPool(); try { initializer = new MultiBackgroundInitializer(exec); @@ -162,6 +163,7 @@ public class MultiBackgroundInitializerTest { assertFalse("Executor was shutdown", exec.isShutdown()); } finally { 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 */ @Test - public void testInitializeChildWithExecutor() throws ConcurrentException { + public void testInitializeChildWithExecutor() throws ConcurrentException, InterruptedException { final String initExec = "childInitializerWithExecutor"; final ExecutorService exec = Executors.newSingleThreadExecutor(); try { @@ -187,6 +189,7 @@ public class MultiBackgroundInitializerTest { checkChild(c2, exec); } finally { exec.shutdown(); + exec.awaitTermination(1, TimeUnit.SECONDS); } } diff --git a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java index 2e985492e..fe110e90f 100644 --- a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java +++ b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java @@ -196,7 +196,7 @@ public class FastDateFormatTest { assertEquals(sdf.toPattern(), format.getPattern()); assertEquals(Locale.getDefault(), format.getLocale()); - assertEquals(TimeZone.getDefault(), format.getTimeZone()); + assertEquals(TimeZone.getDefault(), format.getTimeZone()); } @Test @@ -208,7 +208,7 @@ public class FastDateFormatTest { assertFalse(shortShort.equals(shortLong)); assertFalse(shortShort.equals(longShort)); - assertFalse(shortShort.equals(longLong)); + assertFalse(shortShort.equals(longLong)); assertFalse(shortLong.equals(longShort)); assertFalse(shortLong.equals(longLong)); assertFalse(longShort.equals(longLong)); @@ -308,11 +308,14 @@ public class FastDateFormatTest { e.printStackTrace(); } } - } + } }); } - pool.shutdown(); - if(!pool.awaitTermination(20, TimeUnit.SECONDS)) { + pool.shutdown(); + // 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(); fail("did not complete tasks"); }