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

garydgregory 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 f198e3b19 Clarify duration conversion contracts and expand tests
f198e3b19 is described below

commit f198e3b197e9deea4d9de83bdf0b56597f799363
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 10:56:37 2026 -0500

    Clarify duration conversion contracts and expand tests
    
    Document nanosecond bounds and conversion overflow in
    DurationUtils.accept. Verify callback invocation counts, numeric
    boundaries, and negative durations in ObjectUtils.wait and
    ThreadUtils.sleepQuietly.
    
    Assisted-By: OpenAI Codex
---
 src/changes/changes.xml                            |  1 +
 .../apache/commons/lang3/time/DurationUtils.java   |  6 ++--
 .../org/apache/commons/lang3/ObjectUtilsTest.java  | 11 ++++++++
 .../org/apache/commons/lang3/ThreadUtilsTest.java  |  6 ++++
 .../commons/lang3/time/DurationUtilsTest.java      | 33 +++++++++++++++++++++-
 5 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ff31ba3e6..6f4f35f17 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -103,6 +103,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Preserve interrupt status in BackgroundInitializer.isInitialized 
(#1879).</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Restrict MethodUtils.invokeStaticMethod to static overloads; both 
static invocation variants now throw NoSuchMethodException instead of 
NullPointerException when only an instance method matches (#1881).</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Handle Object methods locally in EventListenerSupport proxies, 
preventing NullPointerException from equals and hashCode and returning an 
identity string from toString.</action>
+    <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Preserve negative durations in DurationUtils.accept so 
ThreadUtils.sleep, ThreadUtils.sleepQuietly, ThreadUtils.join, and 
ObjectUtils.wait reject negative sub-millisecond timeouts (#1882).</action>
     <!-- ADD -->
     <!-- UPDATE -->
   </release>
diff --git a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java 
b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
index c64c843c2..e1c95721b 100644
--- a/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
+++ b/src/main/java/org/apache/commons/lang3/time/DurationUtils.java
@@ -47,14 +47,16 @@ public class DurationUtils {
 
     /**
      * Accepts the consumer with the duration split into milliseconds and 
nanoseconds.
+     *
      * <p>
-     * The nanosecond component is non-negative, and the millisecond component 
is rounded down so that the two components represent the original duration,
-     * including negative durations.
+     * The nanosecond component is between 0 and 999,999, inclusive, and the 
millisecond component is rounded down so that the two components represent the
+     * original duration, including negative durations.
      * </p>
      *
      * @param <T> The function exception.
      * @param consumer Accepting function.
      * @param duration The duration to pick apart.
+     * @throws ArithmeticException Thrown if converting the duration to 
milliseconds overflows.
      * @throws T Thrown as described in the function signature.
      * @see StopWatch
      */
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index fa816b865..c36ab2cb3 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -59,6 +59,8 @@
 import org.apache.commons.lang3.text.StrBuilder;
 import org.apache.commons.lang3.time.DurationUtils;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.mockito.MockedStatic;
 
 /**
@@ -909,6 +911,15 @@ void testWaitDuration() throws InterruptedException {
         }
     }
 
+    @ParameterizedTest
+    @ValueSource(longs = { -1, -999_999, -1_000_001 })
+    void testWaitDurationRejectsNegativeDuration(final long nanos) {
+        final Object lock = new Object();
+        synchronized (lock) {
+            assertIllegalArgumentException(() -> ObjectUtils.wait(lock, 
Duration.ofNanos(nanos)));
+        }
+    }
+
     @Test
     void testWaitDurationThrows() throws InterruptedException {
         assertThrows(IllegalArgumentException.class, () -> 
ObjectUtils.wait(new Object(), Duration.ofSeconds(-10)));
diff --git a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
index 25f6040de..1990fe4d3 100644
--- a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java
@@ -296,6 +296,12 @@ void testSleepQuietlyNormalNoInterrupt() {
         assertFalse(Thread.currentThread().isInterrupted(), "Interrupted flag 
should not be set for normal sleep");
     }
 
+    @ParameterizedTest
+    @ValueSource(longs = { -1, -999_999, -1_000_001 })
+    void testSleepQuietlyRejectsNegativeDuration(final long nanos) {
+        assertIllegalArgumentException(() -> 
ThreadUtils.sleepQuietly(Duration.ofNanos(nanos)));
+    }
+
     @Test
     void testSystemThreadGroupExists() {
         final ThreadGroup systemThreadGroup = 
ThreadUtils.getSystemThreadGroup();
diff --git a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
index 9b3f1e2ad..7782bbeba 100644
--- a/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/DurationUtilsTest.java
@@ -22,12 +22,14 @@
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
 import java.time.Duration;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.commons.lang3.AbstractLangTest;
 import org.apache.commons.lang3.math.NumberUtils;
@@ -42,14 +44,43 @@
  */
 class DurationUtilsTest extends AbstractLangTest {
 
+    @Test
+    void testAcceptMaxMillis() {
+        final AtomicInteger calls = new AtomicInteger();
+        DurationUtils.accept((millis, remainder) -> {
+            calls.incrementAndGet();
+            assertEquals(Long.MAX_VALUE, millis.longValue());
+            assertEquals(999_999, remainder.intValue());
+        }, Duration.ofMillis(Long.MAX_VALUE).plusNanos(999_999));
+        assertEquals(1, calls.get());
+    }
+
+    @ParameterizedTest
+    @ValueSource(longs = { Long.MIN_VALUE, Long.MAX_VALUE })
+    void testAcceptMillisOverflow(final long millis) {
+        final Duration duration = millis == Long.MIN_VALUE ? 
Duration.ofMillis(millis).minusNanos(1) : 
Duration.ofMillis(millis).plusMillis(1);
+        assertThrows(ArithmeticException.class, () -> 
DurationUtils.accept((value, remainder) -> fail("Consumer must not be called on 
overflow"), duration));
+    }
+
     @ParameterizedTest
-    @ValueSource(longs = { -1_000_000_001, -1_000_001, -1_000_000, -999_999, 
-1, 0, 1, 999_999, 1_000_000, 1_000_001, 1_000_000_001 })
+    @ValueSource(longs = { Long.MIN_VALUE, -1_000_000_001, -1_000_001, 
-1_000_000, -999_999, -1, 0, 1, 999_999, 1_000_000, 1_000_001, 1_000_000_001,
+        Long.MAX_VALUE })
     void testAcceptPreservesDuration(final long nanos) {
         final Duration duration = Duration.ofNanos(nanos);
+        final AtomicInteger calls = new AtomicInteger();
         DurationUtils.accept((millis, remainder) -> {
+            calls.incrementAndGet();
             assertEquals(duration, 
Duration.ofMillis(millis).plusNanos(remainder));
             assertTrue(remainder >= 0 && remainder < 1_000_000);
         }, duration);
+        assertEquals(1, calls.get());
+    }
+
+    @ParameterizedTest
+    @ValueSource(longs = { Long.MIN_VALUE, Long.MAX_VALUE })
+    void testAcceptSecondsOverflow(final long seconds) {
+        assertThrows(ArithmeticException.class,
+            () -> DurationUtils.accept((millis, remainder) -> fail("Consumer 
must not be called on overflow"), Duration.ofSeconds(seconds)));
     }
 
     @Test

Reply via email to