Skip to content

Commit 1ff25a1

Browse files
committed
fix race condition (again)
1 parent 928c70a commit 1ff25a1

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/test/java/org/carlmontrobotics/lib199/MotorErrorsTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.Assert.assertTrue;
66
import static org.junit.Assume.assumeNoException;
77

8+
import java.util.concurrent.CountDownLatch;
89
import java.util.concurrent.atomic.AtomicBoolean;
910

1011
import com.ctre.phoenix.ErrorCode;
@@ -234,21 +235,28 @@ private void doTestReportSparkMaxTemp(int id) {
234235
private AutoCloseable blockAsyncPeriodic() {
235236
AtomicBoolean block = new AtomicBoolean(true);
236237
Object lock = new Object();
238+
CountDownLatch latch = new CountDownLatch(1);
237239
Lib199Subsystem.registerAsyncPeriodic(() -> {
238240
synchronized(lock) {
239-
while(block.get()) {
241+
latch.countDown(); // Signal that we've started blocking
242+
while(block.get()) { // Block until released
240243
try {
241-
lock.wait();
244+
lock.wait(); // Wait for the block to be released
242245
} catch(InterruptedException e) {
243246
assumeNoException(e);
244247
}
245248
}
246249
}
247250
});
251+
try {
252+
latch.await(); // Wait for the async thread to start blocking
253+
} catch(InterruptedException e) {
254+
assumeNoException(e);
255+
}
248256
return () -> {
249-
block.set(false);
257+
block.set(false); // Release the block
250258
synchronized(lock) {
251-
lock.notifyAll();
259+
lock.notifyAll(); // Notify the async thread that the block has been released
252260
}
253261
};
254262
}

0 commit comments

Comments
 (0)