Skip to content

Commit

Permalink
pass test name to test_config to construct db name
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 9, 2024
1 parent 6034728 commit b7e9f18
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
9 changes: 4 additions & 5 deletions turbopack/crates/turbo-tasks-backend/tests/test_config.trs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
|initial | {
let path = std::path::PathBuf::from(concat!(
|name, initial| {
let path = std::path::PathBuf::from(format!(concat!(
env!("OUT_DIR"),
"/.cache/",
module_path!(),
));
"/.cache/{}",
), name));
if initial {
let _ = std::fs::remove_dir_all(&path);
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-fetch/tests/test_config.trs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
|_initial | {
|_name, _initial | {
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
|_initial | {
|_name, _initial | {
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
}
2 changes: 1 addition & 1 deletion turbopack/crates/turbo-tasks-memory/tests/test_config.trs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
|_initial | {
|_name, _initial | {
turbo_tasks::TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(usize::MAX))
}
29 changes: 20 additions & 9 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(bool) -> Arc<dyn TurboTasksApi>,
create_turbo_tasks: fn(&str, bool) -> Arc<dyn TurboTasksApi>,
}

impl Registration {
#[doc(hidden)]
pub const fn new(create_turbo_tasks: fn(bool) -> 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, initial: bool) -> Arc<dyn TurboTasksApi> {
(self.create_turbo_tasks)(initial)
pub fn create_turbo_tasks(&self, name: &str, initial: bool) -> Arc<dyn TurboTasksApi> {
(self.create_turbo_tasks)(name, initial)
}
}

Expand All @@ -40,12 +43,12 @@ macro_rules! register {
($($other_register_fns:expr),* $(,)?) => {{
use turbo_tasks::TurboTasksApi;
use std::sync::Arc;
fn create_turbo_tasks(initial: bool) -> Arc<dyn TurboTasksApi> {
fn create_turbo_tasks(name: &str, initial: bool) -> Arc<dyn TurboTasksApi> {
let inner = include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/test_config.trs"
));
(inner)(initial)
(inner)(name, initial)
}
fn register_impl() {
$($other_register_fns();)*
Expand All @@ -69,10 +72,16 @@ where
T: TraceRawVcs + Send + 'static,
{
registration.ensure_registered();
let tt = registration.create_turbo_tasks(true);
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 @@ -82,14 +91,16 @@ where
T: Debug + PartialEq + Eq + TraceRawVcs + Send + 'static,
{
registration.ensure_registered();
let tt = registration.create_turbo_tasks(true);

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.clone(), fut()).await?;
tt.stop_and_wait().await;
assert_eq!(first, second);
let tt = registration.create_turbo_tasks(false);
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?;
tt.stop_and_wait().await;
Expand Down

0 comments on commit b7e9f18

Please sign in to comment.