From 6aa4808a0936e85384100f13e07aa7bea559bc3c Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 17 Jul 2026 18:22:54 +0900 Subject: refactor(tests-async): unify duplicated tokio runtime bridges 11 downloader/installer integration-test files each redefined an identical current_thread `run()` helper to block on async code. Extract one shared multi_thread Runtime into tests/common/async_runtime.rs so concurrent #[test] threads can all block_on it, matching the direction item 7 (top-level Runtime) will take in production code. --- crates/shirabe/tests/common/async_runtime.rs | 23 ++++++++++++++++++++++ .../tests/downloader/download_manager_test.rs | 8 +------- .../tests/downloader/file_downloader_test.rs | 8 +------- .../tests/downloader/fossil_downloader_test.rs | 8 +------- .../tests/downloader/git_downloader_test.rs | 8 +------- .../shirabe/tests/downloader/hg_downloader_test.rs | 8 +------- crates/shirabe/tests/downloader/main.rs | 2 ++ .../tests/downloader/perforce_downloader_test.rs | 8 +------- .../shirabe/tests/downloader/xz_downloader_test.rs | 8 +------- .../tests/downloader/zip_downloader_test.rs | 8 +------- .../tests/installer/installation_manager_test.rs | 8 +------- .../tests/installer/library_installer_test.rs | 8 +------- crates/shirabe/tests/installer/main.rs | 2 ++ .../tests/installer/metapackage_installer_test.rs | 8 +------- 14 files changed, 38 insertions(+), 77 deletions(-) create mode 100644 crates/shirabe/tests/common/async_runtime.rs (limited to 'crates/shirabe/tests') diff --git a/crates/shirabe/tests/common/async_runtime.rs b/crates/shirabe/tests/common/async_runtime.rs new file mode 100644 index 00000000..e047315a --- /dev/null +++ b/crates/shirabe/tests/common/async_runtime.rs @@ -0,0 +1,23 @@ +//! Shared tokio runtime bridge for integration tests. +//! +//! `cargo test` runs `#[test]` fns concurrently on separate OS threads, so a per-call +//! `current_thread` Runtime would need one instance per caller. Instead this holds a single +//! process-wide `multi_thread` Runtime that tolerates concurrent `block_on()` callers, and every +//! test bridges into async code through it. +//! +//! Included into each integration-test binary via +//! `#[path = "../common/async_runtime.rs"] mod async_runtime;`. +#![allow(dead_code)] + +use std::sync::LazyLock; + +static RUNTIME: LazyLock = LazyLock::new(|| { + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() +}); + +pub fn run(future: F) -> F::Output { + RUNTIME.block_on(future) +} diff --git a/crates/shirabe/tests/downloader/download_manager_test.rs b/crates/shirabe/tests/downloader/download_manager_test.rs index f596beed..6f6ed352 100644 --- a/crates/shirabe/tests/downloader/download_manager_test.rs +++ b/crates/shirabe/tests/downloader/download_manager_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/DownloadManagerTest.php +use crate::async_runtime::run; use crate::io_stub::IOStub; use indexmap::IndexMap; use shirabe::downloader::DownloaderInterface; @@ -58,13 +59,6 @@ mockall::mock! { } } -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - /// ref: DownloadManagerTest::createPackageMock /// /// PHPUnit returns a `PackageInterface` mock; a real CompletePackage with the diff --git a/crates/shirabe/tests/downloader/file_downloader_test.rs b/crates/shirabe/tests/downloader/file_downloader_test.rs index e2b19958..a9af69ed 100644 --- a/crates/shirabe/tests/downloader/file_downloader_test.rs +++ b/crates/shirabe/tests/downloader/file_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/FileDownloaderTest.php +use crate::async_runtime::run; use crate::http_downloader_mock::get_http_downloader_mock; use crate::io_mock::{Expectation, get_io_mock}; use indexmap::IndexMap; @@ -28,13 +29,6 @@ fn get_package(name: &str, version: &str) -> PackageInterfaceHandle { CompletePackageHandle::new(name.to_string(), norm_version, version.to_string()).into() } -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - /// ref: TestCase::getConfig fn get_config( config_options: IndexMap, diff --git a/crates/shirabe/tests/downloader/fossil_downloader_test.rs b/crates/shirabe/tests/downloader/fossil_downloader_test.rs index eed3d7fb..e473b2dd 100644 --- a/crates/shirabe/tests/downloader/fossil_downloader_test.rs +++ b/crates/shirabe/tests/downloader/fossil_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/FossilDownloaderTest.php +use crate::async_runtime::run; use crate::config_stub::ConfigStubBuilder; use crate::io_stub::IOStub; use crate::process_executor_mock::get_process_executor_mock; @@ -14,13 +15,6 @@ use shirabe_php_shim::PhpMixed; use shirabe_semver::VersionParser; use tempfile::TempDir; -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - fn set_up() -> TempDir { TempDir::new().unwrap() } diff --git a/crates/shirabe/tests/downloader/git_downloader_test.rs b/crates/shirabe/tests/downloader/git_downloader_test.rs index a73958b4..d4f40b77 100644 --- a/crates/shirabe/tests/downloader/git_downloader_test.rs +++ b/crates/shirabe/tests/downloader/git_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/GitDownloaderTest.php +use crate::async_runtime::run; use crate::config_stub::ConfigStubBuilder; use crate::io_mock::{Expectation, get_io_mock}; use crate::io_stub::IOStub; @@ -20,13 +21,6 @@ use shirabe_php_shim::PhpMixed; use shirabe_semver::VersionParser; use tempfile::TempDir; -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - fn set_up() -> TempDir { // skipIfNotExecutable('git') is irrelevant because every git invocation is mocked. diff --git a/crates/shirabe/tests/downloader/hg_downloader_test.rs b/crates/shirabe/tests/downloader/hg_downloader_test.rs index d68812d8..ac3d50b6 100644 --- a/crates/shirabe/tests/downloader/hg_downloader_test.rs +++ b/crates/shirabe/tests/downloader/hg_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/HgDownloaderTest.php +use crate::async_runtime::run; use crate::io_stub::IOStub; use crate::process_executor_mock::{cmd, get_process_executor_mock}; use shirabe::config::Config; @@ -12,13 +13,6 @@ use shirabe::util::filesystem::{Filesystem, FilesystemMock}; use shirabe_semver::VersionParser; use tempfile::TempDir; -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - fn set_up() -> TempDir { TempDir::new().unwrap() } diff --git a/crates/shirabe/tests/downloader/main.rs b/crates/shirabe/tests/downloader/main.rs index 82440108..88f16266 100644 --- a/crates/shirabe/tests/downloader/main.rs +++ b/crates/shirabe/tests/downloader/main.rs @@ -1,3 +1,5 @@ +#[path = "../common/async_runtime.rs"] +mod async_runtime; #[path = "../common/config_stub.rs"] mod config_stub; #[path = "../common/http_downloader_mock.rs"] diff --git a/crates/shirabe/tests/downloader/perforce_downloader_test.rs b/crates/shirabe/tests/downloader/perforce_downloader_test.rs index 13a818a6..bdf24a2e 100644 --- a/crates/shirabe/tests/downloader/perforce_downloader_test.rs +++ b/crates/shirabe/tests/downloader/perforce_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/PerforceDownloaderTest.php +use crate::async_runtime::run; use crate::io_mock::{Expectation, get_io_mock}; use crate::io_stub::IOStub; use crate::process_executor_mock::get_process_executor_mock; @@ -42,13 +43,6 @@ mockall::mock! { } } -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - /// ref: PerforceDownloaderTest::getConfig (seeds `home` with the temp dir) fn get_config(test_path: &std::path::Path) -> Config { let mut config = Config::new(true, None); diff --git a/crates/shirabe/tests/downloader/xz_downloader_test.rs b/crates/shirabe/tests/downloader/xz_downloader_test.rs index 6d504313..0002f0e6 100644 --- a/crates/shirabe/tests/downloader/xz_downloader_test.rs +++ b/crates/shirabe/tests/downloader/xz_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/XzDownloaderTest.php +use crate::async_runtime::run; use indexmap::IndexMap; use shirabe::config::Config; use shirabe::downloader::DownloaderInterface; @@ -22,13 +23,6 @@ fn get_package(name: &str, version: &str) -> PackageInterfaceHandle { CompletePackageHandle::new(name.to_string(), norm_version, version.to_string()).into() } -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - /// ref: setUp markTestSkipped on Windows / 32bit, expressed as a compile-time /// cfg gate on the test function below. fn set_up() -> TempDir { diff --git a/crates/shirabe/tests/downloader/zip_downloader_test.rs b/crates/shirabe/tests/downloader/zip_downloader_test.rs index b8fddf68..878c8054 100644 --- a/crates/shirabe/tests/downloader/zip_downloader_test.rs +++ b/crates/shirabe/tests/downloader/zip_downloader_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Downloader/ZipDownloaderTest.php +use crate::async_runtime::run; use crate::io_stub::IOStub; use indexmap::IndexMap; use serial_test::serial; @@ -17,13 +18,6 @@ use shirabe_php_shim::{PhpMixed, ZipArchive, ZipArchiveMock}; use shirabe_semver::VersionParser; use tempfile::TempDir; -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - struct SetUp { test_dir: TempDir, io: std::rc::Rc>, diff --git a/crates/shirabe/tests/installer/installation_manager_test.rs b/crates/shirabe/tests/installer/installation_manager_test.rs index f60d1250..e24f440c 100644 --- a/crates/shirabe/tests/installer/installation_manager_test.rs +++ b/crates/shirabe/tests/installer/installation_manager_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Installer/InstallationManagerTest.php +use crate::async_runtime::run; use crate::test_case::get_package; use shirabe::dependency_resolver::operation::{ InstallOperation, UninstallOperation, UpdateOperation, @@ -15,13 +16,6 @@ use shirabe::util::r#loop::Loop; use shirabe_php_shim::PhpMixed; use shirabe_semver::VersionParser; -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - /// ref: setUp(): the PHP loop/io mocks are never exercised by these tests (the loop has its /// constructor disabled), so a real Loop over a real HttpDownloader and a NullIO stand in. struct SetUp { diff --git a/crates/shirabe/tests/installer/library_installer_test.rs b/crates/shirabe/tests/installer/library_installer_test.rs index 72272a07..f2a94cbf 100644 --- a/crates/shirabe/tests/installer/library_installer_test.rs +++ b/crates/shirabe/tests/installer/library_installer_test.rs @@ -1,5 +1,6 @@ //! ref: composer/tests/Composer/Test/Installer/LibraryInstallerTest.php +use crate::async_runtime::run; use crate::test_case::get_package; use indexmap::IndexMap; use shirabe::composer::{ @@ -87,13 +88,6 @@ mockall::mock! { } } -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - /// Mirror of setUp(): builds the Composer/Config over temp root/vendor/bin dirs plus a NullIO and a /// DownloadManager mock. `composer_full` keeps the inner Rc alive for the duration of the test since /// LibraryInstaller only holds a weak handle, and lets tests swap in a configured DownloadManager diff --git a/crates/shirabe/tests/installer/main.rs b/crates/shirabe/tests/installer/main.rs index 4b422780..b3641108 100644 --- a/crates/shirabe/tests/installer/main.rs +++ b/crates/shirabe/tests/installer/main.rs @@ -1,3 +1,5 @@ +#[path = "../common/async_runtime.rs"] +mod async_runtime; #[path = "../common/io_mock.rs"] mod io_mock; #[path = "../common/test_case.rs"] diff --git a/crates/shirabe/tests/installer/metapackage_installer_test.rs b/crates/shirabe/tests/installer/metapackage_installer_test.rs index bc740e74..3380befb 100644 --- a/crates/shirabe/tests/installer/metapackage_installer_test.rs +++ b/crates/shirabe/tests/installer/metapackage_installer_test.rs @@ -3,19 +3,13 @@ //! PHP verifies the mocked repository's add/remove/hasPackage calls; here the same //! behaviour is checked against a real InstalledArrayRepository by observing its state. +use crate::async_runtime::run; use crate::test_case::get_package; use shirabe::installer::{InstallerInterface, MetapackageInstaller}; use shirabe::io::IOInterface; use shirabe::io::null_io::NullIO; use shirabe::repository::{InstalledArrayRepository, RepositoryInterface}; -fn run(future: F) -> F::Output { - tokio::runtime::Builder::new_current_thread() - .build() - .unwrap() - .block_on(future) -} - fn installer() -> MetapackageInstaller { let io: std::rc::Rc> = std::rc::Rc::new(std::cell::RefCell::new(NullIO::new())); -- cgit v1.3.1