Skip to content

Commit

Permalink
#1412 avoid swallowing errors happened in child threads in Issue759Test
Browse files Browse the repository at this point in the history
When any unexpected error happened in Issue759Test#WorkerThread, it was not logged anywhere, just disappeared.
_countDownLatch was not decreased, and the test failed with "Test did not complete within 12 second" error.
  • Loading branch information
asolntsev committed Nov 1, 2024
1 parent 0f50bbe commit 1dc01bb
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/test/java/net/datafaker/Issue759Test.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package net.datafaker;

import org.junit.jupiter.api.RepeatedTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static java.util.Collections.synchronizedList;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

class Issue759Test {
private static final List<Throwable> errors = synchronizedList(new ArrayList<>());
private static final Logger log = LoggerFactory.getLogger(Issue759Test.class);

private static class WorkerThread extends Thread {
private final Faker _faker;
private final int _maxIterations;
Expand All @@ -29,13 +37,17 @@ public void run() {
}

public static void fakeSomeData(Faker faker) {
String state = faker.address().stateAbbr();
String zipCode = faker.address().zipCodeByState(state);
try {
String state = faker.address().stateAbbr();
String zipCode = faker.address().zipCodeByState(state);
String county = faker.address().countyByZipCode(zipCode);
assertThat(county).isNotEqualTo(zipCode);
} catch (RuntimeException expected) {
assertThat(expected).hasMessageStartingWith("County is not configured for postcode " + zipCode);
} catch (RuntimeException | Error e) {
boolean expected = e.getMessage() != null && e.getMessage().startsWith("County is not configured ");
if (!expected) {
errors.add(e);
log.error("Failed to generate county", e);
}
}
}

Expand All @@ -59,5 +71,6 @@ void issue759Test() throws InterruptedException {
assertThat(countDownLatch.await(12, SECONDS))
.overridingErrorMessage("Test did not complete within 12 second")
.isTrue();
assertThat(errors).isEmpty();
}
}

0 comments on commit 1dc01bb

Please sign in to comment.