[commands] Define order of parallel groups (#6602)

This commit is contained in:
Joseph Eng
2024-06-01 12:01:15 -07:00
committed by GitHub
parent 7751f6d1d2
commit 1828fdaaa4
6 changed files with 296 additions and 9 deletions

View File

@@ -4,6 +4,7 @@
package edu.wpi.first.wpilibj2.command;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -57,6 +58,36 @@ class CommandDecoratorTest extends CommandTestBase {
}
}
@Test
void untilOrderTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean firstHasRun = new AtomicBoolean(false);
AtomicBoolean firstWasPolled = new AtomicBoolean(false);
Command first =
new FunctionalCommand(
() -> {},
() -> firstHasRun.set(true),
interrupted -> {},
() -> {
firstWasPolled.set(true);
return true;
});
Command command =
first.until(
() -> {
assertAll(
() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get()));
return true;
});
scheduler.schedule(command);
scheduler.run();
assertAll(() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get()));
}
}
@Test
void onlyWhileTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
@@ -76,6 +107,36 @@ class CommandDecoratorTest extends CommandTestBase {
}
}
@Test
void onlyWhileOrderTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean firstHasRun = new AtomicBoolean(false);
AtomicBoolean firstWasPolled = new AtomicBoolean(false);
Command first =
new FunctionalCommand(
() -> {},
() -> firstHasRun.set(true),
interrupted -> {},
() -> {
firstWasPolled.set(true);
return true;
});
Command command =
first.onlyWhile(
() -> {
assertAll(
() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get()));
return false;
});
scheduler.schedule(command);
scheduler.run();
assertAll(() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get()));
}
}
@Test
void ignoringDisableTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
@@ -179,6 +240,37 @@ class CommandDecoratorTest extends CommandTestBase {
}
}
@Test
void deadlineForOrderTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean dictatorHasRun = new AtomicBoolean(false);
AtomicBoolean dictatorWasPolled = new AtomicBoolean(false);
Command dictator =
new FunctionalCommand(
() -> {},
() -> dictatorHasRun.set(true),
interrupted -> {},
() -> {
dictatorWasPolled.set(true);
return true;
});
Command other =
new RunCommand(
() ->
assertAll(
() -> assertTrue(dictatorHasRun.get()),
() -> assertTrue(dictatorWasPolled.get())));
Command group = dictator.deadlineFor(other);
scheduler.schedule(group);
scheduler.run();
assertAll(() -> assertTrue(dictatorHasRun.get()), () -> assertTrue(dictatorWasPolled.get()));
}
}
@Test
void alongWithTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
@@ -201,6 +293,36 @@ class CommandDecoratorTest extends CommandTestBase {
}
}
@Test
void alongWithOrderTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean firstHasRun = new AtomicBoolean(false);
AtomicBoolean firstWasPolled = new AtomicBoolean(false);
Command command1 =
new FunctionalCommand(
() -> {},
() -> firstHasRun.set(true),
interrupted -> {},
() -> {
firstWasPolled.set(true);
return true;
});
Command command2 =
new RunCommand(
() ->
assertAll(
() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get())));
Command group = command1.alongWith(command2);
scheduler.schedule(group);
scheduler.run();
assertAll(() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get()));
}
}
@Test
void raceWithTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
@@ -216,6 +338,37 @@ class CommandDecoratorTest extends CommandTestBase {
}
}
@Test
void raceWithOrderTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean firstHasRun = new AtomicBoolean(false);
AtomicBoolean firstWasPolled = new AtomicBoolean(false);
Command command1 =
new FunctionalCommand(
() -> {},
() -> firstHasRun.set(true),
interrupted -> {},
() -> {
firstWasPolled.set(true);
return true;
});
Command command2 =
new RunCommand(
() -> {
assertTrue(firstHasRun.get());
assertTrue(firstWasPolled.get());
});
Command group = command1.raceWith(command2);
scheduler.schedule(group);
scheduler.run();
assertAll(() -> assertTrue(firstHasRun.get()), () -> assertTrue(firstWasPolled.get()));
}
}
@Test
void unlessTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {