Skip to content

Commit aa47951

Browse files
fix: Make stopInstant be in sync with stopTimeNanos for split (#1610)
* fix: Make stopInstant in sync with stopTimeNanos * Fix trailing whitespace --------- Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
1 parent 185edc4 commit aa47951

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/main/java/org/apache/commons/lang3/time/StopWatch.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ public void split() {
689689
throw new IllegalStateException("Stopwatch is not running.");
690690
}
691691
stopTimeNanos = System.nanoTime();
692+
stopInstant = Instant.now();
692693
splitState = SplitState.SPLIT;
693694
splits.add(new Split(String.valueOf(splits.size()), Duration.ofNanos(stopTimeNanos - startTimeNanos)));
694695
}
@@ -710,6 +711,7 @@ public void split(final String label) {
710711
throw new IllegalStateException("Stopwatch is not running.");
711712
}
712713
stopTimeNanos = System.nanoTime();
714+
stopInstant = Instant.now();
713715
splitState = SplitState.SPLIT;
714716
splits.add(new Split(label, Duration.ofNanos(stopTimeNanos - startTimeNanos)));
715717
}
@@ -835,5 +837,4 @@ public void unsplit() {
835837
splitState = SplitState.UNSPLIT;
836838
splits.remove(splits.size() - 1);
837839
}
838-
839840
}

src/test/java/org/apache/commons/lang3/time/StopWatchTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
2121
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2223
import static org.junit.jupiter.api.Assertions.assertNull;
2324
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -422,6 +423,22 @@ void testSplitsWithStringLabels() {
422423
assertThrows(IllegalStateException.class, watch::unsplit);
423424
}
424425

426+
@Test
427+
void testSplitGetStopInstant() {
428+
final StopWatch watch = StopWatch.createStarted();
429+
watch.split();
430+
assertNotNull(watch.getStopTime());
431+
assertNotNull(watch.getStopInstant());
432+
}
433+
434+
@Test
435+
void testSplitWithLabelGetStopInstant() {
436+
final StopWatch watch = StopWatch.createStarted();
437+
watch.split("one");
438+
assertNotNull(watch.getStopTime());
439+
assertNotNull(watch.getStopInstant());
440+
}
441+
425442
@Test
426443
void testStatic() {
427444
final StopWatch watch = StopWatch.createStarted();

0 commit comments

Comments
 (0)