[commands] Add interruptor parameter to onCommandInterrupt callbacks (#5461)

This commit is contained in:
Joseph Eng
2023-09-17 20:47:37 -07:00
committed by GitHub
parent e93c233d60
commit b265a68eea
5 changed files with 245 additions and 35 deletions

View File

@@ -43,6 +43,74 @@ TEST_F(SchedulerTest, SchedulerLambdaInterrupt) {
EXPECT_EQ(counter, 1);
}
TEST_F(SchedulerTest, SchedulerLambdaInterruptNoCause) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
scheduler.OnCommandInterrupt(
[&counter](const Command&, const std::optional<Command*>& interruptor) {
EXPECT_FALSE(interruptor);
counter++;
});
RunCommand command([] {});
scheduler.Schedule(&command);
scheduler.Cancel(&command);
EXPECT_EQ(1, counter);
}
TEST_F(SchedulerTest, SchedulerLambdaInterruptCause) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
TestSubsystem subsystem{};
RunCommand command([] {}, {&subsystem});
InstantCommand interruptor([] {}, {&subsystem});
scheduler.OnCommandInterrupt(
[&](const Command&, const std::optional<Command*>& cause) {
ASSERT_TRUE(cause);
EXPECT_EQ(&interruptor, *cause);
counter++;
});
scheduler.Schedule(&command);
scheduler.Schedule(&interruptor);
EXPECT_EQ(1, counter);
}
TEST_F(SchedulerTest, SchedulerLambdaInterruptCauseInRunLoop) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
TestSubsystem subsystem{};
RunCommand command([] {}, {&subsystem});
InstantCommand interruptor([] {}, {&subsystem});
// This command will schedule interruptor in execute() inside the run loop
InstantCommand interruptorScheduler(
[&] { scheduler.Schedule(&interruptor); });
scheduler.OnCommandInterrupt(
[&](const Command&, const std::optional<Command*>& cause) {
ASSERT_TRUE(cause);
EXPECT_EQ(&interruptor, *cause);
counter++;
});
scheduler.Schedule(&command);
scheduler.Schedule(&interruptorScheduler);
scheduler.Run();
EXPECT_EQ(1, counter);
}
TEST_F(SchedulerTest, RegisterSubsystem) {
CommandScheduler scheduler = GetScheduler();
@@ -78,6 +146,10 @@ TEST_F(SchedulerTest, SchedulerCancelAll) {
int counter = 0;
scheduler.OnCommandInterrupt([&counter](const Command&) { counter++; });
scheduler.OnCommandInterrupt(
[](const Command&, const std::optional<Command*>& interruptor) {
EXPECT_FALSE(interruptor);
});
scheduler.Schedule(&command);
scheduler.Schedule(&command2);