Skip to content

Commit

Permalink
Faster StopWatchTest
Browse files Browse the repository at this point in the history
- These string formatting tests do not need to wait 500 millis each
- Refactor magic number into a constant
  • Loading branch information
garydgregory committed Sep 4, 2024
1 parent 386c17f commit 6dd2add
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/
public class StopWatchTest extends AbstractLangTest {

private static final int SPLIT_CLOCK_STR_LEN = 12;
private static final Duration TWO_MILLISECOND = Duration.ofMillis(2);
private static final Duration MILLIS_200 = Duration.ofMillis(200);
private static final Duration MILLIS_550 = Duration.ofMillis(550);
Expand Down Expand Up @@ -325,7 +326,7 @@ public void testSplit() throws InterruptedException {
final long splitTime = watch.getSplitTime();
final Duration splitDuration = watch.getSplitDuration();
assertEquals(splitTime, watch.getSplitDuration().toMillis());
assertEquals(12, watch.toSplitString().length(), "Formatted split string not the correct length");
assertEquals(SPLIT_CLOCK_STR_LEN, watch.toSplitString().length(), "Formatted split string not the correct length");
sleep(MILLIS_550);
// slept ~1100 millis
watch.unsplit();
Expand Down Expand Up @@ -418,27 +419,27 @@ public void testToSplitString() throws InterruptedException {
sleep(TWO_MILLISECOND);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(12, splitStr.length(), "Formatted split string not the correct length");
assertEquals(SPLIT_CLOCK_STR_LEN, splitStr.length(), "Formatted split string not the correct length");
}

@Test
public void testToSplitStringWithMessage() throws InterruptedException {
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
sleep(MILLIS_550);
sleep(TWO_MILLISECOND);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(12 + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length");
assertEquals(SPLIT_CLOCK_STR_LEN + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length");
}

@Test
public void testToString() throws InterruptedException {
//
final StopWatch watch = StopWatch.createStarted();
sleep(MILLIS_550);
sleep(TWO_MILLISECOND);
watch.split();
final String splitStr = watch.toString();
assertEquals(12, splitStr.length(), "Formatted split string not the correct length");
assertEquals(SPLIT_CLOCK_STR_LEN, splitStr.length(), "Formatted split string not the correct length");
}

@Test
Expand All @@ -447,9 +448,9 @@ public void testToStringWithMessage() throws InterruptedException {
//
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
sleep(MILLIS_550);
sleep(TWO_MILLISECOND);
watch.split();
final String splitStr = watch.toString();
assertEquals(12 + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length");
assertEquals(SPLIT_CLOCK_STR_LEN + MESSAGE.length() + 1, splitStr.length(), "Formatted split string not the correct length");
}
}

0 comments on commit 6dd2add

Please sign in to comment.