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 ed8caf905 Fix intermittent StopWatchTest.testSuspend timing failure
ed8caf905 is described below
commit ed8caf905223e0038c257fb08bafb8bf99c60fe4
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 15 09:49:29 2026 -0500
Fix intermittent StopWatchTest.testSuspend timing failure
Measure enclosing intervals with System.nanoTime() before starting and
after suspending the watch. The previous interval excluded time counted
by the watch and relied on an unreliable 1 ms allowance.
Replace the milliseconds-to-nanoseconds comparison with consistent
Duration comparisons and explicit millisecond conversion checks. Assert
that elapsed time stays unchanged while suspended and advances after
resuming. Replace the fixed two-second ceiling with a measured upper
bound for the resumed interval.
Validation:
- All 41 StopWatchTest tests passed on Java 17.
- 150 testSuspend invocations passed on each of Java 8 and Java 17,
including delays that reproduce the original failure.
- Additional 2.1-second delay checks passed on both JDKs.
- Five deliberately introduced suspend/resume and unit-conversion
defects were detected on both JDKs.
- Full mvn -B -ntp passed: 107,531 tests, zero failures or errors,
18 skipped, and all repository checks passed.
Assisted-By: OpenAI Codex
---
.../apache/commons/lang3/time/StopWatchTest.java | 52 ++++++++++------------
1 file changed, 23 insertions(+), 29 deletions(-)
diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
index 859f486d5..c48a86b95 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -491,21 +491,18 @@ void testStopTimeSimple() throws InterruptedException {
@Test
void testSuspend() throws InterruptedException {
- // Watch out comparing measurements from System.currentTimeMillis()
vs. System.nanoTime()
+ // Enclose the watch's active interval with measurements from the same
monotonic clock.
+ final long testStartNanos = System.nanoTime();
final StopWatch watch = StopWatch.createStarted();
final long testStartMillis = System.currentTimeMillis();
- final long testStartNanos = System.nanoTime();
final Instant testStartInstant = Instant.ofEpochMilli(testStartMillis);
final Duration sleepDuration = MIN_DURATION;
- final long sleepMillis = sleepDuration.toMillis();
sleepPlus1(sleepDuration);
final long testSuspendMillis = System.currentTimeMillis();
- final long testSuspendNanos = System.nanoTime();
watch.suspend();
- final long testSuspendTimeNanos = testSuspendNanos - testStartNanos;
- // See sleepPlus1
- final Duration testSuspendDuration =
Duration.ofNanos(testSuspendTimeNanos).plusMillis(1);
- final long suspendTimeFromNanos = watch.getTime();
+ final long testSuspendNanos = System.nanoTime();
+ final Duration testSuspendDuration = Duration.ofNanos(testSuspendNanos
- testStartNanos);
+ final long suspendTimeMillis = watch.getTime();
final Duration suspendDuration = watch.getDuration();
final long stopTimeMillis = watch.getStopTime();
final Instant stopInstant = watch.getStopInstant();
@@ -515,34 +512,31 @@ void testSuspend() throws InterruptedException {
assertTrue(testSuspendMillis <= stopTimeMillis, () ->
String.format("testSuspendMillis %s <= stopTimeMillis %s", testSuspendMillis,
stopTimeMillis));
assertTrue(testSuspendMillis <= stopInstant.toEpochMilli(),
() -> String.format("testSuspendMillis %s <= stopInstant %s",
testSuspendMillis, stopInstant));
+ assertEquals(suspendDuration.toMillis(), suspendTimeMillis);
+ assertTrue(suspendDuration.compareTo(sleepDuration) >= 0,
+ () -> String.format("suspendDuration %s >= sleepDuration %s",
suspendDuration, sleepDuration));
+ assertTrue(suspendDuration.compareTo(testSuspendDuration) <= 0,
+ () -> String.format("suspendDuration %s <= testSuspendDuration
%s", suspendDuration, testSuspendDuration));
sleepPlus1(sleepDuration);
+ assertEquals(suspendDuration, watch.getDuration());
+ assertEquals(suspendTimeMillis, watch.getTime());
+
+ // Measure only the resumed interval so the upper bound excludes time
spent suspended.
+ final long testResumeNanos = System.nanoTime();
watch.resume();
sleepPlus1(sleepDuration);
watch.stop();
- final long totalTimeFromNanos = watch.getTime();
+ final long testStopNanos = System.nanoTime();
+ final Duration testResumeDuration = Duration.ofNanos(testStopNanos -
testResumeNanos);
final Duration totalDuration = watch.getDuration();
+ final Duration resumeDuration = totalDuration.minus(suspendDuration);
- assertTrue(suspendTimeFromNanos >= sleepMillis, () ->
String.format("suspendTimeFromNanos %s >= sleepMillis %s",
suspendTimeFromNanos, sleepMillis));
- assertTrue(suspendDuration.compareTo(Duration.ofMillis(sleepMillis))
>= 0,
- () -> String.format("suspendDuration %s >= sleepMillis %s",
suspendDuration, sleepMillis));
- assertTrue(suspendTimeFromNanos <= testSuspendTimeNanos,
- () -> String.format("suspendTimeFromNanos %s <=
testSuspendTimeNanos %s", suspendTimeFromNanos, testSuspendTimeNanos));
- assertTrue(suspendDuration.compareTo(testSuspendDuration) <= 0,
- () -> String.format("suspendDuration %s <= testSuspendDuration
%s", suspendDuration, testSuspendDuration));
-
- final long sleepMillisX2 = sleepMillis + sleepMillis;
- assertTrue(totalTimeFromNanos >= sleepMillisX2, () ->
String.format("totalTimeFromNanos %s >= sleepMillisX2 %s", totalTimeFromNanos,
sleepMillisX2));
- assertTrue(totalDuration.compareTo(Duration.ofMillis(sleepMillisX2))
>= 0,
- () -> String.format("totalDuration >= sleepMillisX2",
totalDuration, sleepMillisX2));
-
- // Be lenient for slow running builds
- final long testTooLongMillis = sleepMillis * 100;
- assertTrue(totalTimeFromNanos < testTooLongMillis,
- () -> String.format("totalTimeFromNanos %s < testTooLongMillis
%s", totalTimeFromNanos, testTooLongMillis));
-
assertTrue(totalDuration.compareTo(Duration.ofMillis(testTooLongMillis)) < 0,
- () -> String.format("totalDuration %s < testTooLongMillis %s",
totalDuration, testTooLongMillis));
-
+ assertEquals(totalDuration.toMillis(), watch.getTime());
+ assertTrue(resumeDuration.compareTo(sleepDuration) >= 0,
+ () -> String.format("resumeDuration %s >= sleepDuration %s",
resumeDuration, sleepDuration));
+ assertTrue(resumeDuration.compareTo(testResumeDuration) <= 0,
+ () -> String.format("resumeDuration %s <= testResumeDuration
%s", resumeDuration, testResumeDuration));
}
@Test