com.twitter.common.util.testing
Class FakeClock

java.lang.Object
  extended by com.twitter.common.util.testing.FakeClock
All Implemented Interfaces:
Clock

public class FakeClock
extends Object
implements Clock

A clock for use in testing with a configurable value for nowMillis().


Field Summary
 
Fields inherited from interface com.twitter.common.util.Clock
SYSTEM_CLOCK
 
Constructor Summary
FakeClock()
           
 
Method Summary
 void advance(Amount<Long,Time> period)
          Advances the current time by millis milliseconds.
 long nowMillis()
          Returns the current time in milliseconds since the epoch.
 long nowNanos()
          Returns the current time in nanoseconds.
 void setNowMillis(long nowMillis)
          Sets what nowMillis() will return until this method is called again with a new value for now.
 void waitFor(long millis)
          Waits in fake time, immediately returning in real time; however a check of nowMillis() after this method completes will consistently reveal that millis did in fact pass while waiting.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FakeClock

public FakeClock()
Method Detail

setNowMillis

public void setNowMillis(long nowMillis)
Sets what nowMillis() will return until this method is called again with a new value for now.

Parameters:
nowMillis - the current time in milliseconds

advance

public void advance(Amount<Long,Time> period)
Advances the current time by millis milliseconds. Time can be retarded by passing a negative value.

Parameters:
period - the amount of time to advance the current time by

nowMillis

public long nowMillis()
Description copied from interface: Clock
Returns the current time in milliseconds since the epoch.

Specified by:
nowMillis in interface Clock
Returns:
The current time in milliseconds since the epoch.
See Also:
System.currentTimeMillis()

nowNanos

public long nowNanos()
Description copied from interface: Clock
Returns the current time in nanoseconds. Should be used only for relative timing. System.nanoTime() for tips on using the value returned here.

Specified by:
nowNanos in interface Clock
Returns:
A measure of the current time in nanoseconds.

waitFor

public void waitFor(long millis)
Waits in fake time, immediately returning in real time; however a check of nowMillis() after this method completes will consistently reveal that millis did in fact pass while waiting.

Specified by:
waitFor in interface Clock
Parameters:
millis - the amount of time to wait in milliseconds