Skip to main content

moonpool_sim/runner/
workload.rs

1//! Workload trait for simulation testing.
2//!
3//! Workloads define the distributed system behavior to test. Each workload
4//! goes through three lifecycle phases:
5//!
6//! 1. **Setup**: Bind listeners, initialize state (sequential)
7//! 2. **Run**: Main logic, all workloads run concurrently
8//! 3. **Check**: Validate correctness after quiescence (sequential)
9//!
10//! # Usage
11//!
12//! ```ignore
13//! use moonpool_sim::{Workload, SimContext, SimulationResult};
14//!
15//! struct MyServer;
16//!
17//! #[async_trait(?Send)]
18//! impl Workload for MyServer {
19//!     fn name(&self) -> &str { "server" }
20//!     async fn run(&mut self, ctx: &SimContext) -> SimulationResult<()> {
21//!         // server logic...
22//!         Ok(())
23//!     }
24//! }
25//! ```
26
27use async_trait::async_trait;
28
29use crate::SimulationResult;
30
31use super::context::SimContext;
32
33/// A workload that participates in simulation testing.
34///
35/// Workloads are the primary unit of distributed system behavior. The simulation
36/// framework calls lifecycle methods in order: `setup` → `run` → `check`.
37#[async_trait(?Send)]
38pub trait Workload: 'static {
39    /// Name of this workload for topology and reporting.
40    fn name(&self) -> &str;
41
42    /// Setup phase: bind listeners, initialize state.
43    ///
44    /// All workloads complete setup before any `run()` starts.
45    /// Default implementation is a no-op.
46    async fn setup(&mut self, _ctx: &SimContext) -> SimulationResult<()> {
47        Ok(())
48    }
49
50    /// Run phase: main workload logic.
51    ///
52    /// All workloads run concurrently. Ends on completion or shutdown signal.
53    async fn run(&mut self, ctx: &SimContext) -> SimulationResult<()>;
54
55    /// Check phase: validate correctness after quiescence.
56    ///
57    /// Called after all runs complete and pending events drain.
58    /// Default implementation is a no-op.
59    async fn check(&mut self, _ctx: &SimContext) -> SimulationResult<()> {
60        Ok(())
61    }
62}