You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2015/03/26 11:21:24 UTC

svn commit: r1669310 - in /commons/proper/lang/trunk/src: changes/ test/java/org/apache/commons/lang3/builder/ test/java/org/apache/commons/lang3/concurrent/ test/java/org/apache/commons/lang3/time/

Author: britter
Date: Thu Mar 26 10:21:24 2015
New Revision: 1669310

URL: http://svn.apache.org/r1669310
Log:
LANG-1091: Shutdown thread pools in test cases. This fixes #58 from github. Thanks to Fabian Lange.

Modified:
    commons/proper/lang/trunk/src/changes/changes.xml
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java
    commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java

Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Thu Mar 26 10:21:24 2015
@@ -22,6 +22,7 @@
   <body>
 
   <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-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>

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ReflectionToStringBuilderConcurrencyTest.java Thu Mar 26 10:21:24 2015
@@ -27,9 +27,9 @@ import java.util.concurrent.ExecutionExc
 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 ReflectionToStringBuilderCo
         for (final Future<Integer> future : futures) {
             Assert.assertEquals(REPEAT, future.get().intValue());
         }
+        threadPool.shutdown();
+        threadPool.awaitTermination(1, TimeUnit.SECONDS);
     }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/builder/ToStringStyleConcurrencyTest.java Thu Mar 26 10:21:24 2015
@@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionExc
 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 ToStringStyleConcurrencyTes
         for (final Future<Integer> future : futures) {
             future.get();
         }
+        threadPool.shutdown();
+        threadPool.awaitTermination(1, TimeUnit.SECONDS);
     }
 }

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/BackgroundInitializerTest.java Thu Mar 26 10:21:24 2015
@@ -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());
     }
 

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/CallableBackgroundInitializerTest.java Thu Mar 26 10:21:24 2015
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEqu
 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 CallableBackgroundInitializ
      * class.
      */
     @Test
-    public void testInitExecutor() {
+    public void testInitExecutor() throws InterruptedException {
         final ExecutorService exec = Executors.newSingleThreadExecutor();
         final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<Integer>(
                 new TestCallable(), exec);
         assertEquals("Executor not set", exec, init.getExternalExecutor());
+        exec.shutdown();
+        exec.awaitTermination(1, TimeUnit.SECONDS);
     }
 
     /**
@@ -59,9 +62,15 @@ public class CallableBackgroundInitializ
      * This should cause an exception.
      */
     @Test(expected=IllegalArgumentException.class)
-    public void testInitExecutorNullCallable() {
+    public void testInitExecutorNullCallable() throws InterruptedException {
         final ExecutorService exec = Executors.newSingleThreadExecutor();
-        new CallableBackgroundInitializer<Integer>(null, exec);
+        try {
+            new CallableBackgroundInitializer<Integer>(null, exec);
+        } finally {
+            exec.shutdown();
+            exec.awaitTermination(1, TimeUnit.SECONDS);
+        }
+        
     }
 
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/concurrent/MultiBackgroundInitializerTest.java Thu Mar 26 10:21:24 2015
@@ -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 MultiBackgroundInitializerT
      * @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 MultiBackgroundInitializerT
             assertFalse("Executor was shutdown", exec.isShutdown());
         } finally {
             exec.shutdown();
+            exec.awaitTermination(1, TimeUnit.SECONDS);
         }
     }
 
@@ -172,7 +174,7 @@ public class MultiBackgroundInitializerT
      * @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 MultiBackgroundInitializerT
             checkChild(c2, exec);
         } finally {
             exec.shutdown();
+            exec.awaitTermination(1, TimeUnit.SECONDS);
         }
     }
 

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java?rev=1669310&r1=1669309&r2=1669310&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java Thu Mar 26 10:21:24 2015
@@ -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");
         }