tests.rs

Overview

This file contains integration and asynchronous tests designed to verify the behavior of blockchain-related components, specifically focusing on monitoring and reacting to changes in blockchain peer data. It uses a local database and asynchronous concurrency primitives to simulate and observe blockchain state changes in a controlled test environment.

Detailed Explanation

Imports and Dependencies

fn node_db() -> NodeDb

Creates and returns a new instance of NodeDb initialized with a hardcoded path to a local database file.

Parameters: None
Returns: NodeDb — a database handle to the blockchain archive.

Usage:
This function encapsulates the creation of the database instance, providing a consistent database reference for tests or other functions within this file.


#[tokio::test] async fn test_proxy_list()

An asynchronous test function that verifies the ability to watch blockchain events and receive updates about peers.

Purpose:

Parameters: None
Returns: () (unit) — test functions return nothing but signal pass/fail to the test harness.

Step-by-step behavior:

  1. AccountId Creation:
    Creates a default AccountId filled with zeros ([0u8; 32]), representing self_peer_id.

  2. Channel Setup:

    • subscribe_tx, subscribe_rx: A Tokio watch channel transmitting a vector of vectors of peer identifiers (initially empty).

    • peers_tx: A watch channel for a HashMap keyed by peer identifiers, initially empty. The receiver side is unused here.

  3. Spawning Blockchain Watcher:
    A Tokio task is spawned that calls watch_blockchain with:

    • Two copies of the same NodeDb instance.

    • The self_peer_id.

    • The transmitters for subscription and peers.

  4. Sleep Delay:
    The test sleeps asynchronously for 1 second to allow the watcher task to initialize and populate subscription data.

  5. Subscription Data Processing:

    • Clones the current subscribed data from subscribe_rx.

    • Transforms each inner vector of peer identifiers into a comma-separated string using join.

    • Collects these strings into a vector.

  6. Output:
    Prints the vector of formatted peer strings.

Usage Example:

#[tokio::test]
async fn my_test() {
    test_proxy_list().await;
}

This test can be run with cargo test -- --nocapture to see the printed output.


Implementation Details and Algorithms


Interaction with Other Parts of the System

This file acts as a consumer and validator of the blockchain watching mechanism, confirming that updates propagate correctly from the database layer through the watcher into the subscribed observers.


Visual Diagram

flowchart TD
A[Start test_proxy_list] --> B[Create self_peer_id]
B --> C[Create subscribe channel]
B --> D[Create peers channel]
C --> E[Spawn watch_blockchain task]
D --> E
E --> F[watch_blockchain reads from NodeDb]
E --> G[watch_blockchain updates subscribe_tx and peers_tx]
A --> H[Sleep 1 second]
H --> I[Read data from subscribe_rx]
I --> J[Format peer lists as strings]
J --> K[Print formatted info]

This flowchart depicts the asynchronous flow and interactions between the test function, the blockchain watcher, and the asynchronous channels used for data exchange.