|
| 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(®ISTRATION, || 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