Lines
94.37 %
Functions
100 %
Branches
//! Interrupt and close while an eval is running: only the epoch trap can stop it.
use super::*;
use crate::ctx::{EpochBumper, ScriptLimits};
// Nested, so it outlasts any test timeout: only the epoch trap ends it.
const ENDLESS: &str = "(do ((j 0 (+ j 1))) ((>= j 1073741823) j) \
(do ((i 0 (+ i 1))) ((>= i 1073741823) i)))";
fn unbounded_fuel() -> ScriptCtx {
ScriptCtx::new(uuid::Uuid::nil()).with_limits(ScriptLimits {
fuel: u64::MAX,
..ScriptLimits::default()
})
}
/// Stream 1 runs a loop that neither finishes nor runs out of fuel; stream 2 waits behind it.
async fn running_rig() -> (Rig, Vec<EpochBumper>) {
let mut rig = rig_with(unbounded_fuel());
rig.frame(&format!("(:id 1 :stream 1 :form {ENDLESS})"));
rig.frame("(:id 2 :stream 2 :form (+ 1 2))");
while rig.mux.permits.available_permits() != 0 {
tokio::time::sleep(Duration::from_millis(5)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
let bumpers = rig
.mux
.streams
.iter()
.map(|(_, s)| s.bumper.clone())
.collect();
(rig, bumpers)
/// On timeout the loop is trapped by hand first, so a regression fails instead of hanging the runtime.
async fn next_or_trap(rig: &mut Rig, bumpers: &[EpochBumper]) -> String {
let reply = tokio::time::timeout(Duration::from_secs(30), rig.replies.recv()).await;
if let Ok(Some(reply)) = reply {
return reply;
for bumper in bumpers {
bumper.bump();
panic!("no reply: the interrupt never reached the running eval");
async fn assert_trapped_then_neighbour_answers(rig: &mut Rig, bumpers: &[EpochBumper]) {
let trapped = next_or_trap(rig, bumpers).await;
assert!(
trapped.starts_with("(:id 1 :stream 1 :error (:code interrupted"),
"{trapped}"
);
trapped.contains(":detail"),
"not the pre-start abort: {trapped}"
assert_eq!(
next_or_trap(rig, bumpers).await,
"(:id 2 :stream 2 :value 3)"
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn interrupt_traps_the_running_eval_only_on_its_stream() {
let (mut rig, bumpers) = running_rig().await;
rig.frame("(:interrupt 1)");
assert_trapped_then_neighbour_answers(&mut rig, &bumpers).await;
async fn close_traps_the_running_eval_only_on_its_stream() {
rig.frame("(:close 1)");
async fn abort_drops_the_running_eval_and_answers_the_queue_interrupted() {
rig.mux.abort_handle().abort();
let mut replies = [
next_or_trap(&mut rig, &bumpers).await,
];
replies.sort();
for (reply, stream) in replies.iter().zip([1, 2]) {
assert!(reply.contains(":code interrupted"), "{reply}");
assert!(reply.contains(&format!(":stream {stream}")), "{reply}");
// One thread: only an eval that yields lets the sleep, or a reader, run while it works.
#[tokio::test]
async fn a_running_eval_yields_to_the_other_tasks() {
rig.frame("(:id 1 :form (do ((i 0 (+ i 1))) ((>= i 1073741823) i)))");
tokio::time::sleep(Duration::from_millis(100)).await;
rig.mux.permits.available_permits(),
0,
"the eval is running"
rig.frame("(:interrupt 0)");
let reply = rig.next().await;