123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- use super::*;
- use crate::loom::{self, alloc::Track, future, thread};
- #[test]
- #[cfg_attr(ci_skip_slow_models, ignore)]
- fn mpsc_try_send_recv() {
- loom::model(|| {
- let (tx, rx) = channel(3);
- let p1 = {
- let tx = tx.clone();
- thread::spawn(move || {
- *tx.try_send_ref().unwrap() = Track::new(1);
- })
- };
- let p2 = thread::spawn(move || {
- *tx.try_send_ref().unwrap() = Track::new(2);
- *tx.try_send_ref().unwrap() = Track::new(3);
- });
- let mut vals = future::block_on(async move {
- let mut vals = Vec::new();
- while let Some(val) = rx.recv_ref().await {
- vals.push(*val.get_ref());
- }
- vals
- });
- vals.sort_unstable();
- assert_eq!(vals, vec![1, 2, 3]);
- p1.join().unwrap();
- p2.join().unwrap();
- })
- }
- #[test]
- fn rx_closes() {
- const ITERATIONS: usize = 6;
- loom::model(|| {
- let (tx, rx) = channel(ITERATIONS / 2);
- let producer = thread::spawn(move || {
- 'iters: for i in 0..=ITERATIONS {
- test_println!("sending {}...", i);
- 'send: loop {
- match tx.try_send_ref() {
- Ok(mut slot) => {
- *slot = i;
- break 'send;
- }
- Err(TrySendError::Full(_)) => thread::yield_now(),
- Err(TrySendError::Closed(_)) => break 'iters,
- }
- }
- test_println!("sent {}\n", i);
- }
- });
- future::block_on(async move {
- for i in 0..ITERATIONS - 1 {
- test_println!("receiving {}...", i);
- let n = rx.recv().await;
- test_println!("recv {:?}\n", n);
- assert_eq!(n, Some(i));
- }
- });
- producer.join().unwrap();
- })
- }
- #[test]
- fn rx_close_unconsumed_spsc() {
- // Tests that messages that have not been consumed by the receiver are
- // dropped when dropping the channel.
- const MESSAGES: usize = 4;
- loom::model(|| {
- let (tx, rx) = channel(MESSAGES);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- // recieve one message
- let msg = rx.recv().await;
- test_println!("recv {:?}", msg);
- assert!(msg.is_some());
- // drop the receiver...
- })
- });
- future::block_on(async move {
- let mut i = 1;
- while let Ok(mut slot) = tx.send_ref().await {
- test_println!("producer sending {}...", i);
- *slot = Track::new(i);
- i += 1;
- }
- });
- consumer.join().unwrap();
- })
- }
- #[test]
- #[ignore] // This is marked as `ignore` because it takes over an hour to run.
- fn rx_close_unconsumed_mpsc() {
- const MESSAGES: usize = 2;
- async fn do_producer(tx: Sender<Track<i32>>, n: usize) {
- let mut i = 1;
- while let Ok(mut slot) = tx.send_ref().await {
- test_println!("producer {} sending {}...", n, i);
- *slot = Track::new(i);
- i += 1;
- }
- }
- loom::model(|| {
- let (tx, rx) = channel(MESSAGES);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- // recieve one message
- let msg = rx.recv().await;
- test_println!("recv {:?}", msg);
- assert!(msg.is_some());
- // drop the receiver...
- })
- });
- let producer = {
- let tx = tx.clone();
- thread::spawn(move || future::block_on(do_producer(tx, 1)))
- };
- future::block_on(do_producer(tx, 2));
- producer.join().unwrap();
- consumer.join().unwrap();
- })
- }
- #[test]
- fn spsc_recv_then_send() {
- loom::model(|| {
- let (tx, rx) = channel::<i32>(4);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- assert_eq!(rx.recv().await.unwrap(), 10);
- })
- });
- tx.try_send(10).unwrap();
- consumer.join().unwrap();
- })
- }
- #[test]
- fn spsc_recv_then_close() {
- loom::model(|| {
- let (tx, rx) = channel::<i32>(4);
- let producer = thread::spawn(move || {
- drop(tx);
- });
- future::block_on(async move {
- let recv = rx.recv().await;
- assert_eq!(recv, None);
- });
- producer.join().unwrap();
- });
- }
- #[test]
- fn spsc_recv_then_send_then_close() {
- loom::model(|| {
- let (tx, rx) = channel::<i32>(2);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- assert_eq!(rx.recv().await.unwrap(), 10);
- assert_eq!(rx.recv().await.unwrap(), 20);
- assert_eq!(rx.recv().await, None);
- })
- });
- tx.try_send(10).unwrap();
- tx.try_send(20).unwrap();
- drop(tx);
- consumer.join().unwrap();
- })
- }
- #[test]
- fn spsc_send_recv_in_order_no_wrap() {
- const N_SENDS: usize = 4;
- loom::model(|| {
- let (tx, rx) = channel::<usize>(N_SENDS);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- for i in 1..=N_SENDS {
- assert_eq!(rx.recv().await, Some(i));
- }
- assert_eq!(rx.recv().await, None);
- })
- });
- future::block_on(async move {
- for i in 1..=N_SENDS {
- tx.send(i).await.unwrap()
- }
- });
- consumer.join().unwrap();
- })
- }
- #[test]
- fn spsc_send_recv_in_order_wrap() {
- const N_SENDS: usize = 2;
- loom::model(|| {
- let (tx, rx) = channel::<usize>(N_SENDS / 2);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- for i in 1..=N_SENDS {
- assert_eq!(rx.recv().await, Some(i));
- }
- assert_eq!(rx.recv().await, None);
- })
- });
- future::block_on(async move {
- for i in 1..=N_SENDS {
- tx.send(i).await.unwrap()
- }
- });
- consumer.join().unwrap();
- })
- }
- #[test]
- #[cfg_attr(ci_skip_slow_models, ignore)]
- fn mpsc_send_recv_wrap() {
- loom::model(|| {
- let (tx, rx) = channel::<usize>(1);
- let producer1 = do_producer(tx.clone(), 10);
- let producer2 = do_producer(tx, 20);
- let results = future::block_on(async move {
- let mut results = Vec::new();
- while let Some(val) = rx.recv().await {
- test_println!("RECEIVED {:?}", val);
- results.push(val);
- }
- results
- });
- producer1.join().expect("producer 1 panicked");
- producer2.join().expect("producer 2 panicked");
- assert_eq!(results.len(), 2);
- assert!(
- results.contains(&10),
- "missing value from producer 1; results={:?}",
- results
- );
- assert!(
- results.contains(&20),
- "missing value from producer 2; results={:?}",
- results
- );
- })
- }
- #[test]
- fn mpsc_send_recv_no_wrap() {
- loom::model(|| {
- let (tx, rx) = channel::<usize>(2);
- let producer1 = do_producer(tx.clone(), 10);
- let producer2 = do_producer(tx, 20);
- let results = future::block_on(async move {
- let mut results = Vec::new();
- while let Some(val) = rx.recv().await {
- test_println!("RECEIVED {:?}", val);
- results.push(val);
- }
- results
- });
- producer1.join().expect("producer 1 panicked");
- producer2.join().expect("producer 2 panicked");
- assert_eq!(results.len(), 2);
- assert!(
- results.contains(&10),
- "missing value from producer 1; results={:?}",
- results
- );
- assert!(
- results.contains(&20),
- "missing value from producer 2; results={:?}",
- results
- );
- })
- }
- fn do_producer(tx: Sender<usize>, tag: usize) -> thread::JoinHandle<()> {
- thread::spawn(move || {
- future::block_on(async move {
- test_println!("SENDING {:?}", tag);
- tx.send(tag).await.unwrap();
- test_println!("SENT {:?}", tag);
- })
- })
- }
- #[test]
- fn tx_close_wakes() {
- loom::model(|| {
- let (tx, rx) = channel::<i32>(2);
- let consumer = thread::spawn(move || {
- future::block_on(async move {
- assert_eq!(rx.recv().await, None);
- })
- });
- drop(tx);
- consumer.join().unwrap();
- });
- }
- #[test]
- fn tx_close_drains_queue() {
- const LEN: usize = 4;
- loom::model(|| {
- let (tx, rx) = channel(LEN);
- let producer = thread::spawn(move || {
- future::block_on(async move {
- for i in 0..LEN {
- tx.send(i).await.unwrap();
- }
- })
- });
- future::block_on(async move {
- for i in 0..LEN {
- assert_eq!(rx.recv().await, Some(i))
- }
- });
- producer.join().unwrap();
- });
- }
|