pub struct MockWasmRuntime { /* private fields */ }Expand description
Mock runtime that simulates async message passing
This replaces browser APIs like Worker.postMessage() and Worker.onmessage
with a deterministic, testable interface.
Implementations§
Source§impl MockWasmRuntime
impl MockWasmRuntime
Sourcepub fn on_message<F>(&mut self, handler: F)where
F: Fn(&MockMessage) + 'static,
pub fn on_message<F>(&mut self, handler: F)where
F: Fn(&MockMessage) + 'static,
Register a message handler (like worker.onmessage)
Sourcepub fn post_message(&self, msg: MockMessage)
pub fn post_message(&self, msg: MockMessage)
Send message (like worker.postMessage)
This puts a message in the outgoing queue for the component to “send”.
§Browser Fidelity (PROBAR-WASM-003)
Like receive_message, this performs a round-trip serialization to
simulate structuredClone semantics.
§Panics
Panics if the message cannot be serialized. This intentionally mirrors
browser postMessage semantics where non-cloneable objects throw.
Sourcepub fn receive_message(&self, msg: MockMessage)
pub fn receive_message(&self, msg: MockMessage)
Receive a message (simulates worker sending to main thread)
This puts a message in the incoming queue to be processed by handlers.
§Browser Fidelity (PROBAR-WASM-003)
To simulate real browser structuredClone semantics, the message is
serialized and deserialized before being queued. This ensures that:
- Non-serializable types will panic (like they would in a browser)
- Message data is deep-copied (no shared references)
§Panics
Panics if the message cannot be serialized or deserialized. This
simulates browser postMessage behavior where non-cloneable objects
cause errors.
Sourcepub fn tick(&mut self) -> bool
pub fn tick(&mut self) -> bool
Process one message from the incoming queue
Returns true if a message was processed, false if queue was empty.
§Re-entrancy Safety
Handlers may call receive_message() to queue additional messages,
or even register new handlers via on_message(). This is achieved
by temporarily swapping out the handlers vector during processing.
Sourcepub fn drain(&mut self)
pub fn drain(&mut self)
Process all pending messages
§Safety Limit
To prevent infinite loops from recursive message patterns,
this method processes at most 10,000 messages. Use drain_bounded
for explicit control over the limit.
Sourcepub fn drain_bounded(&mut self, max_messages: usize) -> usize
pub fn drain_bounded(&mut self, max_messages: usize) -> usize
Process pending messages with explicit bound
Returns the number of messages processed.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Get pending incoming message count
Sourcepub fn take_outgoing(&self) -> Vec<MockMessage>
pub fn take_outgoing(&self) -> Vec<MockMessage>
Get outgoing messages (for assertions)
Sourcepub fn peek_outgoing(&self) -> Vec<MockMessage>
pub fn peek_outgoing(&self) -> Vec<MockMessage>
Peek at outgoing messages without consuming
Sourcepub fn has_outgoing(&self) -> bool
pub fn has_outgoing(&self) -> bool
Check if there are any outgoing messages
Sourcepub fn total_processed(&self) -> usize
pub fn total_processed(&self) -> usize
Get total messages processed
Sourcepub fn is_started(&self) -> bool
pub fn is_started(&self) -> bool
Check if runtime is started
Trait Implementations§
Source§impl Clone for MockWasmRuntime
impl Clone for MockWasmRuntime
Source§impl Debug for MockWasmRuntime
impl Debug for MockWasmRuntime
Auto Trait Implementations§
impl !RefUnwindSafe for MockWasmRuntime
impl !Send for MockWasmRuntime
impl !Sync for MockWasmRuntime
impl !UnwindSafe for MockWasmRuntime
impl Freeze for MockWasmRuntime
impl Unpin for MockWasmRuntime
impl UnsafeUnpin for MockWasmRuntime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().