|
| 1 | +use crate::predicates::Predicate; |
| 2 | +use async_broadcast::broadcast; |
| 3 | +use hotshot_task_impls::events::HotShotEvent; |
| 4 | + |
| 5 | +use hotshot_task::task::{Task, TaskRegistry, TaskState}; |
| 6 | +use hotshot_types::traits::node_implementation::NodeType; |
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +/// A `TestScript` is a sequence of triples (input sequence, output sequence, assertions). |
| 10 | +type TestScript<TYPES, S> = Vec<TestScriptStage<TYPES, S>>; |
| 11 | + |
| 12 | +pub struct TestScriptStage<TYPES: NodeType, S: TaskState<Event = HotShotEvent<TYPES>>> { |
| 13 | + pub inputs: Vec<HotShotEvent<TYPES>>, |
| 14 | + pub outputs: Vec<Predicate<HotShotEvent<TYPES>>>, |
| 15 | + pub asserts: Vec<Predicate<S>>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<INPUT> std::fmt::Debug for Predicate<INPUT> { |
| 19 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 20 | + write!(f, "{}", self.info) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// `run_test_script` reads a triple (inputs, outputs, asserts) in a `TestScript`, |
| 25 | +/// It broadcasts all given inputs (in order) and waits to receive all outputs (in order). |
| 26 | +/// Once the expected outputs have been received, it validates the task state at that stage |
| 27 | +/// against the given assertions. |
| 28 | +/// |
| 29 | +/// If all assertions pass, it moves onto the next stage. If it receives an unexpected output |
| 30 | +/// or fails to receive an output, the test fails immediately with a panic. |
| 31 | +/// |
| 32 | +/// Note: the task is not spawned with an async thread; instead, the harness just calls `handle_event`. |
| 33 | +/// This has a few implications, e.g. shutting down tasks doesn't really make sense, |
| 34 | +/// and event ordering is deterministic. |
| 35 | +pub async fn run_test_script<TYPES, S: TaskState<Event = HotShotEvent<TYPES>>>( |
| 36 | + mut script: TestScript<TYPES, S>, |
| 37 | + state: S, |
| 38 | +) where |
| 39 | + TYPES: NodeType, |
| 40 | + S: Send + 'static, |
| 41 | +{ |
| 42 | + let registry = Arc::new(TaskRegistry::default()); |
| 43 | + |
| 44 | + let (test_input, task_receiver) = broadcast(1024); |
| 45 | + // let (task_input, mut test_receiver) = broadcast(1024); |
| 46 | + |
| 47 | + let task_input = test_input.clone(); |
| 48 | + let mut test_receiver = task_receiver.clone(); |
| 49 | + |
| 50 | + let mut task = Task::new( |
| 51 | + task_input.clone(), |
| 52 | + task_receiver.clone(), |
| 53 | + registry.clone(), |
| 54 | + state, |
| 55 | + ); |
| 56 | + |
| 57 | + for (stage_number, stage) in script.iter_mut().enumerate() { |
| 58 | + tracing::debug!("Beginning test stage {}", stage_number); |
| 59 | + for input in &mut *stage.inputs { |
| 60 | + if !task.state_mut().filter(input) { |
| 61 | + tracing::debug!("Test sent: {:?}", input); |
| 62 | + |
| 63 | + if let Some(res) = S::handle_event(input.clone(), &mut task).await { |
| 64 | + task.state_mut().handle_result(&res).await; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + for expected in &stage.outputs { |
| 70 | + let output_missing_error = format!( |
| 71 | + "Stage {} | Failed to receive output for predicate: {:?}", |
| 72 | + stage_number, expected |
| 73 | + ); |
| 74 | + |
| 75 | + if let Ok(received_output) = test_receiver.try_recv() { |
| 76 | + tracing::debug!("Test received: {:?}", received_output); |
| 77 | + assert!( |
| 78 | + (expected.function)(&received_output), |
| 79 | + "Stage {} | Output failed to satisfy {:?}", |
| 80 | + stage_number, |
| 81 | + expected |
| 82 | + ); |
| 83 | + } else { |
| 84 | + panic!("{}", output_missing_error); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + for assert in &stage.asserts { |
| 89 | + assert!( |
| 90 | + (assert.function)(task.state()), |
| 91 | + "Stage {} | Assertion on task state failed: {:?}", |
| 92 | + stage_number, |
| 93 | + assert |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + if let Ok(received_output) = test_receiver.try_recv() { |
| 98 | + let extra_output_error = format!( |
| 99 | + "Stage {} | Received unexpected additional output: {:?}", |
| 100 | + stage_number, received_output |
| 101 | + ); |
| 102 | + |
| 103 | + panic!("{}", extra_output_error); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments