Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyHirstROCC committed Aug 16, 2022
1 parent 5063763 commit 262e097
Showing 1 changed file with 62 additions and 0 deletions.
Expand Up @@ -13,6 +13,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -184,4 +185,65 @@ void shutdownMainThreadAbruptly() throws InterruptedException {
.hasRootCauseExactlyInstanceOf(InterruptedException.class);
}

@Test
@Timeout(60)
void solvePartitionedWithProblemChange() throws InterruptedException {

// Uncomment below to run a non-partitioned daemon solver
/*
SolverConfig solverConfig = PlannerTestUtils.buildSolverConfig(TestdataSolution.class, TestdataEntity.class);
solverConfig.setDaemon(true); // Avoid terminating the solver too quickly.
SolverFactory<TestdataSolution> solverFactory = SolverFactory.create(solverConfig);
Solver<TestdataSolution> solver = solverFactory.buildSolver();
*/

//create a partitioned daemon solver
SolverFactory<TestdataSolution> solverFactory = createSolverFactory(true, SolverConfig.MOVE_THREAD_COUNT_NONE,
1);
Solver<TestdataSolution> solver = solverFactory.buildSolver();

final int valueCount = 4;
TestdataSolution solution = TestdataSolution.generateSolution(valueCount, valueCount);

AtomicReference<TestdataSolution> bestSolution = new AtomicReference<>();
CountDownLatch solutionWithProblemChangeReceived = new CountDownLatch(1);

CountDownLatch solvingStarted = new CountDownLatch(1);
((DefaultSolver<TestdataSolution>) solver).addPhaseLifecycleListener(
new PhaseLifecycleListenerAdapter<TestdataSolution>() {
@Override
public void solvingStarted(SolverScope<TestdataSolution> solverScope) {
solvingStarted.countDown();
}
});


solver.addEventListener(bestSolutionChangedEvent -> {
if (bestSolutionChangedEvent.isEveryProblemChangeProcessed()) {
TestdataSolution newBestSolution = bestSolutionChangedEvent.getNewBestSolution();
if (newBestSolution.getValueList().size() == valueCount + 1) {
bestSolution.set(newBestSolution);
solutionWithProblemChangeReceived.countDown();
}
}
});

ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
solver.solve(solution);
});

solvingStarted.await(); // Make sure we submit a ProblemChange only after the Solver started solving.
solver.addProblemChange((workingSolution, problemChangeDirector) -> {
problemChangeDirector.addProblemFact(new TestdataValue("added value"), solution.getValueList()::add);
});

solutionWithProblemChangeReceived.await();
assertThat(bestSolution.get().getValueList()).hasSize(valueCount + 1);

solver.terminateEarly();
executorService.shutdown();
}


}

0 comments on commit 262e097

Please sign in to comment.