Skip to content

Commit

Permalink
Fix flaky tests with Locality and TimeTest
Browse files Browse the repository at this point in the history
* 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)

* flaky test with Locality

Sometimes if failed when two parallel threads modify field `shuffledLocales`.

For example:
```
net.datafaker.integration.FakerRepeatabilityIntegrationTest.shouldCreateUniqueValues -- Time elapsed: 0.890 s <<< ERROR!
java.lang.IndexOutOfBoundsException: Index 69 out of bounds for length 69
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:361)
	at java.base/java.util.ArrayList.remove(ArrayList.java:504)
	at net.datafaker.providers.base.Locality.localeStringWithoutReplacement(Locality.java:174)
	at net.datafaker.providers.base.Locality.localeStringWithoutReplacement(Locality.java:156)
```

* optimize memory usage by Locality.shuffledLocales

it's not needed to remove the element from shuffledLocales and resize the underlying array.
It's enough just to keep an index of current element in this list.
  • Loading branch information
asolntsev committed May 22, 2024
1 parent 7f332b1 commit bf92b8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
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

0 comments on commit bf92b8e

Please sign in to comment.