use simu::{AllOf, AnyOf};
use simu::SimEnv;
use simu::{all_of, any_of};
#[test]
fn any_of_first_timeout_wins() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
env.spawn(async move {
any_of![h.timeout(1.0), h.timeout(5.0)].await;
assert_eq!(h.now(), 1.0);
});
env.run();
}
#[test]
fn any_of_event_beats_timeout() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
let (trigger, signal) = env.event();
{
let h2 = h.clone();
env.spawn(async move {
h2.timeout(2.0).await;
trigger.fire();
});
}
env.spawn(async move {
any_of![h.timeout(10.0), signal].await;
assert_eq!(h.now(), 2.0);
});
env.run();
}
#[test]
fn any_of_already_fired_event() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
let (trigger, signal) = env.event();
trigger.fire();
env.spawn(async move {
any_of![h.timeout(99.0), signal].await;
assert_eq!(h.now(), 0.0); });
env.run();
}
#[test]
#[should_panic(expected = "AnyOf requires at least one future")]
fn any_of_empty_panics() {
let _fut = AnyOf::new(vec![]);
}
#[test]
fn all_of_waits_for_last() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
env.spawn(async move {
all_of![h.timeout(1.0), h.timeout(2.0), h.timeout(5.0)].await;
assert_eq!(h.now(), 5.0);
});
env.run();
}
#[test]
fn all_of_two_events() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
let (trigger_a, event_a) = env.event();
let (trigger_b, event_b) = env.event();
{
let h2 = h.clone();
env.spawn(async move {
h2.timeout(3.0).await;
trigger_a.fire();
});
}
{
let h2 = h.clone();
env.spawn(async move {
h2.timeout(7.0).await;
trigger_b.fire();
});
}
env.spawn(async move {
all_of![event_a, event_b].await;
assert_eq!(h.now(), 7.0);
});
env.run();
}
#[test]
fn all_of_empty_resolves_immediately() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
env.spawn(async move {
AllOf::new(vec![]).await;
assert_eq!(h.now(), 0.0);
});
env.run();
}
#[test]
fn all_of_mixed_timeout_and_event() {
let mut env = SimEnv::with_seed(0);
let h = env.handle();
let (trigger, signal) = env.event();
{
let h2 = h.clone();
env.spawn(async move {
h2.timeout(8.0).await;
trigger.fire();
});
}
env.spawn(async move {
all_of![h.timeout(5.0), signal].await;
assert_eq!(h.now(), 8.0);
});
env.run();
}