Launch tests in parallel.

This commit is contained in:
Tom Alexander 2023-10-13 12:07:52 -04:00
parent b0b795d13b
commit 5ecd7b8bef
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 20 additions and 13 deletions

View File

@ -9,6 +9,7 @@ use futures::future::FutureExt;
use organic::compare::run_anonymous_compare;
use organic::compare::run_compare_on_file;
use tokio::sync::Semaphore;
use tokio::task::JoinError;
#[cfg(feature = "tracing")]
use crate::init_tracing::init_telemetry;
@ -100,11 +101,13 @@ pub(crate) enum TestStatus {
}
impl TestConfig {
fn run_test(self) -> BoxFuture<'static, TestResult> {
fn run_test(self) -> BoxFuture<'static, Result<TestResult, JoinError>> {
async move {
match self {
TestConfig::TestLayer(test) => TestResult::ResultLayer(test.run_test().await),
TestConfig::SingleFile(test) => TestResult::SingleFileResult(test.run_test().await),
TestConfig::TestLayer(test) => Ok(TestResult::ResultLayer(test.run_test().await?)),
TestConfig::SingleFile(test) => {
Ok(TestResult::SingleFileResult(test.run_test().await?))
}
}
}
.boxed()
@ -112,30 +115,34 @@ impl TestConfig {
}
impl SingleFile {
async fn run_test(self) -> SingleFileResult {
async fn run_test(self) -> Result<SingleFileResult, JoinError> {
let _permit = TEST_PERMITS.acquire().await.unwrap();
let result = run_compare_on_file(&self.file_path);
SingleFileResult {
Ok(SingleFileResult {
file_path: self.file_path,
status: if result.is_ok() {
TestStatus::Good
} else {
TestStatus::Bad
},
}
})
}
}
impl TestLayer {
async fn run_test(self) -> ResultLayer {
let mut children = Vec::with_capacity(self.children.len());
for config in self.children {
let result = config.run_test().await;
children.push(result);
async fn run_test(self) -> Result<ResultLayer, JoinError> {
let running_children: Vec<_> = self
.children
.into_iter()
.map(|c| tokio::spawn(c.run_test()))
.collect();
let mut children = Vec::with_capacity(running_children.len());
for c in running_children {
children.push(c.await??);
}
ResultLayer {
Ok(ResultLayer {
name: self.name,
children,
}
})
}
}