Skip to main content

reth_stages_api/
test_utils.rs

1#![allow(missing_docs)]
2
3use crate::{ExecInput, ExecOutput, Stage, StageError, StageId, UnwindInput, UnwindOutput};
4use std::{
5    collections::VecDeque,
6    sync::{
7        atomic::{AtomicUsize, Ordering},
8        Arc,
9    },
10};
11
12/// A test stage that can be used for testing.
13///
14/// This can be used to mock expected outputs of [`Stage::execute`] and [`Stage::unwind`]
15#[derive(Debug)]
16pub struct TestStage {
17    id: StageId,
18    exec_outputs: VecDeque<Result<ExecOutput, StageError>>,
19    unwind_outputs: VecDeque<Result<UnwindOutput, StageError>>,
20    post_execute_commit_counter: Arc<AtomicUsize>,
21    post_unwind_commit_counter: Arc<AtomicUsize>,
22    validate_unwind_outputs: VecDeque<Result<(), StageError>>,
23}
24
25impl TestStage {
26    pub fn new(id: StageId) -> Self {
27        Self {
28            id,
29            exec_outputs: VecDeque::new(),
30            unwind_outputs: VecDeque::new(),
31            post_execute_commit_counter: Arc::new(AtomicUsize::new(0)),
32            post_unwind_commit_counter: Arc::new(AtomicUsize::new(0)),
33            validate_unwind_outputs: VecDeque::new(),
34        }
35    }
36
37    pub fn with_exec(mut self, exec_outputs: VecDeque<Result<ExecOutput, StageError>>) -> Self {
38        self.exec_outputs = exec_outputs;
39        self
40    }
41
42    pub fn with_unwind(
43        mut self,
44        unwind_outputs: VecDeque<Result<UnwindOutput, StageError>>,
45    ) -> Self {
46        self.unwind_outputs = unwind_outputs;
47        self
48    }
49
50    pub fn add_exec(mut self, output: Result<ExecOutput, StageError>) -> Self {
51        self.exec_outputs.push_back(output);
52        self
53    }
54
55    pub fn add_unwind(mut self, output: Result<UnwindOutput, StageError>) -> Self {
56        self.unwind_outputs.push_back(output);
57        self
58    }
59
60    pub fn with_post_execute_commit_counter(mut self) -> (Self, Arc<AtomicUsize>) {
61        let counter = Arc::new(AtomicUsize::new(0));
62        self.post_execute_commit_counter = counter.clone();
63        (self, counter)
64    }
65
66    pub fn with_post_unwind_commit_counter(mut self) -> (Self, Arc<AtomicUsize>) {
67        let counter = Arc::new(AtomicUsize::new(0));
68        self.post_unwind_commit_counter = counter.clone();
69        (self, counter)
70    }
71
72    pub fn add_validate_unwind(mut self, output: Result<(), StageError>) -> Self {
73        self.validate_unwind_outputs.push_back(output);
74        self
75    }
76}
77
78impl<Provider> Stage<Provider> for TestStage {
79    fn id(&self) -> StageId {
80        self.id
81    }
82
83    fn execute(&mut self, _: &Provider, _input: ExecInput) -> Result<ExecOutput, StageError> {
84        self.exec_outputs
85            .pop_front()
86            .unwrap_or_else(|| panic!("Test stage {} executed too many times.", self.id))
87    }
88
89    fn post_execute_commit(&mut self) -> Result<(), StageError> {
90        self.post_execute_commit_counter.fetch_add(1, Ordering::Relaxed);
91
92        Ok(())
93    }
94
95    fn unwind(&mut self, _: &Provider, _input: UnwindInput) -> Result<UnwindOutput, StageError> {
96        self.unwind_outputs
97            .pop_front()
98            .unwrap_or_else(|| panic!("Test stage {} unwound too many times.", self.id))
99    }
100
101    fn post_unwind_commit(&mut self) -> Result<(), StageError> {
102        self.post_unwind_commit_counter.fetch_add(1, Ordering::Relaxed);
103
104        Ok(())
105    }
106
107    fn validate_unwind(&mut self, _: &Provider, _input: UnwindInput) -> Result<(), StageError> {
108        self.validate_unwind_outputs.pop_front().unwrap_or(Ok(()))
109    }
110}