1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
use async_io::block_on;
use std::{
future::Future,
pin::Pin,
task::{Context, Poll, Waker},
time::{Duration, Instant},
};
#[test]
fn doesnt_poll_after_ready() {
#[derive(Default)]
struct Bomb {
returned_ready: bool,
}
impl Future for Bomb {
type Output = ();
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
if self.returned_ready {
panic!("Future was polled again after returning Poll::Ready");
} else {
self.returned_ready = true;
Poll::Ready(())
}
}
}
block_on(Bomb::default())
}
#[test]
fn recursive_wakers_are_different() {
struct Outer;
impl Future for Outer {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let outer_waker = cx.waker();
block_on(Inner { outer_waker });
Poll::Ready(())
}
}
struct Inner<'a> {
pub outer_waker: &'a Waker,
}
impl Future for Inner<'_> {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner_waker = cx.waker();
assert!(!inner_waker.will_wake(self.outer_waker));
Poll::Ready(())
}
}
block_on(Outer);
}
#[test]
fn inner_cannot_wake_outer() {
#[derive(Default)]
struct Outer {
elapsed: Option<Instant>,
}
impl Future for Outer {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(elapsed) = self.elapsed {
assert!(elapsed.elapsed() >= Duration::from_secs(1));
Poll::Ready(())
} else {
let outer_waker = cx.waker().clone();
block_on(Inner);
std::thread::spawn(|| {
std::thread::sleep(Duration::from_secs(1));
outer_waker.wake();
});
self.elapsed = Some(Instant::now());
Poll::Pending
}
}
}
struct Inner;
impl Future for Inner {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner_waker = cx.waker();
inner_waker.wake_by_ref();
Poll::Ready(())
}
}
block_on(Outer::default());
}
#[test]
fn outer_cannot_wake_inner() {
struct Outer;
impl Future for Outer {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let outer_waker = cx.waker();
outer_waker.wake_by_ref();
block_on(Inner::default());
Poll::Ready(())
}
}
#[derive(Default)]
struct Inner {
elapsed: Option<Instant>,
}
impl Future for Inner {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(elapsed) = self.elapsed {
assert!(elapsed.elapsed() >= Duration::from_secs(1));
Poll::Ready(())
} else {
let inner_waker = cx.waker().clone();
std::thread::spawn(|| {
std::thread::sleep(Duration::from_secs(1));
inner_waker.wake();
});
self.elapsed = Some(Instant::now());
Poll::Pending
}
}
}
block_on(Outer);
}
#[test]
fn first_cannot_wake_second() {
struct First;
impl Future for First {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let first_waker = cx.waker();
first_waker.wake_by_ref();
Poll::Ready(())
}
}
#[derive(Default)]
struct Second {
elapsed: Option<Instant>,
}
impl Future for Second {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(elapsed) = self.elapsed {
assert!(elapsed.elapsed() >= Duration::from_secs(1));
Poll::Ready(())
} else {
let second_waker = cx.waker().clone();
std::thread::spawn(|| {
std::thread::sleep(Duration::from_secs(1));
second_waker.wake();
});
self.elapsed = Some(Instant::now());
Poll::Pending
}
}
}
block_on(First);
block_on(Second::default());
}
|