-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add/move tests for Vc generics #8843
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
#![feature(arbitrary_self_types)] | ||
|
||
use std::sync::{Arc, Mutex}; | ||
|
||
use indexmap::{IndexMap, IndexSet}; | ||
use turbo_tasks::{debug::ValueDebug, Invalidator, ReadRef, TaskId, Vc}; | ||
use turbo_tasks_testing::{register, run, Registration}; | ||
|
||
static REGISTRATION: Registration = register!(); | ||
|
||
#[tokio::test] | ||
async fn test_option_some() { | ||
run(®ISTRATION, async move { | ||
let vc_42 = Vc::cell(42); | ||
let option: Vc<Option<Vc<u32>>> = Vc::cell(Some(vc_42)); | ||
assert_eq!(*option.is_some().await.unwrap(), true); | ||
assert_eq!(*option.is_none().await.unwrap(), false); | ||
assert_eq!(&*option.await.unwrap(), &Some(vc_42)); | ||
assert_eq!(option.dbg().await.unwrap().to_string(), "Some(\n 42,\n)"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_option_none() { | ||
run(®ISTRATION, async move { | ||
let option: Vc<Option<Vc<u32>>> = Default::default(); | ||
assert_eq!(*option.is_some().await.unwrap(), false); | ||
assert_eq!(*option.is_none().await.unwrap(), true); | ||
assert_eq!(&*option.await.unwrap(), &None); | ||
assert_eq!(option.dbg().await.unwrap().to_string(), "None"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_vec() { | ||
run(®ISTRATION, async move { | ||
let vc_42 = Vc::cell(42); | ||
let vec: Vc<Vec<Vc<u32>>> = Vc::cell(vec![vc_42]); | ||
assert_eq!(*vec.len().await.unwrap(), 1); | ||
assert_eq!(*vec.is_empty().await.unwrap(), false); | ||
assert_eq!(&*vec.await.unwrap(), &[vc_42]); | ||
assert_eq!(vec.dbg().await.unwrap().to_string(), "[\n 42,\n]"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_empty_vec() { | ||
run(®ISTRATION, async move { | ||
let vec: Vc<Vec<Vc<u32>>> = Default::default(); | ||
assert_eq!(*vec.len().await.unwrap(), 0); | ||
assert_eq!(*vec.is_empty().await.unwrap(), true); | ||
assert_eq!(vec.dbg().await.unwrap().to_string(), "[]"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_nested_empty_vec() { | ||
run(®ISTRATION, async move { | ||
let vec: Vc<Vec<Vc<Vec<Vc<u32>>>>> = Default::default(); | ||
assert_eq!(*vec.len().await.unwrap(), 0); | ||
assert_eq!(vec.dbg().await.unwrap().to_string(), "[]"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_index_set() { | ||
run(®ISTRATION, async move { | ||
let vc_42 = Vc::cell(42); | ||
let set: Vc<IndexSet<Vc<u32>>> = Vc::cell(IndexSet::from([vc_42])); | ||
assert_eq!(*set.len().await.unwrap(), 1); | ||
assert_eq!(*set.is_empty().await.unwrap(), false); | ||
assert_eq!(&*set.await.unwrap(), &IndexSet::from([vc_42])); | ||
assert_eq!(set.dbg().await.unwrap().to_string(), "{\n 42,\n}"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_empty_index_set() { | ||
run(®ISTRATION, async move { | ||
let set: Vc<IndexSet<Vc<u32>>> = Default::default(); | ||
assert_eq!(*set.len().await.unwrap(), 0); | ||
assert_eq!(*set.is_empty().await.unwrap(), true); | ||
assert_eq!(&*set.await.unwrap(), &IndexSet::<Vc<u32>>::default()); | ||
assert_eq!(set.dbg().await.unwrap().to_string(), "{}"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_index_map() { | ||
run(®ISTRATION, async move { | ||
let vc_42 = Vc::cell(42); | ||
let map: Vc<IndexMap<Vc<u32>, _>> = Vc::cell(IndexMap::from([(vc_42, vc_42)])); | ||
assert_eq!(*map.len().await.unwrap(), 1); | ||
assert_eq!(*map.is_empty().await.unwrap(), false); | ||
assert_eq!(&*map.await.unwrap(), &IndexMap::from([(vc_42, vc_42)])); | ||
assert_eq!(map.dbg().await.unwrap().to_string(), "{\n 42: 42,\n}"); | ||
}) | ||
.await | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_empty_index_map() { | ||
run(®ISTRATION, async move { | ||
let map: Vc<IndexMap<Vc<u32>, Vc<u32>>> = Default::default(); | ||
assert_eq!(*map.len().await.unwrap(), 0); | ||
assert_eq!(*map.is_empty().await.unwrap(), true); | ||
assert_eq!( | ||
&*map.await.unwrap(), | ||
&IndexMap::<Vc<u32>, Vc<u32>>::default() | ||
); | ||
assert_eq!(map.dbg().await.unwrap().to_string(), "{}"); | ||
}) | ||
.await | ||
} | ||
|
||
// Simulate a non-deterministic function that stores different generic types in | ||
// it's cells each time it runs. | ||
#[tokio::test] | ||
async fn test_changing_generic() { | ||
run(®ISTRATION, async move { | ||
let state_vc = State::default().cell(); | ||
let state_ref = state_vc.await.unwrap(); | ||
for _i in 0..10 { | ||
let _ = non_deterministic(state_vc) | ||
.resolve_strongly_consistent() | ||
.await | ||
.unwrap(); | ||
state_ref | ||
.inner | ||
.lock() | ||
.unwrap() | ||
.last_invalidator | ||
.take() | ||
.unwrap() | ||
.invalidate(); | ||
} | ||
}) | ||
.await | ||
} | ||
|
||
// Test that we can convert a `Vc` to a `ReadRef`, and then back to a `Vc`. | ||
#[tokio::test] | ||
#[ignore = "TODO: This panics! There's a casting bug in the generics implementation."] | ||
async fn test_read_ref_round_trip() { | ||
run(®ISTRATION, async move { | ||
let c: Vc<Option<Vc<u8>>> = Vc::cell(Some(Vc::cell(1))); | ||
let _ = ReadRef::cell(c.await.unwrap()).await.unwrap(); | ||
}) | ||
.await | ||
} | ||
|
||
#[turbo_tasks::value(eq = "manual")] | ||
#[derive(Default)] | ||
struct State { | ||
#[turbo_tasks(debug_ignore, trace_ignore)] | ||
#[serde(skip)] | ||
inner: Arc<Mutex<StateInner>>, | ||
} | ||
|
||
#[derive(Default)] | ||
struct StateInner { | ||
branch: bool, | ||
last_invalidator: Option<Invalidator>, | ||
last_task_id: Option<TaskId>, | ||
} | ||
|
||
impl PartialEq for State { | ||
fn eq(&self, other: &Self) -> bool { | ||
std::ptr::eq(self as *const _, other as *const _) | ||
} | ||
} | ||
|
||
impl Eq for State {} | ||
|
||
#[turbo_tasks::function] | ||
async fn non_deterministic(state: Vc<State>) { | ||
let state = state.await.unwrap(); | ||
let mut state_inner = state.inner.lock().unwrap(); | ||
|
||
let task_id = if state_inner.branch { | ||
let c: Vc<Option<Vc<u8>>> = Vc::cell(Some(Vc::cell(1))); | ||
println!("u8 branch"); | ||
Vc::into_raw(c).get_task_id() | ||
} else { | ||
let c: Vc<Option<Vc<u32>>> = Vc::cell(Some(Vc::cell(1))); | ||
println!("u32 branch"); | ||
Vc::into_raw(c).get_task_id() | ||
}; | ||
|
||
state_inner.branch = !state_inner.branch; | ||
if let Some(last_task_id) = state_inner.last_task_id { | ||
assert_eq!(last_task_id, task_id); | ||
} | ||
state_inner.last_task_id = Some(task_id); | ||
state_inner.last_invalidator = Some(turbo_tasks::get_invalidator()); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This panics due to a bug in the current generics implementation. Fixed in #8845