diff --git a/crates/wasm-actor/tests/wasm_actor.rs b/crates/wasm-actor/tests/wasm_actor.rs index e5c0436..33112ee 100644 --- a/crates/wasm-actor/tests/wasm_actor.rs +++ b/crates/wasm-actor/tests/wasm_actor.rs @@ -12821,4 +12821,149 @@ fn echo_preserves_fifo_with_incrementing_bytes() { for (i, &b) in msgs.iter().enumerate() { assert_eq!(b, i as u8, "FIFO order preserved"); } +} + +// ── Cycle 89 ───────────────────────────────────────────────────────────────── + +// Guest computes running average (integer) over received bytes +#[test] +fn guest_running_sum_across_messages() { + let wat = r#"(module + (import "swactor" "send" (func $send (param i32 i32 i32))) + (memory (export "memory") 1) + (global $sum (mut i32) (i32.const 0)) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + (local $i i32) + ;; Add all bytes to running sum + (local.set $i (i32.const 0)) + (block $exit + (loop $loop + (br_if $exit (i32.ge_u (local.get $i) (local.get $len))) + (global.set $sum (i32.add (global.get $sum) + (i32.load8_u (i32.add (local.get $ptr) (local.get $i))))) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $loop) + ) + ) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let addr = rt.spawn(actor).unwrap(); + + // Send 3 messages with known byte sums + rt.send_to(addr, ByteMessage(vec![1, 2, 3])).unwrap(); // sum += 6 + rt.send_to(addr, ByteMessage(vec![10, 20])).unwrap(); // sum += 30 + rt.send_to(addr, ByteMessage(vec![100])).unwrap(); // sum += 100 + rt.tick(); // total sum = 136 +} + +// Build same actor type 10 times in parallel threads +#[test] +fn parallel_build_ten_actors() { + let engine = SharedEngine::new().unwrap(); + let actors: Vec = std::thread::scope(|s| { + let handles: Vec<_> = (0..10) + .map(|_| { + let e = engine.clone(); + s.spawn(move || { + WasmActorBuilder::new(e, guest_wasm("echo")).build().unwrap() + }) + }) + .collect(); + handles.into_iter().map(|h| h.join().unwrap()).collect() + }); + + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + for actor in actors { + let addr = rt.spawn(actor).unwrap(); + rt.send_to(addr, framed_msg(inbox.addr(), b"parallel")).unwrap(); + } + rt.tick(); + + let count = std::iter::from_fn(|| inbox.try_recv()).count(); + assert_eq!(count, 10); +} + +// Guest with multiple memory.fill operations at different offsets +#[test] +fn guest_multiple_memory_fills() { + let wat = r#"(module + (memory (export "memory") 1) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + (memory.fill (i32.const 500) (i32.const 0xAA) (i32.const 100)) + (memory.fill (i32.const 700) (i32.const 0xBB) (i32.const 100)) + (memory.fill (i32.const 900) (i32.const 0xCC) (i32.const 100)) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let addr = rt.spawn(actor).unwrap(); + rt.send_to(addr, ByteMessage(vec![0])).unwrap(); + rt.tick(); +} + +// Send 10000 bytes payload to echo — large transfer +#[test] +fn echo_ten_kb_payload() { + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + let payload: Vec = (0..10000).map(|i| ((i * 7 + 13) % 256) as u8).collect(); + rt.send_to(addr, framed_msg(inbox.addr(), &payload)).unwrap(); + rt.tick(); + + let resp = inbox.try_recv().expect("10KB echo"); + assert_eq!(resp.0, payload); +} + +// Guest that returns negative alloc on second call — first message works, second dropped +#[test] +fn alloc_works_then_returns_negative() { + let wat = r#"(module + (import "swactor" "send" (func $send (param i32 i32 i32))) + (memory (export "memory") 1) + (global $call (mut i32) (i32.const 0)) + (func (export "alloc") (param $len i32) (result i32) + (if (result i32) (i32.eqz (global.get $call)) + (then + (global.set $call (i32.const 1)) + (i32.const 1024) + ) + (else (i32.const -1)) + ) + ) + (func (export "handle") (param $ptr i32) (param $len i32) + (if (i32.ge_u (local.get $len) (i32.const 33)) + (then (call $send (local.get $ptr) + (i32.add (local.get $ptr) (i32.const 32)) + (i32.sub (local.get $len) (i32.const 32))))) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + // First: works + rt.send_to(addr, framed_msg(inbox.addr(), b"ok")).unwrap(); + rt.tick(); + assert_eq!(inbox.try_recv().unwrap().0, b"ok"); + + // Second: alloc returns -1, dropped + rt.send_to(addr, framed_msg(inbox.addr(), b"drop")).unwrap(); + rt.tick(); + assert!(inbox.try_recv().is_none()); } \ No newline at end of file