Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tests #1204

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix flaky test TimeTest
Sometimes if failed when "now + 1 minute" was in tomorrow.

For example:
java.lang.IllegalArgumentException: Invalid time range: the upper bound time (00:00:47.593917390) is before the lower bound (23:59:47.593917390)
  • Loading branch information
asolntsev committed May 22, 2024
commit e24f731384e955c622fcf9794707ef3e3a9d247f
2 changes: 1 addition & 1 deletion src/main/java/net/datafaker/providers/base/Time.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public String past(int atMost, int minimum, ChronoUnit unit, String pattern) {
*/
public long between(LocalTime from, LocalTime to) throws IllegalArgumentException {
if (to.isBefore(from)) {
throw new IllegalArgumentException("Invalid time range, the upper bound time is before the lower bound.");
throw new IllegalArgumentException("Invalid time range: the upper bound time (%s) is before the lower bound (%s)".formatted(to, from));
}

if (from.equals(to)) {
Expand Down
21 changes: 16 additions & 5 deletions src/test/java/net/datafaker/providers/base/TimeTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package net.datafaker.providers.base;

import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TimeTest extends BaseFakerTest<BaseFaker> {

private static final Pattern RE_TIME_BETWEEN = Pattern.compile("[0-2][0-9]:[0-5][0-9]:[0-5][0-9]");
private static final long NANOSECONDS_IN_DAY = 24L * 60 * 60 * 1000 * 1000_000L;
private static final long NANOSECONDS_IN_MINUTE = 60 * 1000 * 1000_000L;

@Test
void testFutureTime() {
LocalTime now = LocalTime.now();
Expand Down Expand Up @@ -80,16 +87,20 @@ void testBetweenThenLargerThanNow() {
LocalTime then = now.plusSeconds(1);
assertThatThrownBy(() -> faker.time().between(then, now))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid time range, the upper bound time is before the lower bound.");
.hasMessage("Invalid time range: the upper bound time (%s) is before the lower bound (%s)".formatted(now, then));
}

@Test
@RepeatedTest(10)
void testBetweenWithMask() {
String pattern = "mm:hh:ss";
LocalTime now = LocalTime.now();
String pattern = "HH:mm:ss";
LocalTime now = LocalTime.ofNanoOfDay((long) (Math.random() * (NANOSECONDS_IN_DAY - NANOSECONDS_IN_MINUTE - 1)));
LocalTime then = now.plusMinutes(1);

DateTimeFormatter.ofPattern(pattern).parse(faker.time().between(now, then, pattern));
String result = faker.time().between(now, then, pattern);
assertThat(result).matches(RE_TIME_BETWEEN);
TemporalAccessor timeBetween = DateTimeFormatter.ofPattern(pattern).parse(result);
assertThat(timeBetween.query(LocalTime::from)).isAfter(now.minusSeconds(1));
assertThat(timeBetween.query(LocalTime::from)).isBefore(then.plusSeconds(1));
}

@Test
Expand Down