Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

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

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_bytes call exceeding max_chunk_size is split into multiple frames and correctly reassembled by the decoder tests/frame_stream_tests.rs:7-44
  • Multiplexing : Simulates two independent streams (stream_id 100 and 200) with interleaved bytes. It validates that the FrameMuxStreamDecoder correctly 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_id to 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_bytes calls) without losing data tests/frame_stream_tests.rs:47-72
  • Marker Frames : Verifies that end_stream emits a FrameKind::End frame 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.

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_id and rpc_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 the RpcHeader tests/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.

rpc_respondable_session_tests

Tests the RpcRespondableSession, which adds support for “catch-all” handlers and pre-buffering.

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.

EntityRole
RpcRequestHigh-level request definition containing method ID and params tests/rpc_dispatcher_tests.rs:42-49
RpcDispatcherOrchestrates the call, manages request IDs, and invokes the encoder core/src/rpc/rpc_dispatcher.rs:36-51
RpcStreamEncoderSerializes the RpcHeader into frames and manages chunked writes core/src/rpc/rpc_internals/rpc_respondable_session.rs:48-74
FrameStreamEncoderEncapsulates 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.

FunctionValidation
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