#![cfg(feature = "v8")]
use std::time::{Duration, Instant};
use fetchlet::{App, Backend, Error, Limits, Module, Requirements, V8Options};
const SOURCE: &str = r#"
globalThis.fetchletExports = {
fill() {
for (;;) {
new Array(1000).fill(0);
}
},
passed() {
const fill = (array) => array.fill(0);
for (;;) {
fill(new Array(1000));
}
},
};
"#;
const BUSY: Duration = Duration::from_millis(200);
const DEADLINE: Duration = Duration::from_secs(5);
#[tokio::test]
async fn the_busy_timeout_stops_allocating_loops() -> anyhow::Result<()> {
for export in ["fill", "passed"] {
let mut app = App::builder(Backend::V8(V8Options {
reliable_termination: true,
..V8Options::default()
}))
.module(Module::new(SOURCE))
.limits(Limits {
busy_timeout: Some(BUSY),
timeout: Some(DEADLINE),
..Limits::default()
})
.build()?;
let started = Instant::now();
let error = app.call_json(export, &[]).await.unwrap_err();
if !matches!(error, Error::LimitExceeded("engine busy time")) {
std::mem::forget(app);
panic!("{export}: {error:?} after {:?}", started.elapsed());
}
assert!(!app.is_reusable(), "{export}");
}
Ok(())
}
#[test]
fn reliable_termination_meets_the_preemption_requirement() -> anyhow::Result<()> {
App::builder(Backend::V8(V8Options {
reliable_termination: true,
..V8Options::default()
}))
.module(Module::new(SOURCE).requires(Requirements::new().preemption()))
.build()?;
Ok(())
}