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

116 lines
2.4 KiB
Rust
Raw Normal View History

#![warn(rust_2018_idioms)]
#[path = "../src/oneshot.rs"]
#[allow(warnings)]
mod oneshot;
2019-05-14 10:27:36 -07:00
use loom;
2019-08-07 23:24:22 -07:00
use loom::future::block_on;
2019-02-21 11:56:15 -08:00
use loom::thread;
2019-08-07 23:24:22 -07:00
use futures_util::future::poll_fn;
2019-06-24 12:34:30 -07:00
use std::task::Poll::{Pending, Ready};
#[test]
fn smoke() {
loom::model(|| {
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::model(|| {
2019-02-20 12:50:29 -08:00
let (tx, mut rx) = oneshot::channel();
thread::spawn(move || {
tx.send(1).unwrap();
});
let rx = thread::spawn(move || {
2019-08-07 23:24:22 -07:00
let ready = block_on(poll_fn(|cx| match Pin::new(&mut rx).poll(cx) {
2019-06-24 12:34:30 -07:00
Ready(Ok(value)) => {
2019-02-20 12:50:29 -08:00
assert_eq!(1, value);
2019-08-07 23:24:22 -07:00
Ready(true)
2019-02-21 11:56:15 -08:00
}
2019-06-24 12:34:30 -07:00
Ready(Err(_)) => unimplemented!(),
2019-08-07 23:24:22 -07:00
Pending => Ready(false),
}));
if ready {
None
} else {
Some(rx)
2019-02-20 12:50:29 -08:00
}
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);
}
});
}
2019-06-24 12:34:30 -07:00
// TODO: Move this into `oneshot` proper.
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
struct OnClose<'a> {
tx: &'a mut oneshot::Sender<i32>,
}
impl<'a> OnClose<'a> {
fn new(tx: &'a mut oneshot::Sender<i32>) -> Self {
OnClose { tx }
}
}
2019-08-11 02:01:20 +09:00
impl Future for OnClose<'_> {
2019-08-07 23:24:22 -07:00
type Output = bool;
2019-06-24 12:34:30 -07:00
2019-08-07 23:24:22 -07:00
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<bool> {
let res = self.get_mut().tx.poll_closed(cx);
Ready(res.is_ready())
2019-06-24 12:34:30 -07:00
}
}
2019-02-20 12:50:29 -08:00
#[test]
fn changing_tx_task() {
loom::model(|| {
2019-02-20 12:50:29 -08:00
let (mut tx, rx) = oneshot::channel::<i32>();
thread::spawn(move || {
drop(rx);
});
let tx = thread::spawn(move || {
2019-08-07 23:24:22 -07:00
let t1 = block_on(OnClose::new(&mut tx));
2019-02-20 12:50:29 -08:00
2019-08-07 23:24:22 -07:00
if t1 {
None
} else {
Some(tx)
2019-02-20 12:50:29 -08:00
}
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-06-24 12:34:30 -07:00
block_on(OnClose::new(&mut tx));
2019-02-20 12:50:29 -08:00
}
});
}