Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd] Fix RepeatCommand calling end() twice #5261

Merged
merged 5 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public boolean isFinished() {

@Override
public void end(boolean interrupted) {
m_command.end(interrupted);
// Make sure we didn't already call end() (which would happen if the command finished in the
// last call to our execute())
if (!m_ended) {
m_command.end(interrupted);
m_ended = true;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ bool RepeatCommand::IsFinished() {
}

void RepeatCommand::End(bool interrupted) {
m_command->End(interrupted);
// Make sure we didn't already call end() (which would happen if the command
// finished in the last call to our execute())
if (!m_ended) {
m_command->End(interrupted);
m_ended = true;
}
}

bool RepeatCommand::RunsWhenDisabled() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,69 @@ class RepeatCommandTest extends CommandTestBase
implements SingleCompositionTestBase<RepeatCommand> {
@Test
void callsMethodsCorrectly() {
var initCounter = new AtomicInteger(0);
var exeCounter = new AtomicInteger(0);
var isFinishedCounter = new AtomicInteger(0);
var endCounter = new AtomicInteger(0);
var isFinishedHook = new AtomicBoolean(false);
try (CommandScheduler scheduler = new CommandScheduler()) {
var initCounter = new AtomicInteger(0);
var exeCounter = new AtomicInteger(0);
var isFinishedCounter = new AtomicInteger(0);
var endCounter = new AtomicInteger(0);
var isFinishedHook = new AtomicBoolean(false);

final var command =
new FunctionalCommand(
initCounter::incrementAndGet,
exeCounter::incrementAndGet,
interrupted -> endCounter.incrementAndGet(),
() -> {
isFinishedCounter.incrementAndGet();
return isFinishedHook.get();
})
.repeatedly();
final var command =
new FunctionalCommand(
initCounter::incrementAndGet,
exeCounter::incrementAndGet,
interrupted -> endCounter.incrementAndGet(),
() -> {
isFinishedCounter.incrementAndGet();
return isFinishedHook.get();
})
.repeatedly();

assertEquals(0, initCounter.get());
assertEquals(0, exeCounter.get());
assertEquals(0, isFinishedCounter.get());
assertEquals(0, endCounter.get());
assertEquals(0, initCounter.get());
assertEquals(0, exeCounter.get());
assertEquals(0, isFinishedCounter.get());
assertEquals(0, endCounter.get());

CommandScheduler.getInstance().schedule(command);
assertEquals(1, initCounter.get());
assertEquals(0, exeCounter.get());
assertEquals(0, isFinishedCounter.get());
assertEquals(0, endCounter.get());
scheduler.schedule(command);
assertEquals(1, initCounter.get());
assertEquals(0, exeCounter.get());
assertEquals(0, isFinishedCounter.get());
assertEquals(0, endCounter.get());

isFinishedHook.set(false);
CommandScheduler.getInstance().run();
assertEquals(1, initCounter.get());
assertEquals(1, exeCounter.get());
assertEquals(1, isFinishedCounter.get());
assertEquals(0, endCounter.get());
isFinishedHook.set(false);
scheduler.run();
assertEquals(1, initCounter.get());
assertEquals(1, exeCounter.get());
assertEquals(1, isFinishedCounter.get());
assertEquals(0, endCounter.get());

isFinishedHook.set(true);
CommandScheduler.getInstance().run();
assertEquals(1, initCounter.get());
assertEquals(2, exeCounter.get());
assertEquals(2, isFinishedCounter.get());
assertEquals(1, endCounter.get());
isFinishedHook.set(true);
scheduler.run();
assertEquals(1, initCounter.get());
assertEquals(2, exeCounter.get());
assertEquals(2, isFinishedCounter.get());
assertEquals(1, endCounter.get());

isFinishedHook.set(false);
CommandScheduler.getInstance().run();
assertEquals(2, initCounter.get());
assertEquals(3, exeCounter.get());
assertEquals(3, isFinishedCounter.get());
assertEquals(1, endCounter.get());
isFinishedHook.set(false);
scheduler.run();
assertEquals(2, initCounter.get());
assertEquals(3, exeCounter.get());
assertEquals(3, isFinishedCounter.get());
assertEquals(1, endCounter.get());

isFinishedHook.set(true);
scheduler.run();
assertEquals(2, initCounter.get());
assertEquals(4, exeCounter.get());
assertEquals(4, isFinishedCounter.get());
assertEquals(2, endCounter.get());

scheduler.cancel(command);
assertEquals(2, initCounter.get());
assertEquals(4, exeCounter.get());
assertEquals(4, isFinishedCounter.get());
assertEquals(2, endCounter.get());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ TEST_F(RepeatCommandTest, CallsMethodsCorrectly) {
EXPECT_EQ(3, exeCounter);
EXPECT_EQ(3, isFinishedCounter);
EXPECT_EQ(1, endCounter);

isFinishedHook = true;
scheduler.Run();
EXPECT_EQ(2, initCounter);
EXPECT_EQ(4, exeCounter);
EXPECT_EQ(4, isFinishedCounter);
EXPECT_EQ(2, endCounter);

command.Cancel();
EXPECT_EQ(2, initCounter);
EXPECT_EQ(4, exeCounter);
EXPECT_EQ(4, isFinishedCounter);
EXPECT_EQ(2, endCounter);
}

INSTANTIATE_SINGLE_COMMAND_COMPOSITION_TEST_SUITE(RepeatCommandTest,
Expand Down