mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[commands] Fix C++ iterator invalidation bug (#7554)
Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com> Co-authored-by: Ryan Blue <13878527+rzblue@users.noreply.github.com>
This commit is contained in:
@@ -6,6 +6,9 @@
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
|
||||
#include "CommandTestBase.h"
|
||||
#include "frc2/command/FunctionalCommand.h"
|
||||
#include "frc2/command/InstantCommand.h"
|
||||
#include "frc2/command/RunCommand.h"
|
||||
|
||||
using namespace frc2;
|
||||
class CommandScheduleTest : public CommandTestBase {};
|
||||
@@ -95,6 +98,51 @@ TEST_F(CommandScheduleTest, SchedulerCancel) {
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command));
|
||||
}
|
||||
|
||||
TEST_F(CommandScheduleTest, CommandKnowsWhenItEnded) {
|
||||
CommandScheduler scheduler = GetScheduler();
|
||||
|
||||
frc2::FunctionalCommand* commandPtr = nullptr;
|
||||
auto command = frc2::FunctionalCommand(
|
||||
[] {}, [] {},
|
||||
[&](auto isForced) {
|
||||
EXPECT_FALSE(scheduler.IsScheduled(commandPtr))
|
||||
<< "Command shouldn't be scheduled when its end is called";
|
||||
},
|
||||
[] { return true; });
|
||||
commandPtr = &command;
|
||||
|
||||
scheduler.Schedule(commandPtr);
|
||||
scheduler.Run();
|
||||
EXPECT_FALSE(scheduler.IsScheduled(commandPtr))
|
||||
<< "Command should be removed from scheduler when its isFinished() "
|
||||
"returns true";
|
||||
}
|
||||
|
||||
TEST_F(CommandScheduleTest, ScheduleCommandInCommand) {
|
||||
CommandScheduler scheduler = GetScheduler();
|
||||
int counter = 0;
|
||||
frc2::InstantCommand commandToGetScheduled{[&counter] { counter++; }};
|
||||
|
||||
auto command =
|
||||
frc2::RunCommand([&counter, &scheduler, &commandToGetScheduled] {
|
||||
scheduler.Schedule(&commandToGetScheduled);
|
||||
EXPECT_EQ(counter, 1)
|
||||
<< "Scheduled command's init was not run immediately "
|
||||
"after getting scheduled";
|
||||
});
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Run();
|
||||
EXPECT_EQ(counter, 1) << "Command 2 was not run when it should have been";
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&commandToGetScheduled))
|
||||
<< "Command 2 was not added to scheduler";
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_EQ(counter, 1) << "Command 2 was run when it shouldn't have been";
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&commandToGetScheduled))
|
||||
<< "Command 2 did not end when it should have";
|
||||
}
|
||||
|
||||
TEST_F(CommandScheduleTest, NotScheduledCancel) {
|
||||
CommandScheduler scheduler = GetScheduler();
|
||||
MockCommand command;
|
||||
|
||||
@@ -339,6 +339,45 @@ TEST_F(SchedulingRecursionTest, CancelDefaultCommandFromEnd) {
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&other));
|
||||
}
|
||||
|
||||
TEST_F(SchedulingRecursionTest, CancelNextCommandFromCommand) {
|
||||
CommandScheduler scheduler = GetScheduler();
|
||||
|
||||
frc2::RunCommand* command1Ptr = nullptr;
|
||||
frc2::RunCommand* command2Ptr = nullptr;
|
||||
int counter = 0;
|
||||
|
||||
auto command1 = frc2::RunCommand([&counter, &command2Ptr, &scheduler] {
|
||||
scheduler.Cancel(command2Ptr);
|
||||
counter++;
|
||||
});
|
||||
auto command2 = frc2::RunCommand([&counter, &command1Ptr, &scheduler] {
|
||||
scheduler.Cancel(command1Ptr);
|
||||
counter++;
|
||||
});
|
||||
|
||||
command1Ptr = &command1;
|
||||
command2Ptr = &command2;
|
||||
|
||||
scheduler.Schedule(&command1);
|
||||
scheduler.Schedule(&command2);
|
||||
scheduler.Run();
|
||||
|
||||
EXPECT_EQ(counter, 1) << "Second command was run when it shouldn't have been";
|
||||
|
||||
// only one of the commands should be canceled.
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command1) &&
|
||||
scheduler.IsScheduled(&command2))
|
||||
<< "Both commands are running when only one should be";
|
||||
// one of the commands shouldn't be canceled because the other one is canceled
|
||||
// first
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&command1) ||
|
||||
scheduler.IsScheduled(&command2))
|
||||
<< "Both commands are canceled when only one should be";
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_EQ(counter, 2);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
SchedulingRecursionTests, SchedulingRecursionTest,
|
||||
testing::Values(Command::InterruptionBehavior::kCancelSelf,
|
||||
|
||||
Reference in New Issue
Block a user