forked from rust-lang/rfcs
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a method to learn about cancel on Complete
This commits adds a `poll_cancel` method to learn about when the `Oneshot` half of a complete/oneshot pair has gone away. This can then be used to detect when a computation is no longer wanted. Closes rust-lang#63
- Loading branch information
1 parent
df0e550
commit f30ba82
Showing
2 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
extern crate futures; | ||
|
||
use std::sync::mpsc::{channel, Sender}; | ||
use std::thread; | ||
|
||
use futures::{oneshot, Complete, Future, Poll}; | ||
use futures::task::Task; | ||
|
||
#[test] | ||
fn smoke_poll() { | ||
let (mut tx, rx) = oneshot::<u32>(); | ||
Task::new().enter(|| { | ||
assert!(tx.poll_cancel().is_not_ready()); | ||
assert!(tx.poll_cancel().is_not_ready()); | ||
drop(rx); | ||
assert!(tx.poll_cancel().is_ready()); | ||
assert!(tx.poll_cancel().is_ready()); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn cancel_notifies() { | ||
let (tx, rx) = oneshot::<u32>(); | ||
let (tx2, rx2) = channel(); | ||
|
||
WaitForCancel { tx: tx }.then(move |v| tx2.send(v)).forget(); | ||
drop(rx); | ||
rx2.recv().unwrap().unwrap(); | ||
} | ||
|
||
struct WaitForCancel { | ||
tx: Complete<u32>, | ||
} | ||
|
||
impl Future for WaitForCancel { | ||
type Item = (); | ||
type Error = (); | ||
|
||
fn poll(&mut self) -> Poll<(), ()> { | ||
self.tx.poll_cancel() | ||
} | ||
} | ||
|
||
#[test] | ||
fn cancel_lots() { | ||
let (tx, rx) = channel::<(Complete<_>, Sender<_>)>(); | ||
let t = thread::spawn(move || { | ||
for (tx, tx2) in rx { | ||
WaitForCancel { tx: tx }.then(move |v| tx2.send(v)).forget(); | ||
} | ||
|
||
}); | ||
|
||
for _ in 0..20000 { | ||
let (otx, orx) = oneshot::<u32>(); | ||
let (tx2, rx2) = channel(); | ||
tx.send((otx, tx2)).unwrap(); | ||
drop(orx); | ||
rx2.recv().unwrap().unwrap(); | ||
} | ||
drop(tx); | ||
|
||
t.join().unwrap(); | ||
} |