diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/MockHardwareExtension.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/MockHardwareExtension.java index b842c282a0..37f1e22b86 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/MockHardwareExtension.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/MockHardwareExtension.java @@ -29,12 +29,9 @@ public final class MockHardwareExtension implements BeforeAllCallback { private void initializeHardware() { HAL.initialize(500, 0); - DriverStationSim dsSim = new DriverStationSim(); - dsSim.setDsAttached(true); - dsSim.setAutonomous(false); - dsSim.setEnabled(true); - dsSim.setTest(true); - - + DriverStationSim.setDsAttached(true); + DriverStationSim.setAutonomous(false); + DriverStationSim.setEnabled(true); + DriverStationSim.setTest(true); } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandDecoratorTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandDecoratorTest.java index 23f63c776b..05dab62073 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandDecoratorTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandDecoratorTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -17,166 +17,166 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class CommandDecoratorTest extends CommandTestBase { @Test void withTimeoutTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Command command1 = new WaitCommand(10); - Command command1 = new WaitCommand(10); + Command timeout = command1.withTimeout(2); - Command timeout = command1.withTimeout(2); + scheduler.schedule(timeout); + scheduler.run(); - scheduler.schedule(timeout); - scheduler.run(); + assertFalse(scheduler.isScheduled(command1)); + assertTrue(scheduler.isScheduled(timeout)); - assertFalse(scheduler.isScheduled(command1)); - assertTrue(scheduler.isScheduled(timeout)); + Timer.delay(3); + scheduler.run(); - Timer.delay(3); - scheduler.run(); - - assertFalse(scheduler.isScheduled(timeout)); + assertFalse(scheduler.isScheduled(timeout)); + } } @Test void withInterruptTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); - ConditionHolder condition = new ConditionHolder(); + Command command = new WaitCommand(10).withInterrupt(condition::getCondition); - Command command = new WaitCommand(10).withInterrupt(condition::getCondition); - - scheduler.schedule(command); - scheduler.run(); - assertTrue(scheduler.isScheduled(command)); - condition.setCondition(true); - scheduler.run(); - assertFalse(scheduler.isScheduled(command)); + scheduler.schedule(command); + scheduler.run(); + assertTrue(scheduler.isScheduled(command)); + condition.setCondition(true); + scheduler.run(); + assertFalse(scheduler.isScheduled(command)); + } } @Test void beforeStartingTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); + condition.setCondition(false); - ConditionHolder condition = new ConditionHolder(); - condition.setCondition(false); + Command command = new InstantCommand(); - Command command = new InstantCommand(); + scheduler.schedule(command.beforeStarting(() -> condition.setCondition(true))); - scheduler.schedule(command.beforeStarting(() -> condition.setCondition(true))); - - assertTrue(condition.getCondition()); + assertTrue(condition.getCondition()); + } } @Test void andThenLambdaTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); + condition.setCondition(false); - ConditionHolder condition = new ConditionHolder(); - condition.setCondition(false); + Command command = new InstantCommand(); - Command command = new InstantCommand(); + scheduler.schedule(command.andThen(() -> condition.setCondition(true))); - scheduler.schedule(command.andThen(() -> condition.setCondition(true))); + assertFalse(condition.getCondition()); - assertFalse(condition.getCondition()); + scheduler.run(); - scheduler.run(); - - assertTrue(condition.getCondition()); + assertTrue(condition.getCondition()); + } } @Test void andThenTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); + condition.setCondition(false); - ConditionHolder condition = new ConditionHolder(); - condition.setCondition(false); + Command command1 = new InstantCommand(); + Command command2 = new InstantCommand(() -> condition.setCondition(true)); - Command command1 = new InstantCommand(); - Command command2 = new InstantCommand(() -> condition.setCondition(true)); + scheduler.schedule(command1.andThen(command2)); - scheduler.schedule(command1.andThen(command2)); + assertFalse(condition.getCondition()); - assertFalse(condition.getCondition()); + scheduler.run(); - scheduler.run(); - - assertTrue(condition.getCondition()); + assertTrue(condition.getCondition()); + } } @Test void deadlineWithTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); + condition.setCondition(false); - ConditionHolder condition = new ConditionHolder(); - condition.setCondition(false); + Command dictator = new WaitUntilCommand(condition::getCondition); + Command endsBefore = new InstantCommand(); + Command endsAfter = new WaitUntilCommand(() -> false); - Command dictator = new WaitUntilCommand(condition::getCondition); - Command endsBefore = new InstantCommand(); - Command endsAfter = new WaitUntilCommand(() -> false); + Command group = dictator.deadlineWith(endsBefore, endsAfter); - Command group = dictator.deadlineWith(endsBefore, endsAfter); + scheduler.schedule(group); + scheduler.run(); - scheduler.schedule(group); - scheduler.run(); + assertTrue(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(group)); + condition.setCondition(true); + scheduler.run(); - condition.setCondition(true); - scheduler.run(); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void alongWithTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); + condition.setCondition(false); - ConditionHolder condition = new ConditionHolder(); - condition.setCondition(false); + Command command1 = new WaitUntilCommand(condition::getCondition); + Command command2 = new InstantCommand(); - Command command1 = new WaitUntilCommand(condition::getCondition); - Command command2 = new InstantCommand(); + Command group = command1.alongWith(command2); - Command group = command1.alongWith(command2); + scheduler.schedule(group); + scheduler.run(); - scheduler.schedule(group); - scheduler.run(); + assertTrue(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(group)); + condition.setCondition(true); + scheduler.run(); - condition.setCondition(true); - scheduler.run(); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void raceWithTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Command command1 = new WaitUntilCommand(() -> false); + Command command2 = new InstantCommand(); - Command command1 = new WaitUntilCommand(() -> false); - Command command2 = new InstantCommand(); + Command group = command1.raceWith(command2); - Command group = command1.raceWith(command2); + scheduler.schedule(group); + scheduler.run(); - scheduler.schedule(group); - scheduler.run(); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void perpetuallyTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Command command = new InstantCommand(); - Command command = new InstantCommand(); + Command perpetual = command.perpetually(); - Command perpetual = command.perpetually(); + scheduler.schedule(perpetual); + scheduler.run(); + scheduler.run(); + scheduler.run(); - scheduler.schedule(perpetual); - scheduler.run(); - scheduler.run(); - scheduler.run(); - - assertTrue(scheduler.isScheduled(perpetual)); + assertTrue(scheduler.isScheduled(perpetual)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java index e453d94c3d..82e78c50d0 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -29,18 +29,18 @@ class CommandGroupErrorTest extends CommandTestBase { @Test void commandInGroupExternallyScheduledTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + @SuppressWarnings("PMD.UnusedLocalVariable") + Command group = new ParallelCommandGroup(command1, command2); - @SuppressWarnings("PMD.UnusedLocalVariable") - Command group = new ParallelCommandGroup(command1, command2); - - assertThrows(IllegalArgumentException.class, - () -> scheduler.schedule(command1)); + assertThrows(IllegalArgumentException.class, + () -> scheduler.schedule(command1)); + } } @Test diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandRequirementsTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandRequirementsTest.java index 10d3ee06a7..5a356e4d0f 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandRequirementsTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandRequirementsTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -18,62 +18,62 @@ import static org.mockito.Mockito.verify; class CommandRequirementsTest extends CommandTestBase { @Test void requirementInterruptTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Subsystem requirement = new TestSubsystem(); - Subsystem requirement = new TestSubsystem(); + MockCommandHolder interruptedHolder = new MockCommandHolder(true, requirement); + Command interrupted = interruptedHolder.getMock(); + MockCommandHolder interrupterHolder = new MockCommandHolder(true, requirement); + Command interrupter = interrupterHolder.getMock(); - MockCommandHolder interruptedHolder = new MockCommandHolder(true, requirement); - Command interrupted = interruptedHolder.getMock(); - MockCommandHolder interrupterHolder = new MockCommandHolder(true, requirement); - Command interrupter = interrupterHolder.getMock(); + scheduler.schedule(interrupted); + scheduler.run(); + scheduler.schedule(interrupter); + scheduler.run(); - scheduler.schedule(interrupted); - scheduler.run(); - scheduler.schedule(interrupter); - scheduler.run(); + verify(interrupted).initialize(); + verify(interrupted).execute(); + verify(interrupted).end(true); - verify(interrupted).initialize(); - verify(interrupted).execute(); - verify(interrupted).end(true); + verify(interrupter).initialize(); + verify(interrupter).execute(); - verify(interrupter).initialize(); - verify(interrupter).execute(); - - assertFalse(scheduler.isScheduled(interrupted)); - assertTrue(scheduler.isScheduled(interrupter)); + assertFalse(scheduler.isScheduled(interrupted)); + assertTrue(scheduler.isScheduled(interrupter)); + } } @Test void requirementUninterruptibleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Subsystem requirement = new TestSubsystem(); - Subsystem requirement = new TestSubsystem(); + MockCommandHolder interruptedHolder = new MockCommandHolder(true, requirement); + Command notInterrupted = interruptedHolder.getMock(); + MockCommandHolder interrupterHolder = new MockCommandHolder(true, requirement); + Command interrupter = interrupterHolder.getMock(); - MockCommandHolder interruptedHolder = new MockCommandHolder(true, requirement); - Command notInterrupted = interruptedHolder.getMock(); - MockCommandHolder interrupterHolder = new MockCommandHolder(true, requirement); - Command interrupter = interrupterHolder.getMock(); + scheduler.schedule(false, notInterrupted); + scheduler.schedule(interrupter); - scheduler.schedule(false, notInterrupted); - scheduler.schedule(interrupter); - - assertTrue(scheduler.isScheduled(notInterrupted)); - assertFalse(scheduler.isScheduled(interrupter)); + assertTrue(scheduler.isScheduled(notInterrupted)); + assertFalse(scheduler.isScheduled(interrupter)); + } } @Test void defaultCommandRequirementErrorTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Subsystem system = new TestSubsystem(); - Subsystem system = new TestSubsystem(); + Command missingRequirement = new WaitUntilCommand(() -> false); + Command ends = new InstantCommand(() -> { + }, system); - Command missingRequirement = new WaitUntilCommand(() -> false); - Command ends = new InstantCommand(() -> { - }, system); - - assertThrows(IllegalArgumentException.class, - () -> scheduler.setDefaultCommand(system, missingRequirement)); - assertThrows(IllegalArgumentException.class, - () -> scheduler.setDefaultCommand(system, ends)); + assertThrows(IllegalArgumentException.class, + () -> scheduler.setDefaultCommand(system, missingRequirement)); + assertThrows(IllegalArgumentException.class, + () -> scheduler.setDefaultCommand(system, ends)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandScheduleTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandScheduleTest.java index aee30db89e..74d2d5dc55 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandScheduleTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandScheduleTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -19,104 +19,104 @@ import static org.mockito.Mockito.verify; class CommandScheduleTest extends CommandTestBase { @Test void instantScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder holder = new MockCommandHolder(true); + holder.setFinished(true); + Command mockCommand = holder.getMock(); - MockCommandHolder holder = new MockCommandHolder(true); - holder.setFinished(true); - Command mockCommand = holder.getMock(); + scheduler.schedule(mockCommand); + assertTrue(scheduler.isScheduled(mockCommand)); + verify(mockCommand).initialize(); - scheduler.schedule(mockCommand); - assertTrue(scheduler.isScheduled(mockCommand)); - verify(mockCommand).initialize(); + scheduler.run(); - scheduler.run(); + verify(mockCommand).execute(); + verify(mockCommand).end(false); - verify(mockCommand).execute(); - verify(mockCommand).end(false); - - assertFalse(scheduler.isScheduled(mockCommand)); + assertFalse(scheduler.isScheduled(mockCommand)); + } } @Test void singleIterationScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder holder = new MockCommandHolder(true); + Command mockCommand = holder.getMock(); - MockCommandHolder holder = new MockCommandHolder(true); - Command mockCommand = holder.getMock(); + scheduler.schedule(mockCommand); - scheduler.schedule(mockCommand); + assertTrue(scheduler.isScheduled(mockCommand)); - assertTrue(scheduler.isScheduled(mockCommand)); + scheduler.run(); + holder.setFinished(true); + scheduler.run(); - scheduler.run(); - holder.setFinished(true); - scheduler.run(); + verify(mockCommand).initialize(); + verify(mockCommand, times(2)).execute(); + verify(mockCommand).end(false); - verify(mockCommand).initialize(); - verify(mockCommand, times(2)).execute(); - verify(mockCommand).end(false); - - assertFalse(scheduler.isScheduled(mockCommand)); + assertFalse(scheduler.isScheduled(mockCommand)); + } } @Test void multiScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); + scheduler.schedule(true, command1, command2, command3); + assertTrue(scheduler.isScheduled(command1, command2, command3)); + scheduler.run(); + assertTrue(scheduler.isScheduled(command1, command2, command3)); - scheduler.schedule(true, command1, command2, command3); - assertTrue(scheduler.isScheduled(command1, command2, command3)); - scheduler.run(); - assertTrue(scheduler.isScheduled(command1, command2, command3)); + command1Holder.setFinished(true); + scheduler.run(); + assertTrue(scheduler.isScheduled(command2, command3)); + assertFalse(scheduler.isScheduled(command1)); - command1Holder.setFinished(true); - scheduler.run(); - assertTrue(scheduler.isScheduled(command2, command3)); - assertFalse(scheduler.isScheduled(command1)); + command2Holder.setFinished(true); + scheduler.run(); + assertTrue(scheduler.isScheduled(command3)); + assertFalse(scheduler.isScheduled(command1, command2)); - command2Holder.setFinished(true); - scheduler.run(); - assertTrue(scheduler.isScheduled(command3)); - assertFalse(scheduler.isScheduled(command1, command2)); - - command3Holder.setFinished(true); - scheduler.run(); - assertFalse(scheduler.isScheduled(command1, command2, command3)); + command3Holder.setFinished(true); + scheduler.run(); + assertFalse(scheduler.isScheduled(command1, command2, command3)); + } } @Test void schedulerCancelTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder holder = new MockCommandHolder(true); + Command mockCommand = holder.getMock(); - MockCommandHolder holder = new MockCommandHolder(true); - Command mockCommand = holder.getMock(); + scheduler.schedule(mockCommand); - scheduler.schedule(mockCommand); + scheduler.run(); + scheduler.cancel(mockCommand); + scheduler.run(); - scheduler.run(); - scheduler.cancel(mockCommand); - scheduler.run(); + verify(mockCommand).execute(); + verify(mockCommand).end(true); + verify(mockCommand, never()).end(false); - verify(mockCommand).execute(); - verify(mockCommand).end(true); - verify(mockCommand, never()).end(false); - - assertFalse(scheduler.isScheduled(mockCommand)); + assertFalse(scheduler.isScheduled(mockCommand)); + } } @Test void notScheduledCancelTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder holder = new MockCommandHolder(true); + Command mockCommand = holder.getMock(); - MockCommandHolder holder = new MockCommandHolder(true); - Command mockCommand = holder.getMock(); - - assertDoesNotThrow(() -> scheduler.cancel(mockCommand)); + assertDoesNotThrow(() -> scheduler.cancel(mockCommand)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandTestBase.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandTestBase.java index 5e443b6332..6500f697f0 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandTestBase.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandTestBase.java @@ -33,11 +33,10 @@ public abstract class CommandTestBase { } public void setDSEnabled(boolean enabled) { - DriverStationSim sim = new DriverStationSim(); - sim.setDsAttached(true); + DriverStationSim.setDsAttached(true); - sim.setEnabled(enabled); - sim.notifyNewData(); + DriverStationSim.setEnabled(enabled); + DriverStationSim.notifyNewData(); DriverStation.getInstance().isNewControlData(); while (DriverStation.getInstance().isEnabled() != enabled) { try { diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ConditionalCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ConditionalCommandTest.java index 0740565b71..939fa01c67 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ConditionalCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ConditionalCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -16,26 +16,27 @@ import static org.mockito.Mockito.verify; class ConditionalCommandTest extends CommandTestBase { @Test void conditionalCommandTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + command1Holder.setFinished(true); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - command1Holder.setFinished(true); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + ConditionalCommand conditionalCommand = + new ConditionalCommand(command1, command2, () -> true); - ConditionalCommand conditionalCommand = new ConditionalCommand(command1, command2, () -> true); + scheduler.schedule(conditionalCommand); + scheduler.run(); - scheduler.schedule(conditionalCommand); - scheduler.run(); + verify(command1).initialize(); + verify(command1).execute(); + verify(command1).end(false); - verify(command1).initialize(); - verify(command1).execute(); - verify(command1).end(false); - - verify(command2, never()).initialize(); - verify(command2, never()).execute(); - verify(command2, never()).end(false); + verify(command2, never()).initialize(); + verify(command2, never()).execute(); + verify(command2, never()).end(false); + } } @Test @@ -44,22 +45,23 @@ class ConditionalCommandTest extends CommandTestBase { Subsystem system2 = new TestSubsystem(); Subsystem system3 = new TestSubsystem(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true, system3); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true, system3); - Command command2 = command2Holder.getMock(); + ConditionalCommand conditionalCommand = + new ConditionalCommand(command1, command2, () -> true); - ConditionalCommand conditionalCommand = new ConditionalCommand(command1, command2, () -> true); + scheduler.schedule(conditionalCommand); + scheduler.schedule(new InstantCommand(() -> { + }, system3)); - scheduler.schedule(conditionalCommand); - scheduler.schedule(new InstantCommand(() -> { - }, system3)); + assertFalse(scheduler.isScheduled(conditionalCommand)); - assertFalse(scheduler.isScheduled(conditionalCommand)); - - verify(command1).end(true); - verify(command2, never()).end(true); + verify(command1).end(true); + verify(command2, never()).end(true); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/DefaultCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/DefaultCommandTest.java index 033dcc5e43..dccf0ffff8 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/DefaultCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/DefaultCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -16,68 +16,68 @@ import static org.mockito.Mockito.verify; class DefaultCommandTest extends CommandTestBase { @Test void defaultCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + TestSubsystem hasDefaultCommand = new TestSubsystem(); - TestSubsystem hasDefaultCommand = new TestSubsystem(); + MockCommandHolder defaultHolder = new MockCommandHolder(true, hasDefaultCommand); + Command defaultCommand = defaultHolder.getMock(); - MockCommandHolder defaultHolder = new MockCommandHolder(true, hasDefaultCommand); - Command defaultCommand = defaultHolder.getMock(); + scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand); + scheduler.run(); - scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand); - scheduler.run(); - - assertTrue(scheduler.isScheduled(defaultCommand)); + assertTrue(scheduler.isScheduled(defaultCommand)); + } } @Test void defaultCommandInterruptResumeTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + TestSubsystem hasDefaultCommand = new TestSubsystem(); - TestSubsystem hasDefaultCommand = new TestSubsystem(); + MockCommandHolder defaultHolder = new MockCommandHolder(true, hasDefaultCommand); + Command defaultCommand = defaultHolder.getMock(); + MockCommandHolder interrupterHolder = new MockCommandHolder(true, hasDefaultCommand); + Command interrupter = interrupterHolder.getMock(); - MockCommandHolder defaultHolder = new MockCommandHolder(true, hasDefaultCommand); - Command defaultCommand = defaultHolder.getMock(); - MockCommandHolder interrupterHolder = new MockCommandHolder(true, hasDefaultCommand); - Command interrupter = interrupterHolder.getMock(); + scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand); + scheduler.run(); + scheduler.schedule(interrupter); - scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand); - scheduler.run(); - scheduler.schedule(interrupter); + assertFalse(scheduler.isScheduled(defaultCommand)); + assertTrue(scheduler.isScheduled(interrupter)); - assertFalse(scheduler.isScheduled(defaultCommand)); - assertTrue(scheduler.isScheduled(interrupter)); + scheduler.cancel(interrupter); + scheduler.run(); - scheduler.cancel(interrupter); - scheduler.run(); - - assertTrue(scheduler.isScheduled(defaultCommand)); - assertFalse(scheduler.isScheduled(interrupter)); + assertTrue(scheduler.isScheduled(defaultCommand)); + assertFalse(scheduler.isScheduled(interrupter)); + } } @Test void defaultCommandDisableResumeTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + TestSubsystem hasDefaultCommand = new TestSubsystem(); - TestSubsystem hasDefaultCommand = new TestSubsystem(); + MockCommandHolder defaultHolder = new MockCommandHolder(false, hasDefaultCommand); + Command defaultCommand = defaultHolder.getMock(); - MockCommandHolder defaultHolder = new MockCommandHolder(false, hasDefaultCommand); - Command defaultCommand = defaultHolder.getMock(); + scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand); + scheduler.run(); - scheduler.setDefaultCommand(hasDefaultCommand, defaultCommand); - scheduler.run(); + assertTrue(scheduler.isScheduled(defaultCommand)); - assertTrue(scheduler.isScheduled(defaultCommand)); + setDSEnabled(false); + scheduler.run(); - setDSEnabled(false); - scheduler.run(); + assertFalse(scheduler.isScheduled(defaultCommand)); - assertFalse(scheduler.isScheduled(defaultCommand)); + setDSEnabled(true); + scheduler.run(); - setDSEnabled(true); - scheduler.run(); + assertTrue(scheduler.isScheduled(defaultCommand)); - assertTrue(scheduler.isScheduled(defaultCommand)); - - verify(defaultCommand).end(true); + verify(defaultCommand).end(true); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/FunctionalCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/FunctionalCommandTest.java index 23f508a339..484cdf9ac8 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/FunctionalCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/FunctionalCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -15,29 +15,29 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class FunctionalCommandTest extends CommandTestBase { @Test void functionalCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder cond1 = new ConditionHolder(); + ConditionHolder cond2 = new ConditionHolder(); + ConditionHolder cond3 = new ConditionHolder(); + ConditionHolder cond4 = new ConditionHolder(); - ConditionHolder cond1 = new ConditionHolder(); - ConditionHolder cond2 = new ConditionHolder(); - ConditionHolder cond3 = new ConditionHolder(); - ConditionHolder cond4 = new ConditionHolder(); + FunctionalCommand command = + new FunctionalCommand(() -> cond1.setCondition(true), () -> cond2.setCondition(true), + interrupted -> cond3.setCondition(true), cond4::getCondition); - FunctionalCommand command = - new FunctionalCommand(() -> cond1.setCondition(true), () -> cond2.setCondition(true), - interrupted -> cond3.setCondition(true), cond4::getCondition); + scheduler.schedule(command); + scheduler.run(); - scheduler.schedule(command); - scheduler.run(); + assertTrue(scheduler.isScheduled(command)); - assertTrue(scheduler.isScheduled(command)); + cond4.setCondition(true); - cond4.setCondition(true); + scheduler.run(); - scheduler.run(); - - assertFalse(scheduler.isScheduled(command)); - assertTrue(cond1.getCondition()); - assertTrue(cond2.getCondition()); - assertTrue(cond3.getCondition()); + assertFalse(scheduler.isScheduled(command)); + assertTrue(cond1.getCondition()); + assertTrue(cond2.getCondition()); + assertTrue(cond3.getCondition()); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/InstantCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/InstantCommandTest.java index a109ed65c6..e2b9dbee57 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/InstantCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/InstantCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -15,16 +15,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class InstantCommandTest extends CommandTestBase { @Test void instantCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder cond = new ConditionHolder(); - ConditionHolder cond = new ConditionHolder(); + InstantCommand command = new InstantCommand(() -> cond.setCondition(true)); - InstantCommand command = new InstantCommand(() -> cond.setCondition(true)); + scheduler.schedule(command); + scheduler.run(); - scheduler.schedule(command); - scheduler.run(); - - assertTrue(cond.getCondition()); - assertFalse(scheduler.isScheduled(command)); + assertTrue(cond.getCondition()); + assertFalse(scheduler.isScheduled(command)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/NotifierCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/NotifierCommandTest.java index fb06844ffc..c8107bf7ee 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/NotifierCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/NotifierCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -19,17 +19,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class NotifierCommandTest extends CommandTestBase { @Test void notifierCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Counter counter = new Counter(); - Counter counter = new Counter(); + NotifierCommand command = new NotifierCommand(counter::increment, 0.01); - NotifierCommand command = new NotifierCommand(counter::increment, 0.01); + scheduler.schedule(command); + Timer.delay(0.25); + scheduler.cancel(command); - scheduler.schedule(command); - Timer.delay(0.25); - scheduler.cancel(command); - - assertTrue(counter.m_counter > 10, "Should have hit at least 10 triggers"); - assertTrue(counter.m_counter < 100, "Shouldn't hit more then 100 triggers"); + assertTrue(counter.m_counter > 10, "Should have hit at least 10 triggers"); + assertTrue(counter.m_counter < 100, "Shouldn't hit more then 100 triggers"); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroupTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroupTest.java index d03b4aa4dd..a32b994fa4 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroupTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelCommandGroupTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -20,73 +20,74 @@ import static org.mockito.Mockito.verify; class ParallelCommandGroupTest extends CommandTestBase { @Test void parallelGroupScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new ParallelCommandGroup(command1, command2); - Command group = new ParallelCommandGroup(command1, command2); + scheduler.schedule(group); - scheduler.schedule(group); + verify(command1).initialize(); + verify(command2).initialize(); - verify(command1).initialize(); - verify(command2).initialize(); + command1Holder.setFinished(true); + scheduler.run(); + command2Holder.setFinished(true); + scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); - command2Holder.setFinished(true); - scheduler.run(); + verify(command1).execute(); + verify(command1).end(false); + verify(command2, times(2)).execute(); + verify(command2).end(false); - verify(command1).execute(); - verify(command1).end(false); - verify(command2, times(2)).execute(); - verify(command2).end(false); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void parallelGroupInterruptTest() { - CommandScheduler scheduler = new CommandScheduler(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - Command group = new ParallelCommandGroup(command1, command2); + Command group = new ParallelCommandGroup(command1, command2); - scheduler.schedule(group); + scheduler.schedule(group); - command1Holder.setFinished(true); - scheduler.run(); - scheduler.run(); - scheduler.cancel(group); + command1Holder.setFinished(true); + scheduler.run(); + scheduler.run(); + scheduler.cancel(group); - verify(command1).execute(); - verify(command1).end(false); - verify(command1, never()).end(true); + verify(command1).execute(); + verify(command1).end(false); + verify(command1, never()).end(true); - verify(command2, times(2)).execute(); - verify(command2, never()).end(false); - verify(command2).end(true); + verify(command2, times(2)).execute(); + verify(command2, never()).end(false); + verify(command2).end(true); - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void notScheduledCancelTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new ParallelCommandGroup(command1, command2); - Command group = new ParallelCommandGroup(command1, command2); - - assertDoesNotThrow(() -> scheduler.cancel(group)); + assertDoesNotThrow(() -> scheduler.cancel(group)); + } } @Test @@ -96,22 +97,22 @@ class ParallelCommandGroupTest extends CommandTestBase { Subsystem system3 = new TestSubsystem(); Subsystem system4 = new TestSubsystem(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true, system3); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true, system3); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); - Command command3 = command3Holder.getMock(); + Command group = new ParallelCommandGroup(command1, command2); - Command group = new ParallelCommandGroup(command1, command2); + scheduler.schedule(group); + scheduler.schedule(command3); - scheduler.schedule(group); - scheduler.schedule(command3); - - assertFalse(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(command3)); + assertFalse(scheduler.isScheduled(group)); + assertTrue(scheduler.isScheduled(command3)); + } } @Test diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroupTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroupTest.java index c7e3769f51..bc10a0e812 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroupTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelDeadlineGroupTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -19,71 +19,71 @@ import static org.mockito.internal.verification.VerificationModeFactory.times; class ParallelDeadlineGroupTest extends CommandTestBase { @Test void parallelDeadlineScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + command2Holder.setFinished(true); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - command2Holder.setFinished(true); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); + Command group = new ParallelDeadlineGroup(command1, command2, command3); - Command group = new ParallelDeadlineGroup(command1, command2, command3); + scheduler.schedule(group); + scheduler.run(); - scheduler.schedule(group); - scheduler.run(); + assertTrue(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(group)); + command1Holder.setFinished(true); + scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); + assertFalse(scheduler.isScheduled(group)); - assertFalse(scheduler.isScheduled(group)); + verify(command2).initialize(); + verify(command2).execute(); + verify(command2).end(false); + verify(command2, never()).end(true); - verify(command2).initialize(); - verify(command2).execute(); - verify(command2).end(false); - verify(command2, never()).end(true); + verify(command1).initialize(); + verify(command1, times(2)).execute(); + verify(command1).end(false); + verify(command1, never()).end(true); - verify(command1).initialize(); - verify(command1, times(2)).execute(); - verify(command1).end(false); - verify(command1, never()).end(true); - - verify(command3).initialize(); - verify(command3, times(2)).execute(); - verify(command3, never()).end(false); - verify(command3).end(true); + verify(command3).initialize(); + verify(command3, times(2)).execute(); + verify(command3, never()).end(false); + verify(command3).end(true); + } } @Test void parallelDeadlineInterruptTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + command2Holder.setFinished(true); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - command2Holder.setFinished(true); + Command group = new ParallelDeadlineGroup(command1, command2); - Command group = new ParallelDeadlineGroup(command1, command2); + scheduler.schedule(group); - scheduler.schedule(group); + scheduler.run(); + scheduler.run(); + scheduler.cancel(group); - scheduler.run(); - scheduler.run(); - scheduler.cancel(group); + verify(command1, times(2)).execute(); + verify(command1, never()).end(false); + verify(command1).end(true); - verify(command1, times(2)).execute(); - verify(command1, never()).end(false); - verify(command1).end(true); + verify(command2).execute(); + verify(command2).end(false); + verify(command2, never()).end(true); - verify(command2).execute(); - verify(command2).end(false); - verify(command2, never()).end(true); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @@ -94,22 +94,22 @@ class ParallelDeadlineGroupTest extends CommandTestBase { Subsystem system3 = new TestSubsystem(); Subsystem system4 = new TestSubsystem(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true, system3); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true, system3); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); - Command command3 = command3Holder.getMock(); + Command group = new ParallelDeadlineGroup(command1, command2); - Command group = new ParallelDeadlineGroup(command1, command2); + scheduler.schedule(group); + scheduler.schedule(command3); - scheduler.schedule(group); - scheduler.schedule(command3); - - assertFalse(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(command3)); + assertFalse(scheduler.isScheduled(group)); + assertTrue(scheduler.isScheduled(command3)); + } } @Test diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroupTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroupTest.java index e228c6034a..360882dc3b 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroupTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ParallelRaceGroupTest.java @@ -22,74 +22,74 @@ import static org.mockito.Mockito.verify; class ParallelRaceGroupTest extends CommandTestBase { @Test void parallelRaceScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new ParallelRaceGroup(command1, command2); - Command group = new ParallelRaceGroup(command1, command2); + scheduler.schedule(group); - scheduler.schedule(group); + verify(command1).initialize(); + verify(command2).initialize(); - verify(command1).initialize(); - verify(command2).initialize(); + command1Holder.setFinished(true); + scheduler.run(); + command2Holder.setFinished(true); + scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); - command2Holder.setFinished(true); - scheduler.run(); + verify(command1).execute(); + verify(command1).end(false); + verify(command2).execute(); + verify(command2).end(true); + verify(command2, never()).end(false); - verify(command1).execute(); - verify(command1).end(false); - verify(command2).execute(); - verify(command2).end(true); - verify(command2, never()).end(false); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void parallelRaceInterruptTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new ParallelRaceGroup(command1, command2); - Command group = new ParallelRaceGroup(command1, command2); + scheduler.schedule(group); - scheduler.schedule(group); + scheduler.run(); + scheduler.run(); + scheduler.cancel(group); - scheduler.run(); - scheduler.run(); - scheduler.cancel(group); + verify(command1, times(2)).execute(); + verify(command1, never()).end(false); + verify(command1).end(true); - verify(command1, times(2)).execute(); - verify(command1, never()).end(false); - verify(command1).end(true); + verify(command2, times(2)).execute(); + verify(command2, never()).end(false); + verify(command2).end(true); - verify(command2, times(2)).execute(); - verify(command2, never()).end(false); - verify(command2).end(true); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void notScheduledCancelTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new ParallelRaceGroup(command1, command2); - Command group = new ParallelRaceGroup(command1, command2); - - assertDoesNotThrow(() -> scheduler.cancel(group)); + assertDoesNotThrow(() -> scheduler.cancel(group)); + } } @@ -100,22 +100,22 @@ class ParallelRaceGroupTest extends CommandTestBase { Subsystem system3 = new TestSubsystem(); Subsystem system4 = new TestSubsystem(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true, system3); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true, system3); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); - Command command3 = command3Holder.getMock(); + Command group = new ParallelRaceGroup(command1, command2); - Command group = new ParallelRaceGroup(command1, command2); + scheduler.schedule(group); + scheduler.schedule(command3); - scheduler.schedule(group); - scheduler.schedule(command3); - - assertFalse(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(command3)); + assertFalse(scheduler.isScheduled(group)); + assertTrue(scheduler.isScheduled(command3)); + } } @Test @@ -149,62 +149,61 @@ class ParallelRaceGroupTest extends CommandTestBase { assertNotNull(command3); Command group2 = new ParallelRaceGroup(group1, command3); - CommandScheduler scheduler = new CommandScheduler(); - - scheduler.schedule(group2); - scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); - command2Holder.setFinished(true); - // at this point the sequential group should be done - assertDoesNotThrow(() -> scheduler.run()); - + try (CommandScheduler scheduler = new CommandScheduler()) { + scheduler.schedule(group2); + scheduler.run(); + command1Holder.setFinished(true); + scheduler.run(); + command2Holder.setFinished(true); + // at this point the sequential group should be done + assertDoesNotThrow(() -> scheduler.run()); + } } @Test void parallelRaceScheduleTwiceTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new ParallelRaceGroup(command1, command2); - Command group = new ParallelRaceGroup(command1, command2); + scheduler.schedule(group); - scheduler.schedule(group); + verify(command1).initialize(); + verify(command2).initialize(); - verify(command1).initialize(); - verify(command2).initialize(); + command1Holder.setFinished(true); + scheduler.run(); + command2Holder.setFinished(true); + scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); - command2Holder.setFinished(true); - scheduler.run(); + verify(command1).execute(); + verify(command1).end(false); + verify(command2).execute(); + verify(command2).end(true); + verify(command2, never()).end(false); - verify(command1).execute(); - verify(command1).end(false); - verify(command2).execute(); - verify(command2).end(true); - verify(command2, never()).end(false); + assertFalse(scheduler.isScheduled(group)); - assertFalse(scheduler.isScheduled(group)); + reset(command1); + reset(command2); - reset(command1); - reset(command2); + scheduler.schedule(group); - scheduler.schedule(group); + verify(command1).initialize(); + verify(command2).initialize(); - verify(command1).initialize(); - verify(command2).initialize(); + scheduler.run(); + scheduler.run(); + assertTrue(scheduler.isScheduled(group)); + command2Holder.setFinished(true); + scheduler.run(); - scheduler.run(); - scheduler.run(); - assertTrue(scheduler.isScheduled(group)); - command2Holder.setFinished(true); - scheduler.run(); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PerpetualCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PerpetualCommandTest.java index baf037f5cc..58195f3bac 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PerpetualCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PerpetualCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -14,13 +14,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class PerpetualCommandTest extends CommandTestBase { @Test void perpetualCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + PerpetualCommand command = new PerpetualCommand(new InstantCommand()); - PerpetualCommand command = new PerpetualCommand(new InstantCommand()); + scheduler.schedule(command); + scheduler.run(); - scheduler.schedule(command); - scheduler.run(); - - assertTrue(scheduler.isScheduled(command)); + assertTrue(scheduler.isScheduled(command)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PrintCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PrintCommandTest.java index 7484396268..1895fc0545 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PrintCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/PrintCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -18,20 +18,21 @@ import static org.junit.jupiter.api.Assertions.assertFalse; class PrintCommandTest extends CommandTestBase { @Test void printCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + final PrintStream originalOut = System.out; + ByteArrayOutputStream testOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testOut)); + try { + PrintCommand command = new PrintCommand("Test!"); - final PrintStream originalOut = System.out; - ByteArrayOutputStream testOut = new ByteArrayOutputStream(); - System.setOut(new PrintStream(testOut)); + scheduler.schedule(command); + scheduler.run(); - PrintCommand command = new PrintCommand("Test!"); - - scheduler.schedule(command); - scheduler.run(); - - assertFalse(scheduler.isScheduled(command)); - assertEquals(testOut.toString(), "Test!" + System.lineSeparator()); - - System.setOut(originalOut); + assertFalse(scheduler.isScheduled(command)); + assertEquals(testOut.toString(), "Test!" + System.lineSeparator()); + } finally { + System.setOut(originalOut); + } + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ProxyScheduleCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ProxyScheduleCommandTest.java index b566aae568..5364b952eb 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ProxyScheduleCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ProxyScheduleCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -16,36 +16,36 @@ import static org.mockito.Mockito.verify; class ProxyScheduleCommandTest extends CommandTestBase { @Test void proxyScheduleCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); + ProxyScheduleCommand scheduleCommand = new ProxyScheduleCommand(command1); - ProxyScheduleCommand scheduleCommand = new ProxyScheduleCommand(command1); + scheduler.schedule(scheduleCommand); - scheduler.schedule(scheduleCommand); - - verify(command1).schedule(); + verify(command1).schedule(); + } } @Test void proxyScheduleCommandEndTest() { - CommandScheduler scheduler = CommandScheduler.getInstance(); + try (CommandScheduler scheduler = CommandScheduler.getInstance()) { + ConditionHolder cond = new ConditionHolder(); - ConditionHolder cond = new ConditionHolder(); + WaitUntilCommand command = new WaitUntilCommand(cond::getCondition); - WaitUntilCommand command = new WaitUntilCommand(cond::getCondition); + ProxyScheduleCommand scheduleCommand = new ProxyScheduleCommand(command); - ProxyScheduleCommand scheduleCommand = new ProxyScheduleCommand(command); + scheduler.schedule(scheduleCommand); - scheduler.schedule(scheduleCommand); + scheduler.run(); + assertTrue(scheduler.isScheduled(scheduleCommand)); - scheduler.run(); - assertTrue(scheduler.isScheduled(scheduleCommand)); - - cond.setCondition(true); - scheduler.run(); - scheduler.run(); - assertFalse(scheduler.isScheduled(scheduleCommand)); + cond.setCondition(true); + scheduler.run(); + scheduler.run(); + assertFalse(scheduler.isScheduled(scheduleCommand)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RobotDisabledCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RobotDisabledCommandTest.java index e862216c5e..3b607bf7a3 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RobotDisabledCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RobotDisabledCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -18,94 +18,94 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class RobotDisabledCommandTest extends CommandTestBase { @Test void robotDisabledCommandCancelTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder holder = new MockCommandHolder(false); + Command mockCommand = holder.getMock(); - MockCommandHolder holder = new MockCommandHolder(false); - Command mockCommand = holder.getMock(); + scheduler.schedule(mockCommand); - scheduler.schedule(mockCommand); + assertTrue(scheduler.isScheduled(mockCommand)); - assertTrue(scheduler.isScheduled(mockCommand)); + setDSEnabled(false); - setDSEnabled(false); + scheduler.run(); - scheduler.run(); + assertFalse(scheduler.isScheduled(mockCommand)); - assertFalse(scheduler.isScheduled(mockCommand)); - - setDSEnabled(true); + setDSEnabled(true); + } } @Test void runWhenDisabledTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder holder = new MockCommandHolder(true); + Command mockCommand = holder.getMock(); - MockCommandHolder holder = new MockCommandHolder(true); - Command mockCommand = holder.getMock(); + scheduler.schedule(mockCommand); - scheduler.schedule(mockCommand); + assertTrue(scheduler.isScheduled(mockCommand)); - assertTrue(scheduler.isScheduled(mockCommand)); + setDSEnabled(false); - setDSEnabled(false); + scheduler.run(); - scheduler.run(); - - assertTrue(scheduler.isScheduled(mockCommand)); + assertTrue(scheduler.isScheduled(mockCommand)); + } } @Test void sequentialGroupRunWhenDisabledTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); + MockCommandHolder command4Holder = new MockCommandHolder(false); + Command command4 = command4Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); - MockCommandHolder command4Holder = new MockCommandHolder(false); - Command command4 = command4Holder.getMock(); + Command runWhenDisabled = new SequentialCommandGroup(command1, command2); + Command dontRunWhenDisabled = new SequentialCommandGroup(command3, command4); - Command runWhenDisabled = new SequentialCommandGroup(command1, command2); - Command dontRunWhenDisabled = new SequentialCommandGroup(command3, command4); + scheduler.schedule(runWhenDisabled); + scheduler.schedule(dontRunWhenDisabled); - scheduler.schedule(runWhenDisabled); - scheduler.schedule(dontRunWhenDisabled); + setDSEnabled(false); - setDSEnabled(false); + scheduler.run(); - scheduler.run(); - - assertTrue(scheduler.isScheduled(runWhenDisabled)); - assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + assertTrue(scheduler.isScheduled(runWhenDisabled)); + assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + } } @Test void parallelGroupRunWhenDisabledTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); + MockCommandHolder command4Holder = new MockCommandHolder(false); + Command command4 = command4Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); - MockCommandHolder command4Holder = new MockCommandHolder(false); - Command command4 = command4Holder.getMock(); + Command runWhenDisabled = new ParallelCommandGroup(command1, command2); + Command dontRunWhenDisabled = new ParallelCommandGroup(command3, command4); - Command runWhenDisabled = new ParallelCommandGroup(command1, command2); - Command dontRunWhenDisabled = new ParallelCommandGroup(command3, command4); + scheduler.schedule(runWhenDisabled); + scheduler.schedule(dontRunWhenDisabled); - scheduler.schedule(runWhenDisabled); - scheduler.schedule(dontRunWhenDisabled); + setDSEnabled(false); - setDSEnabled(false); + scheduler.run(); - scheduler.run(); - - assertTrue(scheduler.isScheduled(runWhenDisabled)); - assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + assertTrue(scheduler.isScheduled(runWhenDisabled)); + assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + } } @Test @@ -121,15 +121,15 @@ class RobotDisabledCommandTest extends CommandTestBase { MockCommandHolder command4Holder = new MockCommandHolder(false); Command command4 = command4Holder.getMock(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Command runWhenDisabled = new ConditionalCommand(command1, command2, () -> true); + Command dontRunWhenDisabled = new ConditionalCommand(command3, command4, () -> true); - Command runWhenDisabled = new ConditionalCommand(command1, command2, () -> true); - Command dontRunWhenDisabled = new ConditionalCommand(command3, command4, () -> true); + scheduler.schedule(runWhenDisabled, dontRunWhenDisabled); - scheduler.schedule(runWhenDisabled, dontRunWhenDisabled); - - assertTrue(scheduler.isScheduled(runWhenDisabled)); - assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + assertTrue(scheduler.isScheduled(runWhenDisabled)); + assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + } } @Test @@ -148,12 +148,12 @@ class RobotDisabledCommandTest extends CommandTestBase { Command runWhenDisabled = new SelectCommand(Map.of(1, command1, 2, command2), () -> 1); Command dontRunWhenDisabled = new SelectCommand(Map.of(1, command3, 2, command4), () -> 1); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + scheduler.schedule(runWhenDisabled, dontRunWhenDisabled); - scheduler.schedule(runWhenDisabled, dontRunWhenDisabled); - - assertTrue(scheduler.isScheduled(runWhenDisabled)); - assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + assertTrue(scheduler.isScheduled(runWhenDisabled)); + assertFalse(scheduler.isScheduled(dontRunWhenDisabled)); + } } @Test @@ -169,15 +169,15 @@ class RobotDisabledCommandTest extends CommandTestBase { MockCommandHolder command4Holder = new MockCommandHolder(false); Command command4 = command4Holder.getMock(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Command runWhenDisabled = new ConditionalCommand(command1, command2, () -> true); + Command dontRunWhenDisabled = new ConditionalCommand(command3, command4, () -> true); - Command runWhenDisabled = new ConditionalCommand(command1, command2, () -> true); - Command dontRunWhenDisabled = new ConditionalCommand(command3, command4, () -> true); + Command parallel = parallel(runWhenDisabled, dontRunWhenDisabled); - Command parallel = parallel(runWhenDisabled, dontRunWhenDisabled); + scheduler.schedule(parallel); - scheduler.schedule(parallel); - - assertFalse(scheduler.isScheduled(runWhenDisabled)); + assertFalse(scheduler.isScheduled(runWhenDisabled)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RunCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RunCommandTest.java index 3ff8c3dfdf..07c18dd51c 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RunCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/RunCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -14,17 +14,17 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class RunCommandTest extends CommandTestBase { @Test void runCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Counter counter = new Counter(); - Counter counter = new Counter(); + RunCommand command = new RunCommand(counter::increment); - RunCommand command = new RunCommand(counter::increment); + scheduler.schedule(command); + scheduler.run(); + scheduler.run(); + scheduler.run(); - scheduler.schedule(command); - scheduler.run(); - scheduler.run(); - scheduler.run(); - - assertEquals(3, counter.m_counter); + assertEquals(3, counter.m_counter); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ScheduleCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ScheduleCommandTest.java index 3e371fe391..c8d51274ef 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ScheduleCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/ScheduleCommandTest.java @@ -15,33 +15,33 @@ import static org.mockito.Mockito.verify; class ScheduleCommandTest extends CommandTestBase { @Test void scheduleCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + ScheduleCommand scheduleCommand = new ScheduleCommand(command1, command2); - ScheduleCommand scheduleCommand = new ScheduleCommand(command1, command2); + scheduler.schedule(scheduleCommand); - scheduler.schedule(scheduleCommand); - - verify(command1).schedule(); - verify(command2).schedule(); + verify(command1).schedule(); + verify(command2).schedule(); + } } @Test void scheduleCommandDuringRunTest() { - CommandScheduler scheduler = CommandScheduler.getInstance(); + try (CommandScheduler scheduler = CommandScheduler.getInstance()) { + InstantCommand toSchedule = new InstantCommand(); + ScheduleCommand scheduleCommand = new ScheduleCommand(toSchedule); + SequentialCommandGroup group = + new SequentialCommandGroup(new InstantCommand(), scheduleCommand); - InstantCommand toSchedule = new InstantCommand(); - ScheduleCommand scheduleCommand = new ScheduleCommand(toSchedule); - SequentialCommandGroup group = - new SequentialCommandGroup(new InstantCommand(), scheduleCommand); - - scheduler.schedule(group); - scheduler.schedule(new InstantCommand().perpetually()); - scheduler.run(); - assertDoesNotThrow(scheduler::run); + scheduler.schedule(group); + scheduler.schedule(new InstantCommand().perpetually()); + scheduler.run(); + assertDoesNotThrow(scheduler::run); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SchedulerTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SchedulerTest.java index 37ab0afc31..c148816d76 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SchedulerTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SchedulerTest.java @@ -15,61 +15,61 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class SchedulerTest extends CommandTestBase { @Test void schedulerLambdaTestNoInterrupt() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Counter counter = new Counter(); - Counter counter = new Counter(); + scheduler.onCommandInitialize(command -> counter.increment()); + scheduler.onCommandExecute(command -> counter.increment()); + scheduler.onCommandFinish(command -> counter.increment()); - scheduler.onCommandInitialize(command -> counter.increment()); - scheduler.onCommandExecute(command -> counter.increment()); - scheduler.onCommandFinish(command -> counter.increment()); + scheduler.schedule(new InstantCommand()); + scheduler.run(); - scheduler.schedule(new InstantCommand()); - scheduler.run(); - - assertEquals(counter.m_counter, 3); + assertEquals(counter.m_counter, 3); + } } @Test void schedulerInterruptLambdaTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Counter counter = new Counter(); - Counter counter = new Counter(); + scheduler.onCommandInterrupt(command -> counter.increment()); - scheduler.onCommandInterrupt(command -> counter.increment()); + Command command = new WaitCommand(10); - Command command = new WaitCommand(10); + scheduler.schedule(command); + scheduler.cancel(command); - scheduler.schedule(command); - scheduler.cancel(command); - - assertEquals(counter.m_counter, 1); + assertEquals(counter.m_counter, 1); + } } @Test void unregisterSubsystemTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Subsystem system = new TestSubsystem(); - Subsystem system = new TestSubsystem(); - - scheduler.registerSubsystem(system); - assertDoesNotThrow(() -> scheduler.unregisterSubsystem(system)); + scheduler.registerSubsystem(system); + assertDoesNotThrow(() -> scheduler.unregisterSubsystem(system)); + } } @Test void schedulerCancelAllTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + Counter counter = new Counter(); - Counter counter = new Counter(); + scheduler.onCommandInterrupt(command -> counter.increment()); - scheduler.onCommandInterrupt(command -> counter.increment()); + Command command = new WaitCommand(10); + Command command2 = new WaitCommand(10); - Command command = new WaitCommand(10); - Command command2 = new WaitCommand(10); + scheduler.schedule(command); + scheduler.schedule(command2); + scheduler.cancelAll(); - scheduler.schedule(command); - scheduler.schedule(command2); - scheduler.cancelAll(); - - assertEquals(counter.m_counter, 2); + assertEquals(counter.m_counter, 2); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SelectCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SelectCommandTest.java index df52855f46..53f6a98e24 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SelectCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SelectCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -19,59 +19,59 @@ import static org.mockito.Mockito.verify; class SelectCommandTest extends CommandTestBase { @Test void selectCommandTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + command1Holder.setFinished(true); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - command1Holder.setFinished(true); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); + SelectCommand selectCommand = + new SelectCommand(Map.ofEntries( + Map.entry("one", command1), + Map.entry("two", command2), + Map.entry("three", command3)), + () -> "one"); - SelectCommand selectCommand = - new SelectCommand(Map.ofEntries( - Map.entry("one", command1), - Map.entry("two", command2), - Map.entry("three", command3)), - () -> "one"); + scheduler.schedule(selectCommand); + scheduler.run(); - scheduler.schedule(selectCommand); - scheduler.run(); + verify(command1).initialize(); + verify(command1).execute(); + verify(command1).end(false); - verify(command1).initialize(); - verify(command1).execute(); - verify(command1).end(false); + verify(command2, never()).initialize(); + verify(command2, never()).execute(); + verify(command2, never()).end(false); - verify(command2, never()).initialize(); - verify(command2, never()).execute(); - verify(command2, never()).end(false); - - verify(command3, never()).initialize(); - verify(command3, never()).execute(); - verify(command3, never()).end(false); + verify(command3, never()).initialize(); + verify(command3, never()).execute(); + verify(command3, never()).end(false); + } } @Test void selectCommandInvalidKeyTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + command1Holder.setFinished(true); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - command1Holder.setFinished(true); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); + SelectCommand selectCommand = + new SelectCommand(Map.ofEntries( + Map.entry("one", command1), + Map.entry("two", command2), + Map.entry("three", command3)), + () -> "four"); - SelectCommand selectCommand = - new SelectCommand(Map.ofEntries( - Map.entry("one", command1), - Map.entry("two", command2), - Map.entry("three", command3)), - () -> "four"); - - assertDoesNotThrow(() -> scheduler.schedule(selectCommand)); + assertDoesNotThrow(() -> scheduler.schedule(selectCommand)); + } } @@ -82,27 +82,27 @@ class SelectCommandTest extends CommandTestBase { Subsystem system3 = new TestSubsystem(); Subsystem system4 = new TestSubsystem(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true, system3); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true, system3); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); - Command command3 = command3Holder.getMock(); + SelectCommand selectCommand = new SelectCommand( + Map.ofEntries(Map.entry("one", command1), Map.entry("two", command2), + Map.entry("three", command3)), () -> "one"); - SelectCommand selectCommand = new SelectCommand( - Map.ofEntries(Map.entry("one", command1), Map.entry("two", command2), - Map.entry("three", command3)), () -> "one"); + scheduler.schedule(selectCommand); + scheduler.schedule(new InstantCommand(() -> { + }, system3)); - scheduler.schedule(selectCommand); - scheduler.schedule(new InstantCommand(() -> { - }, system3)); + assertFalse(scheduler.isScheduled(selectCommand)); - assertFalse(scheduler.isScheduled(selectCommand)); - - verify(command1).end(true); - verify(command2, never()).end(true); - verify(command3, never()).end(true); + verify(command1).end(true); + verify(command2, never()).end(true); + verify(command3, never()).end(true); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroupTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroupTest.java index 8582140547..a47f865ce3 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroupTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SequentialCommandGroupTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -18,86 +18,86 @@ import static org.mockito.Mockito.verify; class SequentialCommandGroupTest extends CommandTestBase { @Test void sequentialGroupScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new SequentialCommandGroup(command1, command2); - Command group = new SequentialCommandGroup(command1, command2); + scheduler.schedule(group); - scheduler.schedule(group); + verify(command1).initialize(); + verify(command2, never()).initialize(); - verify(command1).initialize(); - verify(command2, never()).initialize(); + command1Holder.setFinished(true); + scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); + verify(command1).execute(); + verify(command1).end(false); + verify(command2).initialize(); + verify(command2, never()).execute(); + verify(command2, never()).end(false); - verify(command1).execute(); - verify(command1).end(false); - verify(command2).initialize(); - verify(command2, never()).execute(); - verify(command2, never()).end(false); + command2Holder.setFinished(true); + scheduler.run(); - command2Holder.setFinished(true); - scheduler.run(); + verify(command1).execute(); + verify(command1).end(false); + verify(command2).execute(); + verify(command2).end(false); - verify(command1).execute(); - verify(command1).end(false); - verify(command2).execute(); - verify(command2).end(false); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void sequentialGroupInterruptTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true); - Command command3 = command3Holder.getMock(); + Command group = new SequentialCommandGroup(command1, command2, command3); - Command group = new SequentialCommandGroup(command1, command2, command3); + scheduler.schedule(group); - scheduler.schedule(group); + command1Holder.setFinished(true); + scheduler.run(); + scheduler.cancel(group); + scheduler.run(); - command1Holder.setFinished(true); - scheduler.run(); - scheduler.cancel(group); - scheduler.run(); + verify(command1).execute(); + verify(command1, never()).end(true); + verify(command1).end(false); + verify(command2, never()).execute(); + verify(command2).end(true); + verify(command2, never()).end(false); + verify(command3, never()).initialize(); + verify(command3, never()).execute(); + verify(command3, never()).end(true); + verify(command3, never()).end(false); - verify(command1).execute(); - verify(command1, never()).end(true); - verify(command1).end(false); - verify(command2, never()).execute(); - verify(command2).end(true); - verify(command2, never()).end(false); - verify(command3, never()).initialize(); - verify(command3, never()).execute(); - verify(command3, never()).end(true); - verify(command3, never()).end(false); - - assertFalse(scheduler.isScheduled(group)); + assertFalse(scheduler.isScheduled(group)); + } } @Test void notScheduledCancelTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true); + Command command2 = command2Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); + Command group = new SequentialCommandGroup(command1, command2); - Command group = new SequentialCommandGroup(command1, command2); - - assertDoesNotThrow(() -> scheduler.cancel(group)); + assertDoesNotThrow(() -> scheduler.cancel(group)); + } } @@ -108,21 +108,21 @@ class SequentialCommandGroupTest extends CommandTestBase { Subsystem system3 = new TestSubsystem(); Subsystem system4 = new TestSubsystem(); - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); + Command command1 = command1Holder.getMock(); + MockCommandHolder command2Holder = new MockCommandHolder(true, system3); + Command command2 = command2Holder.getMock(); + MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); + Command command3 = command3Holder.getMock(); - MockCommandHolder command1Holder = new MockCommandHolder(true, system1, system2); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true, system3); - Command command2 = command2Holder.getMock(); - MockCommandHolder command3Holder = new MockCommandHolder(true, system3, system4); - Command command3 = command3Holder.getMock(); + Command group = new SequentialCommandGroup(command1, command2); - Command group = new SequentialCommandGroup(command1, command2); + scheduler.schedule(group); + scheduler.schedule(command3); - scheduler.schedule(group); - scheduler.schedule(command3); - - assertFalse(scheduler.isScheduled(group)); - assertTrue(scheduler.isScheduled(command3)); + assertFalse(scheduler.isScheduled(group)); + assertTrue(scheduler.isScheduled(command3)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/StartEndCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/StartEndCommandTest.java index 93921e04e2..90ad41a600 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/StartEndCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/StartEndCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -15,23 +15,23 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class StartEndCommandTest extends CommandTestBase { @Test void startEndCommandScheduleTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder cond1 = new ConditionHolder(); + ConditionHolder cond2 = new ConditionHolder(); - ConditionHolder cond1 = new ConditionHolder(); - ConditionHolder cond2 = new ConditionHolder(); + StartEndCommand command = + new StartEndCommand(() -> cond1.setCondition(true), () -> cond2.setCondition(true)); - StartEndCommand command = - new StartEndCommand(() -> cond1.setCondition(true), () -> cond2.setCondition(true)); + scheduler.schedule(command); + scheduler.run(); - scheduler.schedule(command); - scheduler.run(); + assertTrue(scheduler.isScheduled(command)); - assertTrue(scheduler.isScheduled(command)); + scheduler.cancel(command); - scheduler.cancel(command); - - assertFalse(scheduler.isScheduled(command)); - assertTrue(cond1.getCondition()); - assertTrue(cond2.getCondition()); + assertFalse(scheduler.isScheduled(command)); + assertTrue(cond1.getCondition()); + assertTrue(cond2.getCondition()); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitCommandTest.java index 5a522af28d..da0ef0366f 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -21,47 +21,47 @@ import static org.mockito.Mockito.when; class WaitCommandTest extends CommandTestBase { @Test void waitCommandTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + WaitCommand waitCommand = new WaitCommand(2); - WaitCommand waitCommand = new WaitCommand(2); + scheduler.schedule(waitCommand); + scheduler.run(); + Timer.delay(1); + scheduler.run(); - scheduler.schedule(waitCommand); - scheduler.run(); - Timer.delay(1); - scheduler.run(); + assertTrue(scheduler.isScheduled(waitCommand)); - assertTrue(scheduler.isScheduled(waitCommand)); + Timer.delay(2); - Timer.delay(2); + scheduler.run(); - scheduler.run(); - - assertFalse(scheduler.isScheduled(waitCommand)); + assertFalse(scheduler.isScheduled(waitCommand)); + } } @Test void withTimeoutTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + MockCommandHolder command1Holder = new MockCommandHolder(true); + Command command1 = command1Holder.getMock(); + when(command1.withTimeout(anyDouble())).thenCallRealMethod(); - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - when(command1.withTimeout(anyDouble())).thenCallRealMethod(); + Command timeout = command1.withTimeout(2); - Command timeout = command1.withTimeout(2); + scheduler.schedule(timeout); + scheduler.run(); - scheduler.schedule(timeout); - scheduler.run(); + verify(command1).initialize(); + verify(command1).execute(); + assertFalse(scheduler.isScheduled(command1)); + assertTrue(scheduler.isScheduled(timeout)); - verify(command1).initialize(); - verify(command1).execute(); - assertFalse(scheduler.isScheduled(command1)); - assertTrue(scheduler.isScheduled(timeout)); + Timer.delay(3); + scheduler.run(); - Timer.delay(3); - scheduler.run(); - - verify(command1).end(true); - verify(command1, never()).end(false); - assertFalse(scheduler.isScheduled(timeout)); + verify(command1).end(true); + verify(command1, never()).end(false); + assertFalse(scheduler.isScheduled(timeout)); + } } } diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitUntilCommandTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitUntilCommandTest.java index a99a18cac5..c38feeebcf 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitUntilCommandTest.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/WaitUntilCommandTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -15,17 +15,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue; class WaitUntilCommandTest extends CommandTestBase { @Test void waitUntilTest() { - CommandScheduler scheduler = new CommandScheduler(); + try (CommandScheduler scheduler = new CommandScheduler()) { + ConditionHolder condition = new ConditionHolder(); - ConditionHolder condition = new ConditionHolder(); + Command command = new WaitUntilCommand(condition::getCondition); - Command command = new WaitUntilCommand(condition::getCondition); - - scheduler.schedule(command); - scheduler.run(); - assertTrue(scheduler.isScheduled(command)); - condition.setCondition(true); - scheduler.run(); - assertFalse(scheduler.isScheduled(command)); + scheduler.schedule(command); + scheduler.run(); + assertTrue(scheduler.isScheduled(command)); + condition.setCondition(true); + scheduler.run(); + assertFalse(scheduler.isScheduled(command)); + } } } diff --git a/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java b/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java index 5bc194938f..108c49b84d 100644 --- a/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java +++ b/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandParallelGroupTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -21,35 +21,36 @@ class CommandParallelGroupTest extends AbstractCommandTest { final MockCommand command1 = new MockCommand(); final MockCommand command2 = new MockCommand(); - final CommandGroup commandGroup = new CommandGroup(); - commandGroup.addParallel(command1); - commandGroup.addParallel(command2); + try (CommandGroup commandGroup = new CommandGroup()) { + commandGroup.addParallel(command1); + commandGroup.addParallel(command2); - assertCommandState(command1, 0, 0, 0, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - commandGroup.start(); - assertCommandState(command1, 0, 0, 0, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 0, 0, 0, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 0); - assertCommandState(command2, 1, 1, 1, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 2, 2, 0, 0); - assertCommandState(command2, 1, 2, 2, 0, 0); - command1.setHasFinished(true); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 3, 3, 1, 0); - assertCommandState(command2, 1, 3, 3, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 3, 3, 1, 0); - assertCommandState(command2, 1, 4, 4, 0, 0); - command2.setHasFinished(true); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 3, 3, 1, 0); - assertCommandState(command2, 1, 5, 5, 1, 0); + assertCommandState(command1, 0, 0, 0, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + commandGroup.start(); + assertCommandState(command1, 0, 0, 0, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 0, 0, 0, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 0); + assertCommandState(command2, 1, 1, 1, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 2, 2, 0, 0); + assertCommandState(command2, 1, 2, 2, 0, 0); + command1.setHasFinished(true); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 3, 3, 1, 0); + assertCommandState(command2, 1, 3, 3, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 3, 3, 1, 0); + assertCommandState(command2, 1, 4, 4, 0, 0); + command2.setHasFinished(true); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 3, 3, 1, 0); + assertCommandState(command2, 1, 5, 5, 1, 0); + } } } diff --git a/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java b/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java index 680a55dcc0..ce920c74ce 100644 --- a/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java +++ b/wpilibOldCommands/src/test/java/edu/wpi/first/wpilibj/command/CommandSequentialGroupTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -34,60 +34,61 @@ class CommandSequentialGroupTest extends AbstractCommandTest { final MockCommand command3 = new MockCommand(subsystem); logger.finest("Creating Command Group"); - final CommandGroup commandGroup = new CommandGroup(); - commandGroup.addSequential(command1, 1.0); - commandGroup.addSequential(command2, 2.0); - commandGroup.addSequential(command3); + try (CommandGroup commandGroup = new CommandGroup()) { + commandGroup.addSequential(command1, 1.0); + commandGroup.addSequential(command2, 2.0); + commandGroup.addSequential(command3); - assertCommandState(command1, 0, 0, 0, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - assertCommandState(command3, 0, 0, 0, 0, 0); - logger.finest("Starting Command group"); - commandGroup.start(); - assertCommandState(command1, 0, 0, 0, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - assertCommandState(command3, 0, 0, 0, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 0, 0, 0, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - assertCommandState(command3, 0, 0, 0, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 0); - assertCommandState(command2, 0, 0, 0, 0, 0); - assertCommandState(command3, 0, 0, 0, 0, 0); - sleep(1250); // command 1 timeout - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 1, 1, 0, 0); - assertCommandState(command3, 0, 0, 0, 0, 0); + assertCommandState(command1, 0, 0, 0, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + assertCommandState(command3, 0, 0, 0, 0, 0); + logger.finest("Starting Command group"); + commandGroup.start(); + assertCommandState(command1, 0, 0, 0, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + assertCommandState(command3, 0, 0, 0, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 0, 0, 0, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + assertCommandState(command3, 0, 0, 0, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 0); + assertCommandState(command2, 0, 0, 0, 0, 0); + assertCommandState(command3, 0, 0, 0, 0, 0); + sleep(1250); // command 1 timeout + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 1, 1, 0, 0); + assertCommandState(command3, 0, 0, 0, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 2, 2, 0, 0); - assertCommandState(command3, 0, 0, 0, 0, 0); - sleep(2500); // command 2 timeout - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 2, 2, 0, 1); - assertCommandState(command3, 1, 1, 1, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 2, 2, 0, 0); + assertCommandState(command3, 0, 0, 0, 0, 0); + sleep(2500); // command 2 timeout + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 2, 2, 0, 1); + assertCommandState(command3, 1, 1, 1, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 2, 2, 0, 1); - assertCommandState(command3, 1, 2, 2, 0, 0); - command3.setHasFinished(true); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 2, 2, 0, 1); - assertCommandState(command3, 1, 2, 2, 0, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 2, 2, 0, 1); - assertCommandState(command3, 1, 3, 3, 1, 0); - Scheduler.getInstance().run(); - assertCommandState(command1, 1, 1, 1, 0, 1); - assertCommandState(command2, 1, 2, 2, 0, 1); - assertCommandState(command3, 1, 3, 3, 1, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 2, 2, 0, 1); + assertCommandState(command3, 1, 2, 2, 0, 0); + command3.setHasFinished(true); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 2, 2, 0, 1); + assertCommandState(command3, 1, 2, 2, 0, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 2, 2, 0, 1); + assertCommandState(command3, 1, 3, 3, 1, 0); + Scheduler.getInstance().run(); + assertCommandState(command1, 1, 1, 1, 0, 1); + assertCommandState(command2, 1, 2, 2, 0, 1); + assertCommandState(command3, 1, 3, 3, 1, 0); + } } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockHardwareExtension.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockHardwareExtension.java index a53f41b276..467cb47ffe 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockHardwareExtension.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/MockHardwareExtension.java @@ -29,12 +29,9 @@ public final class MockHardwareExtension implements BeforeAllCallback { private void initializeHardware() { HAL.initialize(500, 0); - DriverStationSim dsSim = new DriverStationSim(); - dsSim.setDsAttached(true); - dsSim.setAutonomous(false); - dsSim.setEnabled(true); - dsSim.setTest(true); - - + DriverStationSim.setDsAttached(true); + DriverStationSim.setAutonomous(false); + DriverStationSim.setEnabled(true); + DriverStationSim.setTest(true); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/WatchdogTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/WatchdogTest.java index b95644616b..2493ee3ce0 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/WatchdogTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/WatchdogTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -23,172 +23,163 @@ class WatchdogTest { void enableDisableTest() { final AtomicInteger watchdogCounter = new AtomicInteger(0); - final Watchdog watchdog = new Watchdog(0.4, () -> watchdogCounter.addAndGet(1)); + try (Watchdog watchdog = new Watchdog(0.4, () -> watchdogCounter.addAndGet(1))) { + System.out.println("Run 1"); + watchdog.enable(); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.disable(); - System.out.println("Run 1"); - watchdog.enable(); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); + assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); + + System.out.println("Run 2"); + watchdogCounter.set(0); + watchdog.enable(); + try { + Thread.sleep(600); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.disable(); + + assertEquals(1, watchdogCounter.get(), + "Watchdog either didn't trigger or triggered more than once"); + + // Run 3 + watchdogCounter.set(0); + watchdog.enable(); + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.disable(); + + assertEquals(1, watchdogCounter.get(), + "Watchdog either didn't trigger or triggered more than once"); } - watchdog.disable(); - - assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); - - System.out.println("Run 2"); - watchdogCounter.set(0); - watchdog.enable(); - try { - Thread.sleep(600); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.disable(); - - assertEquals(1, watchdogCounter.get(), - "Watchdog either didn't trigger or triggered more than once"); - - // Run 3 - watchdogCounter.set(0); - watchdog.enable(); - try { - Thread.sleep(1000); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.disable(); - - assertEquals(1, watchdogCounter.get(), - "Watchdog either didn't trigger or triggered more than once"); - - watchdog.close(); } @Test void resetTest() { final AtomicInteger watchdogCounter = new AtomicInteger(0); - final Watchdog watchdog = new Watchdog(0.4, () -> watchdogCounter.addAndGet(1)); + try (Watchdog watchdog = new Watchdog(0.4, () -> watchdogCounter.addAndGet(1))) { + watchdog.enable(); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.reset(); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.disable(); - watchdog.enable(); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); + assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); } - watchdog.reset(); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.disable(); - - assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); - - watchdog.close(); } @Test void setTimeoutTest() { final AtomicInteger watchdogCounter = new AtomicInteger(0); - final Watchdog watchdog = new Watchdog(1.0, () -> watchdogCounter.addAndGet(1)); + try (Watchdog watchdog = new Watchdog(1.0, () -> watchdogCounter.addAndGet(1))) { + watchdog.enable(); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.setTimeout(0.2); - watchdog.enable(); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); + assertEquals(0.2, watchdog.getTimeout()); + assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); + + try { + Thread.sleep(300); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.disable(); + + assertEquals(1, watchdogCounter.get(), + "Watchdog either didn't trigger or triggered more than once"); } - watchdog.setTimeout(0.2); - - assertEquals(0.2, watchdog.getTimeout()); - assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); - - try { - Thread.sleep(300); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.disable(); - - assertEquals(1, watchdogCounter.get(), - "Watchdog either didn't trigger or triggered more than once"); - - watchdog.close(); } @Test void isExpiredTest() { - final Watchdog watchdog = new Watchdog(0.2, () -> { - }); - assertFalse(watchdog.isExpired()); - watchdog.enable(); + try (Watchdog watchdog = new Watchdog(0.2, () -> { + })) { + assertFalse(watchdog.isExpired()); + watchdog.enable(); - assertFalse(watchdog.isExpired()); - try { - Thread.sleep(300); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); + assertFalse(watchdog.isExpired()); + try { + Thread.sleep(300); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + assertTrue(watchdog.isExpired()); + + watchdog.disable(); + assertTrue(watchdog.isExpired()); + + watchdog.reset(); + assertFalse(watchdog.isExpired()); } - assertTrue(watchdog.isExpired()); - - watchdog.disable(); - assertTrue(watchdog.isExpired()); - - watchdog.reset(); - assertFalse(watchdog.isExpired()); - - watchdog.close(); } @Test void epochsTest() { final AtomicInteger watchdogCounter = new AtomicInteger(0); - final Watchdog watchdog = new Watchdog(0.4, () -> watchdogCounter.addAndGet(1)); + try (Watchdog watchdog = new Watchdog(0.4, () -> watchdogCounter.addAndGet(1))) { + System.out.println("Run 1"); + watchdog.enable(); + watchdog.addEpoch("Epoch 1"); + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.addEpoch("Epoch 2"); + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.addEpoch("Epoch 3"); + watchdog.disable(); - System.out.println("Run 1"); - watchdog.enable(); - watchdog.addEpoch("Epoch 1"); - try { - Thread.sleep(100); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); + assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); + + System.out.println("Run 2"); + watchdog.enable(); + watchdog.addEpoch("Epoch 1"); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.reset(); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog.addEpoch("Epoch 2"); + watchdog.disable(); + + assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); } - watchdog.addEpoch("Epoch 2"); - try { - Thread.sleep(100); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.addEpoch("Epoch 3"); - watchdog.disable(); - - assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); - - System.out.println("Run 2"); - watchdog.enable(); - watchdog.addEpoch("Epoch 1"); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.reset(); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog.addEpoch("Epoch 2"); - watchdog.disable(); - - assertEquals(0, watchdogCounter.get(), "Watchdog triggered early"); - - watchdog.close(); } @Test @@ -196,33 +187,30 @@ class WatchdogTest { final AtomicInteger watchdogCounter1 = new AtomicInteger(0); final AtomicInteger watchdogCounter2 = new AtomicInteger(0); - final Watchdog watchdog1 = new Watchdog(0.2, () -> watchdogCounter1.addAndGet(1)); - final Watchdog watchdog2 = new Watchdog(0.6, () -> watchdogCounter2.addAndGet(1)); + try (Watchdog watchdog1 = new Watchdog(0.2, () -> watchdogCounter1.addAndGet(1)); + Watchdog watchdog2 = new Watchdog(0.6, () -> watchdogCounter2.addAndGet(1))) { + watchdog2.enable(); + try { + Thread.sleep(200); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + assertEquals(0, watchdogCounter1.get(), "Watchdog triggered early"); + assertEquals(0, watchdogCounter2.get(), "Watchdog triggered early"); - watchdog2.enable(); - try { - Thread.sleep(200); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); + // Sleep enough such that only the watchdog enabled later times out first + watchdog1.enable(); + try { + Thread.sleep(300); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + watchdog1.disable(); + watchdog2.disable(); + + assertEquals(1, watchdogCounter1.get(), + "Watchdog either didn't trigger or triggered more than once"); + assertEquals(0, watchdogCounter2.get(), "Watchdog triggered early"); } - assertEquals(0, watchdogCounter1.get(), "Watchdog triggered early"); - assertEquals(0, watchdogCounter2.get(), "Watchdog triggered early"); - - // Sleep enough such that only the watchdog enabled later times out first - watchdog1.enable(); - try { - Thread.sleep(300); - } catch (InterruptedException ex) { - Thread.currentThread().interrupt(); - } - watchdog1.disable(); - watchdog2.disable(); - - assertEquals(1, watchdogCounter1.get(), - "Watchdog either didn't trigger or triggered more than once"); - assertEquals(0, watchdogCounter2.get(), "Watchdog triggered early"); - - watchdog1.close(); - watchdog2.close(); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/controller/PIDToleranceTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/controller/PIDToleranceTest.java index cd2aba4b55..b2d43c634c 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/controller/PIDToleranceTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/controller/PIDToleranceTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -19,36 +19,38 @@ class PIDToleranceTest { @Test void initialToleranceTest() { - var controller = new PIDController(0.05, 0.0, 0.0); - controller.enableContinuousInput(-kRange / 2, kRange / 2); + try (var controller = new PIDController(0.05, 0.0, 0.0)) { + controller.enableContinuousInput(-kRange / 2, kRange / 2); - assertTrue(controller.atSetpoint()); + assertTrue(controller.atSetpoint()); + } } @Test void absoluteToleranceTest() { - var controller = new PIDController(0.05, 0.0, 0.0); - controller.enableContinuousInput(-kRange / 2, kRange / 2); + try (var controller = new PIDController(0.05, 0.0, 0.0)) { + controller.enableContinuousInput(-kRange / 2, kRange / 2); - controller.setTolerance(kTolerance); - controller.setSetpoint(kSetpoint); + controller.setTolerance(kTolerance); + controller.setSetpoint(kSetpoint); - controller.calculate(0.0); + controller.calculate(0.0); - assertFalse(controller.atSetpoint(), - "Error was in tolerance when it should not have been. Error was " + controller - .getPositionError()); + assertFalse(controller.atSetpoint(), + "Error was in tolerance when it should not have been. Error was " + + controller.getPositionError()); - controller.calculate(kSetpoint + kTolerance / 2); + controller.calculate(kSetpoint + kTolerance / 2); - assertTrue(controller.atSetpoint(), - "Error was not in tolerance when it should have been. Error was " + controller - .getPositionError()); + assertTrue(controller.atSetpoint(), + "Error was not in tolerance when it should have been. Error was " + + controller.getPositionError()); - controller.calculate(kSetpoint + 10 * kTolerance); + controller.calculate(kSetpoint + 10 * kTolerance); - assertFalse(controller.atSetpoint(), - "Error was in tolerance when it should not have been. Error was " + controller - .getPositionError()); + assertFalse(controller.atSetpoint(), + "Error was in tolerance when it should not have been. Error was " + + controller.getPositionError()); + } } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogInputSimTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogInputSimTest.java index e78abf9d8e..a707725ace 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogInputSimTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogInputSimTest.java @@ -25,21 +25,18 @@ class AnalogInputSimTest { void setCallbackTest() { HAL.initialize(500, 0); - AnalogInput input = new AnalogInput(5); - AnalogInputSim inputSim = new AnalogInputSim(input); + try (AnalogInput input = new AnalogInput(5)) { + AnalogInputSim inputSim = new AnalogInputSim(input); - for (double i = 0; i < 5.0; i += 0.1) { - inputSim.setVoltage(0); + for (double i = 0; i < 5.0; i += 0.1) { + inputSim.setVoltage(0); - assertEquals(input.getVoltage(), 0, 0.001); + assertEquals(input.getVoltage(), 0, 0.001); - inputSim.setVoltage(i); - - assertEquals(input.getVoltage(), i, 0.001); + inputSim.setVoltage(i); + assertEquals(input.getVoltage(), i, 0.001); + } } - - input.close(); - } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogOutputSimTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogOutputSimTest.java index fdb7384902..fe1bba8d12 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogOutputSimTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/simulation/AnalogOutputSimTest.java @@ -34,38 +34,36 @@ class AnalogOutputSimTest { HAL.initialize(500, 0); - AnalogOutput output = new AnalogOutput(0); - output.setVoltage(0.5); + try (AnalogOutput output = new AnalogOutput(0)) { + output.setVoltage(0.5); - AnalogOutputSim outputSim = new AnalogOutputSim(output); + AnalogOutputSim outputSim = new AnalogOutputSim(output); - DoubleStore store = new DoubleStore(); + DoubleStore store = new DoubleStore(); - try (CallbackStore cb = outputSim.registerVoltageCallback((name, value) -> { - store.m_wasTriggered = true; - store.m_wasCorrectType = true; - store.m_setValue = value.getDouble(); - }, false)) { - assertFalse(store.m_wasTriggered); + try (CallbackStore cb = outputSim.registerVoltageCallback((name, value) -> { + store.m_wasTriggered = true; + store.m_wasCorrectType = true; + store.m_setValue = value.getDouble(); + }, false)) { + assertFalse(store.m_wasTriggered); - for (double i = 0.1; i < 5.0; i += 0.1) { - store.reset(); + for (double i = 0.1; i < 5.0; i += 0.1) { + store.reset(); - output.setVoltage(0); + output.setVoltage(0); - assertTrue(store.m_wasTriggered); - assertEquals(store.m_setValue, 0, 0.001); + assertTrue(store.m_wasTriggered); + assertEquals(store.m_setValue, 0, 0.001); - store.reset(); + store.reset(); - output.setVoltage(i); - - assertTrue(store.m_wasTriggered); - assertEquals(store.m_setValue, i, 0.001); + output.setVoltage(i); + assertTrue(store.m_wasTriggered); + assertEquals(store.m_setValue, i, 0.001); + } } } - - output.close(); } } diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/CubicHermiteSplineTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/CubicHermiteSplineTest.java index 2aac93da08..710bab55bc 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/CubicHermiteSplineTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/CubicHermiteSplineTest.java @@ -32,7 +32,7 @@ class CubicHermiteSplineTest { @SuppressWarnings({"ParameterName", "PMD.UnusedLocalVariable"}) private void run(Pose2d a, List waypoints, Pose2d b) { // Start the timer. - var start = System.nanoTime(); + //var start = System.nanoTime(); // Generate and parameterize the spline. var controlVectors = @@ -51,10 +51,10 @@ class CubicHermiteSplineTest { } // End the timer. - var end = System.nanoTime(); + //var end = System.nanoTime(); // Calculate the duration (used when benchmarking) - var durationMicroseconds = (end - start) / 1000.0; + //var durationMicroseconds = (end - start) / 1000.0; for (int i = 0; i < poses.size() - 1; i++) { var p0 = poses.get(i); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/QuinticHermiteSplineTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/QuinticHermiteSplineTest.java index 052bd0468d..b84f1f750d 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/QuinticHermiteSplineTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/spline/QuinticHermiteSplineTest.java @@ -28,7 +28,7 @@ class QuinticHermiteSplineTest { @SuppressWarnings({ "ParameterName", "PMD.UnusedLocalVariable" }) private void run(Pose2d a, Pose2d b) { // Start the timer. - var start = System.nanoTime(); + //var start = System.nanoTime(); // Generate and parameterize the spline. var controlVectors = SplineHelper.getQuinticControlVectorsFromWaypoints(List.of(a, b)); @@ -37,10 +37,10 @@ class QuinticHermiteSplineTest { var poses = SplineParameterizer.parameterize(spline); // End the timer. - var end = System.nanoTime(); + //var end = System.nanoTime(); // Calculate the duration (used when benchmarking) - var durationMicroseconds = (end - start) / 1000.0; + //var durationMicroseconds = (end - start) / 1000.0; for (int i = 0; i < poses.size() - 1; i++) { var p0 = poses.get(i); diff --git a/wpilibj/src/test/java/edu/wpi/first/wpilibj/trajectory/TrajectoryGeneratorTest.java b/wpilibj/src/test/java/edu/wpi/first/wpilibj/trajectory/TrajectoryGeneratorTest.java index 75323672f3..19e3fa09a6 100644 --- a/wpilibj/src/test/java/edu/wpi/first/wpilibj/trajectory/TrajectoryGeneratorTest.java +++ b/wpilibj/src/test/java/edu/wpi/first/wpilibj/trajectory/TrajectoryGeneratorTest.java @@ -75,8 +75,7 @@ class TrajectoryGeneratorTest { @Test void testMalformedTrajectory() { - var dsSim = new DriverStationSim(); - dsSim.setSendError(false); + DriverStationSim.setSendError(false); var traj = TrajectoryGenerator.generateTrajectory( @@ -90,6 +89,6 @@ class TrajectoryGeneratorTest { assertEquals(traj.getStates().size(), 1); assertEquals(traj.getTotalTimeSeconds(), 0); - dsSim.setSendError(true); + DriverStationSim.setSendError(true); } } diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/MotorEncoderTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/MotorEncoderTest.java index 7fcd01f0d9..e6e48a60d0 100644 --- a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/MotorEncoderTest.java +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/MotorEncoderTest.java @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ +/* Copyright (c) 2008-2020 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -177,47 +177,45 @@ public class MotorEncoderTest extends AbstractComsSetup { @Test public void testPositionPIDController() { - PIDController pidController = new PIDController(0.001, 0.0005, 0); - pidController.setTolerance(50.0); - pidController.setIntegratorRange(-0.2, 0.2); - pidController.setSetpoint(1000); + try (PIDController pidController = new PIDController(0.001, 0.0005, 0)) { + pidController.setTolerance(50.0); + pidController.setIntegratorRange(-0.2, 0.2); + pidController.setSetpoint(1000); - Notifier pidRunner = new Notifier(() -> { - var speed = pidController.calculate(me.getEncoder().getDistance()); - me.getMotor().set(MathUtil.clamp(speed, -0.2, 0.2)); - }); + try (Notifier pidRunner = new Notifier(() -> { + var speed = pidController.calculate(me.getEncoder().getDistance()); + me.getMotor().set(MathUtil.clamp(speed, -0.2, 0.2)); + })) { + pidRunner.startPeriodic(pidController.getPeriod()); + Timer.delay(10.0); + pidRunner.stop(); - pidRunner.startPeriodic(pidController.getPeriod()); - Timer.delay(10.0); - pidRunner.stop(); - - assertTrue( - "PID loop did not reach reference within 10 seconds. The current error was" + pidController - .getPositionError(), pidController.atSetpoint()); - - pidRunner.close(); + assertTrue( + "PID loop did not reach reference within 10 seconds. The current error was" + + pidController.getPositionError(), pidController.atSetpoint()); + } + } } @Test public void testVelocityPIDController() { LinearFilter filter = LinearFilter.movingAverage(50); - PIDController pidController = new PIDController(1e-5, 0.0, 0.0006); - pidController.setTolerance(200); - pidController.setSetpoint(600); + try (PIDController pidController = new PIDController(1e-5, 0.0, 0.0006)) { + pidController.setTolerance(200); + pidController.setSetpoint(600); - Notifier pidRunner = new Notifier(() -> { - var speed = filter.calculate(me.getEncoder().getRate()); - me.getMotor().set(MathUtil.clamp(speed, -0.3, 0.3)); - }); + try (Notifier pidRunner = new Notifier(() -> { + var speed = filter.calculate(me.getEncoder().getRate()); + me.getMotor().set(MathUtil.clamp(speed, -0.3, 0.3)); + })) { + pidRunner.startPeriodic(pidController.getPeriod()); + Timer.delay(10.0); + pidRunner.stop(); - pidRunner.startPeriodic(pidController.getPeriod()); - Timer.delay(10.0); - pidRunner.stop(); - - assertTrue("PID loop did not reach reference within 10 seconds. The error was: " + pidController - .getPositionError(), pidController.atSetpoint()); - - pidRunner.close(); + assertTrue("PID loop did not reach reference within 10 seconds. The error was: " + + pidController.getPositionError(), pidController.atSetpoint()); + } + } } /**