docs: specify managed coroutine ownership and interleaving regressions

This commit is contained in:
2026-10-23 20:01:58 +08:00
parent e6b27ba7b6
commit b33d322690
5 changed files with 133 additions and 1 deletions
+119
View File
@@ -1503,3 +1503,122 @@ fn suspended_call_cancellation_and_total_budget_restore_read_only_state() {
vm.release(shared).unwrap();
assert_eq!(vm.object_count(), 0);
}
#[test]
fn managed_coroutines_interleave_without_replaying_writes() {
use endor_core::CoroutineProgress;
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let code = make_code(
&mut vm,
&[
op(Opcode::LoadConst, 0),
op(Opcode::StoreGlobal, 0),
op(Opcode::LoadConst, 1),
op(Opcode::StoreGlobal, 1),
op(Opcode::LoadConst, 2),
simple(Opcode::ReturnValue),
],
vec![text("first"), text("second"), Value::Nil],
CodeLayout {
names: vec!["x".into(), "y".into()],
..CodeLayout::default()
},
);
let function = vm
.new_function("work", code, globals, vec![], vec![])
.unwrap();
let first = vm
.new_coroutine(function, &[], ExecutionLimits::default())
.unwrap();
let second = vm
.new_coroutine(function, &[], ExecutionLimits::default())
.unwrap();
assert!(matches!(
vm.resume_coroutine(first, 2).unwrap(),
CoroutineProgress::Pending { instructions: 2 }
));
assert!(matches!(
vm.resume_coroutine(second, 2).unwrap(),
CoroutineProgress::Pending { instructions: 2 }
));
vm.dict_set(globals, text("x"), text("interleaved"))
.unwrap();
assert!(matches!(
vm.resume_coroutine(first, 16).unwrap(),
CoroutineProgress::Complete {
value: Value::Nil,
instructions: 6
}
));
assert_eq!(
vm.dict_get(globals, &text("x")).unwrap(),
Some(text("interleaved"))
);
assert_eq!(
vm.dict_get(globals, &text("y")).unwrap(),
Some(text("second"))
);
assert!(vm.cancel_coroutine(second).unwrap());
assert!(!vm.cancel_coroutine(second).unwrap());
assert!(vm.resume_coroutine(first, 1).is_err());
for object in [first, second, function, code, globals] {
vm.release(object).unwrap();
}
assert_eq!(vm.object_count(), 0);
}
#[test]
fn parked_coroutine_cycles_are_collectable_without_cancellation() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let code = return_constant(&mut vm, Value::Nil);
let function = vm
.new_function("cycle", code, globals, vec![], vec![])
.unwrap();
let coroutine = vm
.new_coroutine(function, &[], ExecutionLimits::default())
.unwrap();
vm.dict_set(globals, text("task"), Value::Object(coroutine))
.unwrap();
for object in [coroutine, function, code, globals] {
vm.release(object).unwrap();
}
assert!(vm.collect_cycles() > 0);
assert_eq!(vm.object_count(), 0);
}
#[test]
fn managed_coroutine_hard_budget_accumulates_and_error_releases_frames() {
use endor_core::CoroutineProgress;
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let code = return_constant(&mut vm, Value::Nil);
let function = vm
.new_function("limited", code, globals, vec![], vec![])
.unwrap();
let coroutine = vm
.new_coroutine(
function,
&[],
ExecutionLimits {
max_frames: 4,
max_instructions: 1,
},
)
.unwrap();
assert!(matches!(
vm.resume_coroutine(coroutine, 1).unwrap(),
CoroutineProgress::Pending { instructions: 1 }
));
assert!(matches!(
vm.resume_coroutine(coroutine, 1),
Err(VmError::ExecutionLimit("instructions"))
));
assert!(!vm.last_traceback().is_empty());
assert!(!vm.cancel_coroutine(coroutine).unwrap());
for object in [coroutine, function, code, globals] {
vm.release(object).unwrap();
}
assert_eq!(vm.object_count(), 0);
}