d_engine_core/test_utils/mock/
mod.rs

1//! Unit test module for isolating RaftContext components via mocked
2//! dependencies.
3//!
4//! This module provides mock implementations of storage, network and handler
5//! components using the [mockall] framework. Designed for granular testing of
6//! Raft consensus algorithm components with the following characteristics:
7//!
8//! - Uses **mocked storage interfaces** with in-memory state control
9//! - Simulates network transport with deterministic responses
10//! - Allows precise behavior injection for handlers
11//! - Enables isolated testing of component interactions
12//!
13//! The mock context encapsulates a controlled testing environment containing:
14//! - Mock storage implementations (raft log, state machine)
15//! - Simulated network layer
16//! - Configurable cluster membership
17//! - Instrumented handler implementations
18//!
19//! Mock initialization provides a test environment with:
20//! - Auto-generated mock objects via [mockall] attributes
21//! - Preconfigured peer responses
22//! - Deterministic transport simulation
23//! - Component interaction tracking
24//!
25//! This differs from integration tests in that:
26//! - I/O operations use mocked storage with ephemeral state
27//! - Network communication is simulated without actual ports binding
28//! - Component states reset between tests
29//! - Specific interaction patterns can be enforced
30//!
31//! Typical usage scenarios:
32//! - Unit testing of individual Raft components
33//! - Validation of state machine edge cases
34//! - Network partition simulation
35//! - Protocol violation testing
36//! - Fast feedback during development iterations
37//!
38//! [mockall]: https://docs.rs/mockall/latest/mockall/
39
40pub mod mock_raft_builder;
41mod mock_rpc;
42mod mock_rpc_service;
43mod mock_storage_engine;
44mod mock_type_config;
45
46use d_engine_proto::server::cluster::NodeMeta;
47pub use mock_raft_builder::*;
48pub use mock_rpc::*;
49pub use mock_rpc_service::*;
50pub use mock_storage_engine::*;
51pub use mock_type_config::*;
52use tokio::sync::watch;
53
54use super::node_config;
55use crate::RaftContext;
56
57pub fn mock_raft_context(
58    db_path: &str,
59    shutdown_signal: watch::Receiver<()>,
60    peers_meta_option: Option<Vec<NodeMeta>>,
61) -> RaftContext<MockTypeConfig> {
62    let mut node_config = node_config(db_path);
63    if let Some(peers_meta) = peers_meta_option {
64        node_config.cluster.initial_cluster = peers_meta;
65    }
66    node_config.raft.replication.rpc_append_entries_in_batch_threshold = 1;
67    // Reduce timeout for test
68    node_config.retry.auto_discovery.timeout_ms = 10;
69
70    MockBuilder::new(shutdown_signal).with_node_config(node_config).build_context()
71}