Files
tokio/tokio-sync/tests/fuzz_oneshot.rs
T

91 lines
2.0 KiB
Rust
Raw Normal View History

#![deny(warnings)]
extern crate futures;
extern crate loom;
#[path = "../src/oneshot.rs"]
#[allow(warnings)]
mod oneshot;
2019-02-20 12:50:29 -08:00
use futures::{Async, Future};
use loom::futures::block_on;
2019-02-21 11:56:15 -08:00
use loom::thread;
#[test]
fn smoke() {
loom::fuzz(|| {
let (tx, rx) = oneshot::channel();
thread::spawn(move || {
tx.send(1).unwrap();
});
let value = block_on(rx).unwrap();
assert_eq!(1, value);
});
}
2019-02-20 12:50:29 -08:00
#[test]
fn changing_rx_task() {
loom::fuzz(|| {
let (tx, mut rx) = oneshot::channel();
thread::spawn(move || {
tx.send(1).unwrap();
});
let rx = thread::spawn(move || {
2019-02-21 11:56:15 -08:00
let t1 = block_on(futures::future::poll_fn(|| Ok::<_, ()>(rx.poll().into()))).unwrap();
2019-02-20 12:50:29 -08:00
match t1 {
Ok(Async::Ready(value)) => {
// ok
assert_eq!(1, value);
None
2019-02-21 11:56:15 -08:00
}
Ok(Async::NotReady) => Some(rx),
2019-02-20 12:50:29 -08:00
Err(_) => unreachable!(),
}
2019-02-21 11:56:15 -08:00
})
.join()
.unwrap();
2019-02-20 12:50:29 -08:00
if let Some(rx) = rx {
// Previous task parked, use a new task...
let value = block_on(rx).unwrap();
assert_eq!(1, value);
}
});
}
#[test]
fn changing_tx_task() {
loom::fuzz(|| {
let (mut tx, rx) = oneshot::channel::<i32>();
thread::spawn(move || {
drop(rx);
});
let tx = thread::spawn(move || {
let t1 = block_on(futures::future::poll_fn(|| {
Ok::<_, ()>(tx.poll_close().into())
2019-02-21 11:56:15 -08:00
}))
.unwrap();
2019-02-20 12:50:29 -08:00
match t1 {
2019-02-21 11:56:15 -08:00
Ok(Async::Ready(())) => None,
Ok(Async::NotReady) => Some(tx),
2019-02-20 12:50:29 -08:00
Err(_) => unreachable!(),
}
2019-02-21 11:56:15 -08:00
})
.join()
.unwrap();
2019-02-20 12:50:29 -08:00
if let Some(mut tx) = tx {
// Previous task parked, use a new task...
2019-02-21 11:56:15 -08:00
block_on(futures::future::poll_fn(move || tx.poll_close())).unwrap();
2019-02-20 12:50:29 -08:00
}
});
}