2025-12-01 12:48:39 +01:00
|
|
|
|
# Example: New Workload & Expectation (Rust)
|
|
|
|
|
|
|
|
|
|
|
|
A minimal, end-to-end illustration of adding a custom workload and matching
|
|
|
|
|
|
expectation. This shows the shape of the traits and where to plug into the
|
|
|
|
|
|
framework; expand the logic to fit your real test.
|
|
|
|
|
|
|
|
|
|
|
|
## Workload: simple reachability probe
|
|
|
|
|
|
|
|
|
|
|
|
Key ideas:
|
|
|
|
|
|
- **name**: identifies the workload in logs.
|
|
|
|
|
|
- **expectations**: workloads can bundle defaults so callers don’t forget checks.
|
|
|
|
|
|
- **init**: derive inputs from the generated topology (e.g., pick a target node).
|
|
|
|
|
|
- **start**: drive async activity using the shared `RunContext`.
|
|
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
|
use async_trait::async_trait;
|
2025-12-16 06:55:44 +01:00
|
|
|
|
use testing_framework_core::{
|
|
|
|
|
|
scenario::{DynError, Expectation, RunContext, RunMetrics, Workload},
|
|
|
|
|
|
topology::generation::GeneratedTopology,
|
2025-12-01 12:48:39 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ReachabilityWorkload {
|
|
|
|
|
|
target_idx: usize,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ReachabilityWorkload {
|
|
|
|
|
|
pub fn new(target_idx: usize) -> Self {
|
2025-12-16 06:23:49 +01:00
|
|
|
|
Self { target_idx }
|
2025-12-01 12:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
|
impl Workload for ReachabilityWorkload {
|
2025-12-16 06:23:49 +01:00
|
|
|
|
fn name(&self) -> &str {
|
2025-12-01 12:48:39 +01:00
|
|
|
|
"reachability_workload"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn expectations(&self) -> Vec<Box<dyn Expectation>> {
|
2025-12-16 06:55:44 +01:00
|
|
|
|
vec![Box::new(
|
|
|
|
|
|
crate::custom_workload_example_expectation::ReachabilityExpectation::new(
|
|
|
|
|
|
self.target_idx,
|
|
|
|
|
|
),
|
|
|
|
|
|
)]
|
2025-12-01 12:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn init(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
topology: &GeneratedTopology,
|
2025-12-16 06:55:44 +01:00
|
|
|
|
_run_metrics: &RunMetrics,
|
2025-12-01 12:48:39 +01:00
|
|
|
|
) -> Result<(), DynError> {
|
|
|
|
|
|
if topology.validators().get(self.target_idx).is_none() {
|
2025-12-16 06:55:44 +01:00
|
|
|
|
return Err(Box::new(std::io::Error::new(
|
|
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
|
|
"no validator at requested index",
|
|
|
|
|
|
)));
|
2025-12-01 12:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn start(&self, ctx: &RunContext) -> Result<(), DynError> {
|
|
|
|
|
|
let client = ctx
|
2025-12-16 06:23:49 +01:00
|
|
|
|
.node_clients()
|
|
|
|
|
|
.validator_clients()
|
2025-12-01 12:48:39 +01:00
|
|
|
|
.get(self.target_idx)
|
2025-12-16 06:55:44 +01:00
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
|
Box::new(std::io::Error::new(
|
|
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
|
|
"missing target client",
|
|
|
|
|
|
)) as DynError
|
|
|
|
|
|
})?;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
2025-12-16 06:23:49 +01:00
|
|
|
|
// Lightweight API call to prove reachability.
|
2025-12-16 06:55:44 +01:00
|
|
|
|
client
|
|
|
|
|
|
.consensus_info()
|
|
|
|
|
|
.await
|
|
|
|
|
|
.map(|_| ())
|
|
|
|
|
|
.map_err(|e| e.into())
|
2025-12-01 12:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Expectation: confirm the target stayed reachable
|
|
|
|
|
|
|
|
|
|
|
|
Key ideas:
|
|
|
|
|
|
- **start_capture**: snapshot baseline if needed (not used here).
|
|
|
|
|
|
- **evaluate**: assert the condition after workloads finish.
|
|
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
use testing_framework_core::scenario::{DynError, Expectation, RunContext};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ReachabilityExpectation {
|
|
|
|
|
|
target_idx: usize,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ReachabilityExpectation {
|
|
|
|
|
|
pub fn new(target_idx: usize) -> Self {
|
|
|
|
|
|
Self { target_idx }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
|
impl Expectation for ReachabilityExpectation {
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
|
|
"target_reachable"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async fn evaluate(&mut self, ctx: &RunContext) -> Result<(), DynError> {
|
|
|
|
|
|
let client = ctx
|
2025-12-16 06:23:49 +01:00
|
|
|
|
.node_clients()
|
|
|
|
|
|
.validator_clients()
|
2025-12-01 12:48:39 +01:00
|
|
|
|
.get(self.target_idx)
|
2025-12-16 06:55:44 +01:00
|
|
|
|
.ok_or_else(|| {
|
|
|
|
|
|
Box::new(std::io::Error::new(
|
|
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
|
|
"missing target client",
|
|
|
|
|
|
)) as DynError
|
|
|
|
|
|
})?;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
2025-12-16 06:23:49 +01:00
|
|
|
|
client
|
|
|
|
|
|
.consensus_info()
|
|
|
|
|
|
.await
|
|
|
|
|
|
.map(|_| ())
|
2025-12-16 06:55:44 +01:00
|
|
|
|
.map_err(|e| e.into())
|
2025-12-01 12:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## How to wire it
|
|
|
|
|
|
- Build your scenario as usual and call `.with_workload(ReachabilityWorkload::new(0))`.
|
|
|
|
|
|
- The bundled expectation is attached automatically; you can add more with
|
|
|
|
|
|
`.with_expectation(...)` if needed.
|
|
|
|
|
|
- Keep the logic minimal and fast for smoke tests; grow it into richer probes
|
|
|
|
|
|
for deeper scenarios.
|