Skip to content

Commit e54488f

Browse files
pchickeySilverMira
andcommitted
Add a new test that uses futures that do not pend on wasi pollables
This test demonstrates behavior for a future which does not pend on a pollable, but does wake the root task when pending. As long as the root task has had wake() called, the runtime should keep polling it. This bug was reported in #69 and this test was derived from one written by @SilverMira in https://github.com/SilverMira/wstd/tree/allow_independent_futures Co-Authored-By: SilverMira <[email protected]>
1 parent 2218692 commit e54488f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/runtime/reactor.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,52 @@ mod test {
281281
.await;
282282
})
283283
}
284+
285+
#[test]
286+
fn progresses_wasi_independent_futures() {
287+
crate::runtime::block_on(async {
288+
let start = wasi::clocks::monotonic_clock::now();
289+
290+
let reactor = Reactor::current();
291+
const LONG_DURATION: u64 = 1_000_000_000;
292+
let later = wasi::clocks::monotonic_clock::subscribe_duration(LONG_DURATION);
293+
let later = reactor.schedule(later);
294+
let mut polled_before = false;
295+
let wasi_independent_future = futures_lite::future::poll_fn(|cx| {
296+
if polled_before {
297+
std::task::Poll::Ready(true)
298+
} else {
299+
polled_before = true;
300+
cx.waker().wake_by_ref();
301+
std::task::Poll::Pending
302+
}
303+
});
304+
let later = async {
305+
later.wait_for().await;
306+
false
307+
};
308+
let wasi_independent_future_won =
309+
futures_lite::future::race(wasi_independent_future, later).await;
310+
assert!(
311+
wasi_independent_future_won,
312+
"wasi_independent_future should win the race"
313+
);
314+
const SHORT_DURATION: u64 = LONG_DURATION / 100;
315+
let soon = wasi::clocks::monotonic_clock::subscribe_duration(SHORT_DURATION);
316+
let soon = reactor.schedule(soon);
317+
soon.wait_for().await;
318+
319+
let end = wasi::clocks::monotonic_clock::now();
320+
321+
let duration = end - start;
322+
assert!(
323+
duration > SHORT_DURATION,
324+
"{duration} greater than short duration shows awaited for `soon` properly"
325+
);
326+
assert!(
327+
duration < (2 * SHORT_DURATION),
328+
"{duration} less than double short duration {SHORT_DURATION} shows did not await for `later`"
329+
);
330+
})
331+
}
284332
}

0 commit comments

Comments
 (0)