From d606116bad05588daa9ad138cd55eb36ee458146 Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Sun, 30 Nov 2025 22:33:24 -0800 Subject: [PATCH 1/2] Update WPILibExtension to call stepTiming() after each simulated loop --- .../junit/jupiter/WPILibExtension.java | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java b/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java index 2fbf0474..7e005720 100644 --- a/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java +++ b/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java @@ -1,7 +1,10 @@ package com.team2813.lib2813.testing.junit.jupiter; import edu.wpi.first.hal.HAL; +import edu.wpi.first.wpilibj.RuntimeType; +import edu.wpi.first.wpilibj.TimedRobot; import edu.wpi.first.wpilibj.simulation.DriverStationSim; +import edu.wpi.first.wpilibj.simulation.SimHooks; import edu.wpi.first.wpilibj2.command.CommandScheduler; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; @@ -48,6 +51,7 @@ public final class WPILibExtension AfterEachCallback, BeforeAllCallback, ParameterResolver { + private static final double NANOS_PER_SECOND = 1_000_000_000d; @Override public void beforeAll(ExtensionContext context) { @@ -57,22 +61,27 @@ public void beforeAll(ExtensionContext context) { } DriverStationSim.setEnabled(true); DriverStationSim.notifyNewData(); - CommandScheduler.getInstance().enable(); - CommandScheduler.getInstance().cancelAll(); - CommandScheduler.getInstance().unregisterAllSubsystems(); + SimHooks.setHALRuntimeType(RuntimeType.kSimulation.value); + + CommandScheduler commandScheduler = CommandScheduler.getInstance(); + commandScheduler.enable(); + commandScheduler.cancelAll(); + commandScheduler.unregisterAllSubsystems(); } @Override public void afterEach(ExtensionContext context) { - CommandScheduler.getInstance().cancelAll(); - CommandScheduler.getInstance().unregisterAllSubsystems(); + CommandScheduler commandScheduler = CommandScheduler.getInstance(); + commandScheduler.cancelAll(); + commandScheduler.unregisterAllSubsystems(); } @Override public void afterAll(ExtensionContext context) { - CommandScheduler.getInstance().cancelAll(); - CommandScheduler.getInstance().unregisterAllSubsystems(); - CommandScheduler.getInstance().disable(); + CommandScheduler commandScheduler = CommandScheduler.getInstance(); + commandScheduler.cancelAll(); + commandScheduler.unregisterAllSubsystems(); + commandScheduler.disable(); DriverStationSim.setEnabled(false); DriverStationSim.notifyNewData(); } @@ -87,12 +96,23 @@ public boolean supportsParameter( @Override public CommandTester resolveParameter( ParameterContext parameterContext, ExtensionContext extensionContext) { + CommandScheduler scheduler = CommandScheduler.getInstance(); + return command -> { - CommandScheduler scheduler = CommandScheduler.getInstance(); - scheduler.schedule(command); - do { - scheduler.run(); - } while (scheduler.isScheduled(command)); + SimHooks.pauseTiming(); + try { + scheduler.schedule(command); + do { + long startTimeNanos = System.nanoTime(); + scheduler.run(); + long runTimeNanos = System.nanoTime() - startTimeNanos; + double simSleepSeconds = + Math.max(TimedRobot.kDefaultPeriod - (runTimeNanos / NANOS_PER_SECOND), 0.0); + SimHooks.stepTiming(simSleepSeconds); + } while (scheduler.isScheduled(command)); + } finally { + SimHooks.resumeTiming(); + } }; } } From 0638081b169be6e36490acf9d5d4f3067d3a521a Mon Sep 17 00:00:00 2001 From: Kevin Cooney Date: Mon, 1 Dec 2025 09:02:44 -0800 Subject: [PATCH 2/2] Always progress the timer kDefaultPeriod per loop run --- .../lib2813/testing/junit/jupiter/WPILibExtension.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java b/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java index 7e005720..6aa2fd14 100644 --- a/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java +++ b/testing/src/main/java/com/team2813/lib2813/testing/junit/jupiter/WPILibExtension.java @@ -103,12 +103,8 @@ public CommandTester resolveParameter( try { scheduler.schedule(command); do { - long startTimeNanos = System.nanoTime(); scheduler.run(); - long runTimeNanos = System.nanoTime() - startTimeNanos; - double simSleepSeconds = - Math.max(TimedRobot.kDefaultPeriod - (runTimeNanos / NANOS_PER_SECOND), 0.0); - SimHooks.stepTiming(simSleepSeconds); + SimHooks.stepTiming(TimedRobot.kDefaultPeriod); } while (scheduler.isScheduled(command)); } finally { SimHooks.resumeTiming();