Skip to content

Commit

Permalink
fix: make the tests more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
zepfred committed Jun 25, 2024
1 parent 435c3ef commit 87f85c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ void consumeFinalBestSolution(Solution_ finalBestSolution) {
if (bestSolutionConsumer != null) {
scheduleIntermediateBestSolutionConsumption();
}
/**
* TODO - Do we need to ensure the first initialized solution is also consumed before the final solution?
* The first initialized solution consumer is always consumed before a local search phase, so I couldn't imagine
* a situation where the first solution consumption is missed.
*/
consumerExecutor.submit(() -> {
try {
finalBestSolutionConsumer.accept(finalBestSolution);
Expand Down Expand Up @@ -142,21 +137,25 @@ private void scheduleFirstInitializedSolutionConsumption() {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted when waiting for the first initialized solution consumption.");
}
try {
firstInitializedSolutionConsumer.accept(firstInitializedSolution);
// Clear the solution holder
firstInitializedSolution = null;
} catch (Throwable throwable) {
if (exceptionHandler != null) {
exceptionHandler.accept(problemId, throwable);
}
} finally {
activeConsumption.release();
}
executeFirstInitializedSolutionConsumption();
}
}, consumerExecutor);
}

private void executeFirstInitializedSolutionConsumption() {
try {
firstInitializedSolutionConsumer.accept(firstInitializedSolution);
// Clear the solution holder
firstInitializedSolution = null;
} catch (Throwable throwable) {
if (exceptionHandler != null) {
exceptionHandler.accept(problemId, throwable);
}
} finally {
activeConsumption.release();
}
}

@Override
public void close() {
disposeConsumerThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ void solveWithFirstInitializedSolutionConsumer() throws ExecutionException, Inte

// Default configuration
SolverConfig solverConfig = PlannerTestUtils
.buildSolverConfig(TestdataSolution.class, TestdataEntity.class);
.buildSolverConfig(TestdataSolution.class, TestdataEntity.class)
.withTerminationConfig(new TerminationConfig()
.withUnimprovedMillisecondsSpentLimit(100L));
solverManager = SolverManager
.create(solverConfig, new SolverManagerConfig());
Function<Object, TestdataUnannotatedExtendedSolution> problemFinder = o -> new TestdataUnannotatedExtendedSolution(
Expand Down Expand Up @@ -334,7 +336,7 @@ void solveWithFirstInitializedSolutionConsumer() throws ExecutionException, Inte
.withPhases(new ConstructionHeuristicPhaseConfig(), new ConstructionHeuristicPhaseConfig(),
new LocalSearchPhaseConfig())
.withTerminationConfig(new TerminationConfig()
.withUnimprovedMillisecondsSpentLimit(1L));
.withUnimprovedMillisecondsSpentLimit(100L));
hasInitializedSolution.setFalse();
solverManager = SolverManager
.create(solverConfig, new SolverManagerConfig());
Expand All @@ -360,7 +362,7 @@ void solveWithFirstInitializedSolutionConsumer() throws ExecutionException, Inte
new ConstructionHeuristicPhaseConfig(),
new LocalSearchPhaseConfig())
.withTerminationConfig(new TerminationConfig()
.withUnimprovedMillisecondsSpentLimit(1L));
.withUnimprovedMillisecondsSpentLimit(100L));
solverManager = SolverManager
.create(solverConfig, new SolverManagerConfig());
problemFinder = o -> new TestdataUnannotatedExtendedSolution(
Expand All @@ -386,7 +388,7 @@ void solveWithFirstInitializedSolutionConsumer() throws ExecutionException, Inte
})),
new LocalSearchPhaseConfig())
.withTerminationConfig(new TerminationConfig()
.withUnimprovedMillisecondsSpentLimit(1L));
.withUnimprovedMillisecondsSpentLimit(100L));
solverManager = SolverManager
.create(solverConfig, new SolverManagerConfig());
problemFinder = o -> new TestdataUnannotatedExtendedSolution(
Expand Down Expand Up @@ -414,7 +416,7 @@ void solveWithFirstInitializedSolutionConsumer() throws ExecutionException, Inte
assertThat(hasInitializedSolution.booleanValue()).isFalse();
})))
.withTerminationConfig(new TerminationConfig()
.withUnimprovedMillisecondsSpentLimit(1L));
.withUnimprovedMillisecondsSpentLimit(100L));
solverManager = SolverManager
.create(solverConfig, new SolverManagerConfig());
problemFinder = o -> new TestdataUnannotatedExtendedSolution(
Expand Down

0 comments on commit 87f85c4

Please sign in to comment.