Class PretendClock

java.lang.Object
com.crossbotics.ftchelper.testkit.PretendClock
All Implemented Interfaces:
LongSupplier

public final class PretendClock extends Object implements LongSupplier
A pretend clock for JUnit tests: the time only moves when your test says so. The robot's parts read it (through Time) instead of the real clock, so a test of timing does not wait and gives the same result on every computer.

 @After public void realClock() { PretendClock.stopUsing(); }

 @Test public void theArmPushesAfterStaging() {
     PretendClock clock = PretendClock.use();
     PretendMotor fly = devices.motor("launcher");
     robot.launcher.spinUp();
     for (int i = 0; i < 100; i++) {     // 0.5 s, 5 ms a loop
         clock.addSec(0.005);
         fly.velocity = fly.velocityCommand;   // the pretend wheel reaches the speed asked
         robot.all().update();
         if (i == 60) robot.launcher.fire();   // at 0.3 s: the wheel is ready
     }
     assertEquals(0.7, devices.servo("feeder_arm").position, 1e-9);
 }
 
Docs: docs/modules/testkit.md, docs/tests/03-checks-before-practice-and-matches.md
  • Method Details

    • use

      public static PretendClock use()
      Makes a pretend clock and has the robot's parts use it. Call stopUsing() in @After.
    • stopUsing

      public static void stopUsing()
      The real clock again (put it in @After).
    • addSec

      public void addSec(double sec)
      Moves the time forward. @param sec seconds (0 or more)
    • sec

      public double sec()
      The time now, seconds since this clock was made (it starts at 1).
    • getAsLong

      public long getAsLong()
      Specified by:
      getAsLong in interface LongSupplier