This documentation is part of the "Projects with Books" initiative at zenOSmosis.
The source code for this project is available on GitHub.
Core Library Tests
Loading…
Core Library Tests
Relevant source files
- core/Cargo.toml
- core/src/rpc/rpc_dispatcher.rs
- core/src/rpc/rpc_internals/rpc_respondable_session.rs
- core/src/rpc/rpc_internals/rpc_trait.rs
- extensions/muxio-ext-test/src/endpoint_helpers.rs
- tests/rpc_dispatcher_tests.rs
- tests/rpc_respondable_session_tests.rs
- tests/rpc_stream_tests.rs
- tests/stream_termination_tests.rs
The tests/ directory contains the primary validation suite for the muxio core library. These tests cover the entire stack from low-level binary framing to high-level RPC dispatching and stream lifecycle management.
1. Frame Layer Tests
The frame layer tests validate the binary protocol’s ability to handle chunking, reassembly, and multiplexing across independent streams.
frame_stream_tests
This suite validates the FrameStreamEncoder and FrameMuxStreamDecoder implementations.
- Chunking & Reassembly: Ensures that a single
write_bytescall exceedingmax_chunk_sizeis split into multiple frames and correctly reassembled by the decoder tests/frame_stream_tests.rs:7-44 - Multiplexing : Simulates two independent streams (
stream_id100 and 200) with interleaved bytes. It validates that theFrameMuxStreamDecodercorrectly routes payloads to their respective streams tests/frame_stream_tests.rs:75-146 - Out-of-Order Handling : Shuffles emitted frames to simulate network jitter. It validates that the decoder uses
seq_idto reorder frames before yielding them tests/frame_stream_tests.rs:149-186 - Incomplete Input : Validates that the decoder can handle partial frames (split across multiple
read_bytescalls) without losing data tests/frame_stream_tests.rs:47-72 - Marker Frames : Verifies that
end_streamemits aFrameKind::Endframe with an empty payload tests/frame_stream_tests.rs:189-220
stream_termination_tests
Focuses on the lifecycle of a stream, specifically how it handles closure and cancellation.
- Cancellation : Validates that calling
cancel_stream()on aFrameStreamEncodertests/stream_termination_tests.rs:23-27 prevents further writes and emits aFrameDecodeError::ReadAfterCancelon the decoder side tests/stream_termination_tests.rs:46-56 - Post-Termination Safety : Ensures that attempts to write or cancel after a stream has already ended or been canceled result in a
WriteAfterCancelorWriteAfterEnderror tests/stream_termination_tests.rs:29-41 - Auto-Flush : Confirms that
end_stream()automatically flushes any buffered data before sending theFrameKind::Endmarker tests/stream_termination_tests.rs:158-200 - RPC Abort : Validates that
RpcSessioncorrectly propagates termination errors when an underlying stream is canceled or ended tests/stream_termination_tests.rs:60-155
Sources: tests/frame_stream_tests.rs:1-223 tests/stream_termination_tests.rs:1-201
2. RPC Layer Tests
These tests validate the RpcSession, RpcDispatcher, and RpcRespondableSession components which manage request/response correlation and streaming.
rpc_stream_tests
Validates the low-level RpcSession and RpcStreamEncoder.
- Parallel Streams : Simulates two concurrent RPC calls. It validates that headers (containing
rpc_method_idandrpc_metadata_bytes) are correctly associated with their subsequent payload chunks even when interleaved tests/rpc_stream_tests.rs:9-165 - Metadata Integrity : Specifically tests large or complex metadata structures (serialized via
bitcode) to ensure they are correctly transmitted in theRpcHeadertests/rpc_stream_tests.rs:168-220
rpc_dispatcher_tests
Validates the RpcDispatcher core/src/rpc/rpc_dispatcher.rs:36-51 which provides a higher-level API for calling and responding.
- Call/Respond Loop : Demonstrates a full loop where a client calls a method tests/rpc_dispatcher_tests.rs:74-123 a server reads the bytes, executes logic (like an
ADDorMULTfunction), and responds. It validates the use ofRpcStreamEventfor handling incoming data tests/rpc_dispatcher_tests.rs:85-119 - Request Deletion : Confirms that
delete_rpc_requestcore/src/rpc/rpc_dispatcher.rs:144-146 correctly cleans up internal state once a request is processed.
rpc_respondable_session_tests
Tests the RpcRespondableSession, which adds support for “catch-all” handlers and pre-buffering.
- Catch-All Handlers : Validates
set_catch_all_response_handlercore/src/rpc/rpc_internals/rpc_respondable_session.rs:106-111 which allows a session to process incoming requests that don’t have a specific pre-registered handler tests/rpc_respondable_session_tests.rs:32-63 - Bidirectional Flow : Simulates a client initiating a
Calland the server responding with aResponseusingstart_reply_streamtests/rpc_respondable_session_tests.rs:121-136 - Pre-buffering Toggle : Validates behavior when
is_prebuffering_responseis toggled, ensuring bytes are accumulated into a singlePayloadChunkwhen enabled tests/rpc_respondable_session_tests.rs:7-162
rpc_dispatcher_prebuffered_tests
A specialized suite for the common “Request-Response” pattern where the entire payload is known upfront.
- Synchronous-style Simulation : Uses local dispatchers to simulate a synchronous RPC call by pre-buffering the request parameters and waiting for a finalized response.
Sources: tests/rpc_stream_tests.rs:1-220 tests/rpc_dispatcher_tests.rs:1-170 tests/rpc_respondable_session_tests.rs:1-163 core/src/rpc/rpc_dispatcher.rs:1-170 core/src/rpc/rpc_internals/rpc_respondable_session.rs:1-180
3. Implementation & Data Flow Diagrams
RPC Call to Byte Emission
The following diagram traces the path from an RpcRequest in a test to the raw bytes emitted by the FrameStreamEncoder.
| Entity | Role |
|---|---|
RpcRequest | High-level request definition containing method ID and params tests/rpc_dispatcher_tests.rs:42-49 |
RpcDispatcher | Orchestrates the call, manages request IDs, and invokes the encoder core/src/rpc/rpc_dispatcher.rs:36-51 |
RpcStreamEncoder | Serializes the RpcHeader into frames and manages chunked writes core/src/rpc/rpc_internals/rpc_respondable_session.rs:48-74 |
FrameStreamEncoder | Encapsulates binary framing, handles chunking, and invokes the on_emit callback tests/stream_termination_tests.rs:11-13 |
Data Flow: RPC Dispatching
Sources: core/src/rpc/rpc_dispatcher.rs:74-123 tests/rpc_dispatcher_tests.rs:74-123 core/src/rpc/rpc_internals/rpc_respondable_session.rs:48-74
graph LR
subgraph "Input"
BYTES["&[u8] from Transport"]
end
subgraph "Processing (Code Entities)"
DECODER["FrameMuxStreamDecoder::read_bytes()"]
SESSION["RpcRespondableSession::read_bytes()"]
ROUTER["RpcStreamMethodRouter (Optional)"]
EVENT["RpcStreamEvent::Header / PayloadChunk"]
end
subgraph "Validation"
ASSERT["assert_eq!(received_payload, expected)"]
end
BYTES --> DECODER
DECODER --> SESSION
SESSION --> ROUTER
ROUTER --> EVENT
EVENT --> ASSERT
RPC Byte Reception to Stream Events
This diagram shows how raw bytes are processed back into high-level events within the test suites, bridging the gap between binary transport and application logic.
Data Flow: RPC Byte Reception
Sources: tests/rpc_stream_tests.rs:17-60 core/src/rpc/rpc_internals/rpc_respondable_session.rs:113-143 core/src/rpc/rpc_internals/rpc_trait.rs:22-23
4. Utility Tests
The utils_tests.rs file covers foundational logic used across the library for timekeeping and identity management.
| Function | Validation |
|---|---|
now() | Ensures monotonicity and alignment with system time (within 5ms) tests/utils_tests.rs:6-25 |
increment_u32_id() | Validates uniqueness across 10,000 consecutive calls using a HashSet tests/utils_tests.rs:28-35 |
Sources: tests/utils_tests.rs:1-36