| 1 |
#![allow(unknown_lints, unexpected_cfgs)] |
| 2 |
#![allow(clippy::needless_range_loop)] |
| 3 |
#![warn(rust_2018_idioms)] |
| 4 |
#![cfg(feature = "full")] |
| 5 |
|
| 6 |
// Tests to run on both current-thread & multi-thread runtime variants. |
| 7 |
|
| 8 |
macro_rules! rt_test { |
| 9 |
($($t:tt)*) => { |
| 10 |
mod current_thread_scheduler { |
| 11 |
$($t)* |
| 12 |
|
| 13 |
#[cfg(not(target_os="wasi"))] |
| 14 |
const NUM_WORKERS: usize = 1; |
| 15 |
|
| 16 |
fn rt() -> Arc<Runtime> { |
| 17 |
tokio::runtime::Builder::new_current_thread() |
| 18 |
.enable_all() |
| 19 |
.build() |
| 20 |
.unwrap() |
| 21 |
.into() |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 26 |
mod threaded_scheduler_4_threads { |
| 27 |
$($t)* |
| 28 |
|
| 29 |
const NUM_WORKERS: usize = 4; |
| 30 |
|
| 31 |
fn rt() -> Arc<Runtime> { |
| 32 |
tokio::runtime::Builder::new_multi_thread() |
| 33 |
.worker_threads(4) |
| 34 |
.enable_all() |
| 35 |
.build() |
| 36 |
.unwrap() |
| 37 |
.into() |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 42 |
mod threaded_scheduler_1_thread { |
| 43 |
$($t)* |
| 44 |
|
| 45 |
const NUM_WORKERS: usize = 1; |
| 46 |
|
| 47 |
fn rt() -> Arc<Runtime> { |
| 48 |
tokio::runtime::Builder::new_multi_thread() |
| 49 |
.worker_threads(1) |
| 50 |
.enable_all() |
| 51 |
.build() |
| 52 |
.unwrap() |
| 53 |
.into() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 58 |
#[cfg(tokio_unstable)] |
| 59 |
mod alt_threaded_scheduler_4_threads { |
| 60 |
$($t)* |
| 61 |
|
| 62 |
const NUM_WORKERS: usize = 4; |
| 63 |
|
| 64 |
fn rt() -> Arc<Runtime> { |
| 65 |
tokio::runtime::Builder::new_multi_thread() |
| 66 |
.worker_threads(4) |
| 67 |
.enable_all() |
| 68 |
.build() |
| 69 |
.unwrap() |
| 70 |
.into() |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#[cfg(not(target_os = "wasi"))] // Wasi doesn't support threads |
| 75 |
#[cfg(tokio_unstable)] |
| 76 |
mod alt_threaded_scheduler_1_thread { |
| 77 |
$($t)* |
| 78 |
|
| 79 |
const NUM_WORKERS: usize = 1; |
| 80 |
|
| 81 |
fn rt() -> Arc<Runtime> { |
| 82 |
tokio::runtime::Builder::new_multi_thread() |
| 83 |
.worker_threads(1) |
| 84 |
.enable_all() |
| 85 |
.build() |
| 86 |
.unwrap() |
| 87 |
.into() |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn send_sync_bound() { |
| 95 |
use tokio::runtime::Runtime; |
| 96 |
fn is_send<T: Send + Sync>() {} |
| 97 |
|
| 98 |
is_send::<Runtime>(); |
| 99 |
} |
| 100 |
|
| 101 |
rt_test! { |
| 102 |
#[cfg(not(target_os="wasi"))] |
| 103 |
use tokio::net::{TcpListener, TcpStream}; |
| 104 |
#[cfg(not(target_os="wasi"))] |
| 105 |
use tokio::io::{AsyncReadExt, AsyncWriteExt}; |
| 106 |
|
| 107 |
use tokio::runtime::Runtime; |
| 108 |
use tokio::sync::oneshot; |
| 109 |
use tokio::{task, time}; |
| 110 |
|
| 111 |
#[cfg(not(target_os="wasi"))] |
| 112 |
use tokio_test::assert_err; |
| 113 |
use tokio_test::assert_ok; |
| 114 |
|
| 115 |
use futures::future::poll_fn; |
| 116 |
use std::future::Future; |
| 117 |
use std::pin::Pin; |
| 118 |
|
| 119 |
#[cfg(not(target_os="wasi"))] |
| 120 |
use std::sync::mpsc; |
| 121 |
|
| 122 |
use std::sync::Arc; |
| 123 |
use std::task::{Context, Poll}; |
| 124 |
|
| 125 |
#[cfg(not(target_os="wasi"))] |
| 126 |
use std::thread; |
| 127 |
use std::time::{Duration, Instant}; |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn block_on_sync() { |
| 131 |
let rt = rt(); |
| 132 |
|
| 133 |
let mut win = false; |
| 134 |
rt.block_on(async { |
| 135 |
win = true; |
| 136 |
}); |
| 137 |
|
| 138 |
assert!(win); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
#[cfg(not(target_os="wasi"))] |
| 143 |
#[test] |
| 144 |
fn block_on_async() { |
| 145 |
let rt = rt(); |
| 146 |
|
| 147 |
let out = rt.block_on(async { |
| 148 |
let (tx, rx) = oneshot::channel(); |
| 149 |
|
| 150 |
thread::spawn(move || { |
| 151 |
thread::sleep(Duration::from_millis(50)); |
| 152 |
tx.send("ZOMG").unwrap(); |
| 153 |
}); |
| 154 |
|
| 155 |
assert_ok!(rx.await) |
| 156 |
}); |
| 157 |
|
| 158 |
assert_eq!(out, "ZOMG"); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn spawn_one_bg() { |
| 163 |
let rt = rt(); |
| 164 |
|
| 165 |
let out = rt.block_on(async { |
| 166 |
let (tx, rx) = oneshot::channel(); |
| 167 |
|
| 168 |
tokio::spawn(async move { |
| 169 |
tx.send("ZOMG").unwrap(); |
| 170 |
}); |
| 171 |
|
| 172 |
assert_ok!(rx.await) |
| 173 |
}); |
| 174 |
|
| 175 |
assert_eq!(out, "ZOMG"); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn spawn_one_join() { |
| 180 |
let rt = rt(); |
| 181 |
|
| 182 |
let out = rt.block_on(async { |
| 183 |
let (tx, rx) = oneshot::channel(); |
| 184 |
|
| 185 |
let handle = tokio::spawn(async move { |
| 186 |
tx.send("ZOMG").unwrap(); |
| 187 |
"DONE" |
| 188 |
}); |
| 189 |
|
| 190 |
let msg = assert_ok!(rx.await); |
| 191 |
|
| 192 |
let out = assert_ok!(handle.await); |
| 193 |
assert_eq!(out, "DONE"); |
| 194 |
|
| 195 |
msg |
| 196 |
}); |
| 197 |
|
| 198 |
assert_eq!(out, "ZOMG"); |
| 199 |
} |
| 200 |
|
| 201 |
#[test] |
| 202 |
fn spawn_two() { |
| 203 |
let rt = rt(); |
| 204 |
|
| 205 |
let out = rt.block_on(async { |
| 206 |
let (tx1, rx1) = oneshot::channel(); |
| 207 |
let (tx2, rx2) = oneshot::channel(); |
| 208 |
|
| 209 |
tokio::spawn(async move { |
| 210 |
assert_ok!(tx1.send("ZOMG")); |
| 211 |
}); |
| 212 |
|
| 213 |
tokio::spawn(async move { |
| 214 |
let msg = assert_ok!(rx1.await); |
| 215 |
assert_ok!(tx2.send(msg)); |
| 216 |
}); |
| 217 |
|
| 218 |
assert_ok!(rx2.await) |
| 219 |
}); |
| 220 |
|
| 221 |
assert_eq!(out, "ZOMG"); |
| 222 |
} |
| 223 |
|
| 224 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 225 |
#[test] |
| 226 |
fn spawn_many_from_block_on() { |
| 227 |
use tokio::sync::mpsc; |
| 228 |
|
| 229 |
const ITER: usize = 200; |
| 230 |
|
| 231 |
let rt = rt(); |
| 232 |
|
| 233 |
let out = rt.block_on(async { |
| 234 |
let (done_tx, mut done_rx) = mpsc::unbounded_channel(); |
| 235 |
|
| 236 |
let mut txs = (0..ITER) |
| 237 |
.map(|i| { |
| 238 |
let (tx, rx) = oneshot::channel(); |
| 239 |
let done_tx = done_tx.clone(); |
| 240 |
|
| 241 |
tokio::spawn(async move { |
| 242 |
let msg = assert_ok!(rx.await); |
| 243 |
assert_eq!(i, msg); |
| 244 |
assert_ok!(done_tx.send(msg)); |
| 245 |
}); |
| 246 |
|
| 247 |
tx |
| 248 |
}) |
| 249 |
.collect::<Vec<_>>(); |
| 250 |
|
| 251 |
drop(done_tx); |
| 252 |
|
| 253 |
thread::spawn(move || { |
| 254 |
for (i, tx) in txs.drain(..).enumerate() { |
| 255 |
assert_ok!(tx.send(i)); |
| 256 |
} |
| 257 |
}); |
| 258 |
|
| 259 |
let mut out = vec![]; |
| 260 |
while let Some(i) = done_rx.recv().await { |
| 261 |
out.push(i); |
| 262 |
} |
| 263 |
|
| 264 |
out.sort_unstable(); |
| 265 |
out |
| 266 |
}); |
| 267 |
|
| 268 |
assert_eq!(ITER, out.len()); |
| 269 |
|
| 270 |
for i in 0..ITER { |
| 271 |
assert_eq!(i, out[i]); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 276 |
#[test] |
| 277 |
fn spawn_many_from_task() { |
| 278 |
use tokio::sync::mpsc; |
| 279 |
|
| 280 |
const ITER: usize = 500; |
| 281 |
|
| 282 |
let rt = rt(); |
| 283 |
|
| 284 |
let out = rt.block_on(async { |
| 285 |
tokio::spawn(async move { |
| 286 |
let (done_tx, mut done_rx) = mpsc::unbounded_channel(); |
| 287 |
|
| 288 |
let mut txs = (0..ITER) |
| 289 |
.map(|i| { |
| 290 |
let (tx, rx) = oneshot::channel(); |
| 291 |
let done_tx = done_tx.clone(); |
| 292 |
|
| 293 |
tokio::spawn(async move { |
| 294 |
let msg = assert_ok!(rx.await); |
| 295 |
assert_eq!(i, msg); |
| 296 |
assert_ok!(done_tx.send(msg)); |
| 297 |
}); |
| 298 |
|
| 299 |
tx |
| 300 |
}) |
| 301 |
.collect::<Vec<_>>(); |
| 302 |
|
| 303 |
drop(done_tx); |
| 304 |
|
| 305 |
thread::spawn(move || { |
| 306 |
for (i, tx) in txs.drain(..).enumerate() { |
| 307 |
assert_ok!(tx.send(i)); |
| 308 |
} |
| 309 |
}); |
| 310 |
|
| 311 |
let mut out = vec![]; |
| 312 |
while let Some(i) = done_rx.recv().await { |
| 313 |
out.push(i); |
| 314 |
} |
| 315 |
|
| 316 |
out.sort_unstable(); |
| 317 |
out |
| 318 |
}).await.unwrap() |
| 319 |
}); |
| 320 |
|
| 321 |
assert_eq!(ITER, out.len()); |
| 322 |
|
| 323 |
for i in 0..ITER { |
| 324 |
assert_eq!(i, out[i]); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn spawn_one_from_block_on_called_on_handle() { |
| 330 |
let rt = rt(); |
| 331 |
let (tx, rx) = oneshot::channel(); |
| 332 |
|
| 333 |
#[allow(clippy::async_yields_async)] |
| 334 |
let handle = rt.handle().block_on(async { |
| 335 |
tokio::spawn(async move { |
| 336 |
tx.send("ZOMG").unwrap(); |
| 337 |
"DONE" |
| 338 |
}) |
| 339 |
}); |
| 340 |
|
| 341 |
let out = rt.block_on(async { |
| 342 |
let msg = assert_ok!(rx.await); |
| 343 |
|
| 344 |
let out = assert_ok!(handle.await); |
| 345 |
assert_eq!(out, "DONE"); |
| 346 |
|
| 347 |
msg |
| 348 |
}); |
| 349 |
|
| 350 |
assert_eq!(out, "ZOMG"); |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn spawn_await_chain() { |
| 355 |
let rt = rt(); |
| 356 |
|
| 357 |
let out = rt.block_on(async { |
| 358 |
assert_ok!(tokio::spawn(async { |
| 359 |
assert_ok!(tokio::spawn(async { |
| 360 |
"hello" |
| 361 |
}).await) |
| 362 |
}).await) |
| 363 |
}); |
| 364 |
|
| 365 |
assert_eq!(out, "hello"); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn outstanding_tasks_dropped() { |
| 370 |
let rt = rt(); |
| 371 |
|
| 372 |
let cnt = Arc::new(()); |
| 373 |
|
| 374 |
rt.block_on(async { |
| 375 |
let cnt = cnt.clone(); |
| 376 |
|
| 377 |
tokio::spawn(poll_fn(move |_| { |
| 378 |
assert_eq!(2, Arc::strong_count(&cnt)); |
| 379 |
Poll::<()>::Pending |
| 380 |
})); |
| 381 |
}); |
| 382 |
|
| 383 |
assert_eq!(2, Arc::strong_count(&cnt)); |
| 384 |
|
| 385 |
drop(rt); |
| 386 |
|
| 387 |
assert_eq!(1, Arc::strong_count(&cnt)); |
| 388 |
} |
| 389 |
|
| 390 |
#[test] |
| 391 |
#[should_panic] |
| 392 |
fn nested_rt() { |
| 393 |
let rt1 = rt(); |
| 394 |
let rt2 = rt(); |
| 395 |
|
| 396 |
rt1.block_on(async { rt2.block_on(async { "hello" }) }); |
| 397 |
} |
| 398 |
|
| 399 |
#[test] |
| 400 |
fn create_rt_in_block_on() { |
| 401 |
let rt1 = rt(); |
| 402 |
let rt2 = rt1.block_on(async { rt() }); |
| 403 |
let out = rt2.block_on(async { "ZOMG" }); |
| 404 |
|
| 405 |
assert_eq!(out, "ZOMG"); |
| 406 |
} |
| 407 |
|
| 408 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 409 |
#[test] |
| 410 |
fn complete_block_on_under_load() { |
| 411 |
let rt = rt(); |
| 412 |
|
| 413 |
rt.block_on(async { |
| 414 |
let (tx, rx) = oneshot::channel(); |
| 415 |
|
| 416 |
// Spin hard |
| 417 |
tokio::spawn(async { |
| 418 |
loop { |
| 419 |
yield_once().await; |
| 420 |
} |
| 421 |
}); |
| 422 |
|
| 423 |
thread::spawn(move || { |
| 424 |
thread::sleep(Duration::from_millis(50)); |
| 425 |
assert_ok!(tx.send(())); |
| 426 |
}); |
| 427 |
|
| 428 |
assert_ok!(rx.await); |
| 429 |
}); |
| 430 |
} |
| 431 |
|
| 432 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 433 |
#[test] |
| 434 |
fn complete_task_under_load() { |
| 435 |
let rt = rt(); |
| 436 |
|
| 437 |
rt.block_on(async { |
| 438 |
let (tx1, rx1) = oneshot::channel(); |
| 439 |
let (tx2, rx2) = oneshot::channel(); |
| 440 |
|
| 441 |
// Spin hard |
| 442 |
tokio::spawn(async { |
| 443 |
loop { |
| 444 |
yield_once().await; |
| 445 |
} |
| 446 |
}); |
| 447 |
|
| 448 |
thread::spawn(move || { |
| 449 |
thread::sleep(Duration::from_millis(50)); |
| 450 |
assert_ok!(tx1.send(())); |
| 451 |
}); |
| 452 |
|
| 453 |
tokio::spawn(async move { |
| 454 |
assert_ok!(rx1.await); |
| 455 |
assert_ok!(tx2.send(())); |
| 456 |
}); |
| 457 |
|
| 458 |
assert_ok!(rx2.await); |
| 459 |
}); |
| 460 |
} |
| 461 |
|
| 462 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 463 |
#[test] |
| 464 |
fn spawn_from_other_thread_idle() { |
| 465 |
let rt = rt(); |
| 466 |
let handle = rt.clone(); |
| 467 |
|
| 468 |
let (tx, rx) = oneshot::channel(); |
| 469 |
|
| 470 |
thread::spawn(move || { |
| 471 |
thread::sleep(Duration::from_millis(50)); |
| 472 |
|
| 473 |
handle.spawn(async move { |
| 474 |
assert_ok!(tx.send(())); |
| 475 |
}); |
| 476 |
}); |
| 477 |
|
| 478 |
rt.block_on(async move { |
| 479 |
assert_ok!(rx.await); |
| 480 |
}); |
| 481 |
} |
| 482 |
|
| 483 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 484 |
#[test] |
| 485 |
fn spawn_from_other_thread_under_load() { |
| 486 |
let rt = rt(); |
| 487 |
let handle = rt.clone(); |
| 488 |
|
| 489 |
let (tx, rx) = oneshot::channel(); |
| 490 |
|
| 491 |
thread::spawn(move || { |
| 492 |
handle.spawn(async move { |
| 493 |
assert_ok!(tx.send(())); |
| 494 |
}); |
| 495 |
}); |
| 496 |
|
| 497 |
rt.block_on(async move { |
| 498 |
// Spin hard |
| 499 |
tokio::spawn(async { |
| 500 |
loop { |
| 501 |
yield_once().await; |
| 502 |
} |
| 503 |
}); |
| 504 |
|
| 505 |
assert_ok!(rx.await); |
| 506 |
}); |
| 507 |
} |
| 508 |
|
| 509 |
#[test] |
| 510 |
fn sleep_at_root() { |
| 511 |
let rt = rt(); |
| 512 |
|
| 513 |
let now = Instant::now(); |
| 514 |
let dur = Duration::from_millis(50); |
| 515 |
|
| 516 |
rt.block_on(async move { |
| 517 |
time::sleep(dur).await; |
| 518 |
}); |
| 519 |
|
| 520 |
assert!(now.elapsed() >= dur); |
| 521 |
} |
| 522 |
|
| 523 |
#[test] |
| 524 |
fn sleep_in_spawn() { |
| 525 |
let rt = rt(); |
| 526 |
|
| 527 |
let now = Instant::now(); |
| 528 |
let dur = Duration::from_millis(50); |
| 529 |
|
| 530 |
rt.block_on(async move { |
| 531 |
let (tx, rx) = oneshot::channel(); |
| 532 |
|
| 533 |
tokio::spawn(async move { |
| 534 |
time::sleep(dur).await; |
| 535 |
assert_ok!(tx.send(())); |
| 536 |
}); |
| 537 |
|
| 538 |
assert_ok!(rx.await); |
| 539 |
}); |
| 540 |
|
| 541 |
assert!(now.elapsed() >= dur); |
| 542 |
} |
| 543 |
|
| 544 |
#[cfg(not(target_os="wasi"))] // Wasi does not support bind |
| 545 |
#[test] |
| 546 |
fn block_on_socket() { |
| 547 |
let rt = rt(); |
| 548 |
|
| 549 |
rt.block_on(async move { |
| 550 |
let (tx, rx) = oneshot::channel(); |
| 551 |
|
| 552 |
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 553 |
let addr = listener.local_addr().unwrap(); |
| 554 |
|
| 555 |
tokio::spawn(async move { |
| 556 |
let _ = listener.accept().await; |
| 557 |
tx.send(()).unwrap(); |
| 558 |
}); |
| 559 |
|
| 560 |
TcpStream::connect(&addr).await.unwrap(); |
| 561 |
rx.await.unwrap(); |
| 562 |
}); |
| 563 |
} |
| 564 |
|
| 565 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 566 |
#[test] |
| 567 |
fn spawn_from_blocking() { |
| 568 |
let rt = rt(); |
| 569 |
|
| 570 |
let out = rt.block_on(async move { |
| 571 |
let inner = assert_ok!(tokio::task::spawn_blocking(|| { |
| 572 |
tokio::spawn(async move { "hello" }) |
| 573 |
}).await); |
| 574 |
|
| 575 |
assert_ok!(inner.await) |
| 576 |
}); |
| 577 |
|
| 578 |
assert_eq!(out, "hello") |
| 579 |
} |
| 580 |
|
| 581 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 582 |
#[test] |
| 583 |
fn spawn_blocking_from_blocking() { |
| 584 |
let rt = rt(); |
| 585 |
|
| 586 |
let out = rt.block_on(async move { |
| 587 |
let inner = assert_ok!(tokio::task::spawn_blocking(|| { |
| 588 |
tokio::task::spawn_blocking(|| "hello") |
| 589 |
}).await); |
| 590 |
|
| 591 |
assert_ok!(inner.await) |
| 592 |
}); |
| 593 |
|
| 594 |
assert_eq!(out, "hello") |
| 595 |
} |
| 596 |
|
| 597 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 598 |
#[test] |
| 599 |
fn sleep_from_blocking() { |
| 600 |
let rt = rt(); |
| 601 |
|
| 602 |
rt.block_on(async move { |
| 603 |
assert_ok!(tokio::task::spawn_blocking(|| { |
| 604 |
let now = std::time::Instant::now(); |
| 605 |
let dur = Duration::from_millis(1); |
| 606 |
|
| 607 |
// use the futures' block_on fn to make sure we aren't setting |
| 608 |
// any Tokio context |
| 609 |
futures::executor::block_on(async { |
| 610 |
tokio::time::sleep(dur).await; |
| 611 |
}); |
| 612 |
|
| 613 |
assert!(now.elapsed() >= dur); |
| 614 |
}).await); |
| 615 |
}); |
| 616 |
} |
| 617 |
|
| 618 |
#[cfg(not(target_os="wasi"))] // Wasi does not support bind |
| 619 |
#[test] |
| 620 |
fn socket_from_blocking() { |
| 621 |
let rt = rt(); |
| 622 |
|
| 623 |
rt.block_on(async move { |
| 624 |
let listener = assert_ok!(TcpListener::bind("127.0.0.1:0").await); |
| 625 |
let addr = assert_ok!(listener.local_addr()); |
| 626 |
|
| 627 |
let peer = tokio::task::spawn_blocking(move || { |
| 628 |
// use the futures' block_on fn to make sure we aren't setting |
| 629 |
// any Tokio context |
| 630 |
futures::executor::block_on(async { |
| 631 |
assert_ok!(TcpStream::connect(addr).await); |
| 632 |
}); |
| 633 |
}); |
| 634 |
|
| 635 |
// Wait for the client to connect |
| 636 |
let _ = assert_ok!(listener.accept().await); |
| 637 |
|
| 638 |
assert_ok!(peer.await); |
| 639 |
}); |
| 640 |
} |
| 641 |
|
| 642 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 643 |
#[test] |
| 644 |
fn always_active_parker() { |
| 645 |
// This test it to show that we will always have |
| 646 |
// an active parker even if we call block_on concurrently |
| 647 |
|
| 648 |
let rt = rt(); |
| 649 |
let rt2 = rt.clone(); |
| 650 |
|
| 651 |
let (tx1, rx1) = oneshot::channel(); |
| 652 |
let (tx2, rx2) = oneshot::channel(); |
| 653 |
|
| 654 |
let jh1 = thread::spawn(move || { |
| 655 |
rt.block_on(async move { |
| 656 |
rx2.await.unwrap(); |
| 657 |
time::sleep(Duration::from_millis(5)).await; |
| 658 |
tx1.send(()).unwrap(); |
| 659 |
}); |
| 660 |
}); |
| 661 |
|
| 662 |
let jh2 = thread::spawn(move || { |
| 663 |
rt2.block_on(async move { |
| 664 |
tx2.send(()).unwrap(); |
| 665 |
time::sleep(Duration::from_millis(5)).await; |
| 666 |
rx1.await.unwrap(); |
| 667 |
time::sleep(Duration::from_millis(5)).await; |
| 668 |
}); |
| 669 |
}); |
| 670 |
|
| 671 |
jh1.join().unwrap(); |
| 672 |
jh2.join().unwrap(); |
| 673 |
} |
| 674 |
|
| 675 |
#[test] |
| 676 |
// IOCP requires setting the "max thread" concurrency value. The sane, |
| 677 |
// default, is to set this to the number of cores. Threads that poll I/O |
| 678 |
// become associated with the IOCP handle. Once those threads sleep for any |
| 679 |
// reason (mutex), they yield their ownership. |
| 680 |
// |
| 681 |
// This test hits an edge case on windows where more threads than cores are |
| 682 |
// created, none of those threads ever yield due to being at capacity, so |
| 683 |
// IOCP gets "starved". |
| 684 |
// |
| 685 |
// For now, this is a very edge case that is probably not a real production |
| 686 |
// concern. There also isn't a great/obvious solution to take. For now, the |
| 687 |
// test is disabled. |
| 688 |
#[cfg(not(windows))] |
| 689 |
#[cfg(not(target_os="wasi"))] // Wasi does not support bind or threads |
| 690 |
fn io_driver_called_when_under_load() { |
| 691 |
let rt = rt(); |
| 692 |
|
| 693 |
// Create a lot of constant load. The scheduler will always be busy. |
| 694 |
for _ in 0..100 { |
| 695 |
rt.spawn(async { |
| 696 |
loop { |
| 697 |
// Don't use Tokio's `yield_now()` to avoid special defer |
| 698 |
// logic. |
| 699 |
futures::future::poll_fn::<(), _>(|cx| { |
| 700 |
cx.waker().wake_by_ref(); |
| 701 |
std::task::Poll::Pending |
| 702 |
}).await; |
| 703 |
} |
| 704 |
}); |
| 705 |
} |
| 706 |
|
| 707 |
// Do some I/O work |
| 708 |
rt.block_on(async { |
| 709 |
let listener = assert_ok!(TcpListener::bind("127.0.0.1:0").await); |
| 710 |
let addr = assert_ok!(listener.local_addr()); |
| 711 |
|
| 712 |
let srv = tokio::spawn(async move { |
| 713 |
let (mut stream, _) = assert_ok!(listener.accept().await); |
| 714 |
assert_ok!(stream.write_all(b"hello world").await); |
| 715 |
}); |
| 716 |
|
| 717 |
let cli = tokio::spawn(async move { |
| 718 |
let mut stream = assert_ok!(TcpStream::connect(addr).await); |
| 719 |
let mut dst = vec![0; 11]; |
| 720 |
|
| 721 |
assert_ok!(stream.read_exact(&mut dst).await); |
| 722 |
assert_eq!(dst, b"hello world"); |
| 723 |
}); |
| 724 |
|
| 725 |
assert_ok!(srv.await); |
| 726 |
assert_ok!(cli.await); |
| 727 |
}); |
| 728 |
} |
| 729 |
|
| 730 |
/// Tests that yielded tasks are not scheduled until **after** resource |
| 731 |
/// drivers are polled. |
| 732 |
/// |
| 733 |
/// The OS does not guarantee when I/O events are delivered, so there may be |
| 734 |
/// more yields than anticipated. This makes the test slightly flaky. To |
| 735 |
/// help avoid flakiness, we run the test 10 times and only fail it after |
| 736 |
/// 10 failures in a row. |
| 737 |
/// |
| 738 |
/// Note that if the test fails by panicking rather than by returning false, |
| 739 |
/// then we fail it immediately. That kind of failure should not happen |
| 740 |
/// spuriously. |
| 741 |
#[test] |
| 742 |
#[cfg(not(target_os="wasi"))] |
| 743 |
fn yield_defers_until_park() { |
| 744 |
for _ in 0..10 { |
| 745 |
if yield_defers_until_park_inner() { |
| 746 |
// test passed |
| 747 |
return; |
| 748 |
} |
| 749 |
|
| 750 |
// Wait a bit and run the test again. |
| 751 |
std::thread::sleep(std::time::Duration::from_secs(2)); |
| 752 |
} |
| 753 |
|
| 754 |
panic!("yield_defers_until_park is failing consistently"); |
| 755 |
} |
| 756 |
|
| 757 |
/// Implementation of `yield_defers_until_park` test. Returns `true` if the |
| 758 |
/// test passed. |
| 759 |
#[cfg(not(target_os="wasi"))] |
| 760 |
fn yield_defers_until_park_inner() -> bool { |
| 761 |
use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; |
| 762 |
use std::sync::Barrier; |
| 763 |
|
| 764 |
let rt = rt(); |
| 765 |
|
| 766 |
let flag = Arc::new(AtomicBool::new(false)); |
| 767 |
let barrier = Arc::new(Barrier::new(NUM_WORKERS)); |
| 768 |
|
| 769 |
rt.block_on(async { |
| 770 |
// Make sure other workers cannot steal tasks |
| 771 |
#[allow(clippy::reversed_empty_ranges)] |
| 772 |
for _ in 0..(NUM_WORKERS-1) { |
| 773 |
let flag = flag.clone(); |
| 774 |
let barrier = barrier.clone(); |
| 775 |
|
| 776 |
tokio::spawn(async move { |
| 777 |
barrier.wait(); |
| 778 |
|
| 779 |
while !flag.load(SeqCst) { |
| 780 |
std::thread::sleep(std::time::Duration::from_millis(1)); |
| 781 |
} |
| 782 |
}); |
| 783 |
} |
| 784 |
|
| 785 |
barrier.wait(); |
| 786 |
|
| 787 |
let (fail_test, fail_test_recv) = oneshot::channel::<()>(); |
| 788 |
|
| 789 |
let jh = tokio::spawn(async move { |
| 790 |
// Create a TCP litener |
| 791 |
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 792 |
let addr = listener.local_addr().unwrap(); |
| 793 |
|
| 794 |
tokio::join!( |
| 795 |
async { |
| 796 |
// Done in a blocking manner intentionally. |
| 797 |
let _socket = std::net::TcpStream::connect(addr).unwrap(); |
| 798 |
|
| 799 |
// Yield until connected |
| 800 |
let mut cnt = 0; |
| 801 |
while !flag.load(SeqCst){ |
| 802 |
tokio::task::yield_now().await; |
| 803 |
cnt += 1; |
| 804 |
|
| 805 |
if cnt >= 10 { |
| 806 |
// yielded too many times; report failure and |
| 807 |
// sleep forever so that the `fail_test` branch |
| 808 |
// of the `select!` below triggers. |
| 809 |
let _ = fail_test.send(()); |
| 810 |
futures::future::pending::<()>().await; |
| 811 |
break; |
| 812 |
} |
| 813 |
} |
| 814 |
}, |
| 815 |
async { |
| 816 |
let _ = listener.accept().await.unwrap(); |
| 817 |
flag.store(true, SeqCst); |
| 818 |
} |
| 819 |
); |
| 820 |
}); |
| 821 |
|
| 822 |
// Wait until the spawned task completes or fails. If no message is |
| 823 |
// sent on `fail_test`, then the test succeeds. Otherwise, it fails. |
| 824 |
let success = fail_test_recv.await.is_err(); |
| 825 |
|
| 826 |
if success { |
| 827 |
// Check for panics in spawned task. |
| 828 |
jh.abort(); |
| 829 |
jh.await.unwrap(); |
| 830 |
} |
| 831 |
|
| 832 |
success |
| 833 |
}) |
| 834 |
} |
| 835 |
|
| 836 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 837 |
#[test] |
| 838 |
fn client_server_block_on() { |
| 839 |
let rt = rt(); |
| 840 |
let (tx, rx) = mpsc::channel(); |
| 841 |
|
| 842 |
rt.block_on(async move { client_server(tx).await }); |
| 843 |
|
| 844 |
assert_ok!(rx.try_recv()); |
| 845 |
assert_err!(rx.try_recv()); |
| 846 |
} |
| 847 |
|
| 848 |
#[cfg_attr(target_os = "wasi", ignore = "Wasi does not support threads or panic recovery")] |
| 849 |
#[cfg(panic = "unwind")] |
| 850 |
#[test] |
| 851 |
fn panic_in_task() { |
| 852 |
let rt = rt(); |
| 853 |
let (tx, rx) = oneshot::channel(); |
| 854 |
|
| 855 |
struct Boom(Option<oneshot::Sender<()>>); |
| 856 |
|
| 857 |
impl Future for Boom { |
| 858 |
type Output = (); |
| 859 |
|
| 860 |
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> { |
| 861 |
panic!(); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
impl Drop for Boom { |
| 866 |
fn drop(&mut self) { |
| 867 |
assert!(std::thread::panicking()); |
| 868 |
self.0.take().unwrap().send(()).unwrap(); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
rt.spawn(Boom(Some(tx))); |
| 873 |
assert_ok!(rt.block_on(rx)); |
| 874 |
} |
| 875 |
|
| 876 |
#[test] |
| 877 |
#[should_panic] |
| 878 |
#[cfg_attr(target_os = "wasi", ignore = "Wasi does not support panic recovery")] |
| 879 |
fn panic_in_block_on() { |
| 880 |
let rt = rt(); |
| 881 |
rt.block_on(async { panic!() }); |
| 882 |
} |
| 883 |
|
| 884 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 885 |
async fn yield_once() { |
| 886 |
let mut yielded = false; |
| 887 |
poll_fn(|cx| { |
| 888 |
if yielded { |
| 889 |
Poll::Ready(()) |
| 890 |
} else { |
| 891 |
yielded = true; |
| 892 |
cx.waker().wake_by_ref(); |
| 893 |
Poll::Pending |
| 894 |
} |
| 895 |
}) |
| 896 |
.await |
| 897 |
} |
| 898 |
|
| 899 |
#[test] |
| 900 |
fn enter_and_spawn() { |
| 901 |
let rt = rt(); |
| 902 |
let handle = { |
| 903 |
let _enter = rt.enter(); |
| 904 |
tokio::spawn(async {}) |
| 905 |
}; |
| 906 |
|
| 907 |
assert_ok!(rt.block_on(handle)); |
| 908 |
} |
| 909 |
|
| 910 |
#[test] |
| 911 |
fn eagerly_drops_futures_on_shutdown() { |
| 912 |
use std::sync::mpsc; |
| 913 |
|
| 914 |
struct Never { |
| 915 |
drop_tx: mpsc::Sender<()>, |
| 916 |
} |
| 917 |
|
| 918 |
impl Future for Never { |
| 919 |
type Output = (); |
| 920 |
|
| 921 |
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> { |
| 922 |
Poll::Pending |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
impl Drop for Never { |
| 927 |
fn drop(&mut self) { |
| 928 |
self.drop_tx.send(()).unwrap(); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
let rt = rt(); |
| 933 |
|
| 934 |
let (drop_tx, drop_rx) = mpsc::channel(); |
| 935 |
let (run_tx, run_rx) = oneshot::channel(); |
| 936 |
|
| 937 |
rt.block_on(async move { |
| 938 |
tokio::spawn(async move { |
| 939 |
assert_ok!(run_tx.send(())); |
| 940 |
|
| 941 |
Never { drop_tx }.await |
| 942 |
}); |
| 943 |
|
| 944 |
assert_ok!(run_rx.await); |
| 945 |
}); |
| 946 |
|
| 947 |
drop(rt); |
| 948 |
|
| 949 |
assert_ok!(drop_rx.recv()); |
| 950 |
} |
| 951 |
|
| 952 |
#[test] |
| 953 |
fn wake_while_rt_is_dropping() { |
| 954 |
use tokio::sync::Barrier; |
| 955 |
|
| 956 |
struct OnDrop<F: FnMut()>(F); |
| 957 |
|
| 958 |
impl<F: FnMut()> Drop for OnDrop<F> { |
| 959 |
fn drop(&mut self) { |
| 960 |
(self.0)() |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
let (tx1, rx1) = oneshot::channel(); |
| 965 |
let (tx2, rx2) = oneshot::channel(); |
| 966 |
|
| 967 |
let barrier = Arc::new(Barrier::new(3)); |
| 968 |
let barrier1 = barrier.clone(); |
| 969 |
let barrier2 = barrier.clone(); |
| 970 |
|
| 971 |
let rt = rt(); |
| 972 |
|
| 973 |
rt.spawn(async move { |
| 974 |
let mut tx2 = Some(tx2); |
| 975 |
let _d = OnDrop(move || { |
| 976 |
let _ = tx2.take().unwrap().send(()); |
| 977 |
}); |
| 978 |
|
| 979 |
// Ensure a waker gets stored in oneshot 1. |
| 980 |
let _ = tokio::join!(rx1, barrier1.wait()); |
| 981 |
}); |
| 982 |
|
| 983 |
rt.spawn(async move { |
| 984 |
let mut tx1 = Some(tx1); |
| 985 |
let _d = OnDrop(move || { |
| 986 |
let _ = tx1.take().unwrap().send(()); |
| 987 |
}); |
| 988 |
|
| 989 |
// Ensure a waker gets stored in oneshot 2. |
| 990 |
let _ = tokio::join!(rx2, barrier2.wait()); |
| 991 |
}); |
| 992 |
|
| 993 |
// Wait until every oneshot channel has been polled. |
| 994 |
rt.block_on(barrier.wait()); |
| 995 |
|
| 996 |
// Drop the rt. Regardless of which task is dropped first, its destructor will wake the |
| 997 |
// other task. |
| 998 |
drop(rt); |
| 999 |
} |
| 1000 |
|
| 1001 |
#[cfg(not(target_os="wasi"))] // Wasi doesn't support UDP or bind() |
| 1002 |
#[test] |
| 1003 |
fn io_notify_while_shutting_down() { |
| 1004 |
use tokio::net::UdpSocket; |
| 1005 |
use std::sync::Arc; |
| 1006 |
|
| 1007 |
for _ in 1..10 { |
| 1008 |
let runtime = rt(); |
| 1009 |
|
| 1010 |
runtime.block_on(async { |
| 1011 |
let socket = UdpSocket::bind("127.0.0.1:0").await.unwrap(); |
| 1012 |
let addr = socket.local_addr().unwrap(); |
| 1013 |
let send_half = Arc::new(socket); |
| 1014 |
let recv_half = send_half.clone(); |
| 1015 |
|
| 1016 |
tokio::spawn(async move { |
| 1017 |
let mut buf = [0]; |
| 1018 |
loop { |
| 1019 |
recv_half.recv_from(&mut buf).await.unwrap(); |
| 1020 |
std::thread::sleep(Duration::from_millis(2)); |
| 1021 |
} |
| 1022 |
}); |
| 1023 |
|
| 1024 |
tokio::spawn(async move { |
| 1025 |
let buf = [0]; |
| 1026 |
loop { |
| 1027 |
send_half.send_to(&buf, &addr).await.unwrap(); |
| 1028 |
tokio::time::sleep(Duration::from_millis(1)).await; |
| 1029 |
} |
| 1030 |
}); |
| 1031 |
|
| 1032 |
tokio::time::sleep(Duration::from_millis(5)).await; |
| 1033 |
}); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 1038 |
#[test] |
| 1039 |
fn shutdown_timeout() { |
| 1040 |
let (tx, rx) = oneshot::channel(); |
| 1041 |
let runtime = rt(); |
| 1042 |
|
| 1043 |
runtime.block_on(async move { |
| 1044 |
task::spawn_blocking(move || { |
| 1045 |
tx.send(()).unwrap(); |
| 1046 |
thread::sleep(Duration::from_secs(10_000)); |
| 1047 |
}); |
| 1048 |
|
| 1049 |
rx.await.unwrap(); |
| 1050 |
}); |
| 1051 |
|
| 1052 |
Arc::try_unwrap(runtime).unwrap().shutdown_timeout(Duration::from_millis(100)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
#[cfg(not(target_os="wasi"))] // Wasi does not support threads |
| 1056 |
#[test] |
| 1057 |
fn shutdown_timeout_0() { |
| 1058 |
let runtime = rt(); |
| 1059 |
|
| 1060 |
runtime.block_on(async move { |
| 1061 |
task::spawn_blocking(move || { |
| 1062 |
thread::sleep(Duration::from_secs(10_000)); |
| 1063 |
}); |
| 1064 |
}); |
| 1065 |
|
| 1066 |
let now = Instant::now(); |
| 1067 |
Arc::try_unwrap(runtime).unwrap().shutdown_timeout(Duration::from_nanos(0)); |
| 1068 |
assert!(now.elapsed().as_secs() < 1); |
| 1069 |
} |
| 1070 |
|
| 1071 |
#[test] |
| 1072 |
fn shutdown_wakeup_time() { |
| 1073 |
let runtime = rt(); |
| 1074 |
|
| 1075 |
runtime.block_on(async move { |
| 1076 |
tokio::time::sleep(std::time::Duration::from_millis(100)).await; |
| 1077 |
}); |
| 1078 |
|
| 1079 |
Arc::try_unwrap(runtime).unwrap().shutdown_timeout(Duration::from_secs(10_000)); |
| 1080 |
} |
| 1081 |
|
| 1082 |
// This test is currently ignored on Windows because of a |
| 1083 |
// rust-lang issue in thread local storage destructors. |
| 1084 |
// See https://github.com/rust-lang/rust/issues/74875 |
| 1085 |
#[test] |
| 1086 |
#[cfg(not(windows))] |
| 1087 |
#[cfg_attr(target_os = "wasi", ignore = "Wasi does not support threads")] |
| 1088 |
fn runtime_in_thread_local() { |
| 1089 |
use std::cell::RefCell; |
| 1090 |
use std::thread; |
| 1091 |
|
| 1092 |
thread_local!( |
| 1093 |
static R: RefCell<Option<Runtime>> = const { RefCell::new(None) }; |
| 1094 |
); |
| 1095 |
|
| 1096 |
thread::spawn(|| { |
| 1097 |
R.with(|cell| { |
| 1098 |
let rt = rt(); |
| 1099 |
let rt = Arc::try_unwrap(rt).unwrap(); |
| 1100 |
*cell.borrow_mut() = Some(rt); |
| 1101 |
}); |
| 1102 |
|
| 1103 |
let _rt = rt(); |
| 1104 |
}).join().unwrap(); |
| 1105 |
} |
| 1106 |
|
| 1107 |
#[cfg(not(target_os="wasi"))] // Wasi does not support bind |
| 1108 |
async fn client_server(tx: mpsc::Sender<()>) { |
| 1109 |
let server = assert_ok!(TcpListener::bind("127.0.0.1:0").await); |
| 1110 |
|
| 1111 |
// Get the assigned address |
| 1112 |
let addr = assert_ok!(server.local_addr()); |
| 1113 |
|
| 1114 |
// Spawn the server |
| 1115 |
tokio::spawn(async move { |
| 1116 |
// Accept a socket |
| 1117 |
let (mut socket, _) = server.accept().await.unwrap(); |
| 1118 |
|
| 1119 |
// Write some data |
| 1120 |
socket.write_all(b"hello").await.unwrap(); |
| 1121 |
}); |
| 1122 |
|
| 1123 |
let mut client = TcpStream::connect(&addr).await.unwrap(); |
| 1124 |
|
| 1125 |
let mut buf = vec![]; |
| 1126 |
client.read_to_end(&mut buf).await.unwrap(); |
| 1127 |
|
| 1128 |
assert_eq!(buf, b"hello"); |
| 1129 |
tx.send(()).unwrap(); |
| 1130 |
} |
| 1131 |
|
| 1132 |
#[cfg(not(target_os = "wasi"))] // Wasi does not support bind |
| 1133 |
#[test] |
| 1134 |
fn local_set_block_on_socket() { |
| 1135 |
let rt = rt(); |
| 1136 |
let local = task::LocalSet::new(); |
| 1137 |
|
| 1138 |
local.block_on(&rt, async move { |
| 1139 |
let (tx, rx) = oneshot::channel(); |
| 1140 |
|
| 1141 |
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 1142 |
let addr = listener.local_addr().unwrap(); |
| 1143 |
|
| 1144 |
task::spawn_local(async move { |
| 1145 |
let _ = listener.accept().await; |
| 1146 |
tx.send(()).unwrap(); |
| 1147 |
}); |
| 1148 |
|
| 1149 |
TcpStream::connect(&addr).await.unwrap(); |
| 1150 |
rx.await.unwrap(); |
| 1151 |
}); |
| 1152 |
} |
| 1153 |
|
| 1154 |
#[cfg(not(target_os = "wasi"))] // Wasi does not support bind |
| 1155 |
#[test] |
| 1156 |
fn local_set_client_server_block_on() { |
| 1157 |
let rt = rt(); |
| 1158 |
let (tx, rx) = mpsc::channel(); |
| 1159 |
|
| 1160 |
let local = task::LocalSet::new(); |
| 1161 |
|
| 1162 |
local.block_on(&rt, async move { client_server_local(tx).await }); |
| 1163 |
|
| 1164 |
assert_ok!(rx.try_recv()); |
| 1165 |
assert_err!(rx.try_recv()); |
| 1166 |
} |
| 1167 |
|
| 1168 |
#[cfg(not(target_os = "wasi"))] // Wasi does not support bind |
| 1169 |
async fn client_server_local(tx: mpsc::Sender<()>) { |
| 1170 |
let server = assert_ok!(TcpListener::bind("127.0.0.1:0").await); |
| 1171 |
|
| 1172 |
// Get the assigned address |
| 1173 |
let addr = assert_ok!(server.local_addr()); |
| 1174 |
|
| 1175 |
// Spawn the server |
| 1176 |
task::spawn_local(async move { |
| 1177 |
// Accept a socket |
| 1178 |
let (mut socket, _) = server.accept().await.unwrap(); |
| 1179 |
|
| 1180 |
// Write some data |
| 1181 |
socket.write_all(b"hello").await.unwrap(); |
| 1182 |
}); |
| 1183 |
|
| 1184 |
let mut client = TcpStream::connect(&addr).await.unwrap(); |
| 1185 |
|
| 1186 |
let mut buf = vec![]; |
| 1187 |
client.read_to_end(&mut buf).await.unwrap(); |
| 1188 |
|
| 1189 |
assert_eq!(buf, b"hello"); |
| 1190 |
tx.send(()).unwrap(); |
| 1191 |
} |
| 1192 |
|
| 1193 |
#[test] |
| 1194 |
fn coop() { |
| 1195 |
use std::task::Poll::Ready; |
| 1196 |
use tokio::sync::mpsc; |
| 1197 |
|
| 1198 |
let rt = rt(); |
| 1199 |
|
| 1200 |
rt.block_on(async { |
| 1201 |
let (send, mut recv) = mpsc::unbounded_channel(); |
| 1202 |
|
| 1203 |
// Send a bunch of messages. |
| 1204 |
for _ in 0..1_000 { |
| 1205 |
send.send(()).unwrap(); |
| 1206 |
} |
| 1207 |
|
| 1208 |
poll_fn(|cx| { |
| 1209 |
// At least one response should return pending. |
| 1210 |
for _ in 0..1_000 { |
| 1211 |
if recv.poll_recv(cx).is_pending() { |
| 1212 |
return Ready(()); |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
panic!("did not yield"); |
| 1217 |
}).await; |
| 1218 |
}); |
| 1219 |
} |
| 1220 |
|
| 1221 |
#[test] |
| 1222 |
fn coop_unconstrained() { |
| 1223 |
use std::task::Poll::Ready; |
| 1224 |
use tokio::sync::mpsc; |
| 1225 |
|
| 1226 |
let rt = rt(); |
| 1227 |
|
| 1228 |
rt.block_on(async { |
| 1229 |
let (send, mut recv) = mpsc::unbounded_channel(); |
| 1230 |
|
| 1231 |
// Send a bunch of messages. |
| 1232 |
for _ in 0..1_000 { |
| 1233 |
send.send(()).unwrap(); |
| 1234 |
} |
| 1235 |
|
| 1236 |
tokio::task::unconstrained(poll_fn(|cx| { |
| 1237 |
// All the responses should be ready. |
| 1238 |
for _ in 0..1_000 { |
| 1239 |
assert_eq!(recv.poll_recv(cx), Poll::Ready(Some(()))); |
| 1240 |
} |
| 1241 |
|
| 1242 |
Ready(()) |
| 1243 |
})).await; |
| 1244 |
}); |
| 1245 |
} |
| 1246 |
|
| 1247 |
#[cfg(tokio_unstable)] |
| 1248 |
#[test] |
| 1249 |
fn coop_consume_budget() { |
| 1250 |
let rt = rt(); |
| 1251 |
|
| 1252 |
rt.block_on(async { |
| 1253 |
poll_fn(|cx| { |
| 1254 |
let counter = Arc::new(std::sync::Mutex::new(0)); |
| 1255 |
let counter_clone = Arc::clone(&counter); |
| 1256 |
let mut worker = Box::pin(async move { |
| 1257 |
// Consume the budget until a yield happens |
| 1258 |
for _ in 0..1000 { |
| 1259 |
*counter.lock().unwrap() += 1; |
| 1260 |
task::consume_budget().await |
| 1261 |
} |
| 1262 |
}); |
| 1263 |
// Assert that the worker was yielded and it didn't manage |
| 1264 |
// to finish the whole work (assuming the total budget of 128) |
| 1265 |
assert!(Pin::new(&mut worker).poll(cx).is_pending()); |
| 1266 |
assert!(*counter_clone.lock().unwrap() < 1000); |
| 1267 |
std::task::Poll::Ready(()) |
| 1268 |
}).await; |
| 1269 |
}); |
| 1270 |
} |
| 1271 |
|
| 1272 |
// Tests that the "next task" scheduler optimization is not able to starve |
| 1273 |
// other tasks. |
| 1274 |
#[test] |
| 1275 |
fn ping_pong_saturation() { |
| 1276 |
use std::sync::atomic::{Ordering, AtomicBool}; |
| 1277 |
use tokio::sync::mpsc; |
| 1278 |
|
| 1279 |
const NUM: usize = 100; |
| 1280 |
|
| 1281 |
let rt = rt(); |
| 1282 |
|
| 1283 |
let running = Arc::new(AtomicBool::new(true)); |
| 1284 |
|
| 1285 |
rt.block_on(async { |
| 1286 |
let (spawned_tx, mut spawned_rx) = mpsc::unbounded_channel(); |
| 1287 |
|
| 1288 |
let mut tasks = vec![]; |
| 1289 |
// Spawn a bunch of tasks that ping-pong between each other to |
| 1290 |
// saturate the runtime. |
| 1291 |
for _ in 0..NUM { |
| 1292 |
let (tx1, mut rx1) = mpsc::unbounded_channel(); |
| 1293 |
let (tx2, mut rx2) = mpsc::unbounded_channel(); |
| 1294 |
let spawned_tx = spawned_tx.clone(); |
| 1295 |
let running = running.clone(); |
| 1296 |
tasks.push(task::spawn(async move { |
| 1297 |
spawned_tx.send(()).unwrap(); |
| 1298 |
|
| 1299 |
|
| 1300 |
while running.load(Ordering::Relaxed) { |
| 1301 |
tx1.send(()).unwrap(); |
| 1302 |
rx2.recv().await.unwrap(); |
| 1303 |
} |
| 1304 |
|
| 1305 |
// Close the channel and wait for the other task to exit. |
| 1306 |
drop(tx1); |
| 1307 |
assert!(rx2.recv().await.is_none()); |
| 1308 |
})); |
| 1309 |
|
| 1310 |
tasks.push(task::spawn(async move { |
| 1311 |
while rx1.recv().await.is_some() { |
| 1312 |
tx2.send(()).unwrap(); |
| 1313 |
} |
| 1314 |
})); |
| 1315 |
} |
| 1316 |
|
| 1317 |
for _ in 0..NUM { |
| 1318 |
spawned_rx.recv().await.unwrap(); |
| 1319 |
} |
| 1320 |
|
| 1321 |
// spawn another task and wait for it to complete |
| 1322 |
let handle = task::spawn(async { |
| 1323 |
for _ in 0..5 { |
| 1324 |
// Yielding forces it back into the local queue. |
| 1325 |
task::yield_now().await; |
| 1326 |
} |
| 1327 |
}); |
| 1328 |
handle.await.unwrap(); |
| 1329 |
running.store(false, Ordering::Relaxed); |
| 1330 |
for t in tasks { |
| 1331 |
t.await.unwrap(); |
| 1332 |
} |
| 1333 |
}); |
| 1334 |
} |
| 1335 |
|
| 1336 |
#[test] |
| 1337 |
#[cfg(not(target_os="wasi"))] |
| 1338 |
fn shutdown_concurrent_spawn() { |
| 1339 |
const NUM_TASKS: usize = 10_000; |
| 1340 |
for _ in 0..5 { |
| 1341 |
let (tx, rx) = std::sync::mpsc::channel(); |
| 1342 |
let rt = rt(); |
| 1343 |
|
| 1344 |
let mut txs = vec![]; |
| 1345 |
|
| 1346 |
for _ in 0..NUM_TASKS { |
| 1347 |
let (tx, rx) = tokio::sync::oneshot::channel(); |
| 1348 |
txs.push(tx); |
| 1349 |
rt.spawn(async move { |
| 1350 |
rx.await.unwrap(); |
| 1351 |
}); |
| 1352 |
} |
| 1353 |
|
| 1354 |
// Prime the tasks |
| 1355 |
rt.block_on(async { tokio::task::yield_now().await }); |
| 1356 |
|
| 1357 |
let th = std::thread::spawn(move || { |
| 1358 |
tx.send(()).unwrap(); |
| 1359 |
for tx in txs.drain(..) { |
| 1360 |
let _ = tx.send(()); |
| 1361 |
} |
| 1362 |
}); |
| 1363 |
|
| 1364 |
rx.recv().unwrap(); |
| 1365 |
drop(rt); |
| 1366 |
|
| 1367 |
th.join().unwrap(); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
#[test] |
| 1372 |
#[cfg_attr(target_family = "wasm", ignore)] |
| 1373 |
fn wake_by_ref_from_thread_local() { |
| 1374 |
wake_from_thread_local(true); |
| 1375 |
} |
| 1376 |
|
| 1377 |
#[test] |
| 1378 |
#[cfg_attr(target_family = "wasm", ignore)] |
| 1379 |
fn wake_by_val_from_thread_local() { |
| 1380 |
wake_from_thread_local(false); |
| 1381 |
} |
| 1382 |
|
| 1383 |
fn wake_from_thread_local(by_ref: bool) { |
| 1384 |
use std::cell::RefCell; |
| 1385 |
use std::sync::mpsc::{channel, Sender}; |
| 1386 |
use std::task::Waker; |
| 1387 |
|
| 1388 |
struct TLData { |
| 1389 |
by_ref: bool, |
| 1390 |
waker: Option<Waker>, |
| 1391 |
done: Sender<bool>, |
| 1392 |
} |
| 1393 |
|
| 1394 |
impl Drop for TLData { |
| 1395 |
fn drop(&mut self) { |
| 1396 |
if self.by_ref { |
| 1397 |
self.waker.take().unwrap().wake_by_ref(); |
| 1398 |
} else { |
| 1399 |
self.waker.take().unwrap().wake(); |
| 1400 |
} |
| 1401 |
let _ = self.done.send(true); |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
std::thread_local! { |
| 1406 |
static TL_DATA: RefCell<Option<TLData>> = const { RefCell::new(None) }; |
| 1407 |
}; |
| 1408 |
|
| 1409 |
let (send, recv) = channel(); |
| 1410 |
|
| 1411 |
std::thread::spawn(move || { |
| 1412 |
let rt = rt(); |
| 1413 |
rt.block_on(rt.spawn(poll_fn(move |cx| { |
| 1414 |
let waker = cx.waker().clone(); |
| 1415 |
let send = send.clone(); |
| 1416 |
TL_DATA.with(|tl| { |
| 1417 |
tl.replace(Some(TLData { |
| 1418 |
by_ref, |
| 1419 |
waker: Some(waker), |
| 1420 |
done: send, |
| 1421 |
})); |
| 1422 |
}); |
| 1423 |
Poll::Ready(()) |
| 1424 |
}))) |
| 1425 |
.unwrap(); |
| 1426 |
}) |
| 1427 |
.join() |
| 1428 |
.unwrap(); |
| 1429 |
|
| 1430 |
assert!(recv.recv().unwrap()); |
| 1431 |
} |
| 1432 |
} |