use std::{sync::atomic::{AtomicBool, Ordering}, time::Duration};
use futures::FutureExt;
use smol::{Timer, block_on, future::{race, yield_now}};
use floop::floop;
#[test]
fn counter() {
let mut counter = 0;
let run = AtomicBool::new(true);
let future = race(
floop! {
biased
_ in Timer::after(Duration::from_millis(20)) => {
if counter % 2 == 1 {
counter += 1;
}
}
_ in Timer::after(Duration::from_millis(14)) => {
if counter % 2 == 0 {
counter += 1;
}
if counter >= 21 {
run.store(false, Ordering::Relaxed);
}
}
}.map(drop),
async {
while run.load(Ordering::Relaxed) {
yield_now().await;
}
}
);
block_on(future);
assert_eq!(counter, 21);
}