Skip to content

Commit 2d54082

Browse files
committed
add a test to cancel the next command from the first one
1 parent 2f5c5c4 commit 2d54082

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandScheduleTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package edu.wpi.first.wpilibj2.command;
66

77
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
89
import static org.junit.jupiter.api.Assertions.assertFalse;
910
import static org.junit.jupiter.api.Assertions.assertTrue;
1011
import static org.mockito.Mockito.never;
@@ -13,6 +14,7 @@
1314

1415
import edu.wpi.first.networktables.NetworkTableInstance;
1516
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
17+
import java.util.concurrent.atomic.AtomicInteger;
1618
import org.junit.jupiter.api.Test;
1719

1820
class CommandScheduleTest extends CommandTestBase {
@@ -109,6 +111,49 @@ void schedulerCancelTest() {
109111
}
110112
}
111113

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+
112157
@Test
113158
void notScheduledCancelTest() {
114159
try (CommandScheduler scheduler = new CommandScheduler()) {

wpilibNewCommands/src/test/native/cpp/frc2/command/CommandScheduleTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Open Source Software; you can modify and/or share it under the terms of
33
// the WPILib BSD license file in the root directory of this project.
44

5+
#include <frc2/command/RunCommand.h>
6+
7+
#include <memory>
8+
59
#include <frc/smartdashboard/SmartDashboard.h>
610
#include <networktables/NetworkTableInstance.h>
711

@@ -102,6 +106,43 @@ TEST_F(CommandScheduleTest, NotScheduledCancel) {
102106
EXPECT_NO_FATAL_FAILURE(scheduler.Cancel(&command));
103107
}
104108

109+
TEST_F(CommandScheduleTest, CancelNextCommand) {
110+
CommandScheduler scheduler = GetScheduler();
111+
112+
frc2::RunCommand** command1PtrPtr = new frc2::RunCommand*;
113+
frc2::RunCommand** command2PtrPtr = new frc2::RunCommand*;
114+
auto counterPtr = std::make_shared<int>(0);
115+
116+
auto command1 = frc2::RunCommand([counterPtr, &command2PtrPtr, &scheduler] {
117+
scheduler.Cancel(*command2PtrPtr);
118+
(*counterPtr)++;
119+
});
120+
auto command2 = frc2::RunCommand([counterPtr, &command1PtrPtr, &scheduler] {
121+
scheduler.Cancel(*command1PtrPtr);
122+
(*counterPtr)++;
123+
});
124+
125+
*command1PtrPtr = &command1;
126+
*command2PtrPtr = &command2;
127+
128+
scheduler.Schedule(&command1);
129+
scheduler.Schedule(&command2);
130+
scheduler.Run();
131+
132+
EXPECT_EQ(*counterPtr, 1);
133+
134+
// only one of the commands should be canceled.
135+
EXPECT_FALSE(scheduler.IsScheduled(&command1) &&
136+
scheduler.IsScheduled(&command2));
137+
// one of the commands shouldn't be canceled because the other one is canceled
138+
// first
139+
EXPECT_TRUE(scheduler.IsScheduled(&command1) ||
140+
scheduler.IsScheduled(&command2));
141+
142+
scheduler.Run();
143+
EXPECT_EQ(*counterPtr, 2);
144+
}
145+
105146
TEST_F(CommandScheduleTest, SmartDashboardCancel) {
106147
CommandScheduler scheduler = GetScheduler();
107148
frc::SmartDashboard::PutData("Scheduler", &scheduler);

0 commit comments

Comments
 (0)