You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/08/16 17:56:32 UTC

[commons-lang] branch master updated: [LANG-1706] ThreadUtils find methods should not return null items #1098

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new e6796a0ec [LANG-1706] ThreadUtils find methods should not return null items #1098
e6796a0ec is described below

commit e6796a0ec6105f8c87e5330b51c8c9ee75df353f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Aug 16 13:56:26 2023 -0400

    [LANG-1706] ThreadUtils find methods should not return null items #1098
---
 src/changes/changes.xml                                 |  1 +
 src/main/java/org/apache/commons/lang3/ThreadUtils.java |  4 ++--
 .../java/org/apache/commons/lang3/ThreadUtilsTest.java  | 17 +++++++++++++++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 55e0335dd..aad9a5fa0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -54,6 +54,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action issue="LANG-1704" type="fix" dev="ggregory" due-to="Sung Ho Yoon">[Javadoc] Fix Incorrect Description in Processor isAarch64() #1093.</action>
     <action                   type="fix" dev="ggregory" due-to="ljacqu">[Javadoc] Point to right getShortClassName flavor in Javadoc for relevant notes #1097.</action>
     <action                   type="fix" dev="ggregory" due-to="hduelme">Improve performance of StringUtils.isMixedCase() #1096.</action>
+    <action issue="LANG-1706" type="fix" dev="ggregory" due-to="Alberto Fernández">ThreadUtils find methods should not return null items #1098.</action>
     <!-- ADD -->
     <action                   type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add Functions#function(Function).</action>
     <action                   type="add" dev="ggregory" due-to="Rob Spoor, Gary Gregory">Add FailableFunction#function(FailableFunction).</action>
diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java
index aa1250b3b..b71457de8 100644
--- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java
@@ -284,7 +284,7 @@ public class ThreadUtils {
             count = threadGroup.enumerate(threadGroups, recurse);
             //return value of enumerate() must be strictly less than the array size according to Javadoc
         } while (count >= threadGroups.length);
-        return Collections.unmodifiableCollection(Stream.of(threadGroups).filter(predicate).collect(Collectors.toList()));
+        return Collections.unmodifiableCollection(Stream.of(threadGroups).limit(count).filter(predicate).collect(Collectors.toList()));
     }
 
     /**
@@ -376,7 +376,7 @@ public class ThreadUtils {
             count = threadGroup.enumerate(threads, recurse);
             //return value of enumerate() must be strictly less than the array size according to javadoc
         } while (count >= threads.length);
-        return Collections.unmodifiableCollection(Stream.of(threads).filter(predicate).collect(Collectors.toList()));
+        return Collections.unmodifiableCollection(Stream.of(threads).limit(count).filter(predicate).collect(Collectors.toList()));
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
index 89e135cf3..638fb149e 100644
--- a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
@@ -31,7 +31,9 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.time.Duration;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.CountDownLatch;
 import java.util.function.Predicate;
 
@@ -148,6 +150,20 @@ public class ThreadUtilsTest extends AbstractLangTest {
         assertFalse(Modifier.isFinal(ThreadUtils.class.getModifiers()));
     }
 
+    @Test
+    public void testGetAllThreadGroupsDoesNotReturnNull() {
+        // LANG-1706 getAllThreadGroups and findThreadGroups should not return null items
+        Collection<ThreadGroup> threads = ThreadUtils.getAllThreadGroups();
+        assertEquals(0, threads.stream().filter(Objects::isNull).count());
+    }
+
+    @Test
+    public void testGetAllThreadsDoesNotReturnNull() {
+        // LANG-1706 getAllThreads and findThreads should not return null items
+        Collection<Thread> threads = ThreadUtils.getAllThreads();
+        assertEquals(0, threads.stream().filter(Objects::isNull).count());
+    }
+
     @Test
     public void testInvalidThreadId() {
         assertThrows(IllegalArgumentException.class, () -> ThreadUtils.findThreadById(-5L));
@@ -390,4 +406,5 @@ public class ThreadUtilsTest extends AbstractLangTest {
             alsot1.join();
         }
     }
+
 }