Skip to content

Commit

Permalink
move some tests to schedulingRecursionTest & improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kytpbs committed Dec 16, 2024
1 parent 3c185de commit 3078be4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,45 +101,6 @@ TEST_F(CommandScheduleTest, SchedulerCancel) {
EXPECT_FALSE(scheduler.IsScheduled(&command));
}

TEST_F(CommandScheduleTest, CancelNextCommand) {
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);
}

TEST_F(CommandScheduleTest, CommandKnowsWhenItEnded) {
CommandScheduler scheduler = GetScheduler();

Expand Down Expand Up @@ -169,7 +130,7 @@ TEST_F(CommandScheduleTest, ScheduleCommandInCommand) {
frc2::RunCommand([&counter, &scheduler, &commandToGetScheduled] {
scheduler.Schedule(&commandToGetScheduled);
EXPECT_EQ(counter, 1)
<< "Scheduled cmmand's init was not run immediately "
<< "Scheduled command's init was not run immediately "
"after getting scheduled";
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3078be4

Please sign in to comment.