| #![allow(clippy::needless_return)] |
| #![feature(arbitrary_self_types)] |
| #![feature(arbitrary_self_types_pointers)] |
|
|
| use tokio::{ |
| sync::{Notify, watch}, |
| time::{Duration, sleep, timeout}, |
| }; |
| use turbo_tasks::{ |
| State, TransientInstance, Vc, prevent_gc, |
| trace::{TraceRawVcs, TraceRawVcsContext}, |
| turbo_tasks, |
| }; |
| use turbo_tasks_testing::{Registration, register, run}; |
|
|
| static REGISTRATION: Registration = register!(); |
|
|
| #[tokio::test] |
| async fn test_spawns_detached() -> anyhow::Result<()> { |
| run(®ISTRATION, || async { |
| |
| |
| prevent_gc(); |
| |
| timeout(Duration::from_secs(5), async { |
| let notify = TransientInstance::new(NotifyTaskInput(Notify::new())); |
| let (tx, mut rx) = watch::channel(None); |
| let tx = TransientInstance::new(WatchSenderTaskInput(tx)); |
|
|
| |
| let out_vc = spawns_detached(notify.clone(), tx.clone()); |
|
|
| |
| timeout(Duration::from_millis(100), out_vc.strongly_consistent()) |
| .await |
| .expect_err("should wait on the detached task"); |
|
|
| |
| notify.0.notify_waiters(); |
|
|
| |
| let detached_vc: Vc<u32> = rx.wait_for(|opt| opt.is_some()).await?.unwrap(); |
| assert_eq!(*detached_vc.strongly_consistent().await?, 42); |
|
|
| |
| out_vc.strongly_consistent().await?; |
|
|
| Ok(()) |
| }) |
| .await? |
| }) |
| .await |
| } |
|
|
| #[derive(TraceRawVcs)] |
| struct NotifyTaskInput( |
| |
| #[turbo_tasks(trace_ignore)] Notify, |
| ); |
|
|
| struct WatchSenderTaskInput<T>(watch::Sender<T>); |
|
|
| impl<T: TraceRawVcs> TraceRawVcs for WatchSenderTaskInput<T> { |
| fn trace_raw_vcs(&self, _trace_context: &mut TraceRawVcsContext) { |
| |
| |
| } |
| } |
|
|
| #[turbo_tasks::function] |
| async fn spawns_detached( |
| notify: TransientInstance<NotifyTaskInput>, |
| sender: TransientInstance<WatchSenderTaskInput<Option<Vc<u32>>>>, |
| ) -> Vc<()> { |
| tokio::spawn(turbo_tasks().detached_for_testing(Box::pin(async move { |
| notify.0.notified().await; |
| |
| |
| sender.0.send(Some(Vc::cell(42))).unwrap(); |
| Ok(()) |
| }))); |
| Vc::cell(()) |
| } |
|
|
| #[tokio::test] |
| async fn test_spawns_detached_changing() -> anyhow::Result<()> { |
| run(®ISTRATION, || async { |
| |
| prevent_gc(); |
| |
| timeout(Duration::from_secs(5), async { |
| let (tx, mut rx) = watch::channel(None); |
| let tx = TransientInstance::new(WatchSenderTaskInput(tx)); |
|
|
| |
| let changing_input_detached = ChangingInput { |
| state: State::new(42), |
| } |
| .cell(); |
|
|
| |
| let changing_input_outer = ChangingInput { |
| state: State::new(0), |
| } |
| .cell(); |
|
|
| |
| let out_vc = |
| spawns_detached_changing(tx.clone(), changing_input_detached, changing_input_outer); |
|
|
| |
| let detached_vc: Vc<u32> = rx.wait_for(|opt| opt.is_some()).await.unwrap().unwrap(); |
| assert_eq!(*detached_vc.strongly_consistent().await.unwrap(), 42); |
|
|
| |
| out_vc.strongly_consistent().await.unwrap(); |
|
|
| |
| changing_input_detached.await.unwrap().state.set(43); |
| out_vc.strongly_consistent().await.unwrap(); |
| assert_eq!(*detached_vc.strongly_consistent().await.unwrap(), 43); |
|
|
| changing_input_outer.await.unwrap().state.set(44); |
| assert_eq!(*out_vc.strongly_consistent().await.unwrap(), 44); |
|
|
| Ok(()) |
| }) |
| .await? |
| }) |
| .await |
| } |
|
|
| #[turbo_tasks::value] |
| struct ChangingInput { |
| state: State<u32>, |
| } |
|
|
| #[turbo_tasks::function] |
| async fn spawns_detached_changing( |
| sender: TransientInstance<WatchSenderTaskInput<Option<Vc<u32>>>>, |
| changing_input_detached: Vc<ChangingInput>, |
| changing_input_outer: Vc<ChangingInput>, |
| ) -> Vc<u32> { |
| let tt = turbo_tasks(); |
| tokio::spawn(tt.clone().detached_for_testing(Box::pin(async move { |
| sleep(Duration::from_millis(100)).await; |
| |
| tokio::spawn(tt.clone().detached_for_testing(Box::pin(async move { |
| sleep(Duration::from_millis(100)).await; |
| |
| |
| sender |
| .0 |
| .send(Some(Vc::cell( |
| *read_changing_input(changing_input_detached).await.unwrap(), |
| ))) |
| .unwrap(); |
| Ok(()) |
| }))); |
| Ok(()) |
| }))); |
| Vc::cell(*read_changing_input(changing_input_outer).await.unwrap()) |
| } |
|
|
| |
| #[turbo_tasks::function] |
| async fn read_changing_input(changing_input: Vc<ChangingInput>) -> Vc<u32> { |
| |
| Vc::cell(*changing_input.await.unwrap().state.get()) |
| } |
|
|