Skip to content

Commit

Permalink
Add tests for scheduling/cancelling commands from actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Aug 13, 2023
1 parent 6b262fb commit 11f56b5
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ public InterruptionBehavior getInterruptionBehavior() {
}
}

@EnumSource(InterruptionBehavior.class)
@ParameterizedTest
void cancelFromInitializeAction(InterruptionBehavior interruptionBehavior) {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean hasOtherRun = new AtomicBoolean();
AtomicInteger counter = new AtomicInteger();
Subsystem requirement = new Subsystem() {};
Command selfCancels =
new Command() {
{
addRequirements(requirement);
}

@Override
public void end(boolean interrupted) {
counter.incrementAndGet();
}

@Override
public InterruptionBehavior getInterruptionBehavior() {
return interruptionBehavior;
}
};
Command other = new RunCommand(() -> hasOtherRun.set(true), requirement);

assertDoesNotThrow(
() -> {
scheduler.onCommandInitialize(cmd -> scheduler.cancel(selfCancels));
scheduler.schedule(selfCancels);
scheduler.run();
scheduler.schedule(other);
});
assertFalse(scheduler.isScheduled(selfCancels));
assertTrue(scheduler.isScheduled(other));
assertEquals(1, counter.get());
scheduler.run();
assertTrue(hasOtherRun.get());
}
}

@EnumSource(InterruptionBehavior.class)
@ParameterizedTest
void defaultCommandGetsRescheduledAfterSelfCanceling(InterruptionBehavior interruptionBehavior) {
Expand Down Expand Up @@ -129,6 +169,24 @@ public void end(boolean interrupted) {
}
}

@Test
void cancelFromInterruptAction() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicInteger counter = new AtomicInteger();
Command selfCancels = new RunCommand(() -> {});
scheduler.onCommandInterrupt(
cmd -> {
counter.incrementAndGet();
scheduler.cancel(selfCancels);
});
scheduler.schedule(selfCancels);

assertDoesNotThrow(() -> scheduler.cancel(selfCancels));
assertEquals(1, counter.get());
assertFalse(scheduler.isScheduled(selfCancels));
}
}

@Test
void cancelFromEndLoop() {
try (CommandScheduler scheduler = new CommandScheduler()) {
Expand Down Expand Up @@ -317,6 +375,27 @@ void scheduleFromEndInterrupt() {
}
}

@Test
void scheduleFromEndInterruptAction() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicInteger counter = new AtomicInteger();
Subsystem requirement = new Subsystem() {};
InstantCommand other = new InstantCommand(() -> {}, requirement);
InstantCommand selfCancels = new InstantCommand(() -> {}, requirement);
scheduler.onCommandInterrupt(
cmd -> {
counter.incrementAndGet();
scheduler.schedule(other);
});
scheduler.schedule(selfCancels);

assertDoesNotThrow(() -> scheduler.schedule(other));
assertEquals(1, counter.get());
assertFalse(scheduler.isScheduled(selfCancels));
assertTrue(scheduler.isScheduled(other));
}
}

@ParameterizedTest
@EnumSource(InterruptionBehavior.class)
void scheduleInitializeFromDefaultCommand(InterruptionBehavior interruptionBehavior) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ TEST_P(SchedulingRecursionTest, CancelFromInitialize) {
EXPECT_TRUE(hasOtherRun);
}

TEST_F(SchedulingRecursionTest, CancelFromInitializeAction) {
CommandScheduler scheduler = GetScheduler();
bool hasOtherRun = false;
int counter = 0;
TestSubsystem requirement;
FunctionalCommand selfCancels{[] {},
[] {},
[&counter](bool) { counter++; },
[] { return false; },
{&requirement}};
RunCommand other{[&hasOtherRun] { hasOtherRun = true; }, {&requirement}};
scheduler.OnCommandInitialize([&scheduler, &selfCancels](const Command&) {
scheduler.Cancel(&selfCancels);
});
scheduler.Schedule(&selfCancels);
scheduler.Run();
scheduler.Schedule(&other);

EXPECT_FALSE(scheduler.IsScheduled(&selfCancels));
EXPECT_TRUE(scheduler.IsScheduled(&other));
EXPECT_EQ(1, counter);
scheduler.Run();
EXPECT_TRUE(hasOtherRun);
}

TEST_P(SchedulingRecursionTest,
DefaultCommandGetsRescheduledAfterSelfCanceling) {
CommandScheduler scheduler = GetScheduler();
Expand Down Expand Up @@ -113,6 +138,22 @@ TEST_F(SchedulingRecursionTest, CancelFromEnd) {
EXPECT_FALSE(scheduler.IsScheduled(&selfCancels));
}

TEST_F(SchedulingRecursionTest, CancelFromInterruptAction) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
FunctionalCommand selfCancels{[] {}, [] {}, [](bool) {},
[] { return false; }};
scheduler.OnCommandInterrupt([&](const Command&) {
counter++;
scheduler.Cancel(&selfCancels);
});
scheduler.Schedule(&selfCancels);

EXPECT_NO_THROW({ scheduler.Cancel(&selfCancels); });
EXPECT_EQ(1, counter);
EXPECT_FALSE(scheduler.IsScheduled(&selfCancels));
}

class EndCommand : public CommandHelper<Command, EndCommand> {
public:
explicit EndCommand(std::function<void(bool)> end) : m_end(end) {}
Expand All @@ -130,11 +171,10 @@ TEST_F(SchedulingRecursionTest, CancelFromEndLoop) {
counter++;
scheduler.CancelAll();
});
EndCommand cCancelsD(
[&](bool) {
counter++;
scheduler.Cancel(&dCancelsAll);
});
EndCommand cCancelsD([&](bool) {
counter++;
scheduler.Cancel(&dCancelsAll);
});
EndCommand bCancelsC([&](bool) {
counter++;
scheduler.Cancel(&cCancelsD);
Expand Down Expand Up @@ -250,6 +290,23 @@ TEST_P(SchedulingRecursionTest, ScheduleFromEndInterrupt) {
EXPECT_TRUE(scheduler.IsScheduled(&other));
}

TEST_F(SchedulingRecursionTest, ScheduleFromEndInterruptAction) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
TestSubsystem requirement;
RunCommand selfCancels{[] {}, {&requirement}};
RunCommand other{[] {}, {&requirement}};
scheduler.OnCommandInterrupt([&](const Command&) {
counter++;
scheduler.Schedule(&other);
});
scheduler.Schedule(&selfCancels);
EXPECT_NO_THROW({ scheduler.Schedule(&other); });
EXPECT_EQ(1, counter);
EXPECT_FALSE(scheduler.IsScheduled(&selfCancels));
EXPECT_TRUE(scheduler.IsScheduled(&other));
}

TEST_F(SchedulingRecursionTest, CancelDefaultCommandFromEnd) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
Expand Down

0 comments on commit 11f56b5

Please sign in to comment.