test: make execute runner return value instead of channel

This commit is contained in:
Georgios Konstantopoulos
2022-11-18 15:37:44 -08:00
parent 077ddf020a
commit f8608654f2
4 changed files with 26 additions and 37 deletions

View File

@@ -264,16 +264,15 @@ mod tests {
runner.set_batch_size(10);
// Run the stage
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Check that we only synced around `batch_size` blocks even though the number of blocks
// synced by the previous stage is higher
let output = rx.await.unwrap();
assert_matches!(
output,
result,
Ok(ExecOutput { stage_progress, reached_tip: true, done: false }) if stage_progress < 200
);
assert!(runner.validate_execution(input, output.ok()).is_ok(), "execution validation");
assert!(runner.validate_execution(input, result.ok()).is_ok(), "execution validation");
}
/// Same as [partial_body_download] except the `batch_size` is not hit.
@@ -293,16 +292,15 @@ mod tests {
runner.set_batch_size(40);
// Run the stage
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Check that we synced all blocks successfully, even though our `batch_size` allows us to
// sync more (if there were more headers)
let output = rx.await.unwrap();
assert_matches!(
output,
result,
Ok(ExecOutput { stage_progress: 20, reached_tip: true, done: true })
);
assert!(runner.validate_execution(input, output.ok()).is_ok(), "execution validation");
assert!(runner.validate_execution(input, result.ok()).is_ok(), "execution validation");
}
/// Same as [full_body_download] except we have made progress before
@@ -321,10 +319,9 @@ mod tests {
runner.set_batch_size(10);
// Run the stage
let rx = runner.execute(input);
let first_run = runner.execute(input).await;
// Check that we synced at least 10 blocks
let first_run = rx.await.unwrap();
assert_matches!(
first_run,
Ok(ExecOutput { stage_progress, reached_tip: true, done: false }) if stage_progress >= 10
@@ -336,15 +333,14 @@ mod tests {
previous_stage: Some((PREV_STAGE_ID, previous_stage)),
stage_progress: Some(first_run_progress),
};
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Check that we synced more blocks
let output = rx.await.unwrap();
assert_matches!(
output,
result,
Ok(ExecOutput { stage_progress, reached_tip: true, done: true }) if stage_progress > first_run_progress
);
assert!(runner.validate_execution(input, output.ok()).is_ok(), "execution validation");
assert!(runner.validate_execution(input, result.ok()).is_ok(), "execution validation");
}
/// Checks that the stage asks to unwind if pre-validation of the block fails.
@@ -364,11 +360,11 @@ mod tests {
runner.consensus.set_fail_validation(true);
// Run the stage
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Check that the error bubbles up
assert_matches!(
rx.await.unwrap(),
result,
Err(StageError::Validation { error: consensus::Error::BaseFeeMissing, .. })
);
assert!(runner.validate_execution(input, None).is_ok(), "execution validation");
@@ -391,16 +387,15 @@ mod tests {
runner.set_batch_size(40);
// Run the stage
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Check that we synced all blocks successfully, even though our `batch_size` allows us to
// sync more (if there were more headers)
let output = rx.await.unwrap();
assert_matches!(
output,
result,
Ok(ExecOutput { stage_progress, reached_tip: true, done: true }) if stage_progress == previous_stage
);
let stage_progress = output.unwrap().stage_progress;
let stage_progress = result.unwrap().stage_progress;
runner.validate_db_blocks(stage_progress).expect("Written block data invalid");
// Delete a transaction
@@ -449,10 +444,10 @@ mod tests {
)]));
// Run the stage
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Check that the error bubbles up
assert_matches!(rx.await.unwrap(), Err(StageError::Internal(_)));
assert_matches!(result, Err(StageError::Internal(_)));
assert!(runner.validate_execution(input, None).is_ok(), "execution validation");
}

View File

@@ -206,9 +206,8 @@ mod tests {
let mut runner = HeadersTestRunner::default();
let input = ExecInput::default();
runner.seed_execution(input).expect("failed to seed execution");
let rx = runner.execute(input);
let result = runner.execute(input).await;
runner.consensus.update_tip(H256::from_low_u64_be(1));
let result = rx.await.unwrap();
assert_matches!(
result,
Ok(ExecOutput { done: false, reached_tip: false, stage_progress: 0 })
@@ -223,9 +222,8 @@ mod tests {
runner.consensus.set_fail_validation(true);
let input = ExecInput::default();
let seed = runner.seed_execution(input).expect("failed to seed execution");
let rx = runner.execute(input);
let result = runner.execute(input).await;
runner.after_execution(seed).await.expect("failed to run after execution hook");
let result = rx.await.unwrap();
assert_matches!(result, Err(StageError::Validation { .. }));
assert!(runner.validate_execution(input, result.ok()).is_ok(), "validation failed");
}
@@ -240,7 +238,7 @@ mod tests {
stage_progress: Some(stage_progress),
};
let headers = runner.seed_execution(input).expect("failed to seed execution");
let rx = runner.execute(input);
let result = runner.execute(input).await;
// skip `after_execution` hook for linear downloader
let tip = headers.last().unwrap();
@@ -255,7 +253,6 @@ mod tests {
})
.await;
let result = rx.await.unwrap();
assert_matches!(
result,
Ok(ExecOutput { done: true, reached_tip: true, stage_progress }) if stage_progress == tip.number

View File

@@ -10,7 +10,7 @@ macro_rules! stage_test_suite {
let input = crate::stage::ExecInput::default();
// Run stage execution
let result = runner.execute(input).await.unwrap();
let result = runner.execute(input).await;
assert_matches!(
result,
Err(crate::error::StageError::DatabaseIntegrity(_))
@@ -34,13 +34,12 @@ macro_rules! stage_test_suite {
let seed = runner.seed_execution(input).expect("failed to seed");
// Run stage execution
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Run `after_execution` hook
runner.after_execution(seed).await.expect("failed to run after execution hook");
// Assert the successful result
let result = rx.await.unwrap();
assert_matches!(
result,
Ok(ExecOutput { done, reached_tip, stage_progress })
@@ -63,13 +62,12 @@ macro_rules! stage_test_suite {
stage_progress: Some(stage_progress),
};
let seed = runner.seed_execution(input).expect("failed to seed");
let rx = runner.execute(input);
let result = runner.execute(input).await;
// Run `after_execution` hook
runner.after_execution(seed).await.expect("failed to run after execution hook");
// Assert the successful result
let result = rx.await.unwrap();
assert_matches!(
result,
Ok(ExecOutput { done, reached_tip, stage_progress })
@@ -112,11 +110,10 @@ macro_rules! stage_test_suite {
let seed = runner.seed_execution(execute_input).expect("failed to seed");
// Run stage execution
let rx = runner.execute(execute_input);
let result = runner.execute(execute_input).await;
runner.after_execution(seed).await.expect("failed to run after execution hook");
// Assert the successful execution result
let result = rx.await.unwrap();
assert_matches!(
result,
Ok(ExecOutput { done, reached_tip, stage_progress })

View File

@@ -41,7 +41,7 @@ pub(crate) trait ExecuteStageTestRunner: StageTestRunner {
) -> Result<(), TestRunnerError>;
/// Run [Stage::execute] and return a receiver for the result.
fn execute(&self, input: ExecInput) -> oneshot::Receiver<Result<ExecOutput, StageError>> {
async fn execute(&self, input: ExecInput) -> Result<ExecOutput, StageError> {
let (tx, rx) = oneshot::channel();
let (db, mut stage) = (self.db().inner(), self.stage());
tokio::spawn(async move {
@@ -50,7 +50,7 @@ pub(crate) trait ExecuteStageTestRunner: StageTestRunner {
db.commit().expect("failed to commit");
tx.send(result).expect("failed to send message")
});
rx
Box::pin(rx).await.unwrap()
}
/// Run a hook after [Stage::execute]. Required for Headers & Bodies stages.