| 1 |
#![allow(unknown_lints, unexpected_cfgs)] |
| 2 |
#![warn(rust_2018_idioms)] |
| 3 |
#![cfg(feature = "full")] |
| 4 |
|
| 5 |
use futures::{ |
| 6 |
future::{pending, ready}, |
| 7 |
FutureExt, |
| 8 |
}; |
| 9 |
|
| 10 |
use tokio::runtime; |
| 11 |
use tokio::sync::{mpsc, oneshot}; |
| 12 |
use tokio::task::{self, LocalSet}; |
| 13 |
use tokio::time; |
| 14 |
|
| 15 |
#[cfg(not(target_os = "wasi"))] |
| 16 |
use std::cell::Cell; |
| 17 |
use std::sync::atomic::AtomicBool; |
| 18 |
#[cfg(not(target_os = "wasi"))] |
| 19 |
use std::sync::atomic::AtomicUsize; |
| 20 |
use std::sync::atomic::Ordering; |
| 21 |
#[cfg(not(target_os = "wasi"))] |
| 22 |
use std::sync::atomic::Ordering::SeqCst; |
| 23 |
use std::time::Duration; |
| 24 |
|
| 25 |
#[tokio::test(flavor = "current_thread")] |
| 26 |
async fn local_current_thread_scheduler() { |
| 27 |
LocalSet::new() |
| 28 |
.run_until(async { |
| 29 |
task::spawn_local(async {}).await.unwrap(); |
| 30 |
}) |
| 31 |
.await; |
| 32 |
} |
| 33 |
|
| 34 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 35 |
#[tokio::test(flavor = "multi_thread")] |
| 36 |
async fn local_threadpool() { |
| 37 |
thread_local! { |
| 38 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 39 |
} |
| 40 |
|
| 41 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 42 |
|
| 43 |
LocalSet::new() |
| 44 |
.run_until(async { |
| 45 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 46 |
task::spawn_local(async { |
| 47 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 48 |
}) |
| 49 |
.await |
| 50 |
.unwrap(); |
| 51 |
}) |
| 52 |
.await; |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 56 |
#[tokio::test(flavor = "multi_thread")] |
| 57 |
async fn localset_future_threadpool() { |
| 58 |
thread_local! { |
| 59 |
static ON_LOCAL_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 60 |
} |
| 61 |
|
| 62 |
ON_LOCAL_THREAD.with(|cell| cell.set(true)); |
| 63 |
|
| 64 |
let local = LocalSet::new(); |
| 65 |
local.spawn_local(async move { |
| 66 |
assert!(ON_LOCAL_THREAD.with(|cell| cell.get())); |
| 67 |
}); |
| 68 |
local.await; |
| 69 |
} |
| 70 |
|
| 71 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 72 |
#[tokio::test(flavor = "multi_thread")] |
| 73 |
async fn localset_future_timers() { |
| 74 |
static RAN1: AtomicBool = AtomicBool::new(false); |
| 75 |
static RAN2: AtomicBool = AtomicBool::new(false); |
| 76 |
|
| 77 |
let local = LocalSet::new(); |
| 78 |
local.spawn_local(async move { |
| 79 |
time::sleep(Duration::from_millis(5)).await; |
| 80 |
RAN1.store(true, Ordering::SeqCst); |
| 81 |
}); |
| 82 |
local.spawn_local(async move { |
| 83 |
time::sleep(Duration::from_millis(10)).await; |
| 84 |
RAN2.store(true, Ordering::SeqCst); |
| 85 |
}); |
| 86 |
local.await; |
| 87 |
assert!(RAN1.load(Ordering::SeqCst)); |
| 88 |
assert!(RAN2.load(Ordering::SeqCst)); |
| 89 |
} |
| 90 |
|
| 91 |
#[tokio::test] |
| 92 |
async fn localset_future_drives_all_local_futs() { |
| 93 |
static RAN1: AtomicBool = AtomicBool::new(false); |
| 94 |
static RAN2: AtomicBool = AtomicBool::new(false); |
| 95 |
static RAN3: AtomicBool = AtomicBool::new(false); |
| 96 |
|
| 97 |
let local = LocalSet::new(); |
| 98 |
local.spawn_local(async move { |
| 99 |
task::spawn_local(async { |
| 100 |
task::yield_now().await; |
| 101 |
RAN3.store(true, Ordering::SeqCst); |
| 102 |
}); |
| 103 |
task::yield_now().await; |
| 104 |
RAN1.store(true, Ordering::SeqCst); |
| 105 |
}); |
| 106 |
local.spawn_local(async move { |
| 107 |
task::yield_now().await; |
| 108 |
RAN2.store(true, Ordering::SeqCst); |
| 109 |
}); |
| 110 |
local.await; |
| 111 |
assert!(RAN1.load(Ordering::SeqCst)); |
| 112 |
assert!(RAN2.load(Ordering::SeqCst)); |
| 113 |
assert!(RAN3.load(Ordering::SeqCst)); |
| 114 |
} |
| 115 |
|
| 116 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 117 |
#[tokio::test(flavor = "multi_thread")] |
| 118 |
async fn local_threadpool_timer() { |
| 119 |
// This test ensures that runtime services like the timer are properly |
| 120 |
// set for the local task set. |
| 121 |
thread_local! { |
| 122 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 123 |
} |
| 124 |
|
| 125 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 126 |
|
| 127 |
LocalSet::new() |
| 128 |
.run_until(async { |
| 129 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 130 |
let join = task::spawn_local(async move { |
| 131 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 132 |
time::sleep(Duration::from_millis(10)).await; |
| 133 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 134 |
}); |
| 135 |
join.await.unwrap(); |
| 136 |
}) |
| 137 |
.await; |
| 138 |
} |
| 139 |
#[test] |
| 140 |
fn enter_guard_spawn() { |
| 141 |
let local = LocalSet::new(); |
| 142 |
let _guard = local.enter(); |
| 143 |
// Run the local task set. |
| 144 |
|
| 145 |
let join = task::spawn_local(async { true }); |
| 146 |
let rt = runtime::Builder::new_current_thread() |
| 147 |
.enable_all() |
| 148 |
.build() |
| 149 |
.unwrap(); |
| 150 |
local.block_on(&rt, async move { |
| 151 |
assert!(join.await.unwrap()); |
| 152 |
}); |
| 153 |
} |
| 154 |
|
| 155 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support panic recovery |
| 156 |
#[test] |
| 157 |
// This will panic, since the thread that calls `block_on` cannot use |
| 158 |
// in-place blocking inside of `block_on`. |
| 159 |
#[should_panic] |
| 160 |
fn local_threadpool_blocking_in_place() { |
| 161 |
thread_local! { |
| 162 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 163 |
} |
| 164 |
|
| 165 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 166 |
|
| 167 |
let rt = runtime::Builder::new_current_thread() |
| 168 |
.enable_all() |
| 169 |
.build() |
| 170 |
.unwrap(); |
| 171 |
LocalSet::new().block_on(&rt, async { |
| 172 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 173 |
let join = task::spawn_local(async move { |
| 174 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 175 |
task::block_in_place(|| {}); |
| 176 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 177 |
}); |
| 178 |
join.await.unwrap(); |
| 179 |
}); |
| 180 |
} |
| 181 |
|
| 182 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 183 |
#[tokio::test(flavor = "multi_thread")] |
| 184 |
async fn local_threadpool_blocking_run() { |
| 185 |
thread_local! { |
| 186 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 187 |
} |
| 188 |
|
| 189 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 190 |
|
| 191 |
LocalSet::new() |
| 192 |
.run_until(async { |
| 193 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 194 |
let join = task::spawn_local(async move { |
| 195 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 196 |
task::spawn_blocking(|| { |
| 197 |
assert!( |
| 198 |
!ON_RT_THREAD.with(|cell| cell.get()), |
| 199 |
"blocking must not run on the local task set's thread" |
| 200 |
); |
| 201 |
}) |
| 202 |
.await |
| 203 |
.unwrap(); |
| 204 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 205 |
}); |
| 206 |
join.await.unwrap(); |
| 207 |
}) |
| 208 |
.await; |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 212 |
#[tokio::test(flavor = "multi_thread")] |
| 213 |
async fn all_spawns_are_local() { |
| 214 |
use futures::future; |
| 215 |
thread_local! { |
| 216 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 217 |
} |
| 218 |
|
| 219 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 220 |
|
| 221 |
LocalSet::new() |
| 222 |
.run_until(async { |
| 223 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 224 |
let handles = (0..128) |
| 225 |
.map(|_| { |
| 226 |
task::spawn_local(async { |
| 227 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 228 |
}) |
| 229 |
}) |
| 230 |
.collect::<Vec<_>>(); |
| 231 |
for joined in future::join_all(handles).await { |
| 232 |
joined.unwrap(); |
| 233 |
} |
| 234 |
}) |
| 235 |
.await; |
| 236 |
} |
| 237 |
|
| 238 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 239 |
#[tokio::test(flavor = "multi_thread")] |
| 240 |
async fn nested_spawn_is_local() { |
| 241 |
thread_local! { |
| 242 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 243 |
} |
| 244 |
|
| 245 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 246 |
|
| 247 |
LocalSet::new() |
| 248 |
.run_until(async { |
| 249 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 250 |
task::spawn_local(async { |
| 251 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 252 |
task::spawn_local(async { |
| 253 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 254 |
task::spawn_local(async { |
| 255 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 256 |
task::spawn_local(async { |
| 257 |
assert!(ON_RT_THREAD.with(|cell| cell.get())); |
| 258 |
}) |
| 259 |
.await |
| 260 |
.unwrap(); |
| 261 |
}) |
| 262 |
.await |
| 263 |
.unwrap(); |
| 264 |
}) |
| 265 |
.await |
| 266 |
.unwrap(); |
| 267 |
}) |
| 268 |
.await |
| 269 |
.unwrap(); |
| 270 |
}) |
| 271 |
.await; |
| 272 |
} |
| 273 |
|
| 274 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 275 |
#[test] |
| 276 |
fn join_local_future_elsewhere() { |
| 277 |
thread_local! { |
| 278 |
static ON_RT_THREAD: Cell<bool> = const { Cell::new(false) }; |
| 279 |
} |
| 280 |
|
| 281 |
ON_RT_THREAD.with(|cell| cell.set(true)); |
| 282 |
|
| 283 |
let rt = runtime::Runtime::new().unwrap(); |
| 284 |
let local = LocalSet::new(); |
| 285 |
local.block_on(&rt, async move { |
| 286 |
let (tx, rx) = oneshot::channel(); |
| 287 |
let join = task::spawn_local(async move { |
| 288 |
assert!( |
| 289 |
ON_RT_THREAD.with(|cell| cell.get()), |
| 290 |
"local task must run on local thread, no matter where it is awaited" |
| 291 |
); |
| 292 |
rx.await.unwrap(); |
| 293 |
|
| 294 |
"hello world" |
| 295 |
}); |
| 296 |
let join2 = task::spawn(async move { |
| 297 |
assert!( |
| 298 |
!ON_RT_THREAD.with(|cell| cell.get()), |
| 299 |
"spawned task should be on a worker" |
| 300 |
); |
| 301 |
|
| 302 |
tx.send(()).expect("task shouldn't have ended yet"); |
| 303 |
|
| 304 |
join.await.expect("task should complete successfully"); |
| 305 |
}); |
| 306 |
join2.await.unwrap() |
| 307 |
}); |
| 308 |
} |
| 309 |
|
| 310 |
// Tests for <https://github.com/tokio-rs/tokio/issues/4973> |
| 311 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 312 |
#[tokio::test(flavor = "multi_thread")] |
| 313 |
async fn localset_in_thread_local() { |
| 314 |
thread_local! { |
| 315 |
static LOCAL_SET: LocalSet = LocalSet::new(); |
| 316 |
} |
| 317 |
|
| 318 |
// holds runtime thread until end of main fn. |
| 319 |
let (_tx, rx) = oneshot::channel::<()>(); |
| 320 |
let handle = tokio::runtime::Handle::current(); |
| 321 |
|
| 322 |
std::thread::spawn(move || { |
| 323 |
LOCAL_SET.with(|local_set| { |
| 324 |
handle.block_on(local_set.run_until(async move { |
| 325 |
let _ = rx.await; |
| 326 |
})) |
| 327 |
}); |
| 328 |
}); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn drop_cancels_tasks() { |
| 333 |
use std::rc::Rc; |
| 334 |
|
| 335 |
// This test reproduces issue #1842 |
| 336 |
let rt = rt(); |
| 337 |
let rc1 = Rc::new(()); |
| 338 |
let rc2 = rc1.clone(); |
| 339 |
|
| 340 |
let (started_tx, started_rx) = oneshot::channel(); |
| 341 |
|
| 342 |
let local = LocalSet::new(); |
| 343 |
local.spawn_local(async move { |
| 344 |
// Move this in |
| 345 |
let _rc2 = rc2; |
| 346 |
|
| 347 |
started_tx.send(()).unwrap(); |
| 348 |
futures::future::pending::<()>().await; |
| 349 |
}); |
| 350 |
|
| 351 |
local.block_on(&rt, async { |
| 352 |
started_rx.await.unwrap(); |
| 353 |
}); |
| 354 |
drop(local); |
| 355 |
drop(rt); |
| 356 |
|
| 357 |
assert_eq!(1, Rc::strong_count(&rc1)); |
| 358 |
} |
| 359 |
|
| 360 |
/// Runs a test function in a separate thread, and panics if the test does not |
| 361 |
/// complete within the specified timeout, or if the test function panics. |
| 362 |
/// |
| 363 |
/// This is intended for running tests whose failure mode is a hang or infinite |
| 364 |
/// loop that cannot be detected otherwise. |
| 365 |
fn with_timeout(timeout: Duration, f: impl FnOnce() + Send + 'static) { |
| 366 |
use std::sync::mpsc::RecvTimeoutError; |
| 367 |
|
| 368 |
let (done_tx, done_rx) = std::sync::mpsc::channel(); |
| 369 |
let thread = std::thread::spawn(move || { |
| 370 |
f(); |
| 371 |
|
| 372 |
// Send a message on the channel so that the test thread can |
| 373 |
// determine if we have entered an infinite loop: |
| 374 |
done_tx.send(()).unwrap(); |
| 375 |
}); |
| 376 |
|
| 377 |
// Since the failure mode of this test is an infinite loop, rather than |
| 378 |
// something we can easily make assertions about, we'll run it in a |
| 379 |
// thread. When the test thread finishes, it will send a message on a |
| 380 |
// channel to this thread. We'll wait for that message with a fairly |
| 381 |
// generous timeout, and if we don't receive it, we assume the test |
| 382 |
// thread has hung. |
| 383 |
// |
| 384 |
// Note that it should definitely complete in under a minute, but just |
| 385 |
// in case CI is slow, we'll give it a long timeout. |
| 386 |
match done_rx.recv_timeout(timeout) { |
| 387 |
Err(RecvTimeoutError::Timeout) => panic!( |
| 388 |
"test did not complete within {:?} seconds, \ |
| 389 |
we have (probably) entered an infinite loop!", |
| 390 |
timeout, |
| 391 |
), |
| 392 |
// Did the test thread panic? We'll find out for sure when we `join` |
| 393 |
// with it. |
| 394 |
Err(RecvTimeoutError::Disconnected) => {} |
| 395 |
// Test completed successfully! |
| 396 |
Ok(()) => {} |
| 397 |
} |
| 398 |
|
| 399 |
thread.join().expect("test thread should not panic!") |
| 400 |
} |
| 401 |
|
| 402 |
#[cfg_attr( |
| 403 |
target_os = "wasi", |
| 404 |
ignore = "`unwrap()` in `with_timeout()` panics on Wasi" |
| 405 |
)] |
| 406 |
#[test] |
| 407 |
fn drop_cancels_remote_tasks() { |
| 408 |
// This test reproduces issue #1885. |
| 409 |
with_timeout(Duration::from_secs(60), || { |
| 410 |
let (tx, mut rx) = mpsc::channel::<()>(1024); |
| 411 |
|
| 412 |
let rt = rt(); |
| 413 |
|
| 414 |
let local = LocalSet::new(); |
| 415 |
local.spawn_local(async move { while rx.recv().await.is_some() {} }); |
| 416 |
local.block_on(&rt, async { |
| 417 |
time::sleep(Duration::from_millis(1)).await; |
| 418 |
}); |
| 419 |
|
| 420 |
drop(tx); |
| 421 |
|
| 422 |
// This enters an infinite loop if the remote notified tasks are not |
| 423 |
// properly cancelled. |
| 424 |
drop(local); |
| 425 |
}); |
| 426 |
} |
| 427 |
|
| 428 |
#[cfg_attr( |
| 429 |
target_os = "wasi", |
| 430 |
ignore = "FIXME: `task::spawn_local().await.unwrap()` panics on Wasi" |
| 431 |
)] |
| 432 |
#[test] |
| 433 |
fn local_tasks_wake_join_all() { |
| 434 |
// This test reproduces issue #2460. |
| 435 |
with_timeout(Duration::from_secs(60), || { |
| 436 |
use futures::future::join_all; |
| 437 |
use tokio::task::LocalSet; |
| 438 |
|
| 439 |
let rt = rt(); |
| 440 |
let set = LocalSet::new(); |
| 441 |
let mut handles = Vec::new(); |
| 442 |
|
| 443 |
for _ in 1..=128 { |
| 444 |
handles.push(set.spawn_local(async move { |
| 445 |
tokio::task::spawn_local(async move {}).await.unwrap(); |
| 446 |
})); |
| 447 |
} |
| 448 |
|
| 449 |
rt.block_on(set.run_until(join_all(handles))); |
| 450 |
}); |
| 451 |
} |
| 452 |
|
| 453 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support panic recovery |
| 454 |
#[test] |
| 455 |
fn local_tasks_are_polled_after_tick() { |
| 456 |
// This test depends on timing, so we run it up to five times. |
| 457 |
for _ in 0..4 { |
| 458 |
let res = std::panic::catch_unwind(local_tasks_are_polled_after_tick_inner); |
| 459 |
if res.is_ok() { |
| 460 |
// success |
| 461 |
return; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// Test failed 4 times. Try one more time without catching panics. If it |
| 466 |
// fails again, the test fails. |
| 467 |
local_tasks_are_polled_after_tick_inner(); |
| 468 |
} |
| 469 |
|
| 470 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support panic recovery |
| 471 |
#[tokio::main(flavor = "current_thread")] |
| 472 |
async fn local_tasks_are_polled_after_tick_inner() { |
| 473 |
// Reproduces issues #1899 and #1900 |
| 474 |
|
| 475 |
static RX1: AtomicUsize = AtomicUsize::new(0); |
| 476 |
static RX2: AtomicUsize = AtomicUsize::new(0); |
| 477 |
const EXPECTED: usize = 500; |
| 478 |
|
| 479 |
RX1.store(0, SeqCst); |
| 480 |
RX2.store(0, SeqCst); |
| 481 |
|
| 482 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 483 |
|
| 484 |
let local = LocalSet::new(); |
| 485 |
|
| 486 |
local |
| 487 |
.run_until(async { |
| 488 |
let task2 = task::spawn(async move { |
| 489 |
// Wait a bit |
| 490 |
time::sleep(Duration::from_millis(10)).await; |
| 491 |
|
| 492 |
let mut oneshots = Vec::with_capacity(EXPECTED); |
| 493 |
|
| 494 |
// Send values |
| 495 |
for _ in 0..EXPECTED { |
| 496 |
let (oneshot_tx, oneshot_rx) = oneshot::channel(); |
| 497 |
oneshots.push(oneshot_tx); |
| 498 |
tx.send(oneshot_rx).unwrap(); |
| 499 |
} |
| 500 |
|
| 501 |
time::sleep(Duration::from_millis(10)).await; |
| 502 |
|
| 503 |
for tx in oneshots.drain(..) { |
| 504 |
tx.send(()).unwrap(); |
| 505 |
} |
| 506 |
|
| 507 |
loop { |
| 508 |
time::sleep(Duration::from_millis(20)).await; |
| 509 |
let rx1 = RX1.load(SeqCst); |
| 510 |
let rx2 = RX2.load(SeqCst); |
| 511 |
|
| 512 |
if rx1 == EXPECTED && rx2 == EXPECTED { |
| 513 |
break; |
| 514 |
} |
| 515 |
} |
| 516 |
}); |
| 517 |
|
| 518 |
while let Some(oneshot) = rx.recv().await { |
| 519 |
RX1.fetch_add(1, SeqCst); |
| 520 |
|
| 521 |
task::spawn_local(async move { |
| 522 |
oneshot.await.unwrap(); |
| 523 |
RX2.fetch_add(1, SeqCst); |
| 524 |
}); |
| 525 |
} |
| 526 |
|
| 527 |
task2.await.unwrap(); |
| 528 |
}) |
| 529 |
.await; |
| 530 |
} |
| 531 |
|
| 532 |
#[tokio::test] |
| 533 |
async fn acquire_mutex_in_drop() { |
| 534 |
use futures::future::pending; |
| 535 |
|
| 536 |
let (tx1, rx1) = oneshot::channel(); |
| 537 |
let (tx2, rx2) = oneshot::channel(); |
| 538 |
let local = LocalSet::new(); |
| 539 |
|
| 540 |
local.spawn_local(async move { |
| 541 |
let _ = rx2.await; |
| 542 |
unreachable!(); |
| 543 |
}); |
| 544 |
|
| 545 |
local.spawn_local(async move { |
| 546 |
let _ = rx1.await; |
| 547 |
tx2.send(()).unwrap(); |
| 548 |
unreachable!(); |
| 549 |
}); |
| 550 |
|
| 551 |
// Spawn a task that will never notify |
| 552 |
local.spawn_local(async move { |
| 553 |
pending::<()>().await; |
| 554 |
tx1.send(()).unwrap(); |
| 555 |
}); |
| 556 |
|
| 557 |
// Tick the loop |
| 558 |
local |
| 559 |
.run_until(async { |
| 560 |
task::yield_now().await; |
| 561 |
}) |
| 562 |
.await; |
| 563 |
|
| 564 |
// Drop the LocalSet |
| 565 |
drop(local); |
| 566 |
} |
| 567 |
|
| 568 |
#[tokio::test] |
| 569 |
async fn spawn_wakes_localset() { |
| 570 |
let local = LocalSet::new(); |
| 571 |
futures::select! { |
| 572 |
_ = local.run_until(pending::<()>()).fuse() => unreachable!(), |
| 573 |
ret = async { local.spawn_local(ready(())).await.unwrap()}.fuse() => ret |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/// Checks that the task wakes up with `enter`. |
| 578 |
/// Reproduces <https://github.com/tokio-rs/tokio/issues/5020>. |
| 579 |
#[tokio::test] |
| 580 |
async fn sleep_with_local_enter_guard() { |
| 581 |
let local = LocalSet::new(); |
| 582 |
let _guard = local.enter(); |
| 583 |
|
| 584 |
let (tx, rx) = oneshot::channel(); |
| 585 |
|
| 586 |
local |
| 587 |
.run_until(async move { |
| 588 |
tokio::task::spawn_local(async move { |
| 589 |
time::sleep(Duration::ZERO).await; |
| 590 |
|
| 591 |
tx.send(()).expect("failed to send"); |
| 592 |
}); |
| 593 |
assert_eq!(rx.await, Ok(())); |
| 594 |
}) |
| 595 |
.await; |
| 596 |
} |
| 597 |
|
| 598 |
#[test] |
| 599 |
fn store_local_set_in_thread_local_with_runtime() { |
| 600 |
use tokio::runtime::Runtime; |
| 601 |
|
| 602 |
thread_local! { |
| 603 |
static CURRENT: RtAndLocalSet = RtAndLocalSet::new(); |
| 604 |
} |
| 605 |
|
| 606 |
struct RtAndLocalSet { |
| 607 |
rt: Runtime, |
| 608 |
local: LocalSet, |
| 609 |
} |
| 610 |
|
| 611 |
impl RtAndLocalSet { |
| 612 |
fn new() -> RtAndLocalSet { |
| 613 |
RtAndLocalSet { |
| 614 |
rt: tokio::runtime::Builder::new_current_thread() |
| 615 |
.enable_all() |
| 616 |
.build() |
| 617 |
.unwrap(), |
| 618 |
local: LocalSet::new(), |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
async fn inner_method(&self) { |
| 623 |
self.local |
| 624 |
.run_until(async move { |
| 625 |
tokio::task::spawn_local(async {}); |
| 626 |
}) |
| 627 |
.await |
| 628 |
} |
| 629 |
|
| 630 |
fn method(&self) { |
| 631 |
self.rt.block_on(self.inner_method()); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
CURRENT.with(|f| { |
| 636 |
f.method(); |
| 637 |
}); |
| 638 |
} |
| 639 |
|
| 640 |
#[cfg(tokio_unstable)] |
| 641 |
mod unstable { |
| 642 |
use tokio::runtime::UnhandledPanic; |
| 643 |
use tokio::task::LocalSet; |
| 644 |
|
| 645 |
#[tokio::test] |
| 646 |
#[should_panic( |
| 647 |
expected = "a spawned task panicked and the LocalSet is configured to shutdown on unhandled panic" |
| 648 |
)] |
| 649 |
async fn shutdown_on_panic() { |
| 650 |
LocalSet::new() |
| 651 |
.unhandled_panic(UnhandledPanic::ShutdownRuntime) |
| 652 |
.run_until(async { |
| 653 |
tokio::task::spawn_local(async { |
| 654 |
panic!("boom"); |
| 655 |
}); |
| 656 |
|
| 657 |
futures::future::pending::<()>().await; |
| 658 |
}) |
| 659 |
.await; |
| 660 |
} |
| 661 |
|
| 662 |
// This test compares that, when the task driving `run_until` has already |
| 663 |
// consumed budget, the `run_until` future has less budget than a "spawned" |
| 664 |
// task. |
| 665 |
// |
| 666 |
// "Budget" is a fuzzy metric as the Tokio runtime is able to change values |
| 667 |
// internally. This is why the test uses indirection to test this. |
| 668 |
#[tokio::test] |
| 669 |
async fn run_until_does_not_get_own_budget() { |
| 670 |
// Consume some budget |
| 671 |
tokio::task::consume_budget().await; |
| 672 |
|
| 673 |
LocalSet::new() |
| 674 |
.run_until(async { |
| 675 |
let spawned = tokio::spawn(async { |
| 676 |
let mut spawned_n = 0; |
| 677 |
|
| 678 |
{ |
| 679 |
let mut spawned = tokio_test::task::spawn(async { |
| 680 |
loop { |
| 681 |
spawned_n += 1; |
| 682 |
tokio::task::consume_budget().await; |
| 683 |
} |
| 684 |
}); |
| 685 |
// Poll once |
| 686 |
assert!(!spawned.poll().is_ready()); |
| 687 |
} |
| 688 |
|
| 689 |
spawned_n |
| 690 |
}); |
| 691 |
|
| 692 |
let mut run_until_n = 0; |
| 693 |
{ |
| 694 |
let mut run_until = tokio_test::task::spawn(async { |
| 695 |
loop { |
| 696 |
run_until_n += 1; |
| 697 |
tokio::task::consume_budget().await; |
| 698 |
} |
| 699 |
}); |
| 700 |
// Poll once |
| 701 |
assert!(!run_until.poll().is_ready()); |
| 702 |
} |
| 703 |
|
| 704 |
let spawned_n = spawned.await.unwrap(); |
| 705 |
assert_ne!(spawned_n, 0); |
| 706 |
assert_ne!(run_until_n, 0); |
| 707 |
assert!(spawned_n > run_until_n); |
| 708 |
}) |
| 709 |
.await |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
fn rt() -> runtime::Runtime { |
| 714 |
tokio::runtime::Builder::new_current_thread() |
| 715 |
.enable_all() |
| 716 |
.build() |
| 717 |
.unwrap() |
| 718 |
} |