|
5 | 5 | package edu.wpi.first.wpilibj2.command;
|
6 | 6 |
|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
8 | 9 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
9 | 10 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
10 | 11 | import static org.mockito.Mockito.never;
|
|
13 | 14 |
|
14 | 15 | import edu.wpi.first.networktables.NetworkTableInstance;
|
15 | 16 | import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
16 | 18 | import org.junit.jupiter.api.Test;
|
17 | 19 |
|
18 | 20 | class CommandScheduleTest extends CommandTestBase {
|
@@ -109,6 +111,49 @@ void schedulerCancelTest() {
|
109 | 111 | }
|
110 | 112 | }
|
111 | 113 |
|
| 114 | + @Test |
| 115 | + void cancelNextCommandTest() { |
| 116 | + try (CommandScheduler scheduler = new CommandScheduler()) { |
| 117 | + Command[] commands = new Command[2]; |
| 118 | + var commandRunCounter = new AtomicInteger(0); |
| 119 | + // due to the ordering of sets being non-deterministic, we cancel the other command in the |
| 120 | + // first command and check that the second command is not run |
| 121 | + Command command1 = |
| 122 | + new RunCommand( |
| 123 | + () -> { |
| 124 | + scheduler.cancel(commands[1]); |
| 125 | + commandRunCounter.incrementAndGet(); |
| 126 | + }); |
| 127 | + Command command2 = |
| 128 | + new RunCommand( |
| 129 | + () -> { |
| 130 | + scheduler.cancel(commands[0]); |
| 131 | + commandRunCounter.incrementAndGet(); |
| 132 | + }); |
| 133 | + |
| 134 | + commands[0] = command1; |
| 135 | + commands[1] = command2; |
| 136 | + |
| 137 | + scheduler.schedule(command1, command2); |
| 138 | + scheduler.run(); |
| 139 | + |
| 140 | + assertEquals( |
| 141 | + 1, commandRunCounter.get(), "Second command was run when it shouldn't have been"); |
| 142 | + |
| 143 | + // only one of the commands should be canceled. |
| 144 | + assertFalse( |
| 145 | + scheduler.isScheduled(command1) && scheduler.isScheduled(command2), |
| 146 | + "None of the commands were canceled when one should have been"); |
| 147 | + // one of the commands shouldn't be canceled because the other one is canceled first |
| 148 | + assertTrue( |
| 149 | + scheduler.isScheduled(command1) || scheduler.isScheduled(command2), |
| 150 | + "Both commands were canceled when only one should have been"); |
| 151 | + |
| 152 | + scheduler.run(); |
| 153 | + assertEquals(2, commandRunCounter.get()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
112 | 157 | @Test
|
113 | 158 | void notScheduledCancelTest() {
|
114 | 159 | try (CommandScheduler scheduler = new CommandScheduler()) {
|
|
0 commit comments