Skip to content

Commit

Permalink
pass test name to test_config
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 9, 2024
1 parent 7e4ed66 commit 969a37b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
4 changes: 3 additions & 1 deletion turbopack/crates/turbo-tasks-fetch/tests/test_config.trs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
|_name, _initial | {
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
|_name, _initial | {
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
}
4 changes: 3 additions & 1 deletion turbopack/crates/turbo-tasks-memory/tests/test_config.trs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
|_name, _initial | {
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
}
37 changes: 26 additions & 11 deletions turbopack/crates/turbo-tasks-testing/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use turbo_tasks::{run_once, trace::TraceRawVcs, TurboTasksApi};
pub struct Registration {
execution_lock: OnceLock<()>,
func: fn(),
create_turbo_tasks: fn() -> Arc<dyn TurboTasksApi>,
create_turbo_tasks: fn(&str, bool) -> Arc<dyn TurboTasksApi>,
}

impl Registration {
#[doc(hidden)]
pub const fn new(create_turbo_tasks: fn() -> Arc<dyn TurboTasksApi>, func: fn()) -> Self {
pub const fn new(
create_turbo_tasks: fn(&str, bool) -> Arc<dyn TurboTasksApi>,
func: fn(),
) -> Self {
Registration {
execution_lock: OnceLock::new(),
func,
Expand All @@ -30,8 +33,8 @@ impl Registration {
self.execution_lock.get_or_init(self.func);
}

pub fn create_turbo_tasks(&self) -> Arc<dyn TurboTasksApi> {
(self.create_turbo_tasks)()
pub fn create_turbo_tasks(&self, name: &str, initial: bool) -> Arc<dyn TurboTasksApi> {
(self.create_turbo_tasks)(name, initial)
}
}

Expand All @@ -40,11 +43,12 @@ macro_rules! register {
($($other_register_fns:expr),* $(,)?) => {{
use turbo_tasks::TurboTasksApi;
use std::sync::Arc;
fn create_turbo_tasks() -> Arc<dyn TurboTasksApi> {
include!(concat!(
fn create_turbo_tasks(name: &str, initial: bool) -> Arc<dyn TurboTasksApi> {
let inner = include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/test_config.trs"
))
));
(inner)(name, initial)
}
fn register_impl() {
$($other_register_fns();)*
Expand All @@ -68,10 +72,16 @@ where
T: TraceRawVcs + Send + 'static,
{
registration.ensure_registered();
let tt = registration.create_turbo_tasks();
let name = closure_to_name(&fut);
let tt = registration.create_turbo_tasks(&name, true);
run_once(tt, async move { Ok(fut.await) }).await.unwrap()
}

fn closure_to_name<T>(value: &T) -> String {
let name = std::any::type_name_of_val(value);
name.replace("::{{closure}}", "").replace("::", "_")
}

pub async fn run<T, F>(
registration: &Registration,
fut: impl Fn() -> F + Send + 'static,
Expand All @@ -81,12 +91,17 @@ where
T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static,
{
registration.ensure_registered();
let tt = registration.create_turbo_tasks();

let name = closure_to_name(&fut);
let tt = registration.create_turbo_tasks(&name, true);
println!("Run #1 (without cache)");
let first = run_once(tt.clone(), fut()).await?;
println!("Run #2 (with memory cache, same TurboTasks instance)");
let second = run_once(tt, fut()).await?;
assert_eq!(first, second);
let tt = registration.create_turbo_tasks();
let third = run_once(tt, fut()).await?;
let tt = registration.create_turbo_tasks(&name, false);
println!("Run #3 (with persistent cache if available, new TurboTasks instance)");
let third = run_once(tt.clone(), fut()).await?;
assert_eq!(first, third);
Ok(())
}
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-testing/tests/scope_stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn rectangle_stress() {
.build()
.unwrap();
rt.block_on(async {
let tt = REGISTRATION.create_turbo_tasks();
let tt = REGISTRATION.create_turbo_tasks("scope_stress_rectangle_stress", true);
let size = std::env::var("TURBOPACK_TEST_RECTANGLE_STRESS_SIZE")
.map(|size| size.parse().unwrap())
.unwrap_or(50);
Expand Down

0 comments on commit 969a37b

Please sign in to comment.