Skip to content

Commit

Permalink
extend resumable functions test
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Jan 4, 2023
1 parent df8a182 commit 2f5f563
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions crates/wasmi/tests/e2e/v1/resumable_call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Test to assert that resumable call feature works as intended.

use wasmi::{Engine, Extern, Func, Linker, Module, ResumableCall, Store};
use wasmi_core::{Trap, Value, ValueType};
use wasmi::{Engine, Error, Extern, Func, Linker, Module, ResumableCall, Store};
use wasmi_core::{Trap, TrapCode, Value, ValueType};

fn test_setup() -> Store<()> {
let engine = Engine::default();
Expand Down Expand Up @@ -52,9 +52,18 @@ fn resumable_call() {
.and_then(Extern::into_func)
.unwrap();

run_test(wasm_fn, &mut store, false);
run_test(wasm_fn, &mut store, true);
}

fn run_test(wasm_fn: Func, mut store: &mut Store<()>, wasm_trap: bool) {
let mut results = [Value::I32(0)];
let invocation = match wasm_fn
.call_resumable(&mut store, &[Value::I32(0)], &mut results[..])
.call_resumable(
&mut store,
&[Value::I32(wasm_trap as i32)],
&mut results[..],
)
.unwrap()
{
ResumableCall::Resumable(invocation) => {
Expand All @@ -81,13 +90,28 @@ fn resumable_call() {
}
ResumableCall::Finished(_) => panic!("expected host function trap with exit code 20"),
};
match invocation
.resume(&mut store, &[Value::I32(3)], &mut results[..])
.unwrap()
{
ResumableCall::Resumable(_) => panic!("expected resumed function to finish"),
ResumableCall::Finished(()) => {
assert_eq!(results, [Value::I32(4)]);
let result = invocation.resume(&mut store, &[Value::I32(3)], &mut results[..]);
if wasm_trap {
match result {
Ok(_) => panic!("expected resumed function to trap in Wasm"),
Err(trap) => match trap {
Error::Trap(trap) => {
assert!(matches!(
trap.trap_code(),
Some(TrapCode::UnreachableCodeReached)
));
}
_ => panic!("expected Wasm trap"),
},
}
} else {
match result {
Ok(ResumableCall::Resumable(_)) | Err(_) => {
panic!("expected resumed function to finish")
}
Ok(ResumableCall::Finished(())) => {
assert_eq!(results, [Value::I32(4)]);
}
}
}
}

0 comments on commit 2f5f563

Please sign in to comment.