Skip to content

Commit 4117c61

Browse files
committed
fix flaky test TimeTest.testBetweenWithMask
Sometimes the test failed when * "moment + 1 second" appeared to be in tomorrow, or * "moment - 1 second" appeared to be in yesterday Example of failed build: https://github.com/datafaker-net/datafaker/actions/runs/23998847901/job/69991488685?pr=1789 ``` Expecting actual: 23:59:22 to be strictly before: 00:00:00.106916777 at net.datafaker.providers.base.TimeTest.testBetweenWithMask(TimeTest.java:107) ```
1 parent 47f4512 commit 4117c61

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/test/java/net/datafaker/providers/base/TimeTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
import java.time.temporal.TemporalAccessor;
1212
import java.util.regex.Pattern;
1313

14+
import static java.util.concurrent.TimeUnit.DAYS;
15+
import static java.util.concurrent.TimeUnit.MINUTES;
1416
import static org.assertj.core.api.Assertions.assertThat;
1517
import static org.assertj.core.api.Assertions.assertThatCode;
1618
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1719

1820
public class TimeTest {
1921

2022
private static final Pattern RE_TIME_BETWEEN = Pattern.compile("[0-2][0-9]:[0-5][0-9]:[0-5][0-9]");
21-
private static final long NANOSECONDS_IN_DAY = 24L * 60 * 60 * 1000 * 1000_000L;
22-
private static final long NANOSECONDS_IN_MINUTE = 60 * 1000 * 1000_000L;
23+
private static final long SECONDS_IN_DAY = DAYS.toSeconds(1);
24+
private static final long SECONDS_IN_MINUTE = MINUTES.toSeconds(1);
2325
private final Faker faker = new Faker();
2426

2527
@Test
@@ -97,14 +99,15 @@ void testBetweenThenLargerThanNow() {
9799
@RepeatedTest(10)
98100
void testBetweenWithMask() {
99101
String pattern = "HH:mm:ss";
100-
LocalTime now = LocalTime.ofNanoOfDay((long) (Math.random() * (NANOSECONDS_IN_DAY - NANOSECONDS_IN_MINUTE - 1)));
102+
long moment = faker.random().nextLong(0, SECONDS_IN_DAY - SECONDS_IN_MINUTE);
103+
LocalTime now = LocalTime.ofSecondOfDay(moment);
101104
LocalTime then = now.plusMinutes(1);
102105

103106
String result = faker.time().between(now, then, pattern);
104107
assertThat(result).matches(RE_TIME_BETWEEN);
105108
TemporalAccessor timeBetween = DateTimeFormatter.ofPattern(pattern).parse(result);
106-
assertThat(timeBetween.query(LocalTime::from)).isAfter(now.minusSeconds(1));
107-
assertThat(timeBetween.query(LocalTime::from)).isBefore(then.plusSeconds(1));
109+
assertThat(timeBetween.query(LocalTime::from)).isAfterOrEqualTo(now);
110+
assertThat(timeBetween.query(LocalTime::from)).isBeforeOrEqualTo(then);
108111
}
109112

110113
@Test

0 commit comments

Comments
 (0)