| 1 |
mod queue; |
| 2 |
mod shutdown; |
| 3 |
mod yield_now; |
| 4 |
|
| 5 |
/// Full runtime loom tests. These are heavy tests and take significant time to |
| 6 |
/// run on CI. |
| 7 |
/// |
| 8 |
/// Use `LOOM_MAX_PREEMPTIONS=1` to do a "quick" run as a smoke test. |
| 9 |
/// |
| 10 |
/// In order to speed up the C |
| 11 |
use crate::future::poll_fn; |
| 12 |
use crate::runtime::tests::loom_oneshot as oneshot; |
| 13 |
use crate::runtime::{self, Runtime}; |
| 14 |
use crate::{spawn, task}; |
| 15 |
use tokio_test::assert_ok; |
| 16 |
|
| 17 |
use loom::sync::atomic::{AtomicBool, AtomicUsize}; |
| 18 |
use loom::sync::Arc; |
| 19 |
|
| 20 |
use pin_project_lite::pin_project; |
| 21 |
use std::future::Future; |
| 22 |
use std::pin::Pin; |
| 23 |
use std::sync::atomic::Ordering::{Relaxed, SeqCst}; |
| 24 |
use std::task::{Context, Poll}; |
| 25 |
|
| 26 |
mod atomic_take { |
| 27 |
use loom::sync::atomic::AtomicBool; |
| 28 |
use std::mem::MaybeUninit; |
| 29 |
use std::sync::atomic::Ordering::SeqCst; |
| 30 |
|
| 31 |
pub(super) struct AtomicTake<T> { |
| 32 |
inner: MaybeUninit<T>, |
| 33 |
taken: AtomicBool, |
| 34 |
} |
| 35 |
|
| 36 |
impl<T> AtomicTake<T> { |
| 37 |
pub(super) fn new(value: T) -> Self { |
| 38 |
Self { |
| 39 |
inner: MaybeUninit::new(value), |
| 40 |
taken: AtomicBool::new(false), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub(super) fn take(&self) -> Option<T> { |
| 45 |
// safety: Only one thread will see the boolean change from false |
| 46 |
// to true, so that thread is able to take the value. |
| 47 |
match self.taken.fetch_or(true, SeqCst) { |
| 48 |
false => unsafe { Some(std::ptr::read(self.inner.as_ptr())) }, |
| 49 |
true => None, |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl<T> Drop for AtomicTake<T> { |
| 55 |
fn drop(&mut self) { |
| 56 |
drop(self.take()); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#[derive(Clone)] |
| 62 |
struct AtomicOneshot<T> { |
| 63 |
value: std::sync::Arc<atomic_take::AtomicTake<oneshot::Sender<T>>>, |
| 64 |
} |
| 65 |
impl<T> AtomicOneshot<T> { |
| 66 |
fn new(sender: oneshot::Sender<T>) -> Self { |
| 67 |
Self { |
| 68 |
value: std::sync::Arc::new(atomic_take::AtomicTake::new(sender)), |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
fn assert_send(&self, value: T) { |
| 73 |
self.value.take().unwrap().send(value); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/// Tests are divided into groups to make the runs faster on CI. |
| 78 |
mod group_a { |
| 79 |
use super::*; |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn racy_shutdown() { |
| 83 |
loom::model(|| { |
| 84 |
let pool = mk_pool(1); |
| 85 |
|
| 86 |
// here's the case we want to exercise: |
| 87 |
// |
| 88 |
// a worker that still has tasks in its local queue gets sent to the blocking pool (due to |
| 89 |
// block_in_place). the blocking pool is shut down, so drops the worker. the worker's |
| 90 |
// shutdown method never gets run. |
| 91 |
// |
| 92 |
// we do this by spawning two tasks on one worker, the first of which does block_in_place, |
| 93 |
// and then immediately drop the pool. |
| 94 |
|
| 95 |
pool.spawn(track(async { |
| 96 |
crate::task::block_in_place(|| {}); |
| 97 |
})); |
| 98 |
pool.spawn(track(async {})); |
| 99 |
drop(pool); |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn pool_multi_spawn() { |
| 105 |
loom::model(|| { |
| 106 |
let pool = mk_pool(2); |
| 107 |
let c1 = Arc::new(AtomicUsize::new(0)); |
| 108 |
|
| 109 |
let (tx, rx) = oneshot::channel(); |
| 110 |
let tx1 = AtomicOneshot::new(tx); |
| 111 |
|
| 112 |
// Spawn a task |
| 113 |
let c2 = c1.clone(); |
| 114 |
let tx2 = tx1.clone(); |
| 115 |
pool.spawn(track(async move { |
| 116 |
spawn(track(async move { |
| 117 |
if 1 == c1.fetch_add(1, Relaxed) { |
| 118 |
tx1.assert_send(()); |
| 119 |
} |
| 120 |
})); |
| 121 |
})); |
| 122 |
|
| 123 |
// Spawn a second task |
| 124 |
pool.spawn(track(async move { |
| 125 |
spawn(track(async move { |
| 126 |
if 1 == c2.fetch_add(1, Relaxed) { |
| 127 |
tx2.assert_send(()); |
| 128 |
} |
| 129 |
})); |
| 130 |
})); |
| 131 |
|
| 132 |
rx.recv(); |
| 133 |
}); |
| 134 |
} |
| 135 |
|
| 136 |
fn only_blocking_inner(first_pending: bool) { |
| 137 |
loom::model(move || { |
| 138 |
let pool = mk_pool(1); |
| 139 |
let (block_tx, block_rx) = oneshot::channel(); |
| 140 |
|
| 141 |
pool.spawn(track(async move { |
| 142 |
crate::task::block_in_place(move || { |
| 143 |
block_tx.send(()); |
| 144 |
}); |
| 145 |
if first_pending { |
| 146 |
task::yield_now().await |
| 147 |
} |
| 148 |
})); |
| 149 |
|
| 150 |
block_rx.recv(); |
| 151 |
drop(pool); |
| 152 |
}); |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn only_blocking_without_pending() { |
| 157 |
only_blocking_inner(false) |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn only_blocking_with_pending() { |
| 162 |
only_blocking_inner(true) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
mod group_b { |
| 167 |
use super::*; |
| 168 |
|
| 169 |
fn blocking_and_regular_inner(first_pending: bool) { |
| 170 |
const NUM: usize = 3; |
| 171 |
loom::model(move || { |
| 172 |
let pool = mk_pool(1); |
| 173 |
let cnt = Arc::new(AtomicUsize::new(0)); |
| 174 |
|
| 175 |
let (block_tx, block_rx) = oneshot::channel(); |
| 176 |
let (done_tx, done_rx) = oneshot::channel(); |
| 177 |
let done_tx = AtomicOneshot::new(done_tx); |
| 178 |
|
| 179 |
pool.spawn(track(async move { |
| 180 |
crate::task::block_in_place(move || { |
| 181 |
block_tx.send(()); |
| 182 |
}); |
| 183 |
if first_pending { |
| 184 |
task::yield_now().await |
| 185 |
} |
| 186 |
})); |
| 187 |
|
| 188 |
for _ in 0..NUM { |
| 189 |
let cnt = cnt.clone(); |
| 190 |
let done_tx = done_tx.clone(); |
| 191 |
|
| 192 |
pool.spawn(track(async move { |
| 193 |
if NUM == cnt.fetch_add(1, Relaxed) + 1 { |
| 194 |
done_tx.assert_send(()); |
| 195 |
} |
| 196 |
})); |
| 197 |
} |
| 198 |
|
| 199 |
done_rx.recv(); |
| 200 |
block_rx.recv(); |
| 201 |
|
| 202 |
drop(pool); |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn blocking_and_regular() { |
| 208 |
blocking_and_regular_inner(false); |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn blocking_and_regular_with_pending() { |
| 213 |
blocking_and_regular_inner(true); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn join_output() { |
| 218 |
loom::model(|| { |
| 219 |
let rt = mk_pool(1); |
| 220 |
|
| 221 |
rt.block_on(async { |
| 222 |
let t = crate::spawn(track(async { "hello" })); |
| 223 |
|
| 224 |
let out = assert_ok!(t.await); |
| 225 |
assert_eq!("hello", out.into_inner()); |
| 226 |
}); |
| 227 |
}); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn poll_drop_handle_then_drop() { |
| 232 |
loom::model(|| { |
| 233 |
let rt = mk_pool(1); |
| 234 |
|
| 235 |
rt.block_on(async move { |
| 236 |
let mut t = crate::spawn(track(async { "hello" })); |
| 237 |
|
| 238 |
poll_fn(|cx| { |
| 239 |
let _ = Pin::new(&mut t).poll(cx); |
| 240 |
Poll::Ready(()) |
| 241 |
}) |
| 242 |
.await; |
| 243 |
}); |
| 244 |
}) |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn complete_block_on_under_load() { |
| 249 |
loom::model(|| { |
| 250 |
let pool = mk_pool(1); |
| 251 |
|
| 252 |
pool.block_on(async { |
| 253 |
// Trigger a re-schedule |
| 254 |
crate::spawn(track(async { |
| 255 |
for _ in 0..2 { |
| 256 |
task::yield_now().await; |
| 257 |
} |
| 258 |
})); |
| 259 |
|
| 260 |
gated2(true).await |
| 261 |
}); |
| 262 |
}); |
| 263 |
} |
| 264 |
|
| 265 |
#[test] |
| 266 |
fn shutdown_with_notification() { |
| 267 |
use crate::sync::oneshot; |
| 268 |
|
| 269 |
loom::model(|| { |
| 270 |
let rt = mk_pool(2); |
| 271 |
let (done_tx, done_rx) = oneshot::channel::<()>(); |
| 272 |
|
| 273 |
rt.spawn(track(async move { |
| 274 |
let (tx, rx) = oneshot::channel::<()>(); |
| 275 |
|
| 276 |
crate::spawn(async move { |
| 277 |
crate::task::spawn_blocking(move || { |
| 278 |
let _ = tx.send(()); |
| 279 |
}); |
| 280 |
|
| 281 |
let _ = done_rx.await; |
| 282 |
}); |
| 283 |
|
| 284 |
let _ = rx.await; |
| 285 |
|
| 286 |
let _ = done_tx.send(()); |
| 287 |
})); |
| 288 |
}); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
mod group_c { |
| 293 |
use super::*; |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn pool_shutdown() { |
| 297 |
loom::model(|| { |
| 298 |
let pool = mk_pool(2); |
| 299 |
|
| 300 |
pool.spawn(track(async move { |
| 301 |
gated2(true).await; |
| 302 |
})); |
| 303 |
|
| 304 |
pool.spawn(track(async move { |
| 305 |
gated2(false).await; |
| 306 |
})); |
| 307 |
|
| 308 |
drop(pool); |
| 309 |
}); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
mod group_d { |
| 314 |
use super::*; |
| 315 |
|
| 316 |
#[test] |
| 317 |
fn pool_multi_notify() { |
| 318 |
loom::model(|| { |
| 319 |
let pool = mk_pool(2); |
| 320 |
|
| 321 |
let c1 = Arc::new(AtomicUsize::new(0)); |
| 322 |
|
| 323 |
let (done_tx, done_rx) = oneshot::channel(); |
| 324 |
let done_tx1 = AtomicOneshot::new(done_tx); |
| 325 |
let done_tx2 = done_tx1.clone(); |
| 326 |
|
| 327 |
// Spawn a task |
| 328 |
let c2 = c1.clone(); |
| 329 |
pool.spawn(track(async move { |
| 330 |
multi_gated().await; |
| 331 |
|
| 332 |
if 1 == c1.fetch_add(1, Relaxed) { |
| 333 |
done_tx1.assert_send(()); |
| 334 |
} |
| 335 |
})); |
| 336 |
|
| 337 |
// Spawn a second task |
| 338 |
pool.spawn(track(async move { |
| 339 |
multi_gated().await; |
| 340 |
|
| 341 |
if 1 == c2.fetch_add(1, Relaxed) { |
| 342 |
done_tx2.assert_send(()); |
| 343 |
} |
| 344 |
})); |
| 345 |
|
| 346 |
done_rx.recv(); |
| 347 |
}); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
fn mk_pool(num_threads: usize) -> Runtime { |
| 352 |
runtime::Builder::new_multi_thread() |
| 353 |
.worker_threads(num_threads) |
| 354 |
// Set the intervals to avoid tuning logic |
| 355 |
.event_interval(2) |
| 356 |
.build() |
| 357 |
.unwrap() |
| 358 |
} |
| 359 |
|
| 360 |
fn gated2(thread: bool) -> impl Future<Output = &'static str> { |
| 361 |
use loom::thread; |
| 362 |
use std::sync::Arc; |
| 363 |
|
| 364 |
let gate = Arc::new(AtomicBool::new(false)); |
| 365 |
let mut fired = false; |
| 366 |
|
| 367 |
poll_fn(move |cx| { |
| 368 |
if !fired { |
| 369 |
let gate = gate.clone(); |
| 370 |
let waker = cx.waker().clone(); |
| 371 |
|
| 372 |
if thread { |
| 373 |
thread::spawn(move || { |
| 374 |
gate.store(true, SeqCst); |
| 375 |
waker.wake_by_ref(); |
| 376 |
}); |
| 377 |
} else { |
| 378 |
spawn(track(async move { |
| 379 |
gate.store(true, SeqCst); |
| 380 |
waker.wake_by_ref(); |
| 381 |
})); |
| 382 |
} |
| 383 |
|
| 384 |
fired = true; |
| 385 |
|
| 386 |
return Poll::Pending; |
| 387 |
} |
| 388 |
|
| 389 |
if gate.load(SeqCst) { |
| 390 |
Poll::Ready("hello world") |
| 391 |
} else { |
| 392 |
Poll::Pending |
| 393 |
} |
| 394 |
}) |
| 395 |
} |
| 396 |
|
| 397 |
async fn multi_gated() { |
| 398 |
struct Gate { |
| 399 |
waker: loom::future::AtomicWaker, |
| 400 |
count: AtomicUsize, |
| 401 |
} |
| 402 |
|
| 403 |
let gate = Arc::new(Gate { |
| 404 |
waker: loom::future::AtomicWaker::new(), |
| 405 |
count: AtomicUsize::new(0), |
| 406 |
}); |
| 407 |
|
| 408 |
{ |
| 409 |
let gate = gate.clone(); |
| 410 |
spawn(track(async move { |
| 411 |
for i in 1..3 { |
| 412 |
gate.count.store(i, SeqCst); |
| 413 |
gate.waker.wake(); |
| 414 |
} |
| 415 |
})); |
| 416 |
} |
| 417 |
|
| 418 |
poll_fn(move |cx| { |
| 419 |
gate.waker.register_by_ref(cx.waker()); |
| 420 |
if gate.count.load(SeqCst) < 2 { |
| 421 |
Poll::Pending |
| 422 |
} else { |
| 423 |
Poll::Ready(()) |
| 424 |
} |
| 425 |
}) |
| 426 |
.await; |
| 427 |
} |
| 428 |
|
| 429 |
fn track<T: Future>(f: T) -> Track<T> { |
| 430 |
Track { |
| 431 |
inner: f, |
| 432 |
arc: Arc::new(()), |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
pin_project! { |
| 437 |
struct Track<T> { |
| 438 |
#[pin] |
| 439 |
inner: T, |
| 440 |
// Arc is used to hook into loom's leak tracking. |
| 441 |
arc: Arc<()>, |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
impl<T> Track<T> { |
| 446 |
fn into_inner(self) -> T { |
| 447 |
self.inner |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
impl<T: Future> Future for Track<T> { |
| 452 |
type Output = Track<T::Output>; |
| 453 |
|
| 454 |
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 455 |
let me = self.project(); |
| 456 |
|
| 457 |
Poll::Ready(Track { |
| 458 |
inner: ready!(me.inner.poll(cx)), |
| 459 |
arc: me.arc.clone(), |
| 460 |
}) |
| 461 |
} |
| 462 |
} |