///|
/// The time of a server or a client (design SRV-3). Each time limit uses `now` and
/// `sleep` of one clock, so a test gives a clock that it moves itself. The server
/// and the client packages give the clock of the platform.
pub struct Clock {
  priv now : () -> Int64
  priv sleep : async (Int32) -> Unit
}

///|
/// A clock from `now`, which gives the time in milliseconds from any fixed point,
/// and `sleep`, which waits for a number of milliseconds.
pub fn Clock::new(now~ : () -> Int64, sleep~ : async (Int32) -> Unit) -> Clock {
  { now, sleep, }
}

///|
/// The time in milliseconds.
pub fn Clock::now(self : Clock) -> Int64 {
  (self.now)()
}

///|
/// Wait until the clock reaches `at`.
pub async fn Clock::sleep_until(self : Clock, at : Int64) -> Unit {
  while (self.now)() < at {
    let wait = at - (self.now)()
    (self.sleep)(if wait > 2147483647L { 2147483647 } else { wait.to_int() })
  }
}