Skip to main content

ic_testkit/pic/
time.rs

1use pocket_ic::{PocketIc, Time};
2
3/// Multi-step time helpers that add test policy beyond PocketIC's primitives.
4pub trait PocketIcTimeExt {
5    /// Advance PocketIC by a fixed number of execution rounds.
6    fn tick_n(&self, times: usize);
7
8    /// Capture PocketIC wall-clock time as nanoseconds since the Unix epoch.
9    fn current_time_nanos(&self) -> u64;
10
11    /// Restore wall-clock and certified time from one captured value.
12    fn restore_time_nanos(&self, nanos_since_epoch: u64);
13}
14
15impl PocketIcTimeExt for PocketIc {
16    fn tick_n(&self, times: usize) {
17        for _ in 0..times {
18            self.tick();
19        }
20    }
21
22    fn current_time_nanos(&self) -> u64 {
23        self.get_time().as_nanos_since_unix_epoch()
24    }
25
26    fn restore_time_nanos(&self, nanos_since_epoch: u64) {
27        let restored = Time::from_nanos_since_unix_epoch(nanos_since_epoch);
28        self.set_time(restored);
29        self.set_certified_time(restored);
30    }
31}