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 all commits
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
18 changes: 9 additions & 9 deletions src/main/java/net/datafaker/providers/base/Locality.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
public class Locality extends AbstractProvider<BaseProviders> {

private final List<String> locales;
private List<String> shuffledLocales = new ArrayList<>();
private final List<String> shuffledLocales = new ArrayList<>();
private int shuffledLocaleIndex = 0;

/**
* Constructor for Locality class
Expand Down Expand Up @@ -162,18 +163,17 @@ public String localeStringWithoutReplacement() {
* @param random random number generator (can utilize seed for deterministic random selection)
* @return String of a randomly selected locale (eg. "es", "es-MX")
*/
public String localeStringWithoutReplacement(Random random) {
if (this.shuffledLocales.isEmpty()) {
public synchronized String localeStringWithoutReplacement(Random random) {
if (shuffledLocales.isEmpty() || shuffledLocaleIndex >= shuffledLocales.size() - 1) {
// copy list of locales supported into shuffledLocales
shuffledLocales = new ArrayList<>(this.locales);
shuffledLocales.clear();
shuffledLocales.addAll(this.locales);
shuffledLocaleIndex = 0;
Collections.shuffle(shuffledLocales, random);
}

// retrieve next locale in shuffledLocales and remove from list
String pickedLocale = shuffledLocales.get(shuffledLocales.size() - 1);
shuffledLocales.remove(shuffledLocales.size() - 1);

return pickedLocale;
// retrieve next locale in shuffledLocales and increase the index
return shuffledLocales.get(shuffledLocaleIndex++);
}

}
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
Loading