mirror of
https://github.com/denoland/deno.git
synced 2025-02-02 04:38:21 -05:00
core: run isolate tests within a task
This change is made in preparation for using FuturesUnordered to track futures that are spawned by the isolate. FuturesUnordered sets up notififications for every future that it finds to be not ready when polled, which causes a crash if attempted outside of a task context.
This commit is contained in:
parent
2719631038
commit
dd595220ab
1 changed files with 114 additions and 69 deletions
|
@ -536,8 +536,40 @@ pub fn js_check(r: Result<(), JSError>) {
|
|||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::*;
|
||||
use futures::executor::spawn;
|
||||
use futures::future::lazy;
|
||||
use futures::future::ok;
|
||||
use futures::Async;
|
||||
use std::ops::FnOnce;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
fn run_in_task<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce() -> R,
|
||||
{
|
||||
spawn(lazy(move || ok::<R, ()>(f()))).wait_future().unwrap()
|
||||
}
|
||||
|
||||
fn poll_until_ready<F>(
|
||||
future: &mut F,
|
||||
max_poll_count: usize,
|
||||
) -> Result<F::Item, F::Error>
|
||||
where
|
||||
F: Future,
|
||||
{
|
||||
for _ in 0..max_poll_count {
|
||||
match future.poll() {
|
||||
Ok(NotReady) => continue,
|
||||
Ok(Ready(val)) => return Ok(val),
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"Isolate still not ready after polling {} times.",
|
||||
max_poll_count
|
||||
)
|
||||
}
|
||||
|
||||
pub enum TestDispatchMode {
|
||||
AsyncImmediate,
|
||||
OverflowReqSync,
|
||||
|
@ -687,6 +719,7 @@ pub mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_poll_async_immediate_ops() {
|
||||
run_in_task(|| {
|
||||
let mut isolate = TestDispatch::setup(TestDispatchMode::AsyncImmediate);
|
||||
|
||||
js_check(isolate.execute(
|
||||
|
@ -725,10 +758,12 @@ pub mod tests {
|
|||
assert_eq!(isolate.dispatcher.dispatch_count, 2);
|
||||
// We are idle, so the next poll should be the last.
|
||||
assert_eq!(Ok(Async::Ready(())), isolate.poll());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shared() {
|
||||
run_in_task(|| {
|
||||
let mut isolate = TestDispatch::setup(TestDispatchMode::AsyncImmediate);
|
||||
|
||||
js_check(isolate.execute(
|
||||
|
@ -759,8 +794,8 @@ pub mod tests {
|
|||
));
|
||||
assert_eq!(isolate.dispatcher.dispatch_count, 2);
|
||||
assert_eq!(Ok(Async::Ready(())), isolate.poll());
|
||||
|
||||
js_check(isolate.execute("send1.js", "assert(nrecv === 2);"));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -877,6 +912,7 @@ pub mod tests {
|
|||
|
||||
#[test]
|
||||
fn overflow_req_async() {
|
||||
run_in_task(|| {
|
||||
let mut isolate = TestDispatch::setup(TestDispatchMode::OverflowReqAsync);
|
||||
js_check(isolate.execute(
|
||||
"overflow_req_async.js",
|
||||
|
@ -898,10 +934,12 @@ pub mod tests {
|
|||
assert_eq!(isolate.dispatcher.dispatch_count, 1);
|
||||
assert_eq!(Ok(Async::Ready(())), isolate.poll());
|
||||
js_check(isolate.execute("check.js", "assert(asyncRecv == 1);"));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_res_async() {
|
||||
run_in_task(|| {
|
||||
// TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We
|
||||
// should optimize this.
|
||||
let mut isolate = TestDispatch::setup(TestDispatchMode::OverflowResAsync);
|
||||
|
@ -922,14 +960,16 @@ pub mod tests {
|
|||
"#,
|
||||
));
|
||||
assert_eq!(isolate.dispatcher.dispatch_count, 1);
|
||||
assert_eq!(Ok(Async::Ready(())), isolate.poll());
|
||||
assert_eq!(Ok(()), poll_until_ready(&mut isolate, 3));
|
||||
js_check(isolate.execute("check.js", "assert(asyncRecv == 1);"));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_res_multiple_dispatch_async() {
|
||||
// TODO(ry) This test is quite slow due to memcpy-ing 100MB into JS. We
|
||||
// should optimize this.
|
||||
run_in_task(|| {
|
||||
let mut isolate = TestDispatch::setup(TestDispatchMode::OverflowResAsync);
|
||||
js_check(isolate.execute(
|
||||
"overflow_res_multiple_dispatch_async.js",
|
||||
|
@ -951,17 +991,22 @@ pub mod tests {
|
|||
"#,
|
||||
));
|
||||
assert_eq!(isolate.dispatcher.dispatch_count, 2);
|
||||
assert_eq!(Ok(Async::Ready(())), isolate.poll());
|
||||
assert_eq!(Ok(()), poll_until_ready(&mut isolate, 3));
|
||||
js_check(isolate.execute("check.js", "assert(asyncRecv == 2);"));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_js() {
|
||||
run_in_task(|| {
|
||||
let mut isolate = TestDispatch::setup(TestDispatchMode::AsyncImmediate);
|
||||
js_check(
|
||||
isolate
|
||||
.execute("shared_queue_test.js", include_str!("shared_queue_test.js")),
|
||||
isolate.execute(
|
||||
"shared_queue_test.js",
|
||||
include_str!("shared_queue_test.js"),
|
||||
),
|
||||
);
|
||||
assert_eq!(Ok(Async::Ready(())), isolate.poll());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue