Skip to content

Commit c767498

Browse files
committed
improve test case to test session dependent and restoring
1 parent adc00e8 commit c767498

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

turbopack/crates/turbo-tasks-backend/tests/emptied_cells.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44

55
use anyhow::Result;
66
use turbo_tasks::{State, Vc};
7-
use turbo_tasks_testing::{Registration, register, run_once};
7+
use turbo_tasks_testing::{Registration, register, run};
88

99
static REGISTRATION: Registration = register!();
1010

1111
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1212
async fn recompute() {
13-
run_once(&REGISTRATION, || async {
14-
let input = ChangingInput {
15-
state: State::new(1),
16-
}
17-
.cell();
13+
run(&REGISTRATION, || async {
14+
let input = get_state().resolve().await?;
15+
input.await?.state.set(0);
1816
let output = compute(input);
19-
assert_eq!(*output.await?, 1);
17+
assert_eq!(*output.strongly_consistent().await?, 0);
2018

2119
println!("changing input");
2220
input.await?.state.set(10);
@@ -44,6 +42,14 @@ async fn recompute() {
4442
.unwrap();
4543
}
4644

45+
#[turbo_tasks::function]
46+
fn get_state() -> Vc<ChangingInput> {
47+
ChangingInput {
48+
state: State::new(0),
49+
}
50+
.cell()
51+
}
52+
4753
#[turbo_tasks::value]
4854
struct ChangingInput {
4955
state: State<u32>,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#![feature(arbitrary_self_types)]
2+
#![feature(arbitrary_self_types_pointers)]
3+
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
4+
5+
use anyhow::Result;
6+
use turbo_tasks::{State, Vc, mark_session_dependent};
7+
use turbo_tasks_testing::{Registration, register, run};
8+
9+
static REGISTRATION: Registration = register!();
10+
11+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
12+
async fn recompute() {
13+
run(&REGISTRATION, || async {
14+
let input = get_state().resolve().await?;
15+
input.await?.state.set(0);
16+
let output = compute(input);
17+
assert_eq!(*output.strongly_consistent().await?, 0);
18+
19+
println!("changing input");
20+
input.await?.state.set(10);
21+
assert_eq!(*output.strongly_consistent().await?, 10);
22+
23+
println!("changing input");
24+
input.await?.state.set(5);
25+
assert_eq!(*output.strongly_consistent().await?, 5);
26+
27+
println!("changing input");
28+
input.await?.state.set(20);
29+
assert_eq!(*output.strongly_consistent().await?, 20);
30+
31+
println!("changing input");
32+
input.await?.state.set(15);
33+
assert_eq!(*output.strongly_consistent().await?, 15);
34+
35+
println!("changing input");
36+
input.await?.state.set(1);
37+
assert_eq!(*output.strongly_consistent().await?, 1);
38+
39+
anyhow::Ok(())
40+
})
41+
.await
42+
.unwrap();
43+
}
44+
45+
#[turbo_tasks::function]
46+
fn get_state() -> Vc<ChangingInput> {
47+
ChangingInput {
48+
state: State::new(0),
49+
}
50+
.cell()
51+
}
52+
53+
#[turbo_tasks::value]
54+
struct ChangingInput {
55+
state: State<u32>,
56+
}
57+
58+
#[turbo_tasks::function]
59+
async fn compute(input: Vc<ChangingInput>) -> Result<Vc<u32>> {
60+
println!("compute()");
61+
let value = *inner_compute(input).await?;
62+
Ok(Vc::cell(value))
63+
}
64+
65+
#[turbo_tasks::function]
66+
async fn inner_compute(input: Vc<ChangingInput>) -> Result<Vc<u32>> {
67+
println!("inner_compute()");
68+
let state_value = *input.await?.state.get();
69+
let mut last = None;
70+
for i in 0..=state_value {
71+
last = Some(compute2(Vc::cell(i)));
72+
}
73+
Ok(last.unwrap())
74+
}
75+
76+
#[turbo_tasks::function]
77+
async fn compute2(input: Vc<u32>) -> Result<Vc<u32>> {
78+
mark_session_dependent();
79+
println!("compute2()");
80+
let value = *input.await?;
81+
Ok(Vc::cell(value))
82+
}

0 commit comments

Comments
 (0)