22
33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertNotEquals ;
5+ import static org .junit .Assert .assertTrue ;
56import static org .junit .Assume .assumeNoException ;
67
8+ import java .util .concurrent .atomic .AtomicBoolean ;
9+
710import com .ctre .phoenix .ErrorCode ;
811import com .revrobotics .REVLibError ;
912import com .revrobotics .CANSparkMax ;
@@ -45,7 +48,7 @@ public static interface TemperatureSparkMax {
4548
4649 public static class Instance {
4750
48- private int smartCurrentLimit = 50 ;
51+ private int smartCurrentLimit = MotorConfig . NEO . currentLimitAmps ;
4952 private double temperature = 30 ;
5053 private final int id ;
5154
@@ -69,24 +72,14 @@ public REVLibError setSmartCurrentLimit(int limit) {
6972 smartCurrentLimit = limit ;
7073 return REVLibError .kOk ;
7174 }
72-
75+
7376 public int getDeviceId () {
7477 return id ;
7578 }
7679 }
7780
7881 }
7982
80- private static final Object asyncPeriodicNotifier = new Object ();
81-
82- static {
83- Lib199Subsystem .registerAsyncPeriodic (() -> {
84- synchronized (asyncPeriodicNotifier ) {
85- asyncPeriodicNotifier .notifyAll ();
86- }
87- });
88- }
89-
9083 @ Test
9184 public void testOkErrors () {
9285 errStream .reset ();
@@ -99,7 +92,7 @@ public void testOkErrors() {
9992 assertEquals (0 , errStream .toByteArray ().length );
10093 MotorErrors .reportErrors ((REVLibError )null , null );
10194 assertEquals (0 , errStream .toByteArray ().length );
102-
95+
10396 // Ok Status
10497 MotorErrors .reportError (ErrorCode .OK );
10598 assertEquals (0 , errStream .toByteArray ().length );
@@ -165,53 +158,99 @@ public void testDummySparkMax() {
165158
166159 @ Test
167160 public void testReportSparkMaxTemp () {
161+ assertTrue (MotorErrors .kOverheatTripCount > 0 );
162+
168163 doTestReportSparkMaxTemp (0 );
169164 doTestReportSparkMaxTemp (1 );
170165 doTestReportSparkMaxTemp (2 );
171166 }
172167
173168 private void doTestReportSparkMaxTemp (int id ) {
174169 TemperatureSparkMax spark = (TemperatureSparkMax )Mocks .createMock (CANSparkMax .class , new TemperatureSparkMax .Instance (id ), TemperatureSparkMax .class );
175- MotorErrors .reportSparkMaxTemp ((CANSparkMax )spark , 40 );
176- spark .setSmartCurrentLimit (50 );
177- spark .setTemperature (20 );
178- runAsyncPeriodic ();
179170 String smartDashboardKey = "Port " + id + " Spark Max Temp" ;
180- assertEquals (20 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
181- assertEquals (50 , spark .getSmartCurrentLimit ());
182- spark .setTemperature (20 );
183- runAsyncPeriodic ();
184- assertEquals (20 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
185- assertEquals (50 , spark .getSmartCurrentLimit ());
186- assertEquals (0 , errStream .size ());
187- spark .setTemperature (40 );
188- runAsyncPeriodic ();
189- assertEquals (40 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
190- assertEquals (1 , spark .getSmartCurrentLimit ());
191- assertNotEquals (0 , errStream .size ());
192- errStream .reset ();
193- spark .setTemperature (50 );
194- runAsyncPeriodic ();
195- assertEquals (50 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
196- assertEquals (1 , spark .getSmartCurrentLimit ());
197- spark .setTemperature (20 );
198- runAsyncPeriodic ();
199- assertEquals (20 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
200- assertEquals (1 , spark .getSmartCurrentLimit ());
201- assertEquals (0 , errStream .size ());
202- }
171+ MotorErrors .reportSparkMaxTemp ((CANSparkMax )spark , 40 );
172+
173+ try (AutoCloseable asyncBlock = blockAsyncPeriodic ()) {
174+ spark .setTemperature (20 );
175+ spark .setSmartCurrentLimit (50 );
176+ MotorErrors .doReportSparkMaxTemp ();
177+ assertEquals (20 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
178+ assertEquals (50 , spark .getSmartCurrentLimit ());
179+
180+ spark .setTemperature (20 );
181+ spark .setSmartCurrentLimit (50 );
182+ MotorErrors .doReportSparkMaxTemp ();
183+ assertEquals (20 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
184+ assertEquals (50 , spark .getSmartCurrentLimit ());
203185
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 ();
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+ }
199+
200+ assertEquals (0 , errStream .size ());
201+
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 );
211210 }
212- } catch (InterruptedException e ) {
211+
212+ assertNotEquals (0 , errStream .size ());
213+ errStream .reset ();
214+
215+ spark .setTemperature (51 );
216+ spark .setSmartCurrentLimit (50 );
217+ MotorErrors .doReportSparkMaxTemp ();
218+ assertEquals (51 , SmartDashboard .getNumber (smartDashboardKey , 0 ), 0.01 );
219+ assertEquals (1 , spark .getSmartCurrentLimit ());
220+
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 ());
226+
227+ assertEquals (0 , errStream .size ());
228+ } catch (Exception e ) {
213229 assumeNoException (e );
214230 }
215231 }
216232
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+ }
246+ }
247+ });
248+ return () -> {
249+ block .set (false );
250+ synchronized (lock ) {
251+ lock .notifyAll ();
252+ }
253+ };
254+ }
255+
217256}
0 commit comments