Skip to content

Commit

Permalink
Attempt to fix the issues with latches
Browse files Browse the repository at this point in the history
  • Loading branch information
triceo committed Feb 25, 2025
1 parent d21cf9e commit ae5f349
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -262,6 +263,7 @@ void checkDefaultMeters() {
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));

var updatedTime = new AtomicBoolean();
var latch = new CountDownLatch(1);
solver.addEventListener(event -> {
if (!updatedTime.get()) {
assertThat(meterRegistry.getMeters().stream().map(Meter::getId))
Expand Down Expand Up @@ -308,9 +310,16 @@ void checkDefaultMeters() {
Meter.Type.GAUGE));
updatedTime.set(true);
}
latch.countDown();
});
solver.solve(solution);

try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Assertions.fail("Failed waiting for the event to happen.", e);
}

// Score calculation and problem scale counts should be removed
// since registering multiple gauges with the same id
// make it return the average, and the solver holds
Expand Down Expand Up @@ -349,6 +358,7 @@ void checkDefaultMetersTags() {
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));

var updatedTime = new AtomicBoolean();
var latch = new CountDownLatch(1);
solver.addEventListener(event -> {
if (!updatedTime.get()) {
assertThat(meterRegistry.getMeters().stream().map(Meter::getId))
Expand Down Expand Up @@ -395,9 +405,16 @@ void checkDefaultMetersTags() {
Meter.Type.GAUGE));
updatedTime.set(true);
}
latch.countDown();
});
solver.solve(solution);

try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Assertions.fail("Failed waiting for the event to happen.", e);
}

// Score calculation and problem scale counts should be removed
// since registering multiple gauges with the same id
// make it return the average, and the solver holds
Expand Down Expand Up @@ -434,6 +451,7 @@ void solveMetrics() {
solution.setValueList(Arrays.asList(new TestdataValue("v1"), new TestdataValue("v2")));
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));

var latch = new CountDownLatch(1);
var updatedTime = new AtomicBoolean();
solver.addEventListener(event -> {
if (!updatedTime.get()) {
Expand All @@ -454,9 +472,15 @@ void solveMetrics() {
.isEqualTo(2L);
updatedTime.set(true);
}
latch.countDown();
});
solution = solver.solve(solution);

try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Assertions.fail("Failed waiting for the event to happen.", e);
}
meterRegistry.publish();
assertThat(solution).isNotNull();
assertThat(solution.getScore().isSolutionInitialized()).isTrue();
Expand Down Expand Up @@ -575,6 +599,7 @@ void solveBestScoreMetrics() {
solution.setEntityList(Arrays.asList(new TestdataEntity("e1"), new TestdataEntity("e2")));
var step = new AtomicInteger(-1);

var latch = new CountDownLatch(1);
solver.addEventListener(event -> {
meterRegistry.publish();

Expand All @@ -599,9 +624,15 @@ void solveBestScoreMetrics() {
.isEqualTo(2);
}
step.incrementAndGet();
latch.countDown();
});
solution = solver.solve(solution);

try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
fail("Failed waiting for the event to happen.", e);
}
assertThat(step.get()).isEqualTo(2);
meterRegistry.publish();
assertThat(solution).isNotNull();
Expand Down

0 comments on commit ae5f349

Please sign in to comment.