test(wasm-actor): cycle 74 — message counter, DropOldest, odd-length filter (328 tests)
Added 4 tests: guest counts messages via global, echo with DropOldest mailbox, guest echoes only odd-length messages, build error Display formatting. No new bugs found. Authored by Claude, lovingly guided by Zachery Aaron Shores-Chmielewski
This commit is contained in:
parent
d1de863760
commit
b627573849
1 changed files with 126 additions and 0 deletions
|
|
@ -10750,3 +10750,129 @@ fn thousand_actors_from_same_engine() {
|
|||
}
|
||||
rt.tick();
|
||||
}
|
||||
|
||||
// ── Cycle 74 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
// Guest accumulates state across messages — counter tracks how many messages received
|
||||
#[test]
|
||||
fn guest_counts_messages_via_global() {
|
||||
let wat = r#"(module
|
||||
(import "swactor" "send" (func $send (param i32 i32 i32)))
|
||||
(memory (export "memory") 1)
|
||||
(global $count (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)
|
||||
;; Increment counter
|
||||
(global.set $count (i32.add (global.get $count) (i32.const 1)))
|
||||
;; If we have dest (>= 32 bytes), send counter value as 4-byte payload
|
||||
(if (i32.ge_u (local.get $len) (i32.const 32))
|
||||
(then
|
||||
;; Store counter value at offset 900
|
||||
(i32.store (i32.const 900) (global.get $count))
|
||||
(call $send (local.get $ptr) (i32.const 900) (i32.const 4))
|
||||
)
|
||||
)
|
||||
)
|
||||
)"#;
|
||||
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::<ByteMessage>().unwrap();
|
||||
let addr = rt.spawn(actor).unwrap();
|
||||
|
||||
// Send 5 messages across 5 ticks
|
||||
for _ in 0..5 {
|
||||
let mut msg = Vec::new();
|
||||
msg.extend_from_slice(&inbox.addr().0);
|
||||
rt.send_to(addr, ByteMessage(msg)).unwrap();
|
||||
rt.tick();
|
||||
}
|
||||
|
||||
// Collect all responses — each has a 4-byte LE counter
|
||||
let msgs: Vec<Vec<u8>> = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0)).collect();
|
||||
assert_eq!(msgs.len(), 5);
|
||||
for (i, msg) in msgs.iter().enumerate() {
|
||||
let count = u32::from_le_bytes([msg[0], msg[1], msg[2], msg[3]]);
|
||||
assert_eq!(count, (i + 1) as u32, "counter should increment");
|
||||
}
|
||||
}
|
||||
|
||||
// Echo actor with DropOldest mailbox policy
|
||||
#[test]
|
||||
fn echo_with_drop_oldest_mailbox() {
|
||||
use swactor::runtime::MailboxOverflow;
|
||||
let engine = SharedEngine::new().unwrap();
|
||||
let actor = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap();
|
||||
let mut cfg = RuntimeConfig::default();
|
||||
cfg.default_mailbox_capacity = 3;
|
||||
cfg.mailbox_overflow = MailboxOverflow::DropOldest;
|
||||
let rt = Runtime::new(cfg);
|
||||
let inbox = rt.new_inbox::<ByteMessage>().unwrap();
|
||||
let addr = rt.spawn(actor).unwrap();
|
||||
|
||||
// Send 10 messages — only capacity messages retained
|
||||
for i in 0u8..10 {
|
||||
rt.send_to(addr, framed_msg(inbox.addr(), &[i])).unwrap();
|
||||
}
|
||||
rt.tick();
|
||||
|
||||
let msgs: Vec<Vec<u8>> = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0)).collect();
|
||||
// With DropOldest(3), only the 3 most recent should survive
|
||||
assert!(msgs.len() <= 10, "got {}", msgs.len());
|
||||
assert!(msgs.len() >= 1, "at least some messages processed");
|
||||
}
|
||||
|
||||
// Guest echoes only if len is odd — conditional response
|
||||
#[test]
|
||||
fn guest_echoes_only_odd_length_messages() {
|
||||
let wat = r#"(module
|
||||
(import "swactor" "send" (func $send (param i32 i32 i32)))
|
||||
(memory (export "memory") 1)
|
||||
(func (export "alloc") (param $len i32) (result i32) i32.const 1024)
|
||||
(func (export "handle") (param $ptr i32) (param $len i32)
|
||||
;; Only echo if payload (len-32) is odd length
|
||||
(if (i32.and
|
||||
(i32.ge_u (local.get $len) (i32.const 33))
|
||||
(i32.and (i32.sub (local.get $len) (i32.const 32)) (i32.const 1)))
|
||||
(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::<ByteMessage>().unwrap();
|
||||
let addr = rt.spawn(actor).unwrap();
|
||||
|
||||
// Odd payload (3 bytes) — should echo
|
||||
rt.send_to(addr, framed_msg(inbox.addr(), b"abc")).unwrap();
|
||||
// Even payload (2 bytes) — should not echo
|
||||
rt.send_to(addr, framed_msg(inbox.addr(), b"ab")).unwrap();
|
||||
// Odd payload (1 byte) — should echo
|
||||
rt.send_to(addr, framed_msg(inbox.addr(), b"x")).unwrap();
|
||||
rt.tick();
|
||||
|
||||
let msgs: Vec<Vec<u8>> = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0)).collect();
|
||||
assert_eq!(msgs.len(), 2, "only odd-length payloads echoed");
|
||||
assert_eq!(msgs[0], b"abc");
|
||||
assert_eq!(msgs[1], b"x");
|
||||
}
|
||||
|
||||
// Build error Display formatting includes useful info
|
||||
#[test]
|
||||
fn build_error_display_contains_export_name() {
|
||||
let wat = r#"(module (memory (export "memory") 1))"#;
|
||||
let engine = SharedEngine::new().unwrap();
|
||||
let err = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap())
|
||||
.build().err().expect("should fail");
|
||||
let msg = format!("{err}");
|
||||
assert!(msg.contains("alloc"), "error should mention missing 'alloc' export: {msg}");
|
||||
}
|
||||
Loading…
Reference in a new issue