Skip to content

Commit

Permalink
Rename set and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Aug 3, 2023
1 parent 41743b1 commit 0ab2c4a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static synchronized CommandScheduler getInstance() {
private boolean m_inRunLoop;
private final Set<Command> m_toSchedule = new LinkedHashSet<>();
private final Set<Command> m_toCancel = new LinkedHashSet<>();
private final Set<Command> m_cancelling = new LinkedHashSet<>();
private final Set<Command> m_ending = new LinkedHashSet<>();

private final Watchdog m_watchdog = new Watchdog(TimedRobot.kDefaultPeriod, () -> {});

Expand Down Expand Up @@ -273,9 +273,9 @@ public void run() {
Command command = iterator.next();

if (!command.runsWhenDisabled() && isDisabled) {
m_cancelling.add(command);
m_ending.add(command);
command.end(true);
m_cancelling.remove(command);
m_ending.remove(command);
for (Consumer<Command> action : m_interruptActions) {
action.accept(command);
}
Expand All @@ -291,9 +291,9 @@ public void run() {
}
m_watchdog.addEpoch(command.getName() + ".execute()");
if (command.isFinished()) {
m_cancelling.add(command);
m_ending.add(command);
command.end(false);
m_cancelling.remove(command);
m_ending.remove(command);
for (Consumer<Command> action : m_finishActions) {
action.accept(command);
}
Expand Down Expand Up @@ -452,7 +452,7 @@ public void cancel(Command... commands) {
}

for (Command command : commands) {
if (m_cancelling.contains(command)) {
if (m_ending.contains(command)) {
continue;
}
if (command == null) {
Expand All @@ -463,9 +463,9 @@ public void cancel(Command... commands) {
continue;
}

m_cancelling.add(command);
m_ending.add(command);
command.end(true);
m_cancelling.remove(command);
m_ending.remove(command);
for (Consumer<Command> action : m_interruptActions) {
action.accept(command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class CommandScheduler::Impl {
bool inRunLoop = false;
wpi::SmallVector<Command*, 4> toSchedule;
wpi::SmallVector<Command*, 4> toCancel;
wpi::SmallSet<Command*, 4> cancelling;
wpi::SmallSet<Command*, 4> ending;
};

template <typename TMap, typename TKey>
Expand Down Expand Up @@ -197,9 +197,9 @@ void CommandScheduler::Run() {
// Run scheduled commands, remove finished commands.
for (Command* command : m_impl->scheduledCommands) {
if (!command->RunsWhenDisabled() && isDisabled) {
m_impl->cancelling.insert(command);
m_impl->ending.insert(command);
command->End(true);
m_impl->cancelling.erase(command);
m_impl->ending.erase(command);
for (auto&& action : m_impl->interruptActions) {
action(*command);
}
Expand All @@ -219,9 +219,9 @@ void CommandScheduler::Run() {
m_watchdog.AddEpoch(command->GetName() + ".Execute()");

if (command->IsFinished()) {
m_impl->cancelling.insert(command);
m_impl->ending.insert(command);
command->End(false);
m_impl->cancelling.erase(command);
m_impl->ending.erase(command);
for (auto&& action : m_impl->finishActions) {
action(*command);
}
Expand Down Expand Up @@ -342,15 +342,15 @@ void CommandScheduler::Cancel(Command* command) {
m_impl->toCancel.emplace_back(command);
return;
}
if (m_impl->cancelling.contains(command)) {
if (m_impl->ending.contains(command)) {
return;
}
if (!IsScheduled(command)) {
return;
}
m_impl->cancelling.insert(command);
m_impl->ending.insert(command);
command->End(true);
m_impl->cancelling.erase(command);
m_impl->ending.erase(command);
for (auto&& action : m_impl->interruptActions) {
action(*command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,61 @@ void cancelFromEndLoop() {
}
}

@Test
void cancelFromEndLoopWhileInRunLoop() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicInteger counter = new AtomicInteger();
FunctionalCommand dCancelsAll =
new FunctionalCommand(
() -> {},
() -> {},
interrupted -> {
counter.incrementAndGet();
scheduler.cancelAll();
},
() -> true);
FunctionalCommand cCancelsD =
new FunctionalCommand(
() -> {},
() -> {},
interrupted -> {
counter.incrementAndGet();
scheduler.cancel(dCancelsAll);
},
() -> true);
FunctionalCommand bCancelsC =
new FunctionalCommand(
() -> {},
() -> {},
interrupted -> {
counter.incrementAndGet();
scheduler.cancel(cCancelsD);
},
() -> true);
FunctionalCommand aCancelsB =
new FunctionalCommand(
() -> {},
() -> {},
interrupted -> {
counter.incrementAndGet();
scheduler.cancel(bCancelsC);
},
() -> true);

scheduler.schedule(aCancelsB);
scheduler.schedule(bCancelsC);
scheduler.schedule(cCancelsD);
scheduler.schedule(dCancelsAll);

assertDoesNotThrow(() -> scheduler.run());
assertEquals(4, counter.get());
assertFalse(scheduler.isScheduled(aCancelsB));
assertFalse(scheduler.isScheduled(bCancelsC));
assertFalse(scheduler.isScheduled(cCancelsD));
assertFalse(scheduler.isScheduled(dCancelsAll));
}
}

@Test
void multiCancelFromEnd() {
try (CommandScheduler scheduler = new CommandScheduler()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class EndCommand : public CommandHelper<Command, EndCommand> {
public:
explicit EndCommand(std::function<void(bool)> end) : m_end(end) {}
void End(bool interrupted) override { m_end(interrupted); }

bool IsFinished() override {return true;}
private:
std::function<void(bool)> m_end;
};
Expand Down Expand Up @@ -158,6 +158,41 @@ TEST_F(SchedulingRecursionTest, CancelFromEndLoop) {
EXPECT_FALSE(scheduler.IsScheduled(&dCancelsAll));
}

TEST_F(SchedulingRecursionTest, CancelFromEndLoopWhileInRunLoop) {
CommandScheduler scheduler = GetScheduler();
int counter = 0;
EndCommand dCancelsAll([&counter, scheduler = &scheduler](bool) {
counter++;
scheduler->CancelAll();
});
EndCommand cCancelsD(
[&counter, scheduler = &scheduler, dCancelsAll = &dCancelsAll](bool) {
counter++;
scheduler->Cancel(dCancelsAll);
});
EndCommand bCancelsC(
[&counter, scheduler = &scheduler, cCancelsD = &cCancelsD](bool) {
counter++;
scheduler->Cancel(cCancelsD);
});
EndCommand aCancelsB(
[&counter, scheduler = &scheduler, bCancelsC = &bCancelsC](bool) {
counter++;
scheduler->Cancel(bCancelsC);
});
scheduler.Schedule(&aCancelsB);
scheduler.Schedule(&bCancelsC);
scheduler.Schedule(&cCancelsD);
scheduler.Schedule(&dCancelsAll);

EXPECT_NO_THROW({ scheduler.Run(); });
EXPECT_EQ(4, counter);
EXPECT_FALSE(scheduler.IsScheduled(&aCancelsB));
EXPECT_FALSE(scheduler.IsScheduled(&bCancelsC));
EXPECT_FALSE(scheduler.IsScheduled(&cCancelsD));
EXPECT_FALSE(scheduler.IsScheduled(&dCancelsAll));
}

class MultiCancelCommand : public CommandHelper<Command, MultiCancelCommand> {
public:
MultiCancelCommand(CommandScheduler* scheduler, int& counter,
Expand Down

0 comments on commit 0ab2c4a

Please sign in to comment.