Skip to content

Commit c6ef41a

Browse files
committed
fix race condition
1 parent ec19e3b commit c6ef41a

1 file changed

Lines changed: 65 additions & 59 deletions

File tree

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

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

8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
810
import com.ctre.phoenix.ErrorCode;
911
import com.revrobotics.REVLibError;
1012
import com.revrobotics.CANSparkMax;
@@ -78,16 +80,6 @@ public int getDeviceId() {
7880

7981
}
8082

81-
private static final Object asyncPeriodicNotifier = new Object();
82-
83-
static {
84-
Lib199Subsystem.registerAsyncPeriodic(() -> {
85-
synchronized(asyncPeriodicNotifier) {
86-
asyncPeriodicNotifier.notifyAll();
87-
}
88-
});
89-
}
90-
9183
@Test
9284
public void testOkErrors() {
9385
errStream.reset();
@@ -100,7 +92,7 @@ public void testOkErrors() {
10092
assertEquals(0, errStream.toByteArray().length);
10193
MotorErrors.reportErrors((REVLibError)null, null);
10294
assertEquals(0, errStream.toByteArray().length);
103-
95+
10496
// Ok Status
10597
MotorErrors.reportError(ErrorCode.OK);
10698
assertEquals(0, errStream.toByteArray().length);
@@ -178,73 +170,87 @@ private void doTestReportSparkMaxTemp(int id) {
178170
String smartDashboardKey = "Port " + id + " Spark Max Temp";
179171
MotorErrors.reportSparkMaxTemp((CANSparkMax)spark, 40);
180172

181-
spark.setTemperature(20);
182-
spark.setSmartCurrentLimit(50);
183-
runAsyncPeriodic();
184-
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
185-
assertEquals(50, spark.getSmartCurrentLimit());
186-
187-
spark.setTemperature(20);
188-
spark.setSmartCurrentLimit(50);
189-
runAsyncPeriodic();
190-
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
191-
assertEquals(50, spark.getSmartCurrentLimit());
192-
193-
if(MotorErrors.kOverheatTripCount > 1) {
194-
spark.setTemperature(51);
173+
try(AutoCloseable asyncBlock = blockAsyncPeriodic()) {
174+
spark.setTemperature(20);
195175
spark.setSmartCurrentLimit(50);
196-
runAsyncPeriodic();
197-
assertEquals(51, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
176+
MotorErrors.doReportSparkMaxTemp();
177+
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
198178
assertEquals(50, spark.getSmartCurrentLimit());
199179

200180
spark.setTemperature(20);
201181
spark.setSmartCurrentLimit(50);
202-
runAsyncPeriodic();
182+
MotorErrors.doReportSparkMaxTemp();
203183
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
204184
assertEquals(50, spark.getSmartCurrentLimit());
205-
}
206185

207-
assertEquals(0, errStream.size());
186+
if(MotorErrors.kOverheatTripCount > 1) {
187+
spark.setTemperature(51);
188+
spark.setSmartCurrentLimit(50);
189+
MotorErrors.doReportSparkMaxTemp();
190+
assertEquals(51, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
191+
assertEquals(50, spark.getSmartCurrentLimit());
192+
193+
spark.setTemperature(20);
194+
spark.setSmartCurrentLimit(50);
195+
MotorErrors.doReportSparkMaxTemp();
196+
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
197+
assertEquals(50, spark.getSmartCurrentLimit());
198+
}
208199

209-
for(int i = 0; i < MotorErrors.kOverheatTripCount; i++) {
210-
assertEquals(50, spark.getSmartCurrentLimit());
211200
assertEquals(0, errStream.size());
212201

202+
for(int i = 0; i < MotorErrors.kOverheatTripCount; i++) {
203+
assertEquals(50, spark.getSmartCurrentLimit());
204+
assertEquals(0, errStream.size());
205+
206+
spark.setTemperature(51);
207+
spark.setSmartCurrentLimit(50);
208+
MotorErrors.doReportSparkMaxTemp();
209+
assertEquals(51, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
210+
}
211+
212+
assertNotEquals(0, errStream.size());
213+
errStream.reset();
214+
213215
spark.setTemperature(51);
214216
spark.setSmartCurrentLimit(50);
215-
runAsyncPeriodic();
217+
MotorErrors.doReportSparkMaxTemp();
216218
assertEquals(51, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
217-
}
219+
assertEquals(1, spark.getSmartCurrentLimit());
218220

219-
assertNotEquals(0, errStream.size());
220-
errStream.reset();
221-
222-
spark.setTemperature(51);
223-
spark.setSmartCurrentLimit(50);
224-
runAsyncPeriodic();
225-
assertEquals(51, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
226-
assertEquals(1, spark.getSmartCurrentLimit());
227-
228-
spark.setTemperature(20);
229-
spark.setSmartCurrentLimit(50);
230-
runAsyncPeriodic();
231-
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
232-
assertEquals(1, spark.getSmartCurrentLimit());
221+
spark.setTemperature(20);
222+
spark.setSmartCurrentLimit(50);
223+
MotorErrors.doReportSparkMaxTemp();
224+
assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01);
225+
assertEquals(1, spark.getSmartCurrentLimit());
233226

234-
assertEquals(0, errStream.size());
227+
assertEquals(0, errStream.size());
228+
} catch(Exception e) {
229+
assumeNoException(e);
230+
}
235231
}
236232

237-
// Ensures an update to the asynchronous periodic thread is run
238-
private void runAsyncPeriodic() {
239-
try {
240-
synchronized(asyncPeriodicNotifier) {
241-
// Run twice because we don't know in what order we're called, so make sure all periodic methods are run twice
242-
asyncPeriodicNotifier.wait();
243-
asyncPeriodicNotifier.wait();
233+
// Blocks the Lib199Subsystem's async thread until closed
234+
private AutoCloseable blockAsyncPeriodic() {
235+
AtomicBoolean block = new AtomicBoolean(true);
236+
Object lock = new Object();
237+
Lib199Subsystem.registerAsyncPeriodic(() -> {
238+
synchronized(lock) {
239+
while(block.get()) {
240+
try {
241+
lock.wait();
242+
} catch(InterruptedException e) {
243+
assumeNoException(e);
244+
}
245+
}
244246
}
245-
} catch(InterruptedException e) {
246-
assumeNoException(e);
247-
}
247+
});
248+
return () -> {
249+
block.set(false);
250+
synchronized(lock) {
251+
lock.notifyAll();
252+
}
253+
};
248254
}
249255

250256
}

0 commit comments

Comments
 (0)