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/10/20 19:15:15 UTC
[commons-lang] branch master updated (30af12480 -> 9840b9df5)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
from 30af12480 Javbadoc and comments
new fc7320f98 Javadoc
new 9840b9df5 Try to fix random failures in StopWatchTest.testStopWatchSuspend()
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../org/apache/commons/lang3/time/StopWatch.java | 11 +++--
.../apache/commons/lang3/time/StopWatchTest.java | 56 ++++++++++++----------
2 files changed, 38 insertions(+), 29 deletions(-)
[commons-lang] 02/02: Try to fix random failures in StopWatchTest.testStopWatchSuspend()
Posted by gg...@apache.org.
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
commit 9840b9df5795a9d253611ac7cd451163470b2443
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Oct 20 15:15:10 2023 -0400
Try to fix random failures in StopWatchTest.testStopWatchSuspend()
Watch out comparing measurements from System.currentTimeMillis() vs.
System.nanoTime()
---
.../apache/commons/lang3/time/StopWatchTest.java | 56 ++++++++++++----------
1 file changed, 30 insertions(+), 26 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 8cb7d312e..44e375cde 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -75,7 +75,7 @@ public class StopWatchTest extends AbstractLangTest {
return watch;
}
- private void sleepQuietly(final Duration duration) throws InterruptedException {
+ private void sleep(final Duration duration) throws InterruptedException {
ThreadUtils.sleep(duration);
}
@@ -196,10 +196,10 @@ public class StopWatchTest extends AbstractLangTest {
@Test
public void testLang315() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
- sleepQuietly(MILLIS_200);
+ sleep(MILLIS_200);
watch.suspend();
final long suspendTime = watch.getTime();
- sleepQuietly(MILLIS_200);
+ sleep(MILLIS_200);
watch.stop();
final long totalTime = watch.getTime();
assertEquals(suspendTime, totalTime);
@@ -220,7 +220,7 @@ public class StopWatchTest extends AbstractLangTest {
public void testStopTimeSimple() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
final long testStartMillis = System.currentTimeMillis();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.stop();
final long testEndMillis = System.currentTimeMillis();
final long stopTime = watch.getStopTime();
@@ -250,7 +250,7 @@ public class StopWatchTest extends AbstractLangTest {
@Test
public void testStopWatchSimple() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.stop();
final long time = watch.getTime();
assertEquals(time, watch.getTime());
@@ -269,20 +269,20 @@ public class StopWatchTest extends AbstractLangTest {
assertEquals(ZERO_TIME_ELAPSED, watch.toString());
watch.start();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
assertTrue(watch.getTime() < 2000);
}
@Test
public void testStopWatchSplit() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.split();
final long splitTime = watch.getSplitTime();
final String splitStr = watch.toSplitString();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.unsplit();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.stop();
final long totalTime = watch.getTime();
@@ -301,35 +301,39 @@ public class StopWatchTest extends AbstractLangTest {
@Test
public void testStopWatchSuspend() throws InterruptedException {
+ // Watch out comparing measurements from System.currentTimeMillis() vs. System.nanoTime()
final StopWatch watch = StopWatch.createStarted();
final long testStartMillis = System.currentTimeMillis();
- sleepQuietly(MILLIS_550);
+ final long testStartNanos = System.nanoTime();
+ sleep(MILLIS_550);
watch.suspend();
final long testSuspendMillis = System.currentTimeMillis();
- final long testSuspendTime = testSuspendMillis - testStartMillis;
- final long suspendTime = watch.getTime();
- final long stopTime = watch.getStopTime();
+ final long testSuspendNanos = System.nanoTime();
+ final long testSuspendTimeNanos = testSuspendNanos - testStartNanos;
+ final long suspendTimeFromNanos = watch.getTime();
+ final long stopTimeMillis = watch.getStopTime();
- assertTrue(testStartMillis <= stopTime);
- assertTrue(testSuspendMillis <= stopTime);
+ assertTrue(testStartMillis <= stopTimeMillis);
+ assertTrue(testSuspendMillis <= stopTimeMillis);
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.resume();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.stop();
- final long totalTime = watch.getTime();
+ final long totalTimeFromNanos = watch.getTime();
- assertTrue(suspendTime >= 500, () -> "suspendTime = " + suspendTime);
- assertTrue(suspendTime <= testSuspendTime, () -> String.format("suspendTime = %,d, testSuspendTime = %,d", suspendTime, testSuspendTime));
- assertTrue(totalTime >= 1000, () -> "totalTime = " + totalTime);
+ assertTrue(suspendTimeFromNanos >= 500, () -> "suspendTime = " + suspendTimeFromNanos);
+ assertTrue(suspendTimeFromNanos <= testSuspendTimeNanos,
+ () -> String.format("suspendTime = %,d, testSuspendTime = %,d", suspendTimeFromNanos, testSuspendTimeNanos));
+ assertTrue(totalTimeFromNanos >= 1000, () -> "totalTime = " + totalTimeFromNanos);
// Be lenient for slow running builds
- assertTrue(totalTime < 2500, () -> "totalTime = " + totalTime);
+ assertTrue(totalTimeFromNanos < 2500, () -> "totalTime = " + totalTimeFromNanos);
}
@Test
public void testToSplitString() throws InterruptedException {
final StopWatch watch = StopWatch.createStarted();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
@@ -339,7 +343,7 @@ public class StopWatchTest extends AbstractLangTest {
public void testToSplitStringWithMessage() throws InterruptedException {
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");
@@ -349,7 +353,7 @@ public class StopWatchTest extends AbstractLangTest {
public void testToString() throws InterruptedException {
//
final StopWatch watch = StopWatch.createStarted();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.split();
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
@@ -361,7 +365,7 @@ public class StopWatchTest extends AbstractLangTest {
//
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
- sleepQuietly(MILLIS_550);
+ sleep(MILLIS_550);
watch.split();
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");
[commons-lang] 01/02: Javadoc
Posted by gg...@apache.org.
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
commit fc7320f9812d5140b7efd982e06ec60a97959cd5
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Oct 20 15:15:07 2023 -0400
Javadoc
---
src/main/java/org/apache/commons/lang3/time/StopWatch.java | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
index 0ab42fa8a..beefe77b6 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -212,12 +212,18 @@ public class StopWatch {
private long startTimeNanos;
/**
- * The start time in milliseconds - nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API.
+ * The start time in milliseconds.
+ * <p>
+ * nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API.
+ * </p>
*/
private long startTimeMillis;
/**
- * The end time in milliseconds - nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API.
+ * The end time in milliseconds.
+ * <p>
+ * nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API.
+ * </p>
*/
private long stopTimeMillis;
@@ -228,7 +234,6 @@ public class StopWatch {
/**
* Constructor.
- *
*/
public StopWatch() {
this(null);