#[cfg(test)]
mod tests {
use commonware_macros::select;
use futures::{channel::mpsc, executor::block_on, SinkExt, StreamExt};
use futures_timer::Delay;
use std::{thread, time::Duration};
#[test]
fn test_select_macro() {
block_on(async move {
let (mut tx, mut rx) = mpsc::unbounded();
let mut tx_clone = tx.clone();
thread::spawn(move || {
block_on(async move {
Delay::new(Duration::from_millis(50)).await;
tx_clone.send(2).await.unwrap();
});
});
thread::spawn(move || {
block_on(async move {
tx.send(3).await.unwrap();
});
});
let mut completed = Vec::new();
while completed.len() < 3 {
select! {
_ = Delay::new(Duration::from_millis(45)) => {
completed.push(1);
},
result = rx.next() => {
completed.push(result.unwrap());
},
}
}
assert_eq!(completed, vec![3, 1, 2]);
});
}
}