pub struct Clock { /* private fields */ }Expand description
A clock that reads real time, or a test’s time that moves only on command.
Clones share one clock. Equality compares identity, so all real clocks are
equal and a test clock equals only the handles of its own TestClock.
Implementations§
Source§impl Clock
impl Clock
Sourcepub fn after(&self, duration: Duration) -> Receiver<Instant>
Available on crate feature crossbeam only.
pub fn after(&self, duration: Duration) -> Receiver<Instant>
crossbeam only.Returns a receiver that gets the instant duration from now once this
clock reaches it.
It mirrors crossbeam_channel::after, which it is on the real clock. On
a test clock, an advance to the deadline sends it, and the channel stays
connected while the clock lives. A duration past the end of Instant’s
range returns a receiver that never fires.
Wait for a job or a timeout, driven from a test through wait_timers,
since wait_blocked cannot see a thread blocked in select!:
use darkbio_clock::TestClock;
use darkbio_clock::crossbeam_channel::{select, unbounded};
use std::thread;
use std::time::Duration;
let mut tester = TestClock::new();
let clock = tester.clock();
let (_jobs, queue) = unbounded::<u32>();
let worker = thread::spawn(move || {
select! {
recv(queue) -> job => job.ok(),
recv(clock.after(Duration::from_secs(5))) -> _ => None,
}
});
tester.wait_timers(1);
tester.advance(Duration::from_secs(5));
assert_eq!(worker.join().unwrap(), None);Sourcepub fn at(&self, deadline: Instant) -> Receiver<Instant>
Available on crate feature crossbeam only.
pub fn at(&self, deadline: Instant) -> Receiver<Instant>
crossbeam only.Returns a receiver that gets deadline once this clock reaches it, at
once if it already has.
It mirrors crossbeam_channel::at, which it is on the real clock. On a
test clock, an advance to the deadline sends it, even one that overshoots,
and the channel stays connected while the clock lives.
Sourcepub fn recv_timeout<T>(
&self,
receiver: &Receiver<T>,
timeout: Duration,
) -> Result<T, RecvTimeoutError>
Available on crate feature crossbeam only.
pub fn recv_timeout<T>( &self, receiver: &Receiver<T>, timeout: Duration, ) -> Result<T, RecvTimeoutError>
crossbeam only.Receives a message, waiting up to timeout on this clock.
It mirrors crossbeam’s Receiver::recv_timeout, which it is on the real
clock. A buffered message or a disconnection wins over the timeout. A
timeout past the end of Instant’s range waits like Receiver::recv.
Sourcepub fn recv_deadline<T>(
&self,
receiver: &Receiver<T>,
deadline: Instant,
) -> Result<T, RecvTimeoutError>
Available on crate feature crossbeam only.
pub fn recv_deadline<T>( &self, receiver: &Receiver<T>, deadline: Instant, ) -> Result<T, RecvTimeoutError>
crossbeam only.Receives a message, waiting until this clock reaches deadline.
It mirrors crossbeam’s Receiver::recv_deadline, which it is on the real
clock. A buffered message or a disconnection wins over the deadline. On a
test clock, a waiting receive arms a timer, which wait_timers counts
until it fires or the receive returns.
Source§impl Clock
impl Clock
Sourcepub const fn real() -> Self
pub const fn real() -> Self
Returns the real clock, which reads the system’s monotonic and wall times.
Sourcepub fn elapsed(&self, since: Instant) -> Duration
pub fn elapsed(&self, since: Instant) -> Duration
Returns the time since since, or zero if it is later than now.
Sourcepub fn system_time(&self) -> SystemTime
pub fn system_time(&self) -> SystemTime
Returns the clock’s current wall time.
Sourcepub fn sleep_until(&self, deadline: Instant)
pub fn sleep_until(&self, deadline: Instant)
Blocks the thread until this clock reaches deadline, returning at once
if it already has.
On a test clock, only its advances end the sleep.