Skip to content

Commit 39e6a27

Browse files
committed
fix: fix flaky tests
1 parent 8f43e8e commit 39e6a27

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

core/src/test/java/ai/timefold/solver/core/testutil/AbstractMeterTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import org.junit.jupiter.api.AfterEach;
1010
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.parallel.Execution;
12+
import org.junit.jupiter.api.parallel.ExecutionMode;
1113
import org.junit.jupiter.api.parallel.ResourceLock;
1214

1315
import io.micrometer.core.instrument.Metrics;
@@ -16,7 +18,10 @@
1618
import io.micrometer.core.instrument.simple.SimpleConfig;
1719
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
1820

19-
@ResourceLock(value = "metrics") // Mutual exclusion to prevent flaky tests.
21+
// Mutual exclusion to prevent flaky tests from different test suites
22+
@ResourceLock(value = "metrics")
23+
// We also want to ensure that tests of the same suite do not update the registry concurrently
24+
@Execution(ExecutionMode.SAME_THREAD)
2025
public abstract class AbstractMeterTest {
2126

2227
@BeforeEach // To guard against nasty tests which do not do this.

core/src/test/java/ai/timefold/solver/core/testutil/PlannerTestUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ public static <Solution_> SolverConfig buildSolverConfig(Class<Solution_> soluti
7070
new TerminationConfig().withStepCountLimit(TERMINATION_STEP_COUNT_LIMIT)));
7171
}
7272

73-
public static <Solution_> Solution_ solve(SolverConfig solverConfig, Solution_ problem) {
73+
public static synchronized <Solution_> Solution_ solve(SolverConfig solverConfig, Solution_ problem) {
7474
return solve(solverConfig, problem, true);
7575
}
7676

77-
public static <Solution_> Solution_ solve(SolverConfig solverConfig, Solution_ problem, boolean bestSolutionEventExists) {
77+
public static synchronized <Solution_> Solution_ solve(SolverConfig solverConfig, Solution_ problem,
78+
boolean bestSolutionEventExists) {
7879
SolverFactory<Solution_> solverFactory = SolverFactory.create(solverConfig);
7980
var solver = solverFactory.buildSolver();
8081
var eventBestSolutionRef = new AtomicReference<Solution_>();
@@ -100,10 +101,10 @@ public static TestdataSolution generateTestdataSolution(String code, int entityA
100101
var solution = new TestdataSolution(code);
101102
solution.setValueList(IntStream.range(1, entityAndValueCount + 1)
102103
.mapToObj(i -> new TestdataValue("v" + i))
103-
.collect(Collectors.toList()));
104+
.toList());
104105
solution.setEntityList(IntStream.range(1, entityAndValueCount + 1)
105106
.mapToObj(i -> new TestdataEntity("e" + i))
106-
.collect(Collectors.toList()));
107+
.toList());
107108
return solution;
108109
}
109110

0 commit comments

Comments
 (0)