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

110 lines
2.3 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
#![deny(warnings, rust_2018_idioms)]
#![feature(async_await)]
#[path = "../src/oneshot.rs"]
#[allow(warnings)]
mod oneshot;
2019-06-24 12:34:30 -07:00
// use futures::{self, Async, Future};
2019-05-14 10:27:36 -07:00
use loom;
2019-06-24 12:34:30 -07:00
use loom::futures::{block_on, poll_future};
2019-02-21 11:56:15 -08:00
use loom::thread;
2019-06-24 12:34:30 -07:00
use std::task::Poll::{Pending, Ready};
#[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-06-24 12:34:30 -07:00
match poll_future(&mut rx) {
Ready(Ok(value)) => {
2019-02-20 12:50:29 -08:00
// ok
assert_eq!(1, value);
None
2019-02-21 11:56:15 -08:00
}
2019-06-24 12:34:30 -07:00
Ready(Err(_)) => unimplemented!(),
Pending => 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 }
}
}
impl<'a> Future for OnClose<'a> {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
self.get_mut().tx.poll_closed(cx)
2019-06-24 12:34:30 -07:00
}
}
2019-02-20 12:50:29 -08:00
#[test]
fn changing_tx_task() {
loom::fuzz(|| {
let (mut tx, rx) = oneshot::channel::<i32>();
thread::spawn(move || {
drop(rx);
});
let tx = thread::spawn(move || {
2019-06-24 12:34:30 -07:00
let t1 = poll_future(&mut OnClose::new(&mut tx));
2019-02-20 12:50:29 -08:00
match t1 {
2019-06-24 12:34:30 -07:00
Ready(()) => None,
Pending => 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
}
});
}