File size: 7,675 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | #![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
use std::future::IntoFuture;
use anyhow::Result;
use once_cell::sync::Lazy;
use regex::Regex;
use serde_json::json;
use turbo_tasks::Vc;
use turbo_tasks_testing::{Registration, register, run_without_cache_check};
static REGISTRATION: Registration = register!();
#[tokio::test]
async fn test_simple_task() -> Result<()> {
run_without_cache_check(®ISTRATION, async move {
enable_stats();
for i in 0..10 {
double(i).await.unwrap();
// use cached results
double(i).await.unwrap();
}
for i in 0..5 {
double(i).await.unwrap();
}
assert_eq!(
stats_json(),
json!({
"double": {
"cache_miss": 10,
"cache_hit": 15,
},
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_await_same_vc_multiple_times() -> Result<()> {
run_without_cache_check(®ISTRATION, async move {
enable_stats();
let dvc = double(0);
// this is awaited multiple times, but only resolved once
tokio::try_join!(dvc.into_future(), dvc.into_future()).unwrap();
dvc.await.unwrap();
assert_eq!(
stats_json(),
json!({
"double": {
"cache_miss": 1,
"cache_hit": 0,
},
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_vc_receiving_task() -> Result<()> {
run_without_cache_check(®ISTRATION, async move {
enable_stats();
for i in 0..10 {
let dvc = double(i);
double_vc(dvc).await.unwrap();
// use cached results
double_vc(dvc).await.unwrap();
}
for i in 0..5 {
let dvc = double(i);
double_vc(dvc).await.unwrap();
}
assert_eq!(
stats_json(),
json!({
"double": {
"cache_miss": 10,
"cache_hit": 5,
},
"double_vc": {
"cache_miss": 10,
"cache_hit": 15,
},
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_trait_methods() -> Result<()> {
run_without_cache_check(®ISTRATION, async move {
enable_stats();
for i in 0..10 {
let wvc = wrap(i);
tokio::try_join!(wvc.double().into_future(), wvc.double().into_future()).unwrap();
tokio::try_join!(wvc.double_vc().into_future(), wvc.double_vc().into_future()).unwrap();
}
// use cached results
for i in 0..5 {
let wvc = wrap(i);
wvc.double().await.unwrap();
wvc.double_vc().await.unwrap();
}
assert_eq!(
stats_json(),
json!({
"wrap": {
"cache_miss": 10,
"cache_hit": 5,
},
"WrappedU64::Doublable::double": {
"cache_miss": 10,
"cache_hit": 15,
},
"WrappedU64::Doublable::double_vc": {
"cache_miss": 10,
"cache_hit": 15,
},
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_dyn_trait_methods() -> Result<()> {
run_without_cache_check(®ISTRATION, async move {
enable_stats();
for i in 0..10 {
let wvc: Vc<Box<dyn Doublable>> = Vc::upcast(wrap(i));
let _ = tokio::try_join!(wvc.double().resolve(), wvc.double().resolve()).unwrap();
let _ = tokio::try_join!(wvc.double_vc().resolve(), wvc.double_vc().resolve()).unwrap();
}
// use cached results
for i in 0..5 {
let wvc: Vc<Box<dyn Doublable>> = Vc::upcast(wrap(i));
let _ = wvc.double().resolve().await.unwrap();
let _ = wvc.double_vc().resolve().await.unwrap();
}
// use cached results without dynamic dispatch
for i in 0..2 {
let wvc = wrap(i);
let _ = wvc.double().await.unwrap();
let _ = wvc.double_vc().await.unwrap();
}
assert_eq!(
stats_json(),
json!({
"wrap": {
"cache_miss": 10,
"cache_hit": 7,
},
"WrappedU64::Doublable::double": {
"cache_miss": 10,
"cache_hit": 17,
},
"WrappedU64::Doublable::double_vc": {
"cache_miss": 10,
"cache_hit": 17,
},
})
);
Ok(())
})
.await
}
// creates Vcs, but doesn't ever execute them
#[tokio::test]
async fn test_no_execution() -> Result<()> {
run_without_cache_check(®ISTRATION, async move {
enable_stats();
// don't await this!
let _ = wrap_vc(double_vc(double(123))).double().double_vc();
assert_eq!(
stats_json(),
json!({
"double": {
"cache_miss": 1,
"cache_hit": 0,
},
})
);
Ok(())
})
.await
}
// Internally, this function uses `PersistentTaskType`.
#[turbo_tasks::function]
fn double(val: u64) -> Vc<u64> {
Vc::cell(val * 2)
}
// Internally, this function uses `LocalTaskType::ResolveNative`.
#[turbo_tasks::function]
async fn double_vc(val: Vc<u64>) -> Result<Vc<u64>> {
let val = *val.await?;
Ok(Vc::cell(val * 2))
}
#[turbo_tasks::value]
struct WrappedU64(u64);
#[turbo_tasks::function]
fn wrap(val: u64) -> Vc<WrappedU64> {
WrappedU64(val).cell()
}
#[turbo_tasks::function]
async fn wrap_vc(val: Vc<u64>) -> Result<Vc<WrappedU64>> {
Ok(WrappedU64(*val.await?).cell())
}
#[turbo_tasks::value_trait]
pub trait Doublable {
#[turbo_tasks::function]
fn double(&self) -> Vc<Self>;
#[turbo_tasks::function]
fn double_vc(self: Vc<Self>) -> Vc<Self>;
}
#[turbo_tasks::value_impl]
impl Doublable for WrappedU64 {
#[turbo_tasks::function]
fn double(&self) -> Vc<Self> {
WrappedU64(self.0 * 2).cell()
}
#[turbo_tasks::function]
fn double_vc(&self) -> Result<Vc<Self>> {
let val = self.0;
Ok(WrappedU64(val * 2).cell())
}
}
#[turbo_tasks::function]
fn fail(val: u64) -> Result<Vc<()>> {
anyhow::bail!("failed using {val}");
}
fn enable_stats() {
let tt = turbo_tasks::turbo_tasks();
tt.task_statistics().enable();
}
fn stats_json() -> serde_json::Value {
let tt = turbo_tasks::turbo_tasks();
remove_crate_and_hashes(serde_json::to_value(tt.task_statistics().get()).unwrap())
}
// Global task identifiers can contain a hash of the crate and dependencies.
// Remove that so that we can compare against a stable value in tests.
fn remove_crate_and_hashes(mut json: serde_json::Value) -> serde_json::Value {
static HASH_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^[^:@]+@[^:]+:+").unwrap());
match &mut json {
serde_json::Value::Object(map) => {
let old_map = std::mem::take(map);
for (k, v) in old_map {
map.insert(HASH_RE.replace(&k, "").into_owned(), v);
}
}
_ => unreachable!("expected object"),
};
json
}
|