pub struct TestRuntime { /* private fields */ }Expand description
A test runtime with virtual time for deterministic testing.
Unlike real runtimes, time only advances when you call advance_by() or advance_to().
This allows instant, reproducible tests for time-based operators.
§Example
use agent_rex::TestRuntime;
#[tokio::test]
async fn test_debounce() {
let runtime = TestRuntime::new();
// Create a debounced stream using this runtime
let source = futures::stream::iter(vec![1, 2, 3]);
let debounced = debounce_with::<TestRuntime>(Duration::from_millis(100), source);
// Advance virtual time to trigger debounce
runtime.advance_by(Duration::from_millis(150)).await;
// Collect results - happens instantly!
}Implementations§
Source§impl TestRuntime
impl TestRuntime
Sourcepub async fn advance_by(&self, duration: Duration)
pub async fn advance_by(&self, duration: Duration)
Advance virtual time by the given duration.
This will wake any timers whose target time has been reached.
Sourcepub async fn advance_to(&self, target: Duration)
pub async fn advance_to(&self, target: Duration)
Advance virtual time to a specific point.
Fires all timers between the current time and target time.
Source§impl TestRuntime
impl TestRuntime
Sourcepub fn test_sleep(&self, duration: Duration) -> TestSleep ⓘ
pub fn test_sleep(&self, duration: Duration) -> TestSleep ⓘ
Create a sleep future tied to this runtime instance.
Sourcepub fn test_interval(&self, period: Duration) -> TestInterval
pub fn test_interval(&self, period: Duration) -> TestInterval
Create an interval stream tied to this runtime instance.
Source§impl TestRuntime
impl TestRuntime
Sourcepub async fn run_timed_test<T, F, Fut>(&self, steps: Vec<Duration>, f: F) -> T
pub async fn run_timed_test<T, F, Fut>(&self, steps: Vec<Duration>, f: F) -> T
Run a test with controlled time, returning the result.
This is a convenience method that advances time in steps, useful for testing debounce/throttle behavior.
Sourcepub async fn assert_completes_within<T, Fut>(
&self,
timeout: Duration,
fut: Fut,
) -> Twhere
Fut: Future<Output = T>,
pub async fn assert_completes_within<T, Fut>(
&self,
timeout: Duration,
fut: Fut,
) -> Twhere
Fut: Future<Output = T>,
Assert that a future completes within a virtual time budget.
Trait Implementations§
Source§impl Clone for TestRuntime
impl Clone for TestRuntime
Source§fn clone(&self) -> TestRuntime
fn clone(&self) -> TestRuntime
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Default for TestRuntime
impl Default for TestRuntime
Auto Trait Implementations§
impl Freeze for TestRuntime
impl RefUnwindSafe for TestRuntime
impl Send for TestRuntime
impl Sync for TestRuntime
impl Unpin for TestRuntime
impl UnwindSafe for TestRuntime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more