|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 | import static org.junit.Assert.assertNotEquals; |
| 5 | +import static org.junit.Assume.assumeNoException; |
5 | 6 |
|
6 | 7 | import com.ctre.phoenix.ErrorCode; |
7 | 8 | import com.revrobotics.REVLibError; |
|
12 | 13 | import org.junit.Test; |
13 | 14 |
|
14 | 15 | import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
15 | | -import edu.wpi.first.wpilibj2.command.CommandScheduler; |
16 | 16 |
|
17 | 17 | public class MotorErrorsTest extends ErrStreamTest { |
18 | 18 |
|
@@ -77,6 +77,16 @@ public int getDeviceId() { |
77 | 77 |
|
78 | 78 | } |
79 | 79 |
|
| 80 | + private static final Object asyncPeriodicNotifier = new Object(); |
| 81 | + |
| 82 | + static { |
| 83 | + Lib199Subsystem.registerAsyncPeriodic(() -> { |
| 84 | + synchronized(asyncPeriodicNotifier) { |
| 85 | + asyncPeriodicNotifier.notifyAll(); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
80 | 90 | @Test |
81 | 91 | public void testOkErrors() { |
82 | 92 | errStream.reset(); |
@@ -165,30 +175,43 @@ private void doTestReportSparkMaxTemp(int id) { |
165 | 175 | MotorErrors.reportSparkMaxTemp((CANSparkMax)spark, 40); |
166 | 176 | spark.setSmartCurrentLimit(50); |
167 | 177 | spark.setTemperature(20); |
168 | | - CommandScheduler.getInstance().run(); |
| 178 | + runAsyncPeriodic(); |
169 | 179 | String smartDashboardKey = "Port " + id + " Spark Max Temp"; |
170 | 180 | assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01); |
171 | 181 | assertEquals(50, spark.getSmartCurrentLimit()); |
172 | 182 | spark.setTemperature(20); |
173 | | - CommandScheduler.getInstance().run(); |
| 183 | + runAsyncPeriodic(); |
174 | 184 | assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01); |
175 | 185 | assertEquals(50, spark.getSmartCurrentLimit()); |
176 | 186 | assertEquals(0, errStream.size()); |
177 | 187 | spark.setTemperature(40); |
178 | | - CommandScheduler.getInstance().run(); |
| 188 | + runAsyncPeriodic(); |
179 | 189 | assertEquals(40, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01); |
180 | 190 | assertEquals(1, spark.getSmartCurrentLimit()); |
181 | 191 | assertNotEquals(0, errStream.size()); |
182 | 192 | errStream.reset(); |
183 | 193 | spark.setTemperature(50); |
184 | | - CommandScheduler.getInstance().run(); |
| 194 | + runAsyncPeriodic(); |
185 | 195 | assertEquals(50, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01); |
186 | 196 | assertEquals(1, spark.getSmartCurrentLimit()); |
187 | 197 | spark.setTemperature(20); |
188 | | - CommandScheduler.getInstance().run(); |
| 198 | + runAsyncPeriodic(); |
189 | 199 | assertEquals(20, SmartDashboard.getNumber(smartDashboardKey, 0), 0.01); |
190 | 200 | assertEquals(1, spark.getSmartCurrentLimit()); |
191 | 201 | assertEquals(0, errStream.size()); |
192 | 202 | } |
193 | 203 |
|
| 204 | + // Ensures an update to the asynchronous periodic thread is run |
| 205 | + private void runAsyncPeriodic() { |
| 206 | + try { |
| 207 | + synchronized(asyncPeriodicNotifier) { |
| 208 | + // Run twice because we don't know in what order we're called, so make sure all periodic methods are run twice |
| 209 | + asyncPeriodicNotifier.wait(); |
| 210 | + asyncPeriodicNotifier.wait(); |
| 211 | + } |
| 212 | + } catch(InterruptedException e) { |
| 213 | + assumeNoException(e); |
| 214 | + } |
| 215 | + } |
| 216 | + |
194 | 217 | } |
0 commit comments