Skip to content
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

tests: fix ping pong saturation #3390

Merged
merged 3 commits into from Jan 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions tokio/tests/rt_common.rs
Expand Up @@ -1021,39 +1021,45 @@ rt_test! {
// other tasks.
#[test]
fn ping_pong_saturation() {
use std::sync::atomic::{Ordering, AtomicBool};
use tokio::sync::mpsc;

const NUM: usize = 100;

let rt = rt();

let running = Arc::new(AtomicBool::new(true));

rt.block_on(async {
let (spawned_tx, mut spawned_rx) = mpsc::unbounded_channel();

let mut tasks = vec![];
// Spawn a bunch of tasks that ping ping between each other to
// saturate the runtime.
for _ in 0..NUM {
let (tx1, mut rx1) = mpsc::unbounded_channel();
let (tx2, mut rx2) = mpsc::unbounded_channel();
let spawned_tx = spawned_tx.clone();

task::spawn(async move {
let running = running.clone();
tasks.push(task::spawn(async move {
spawned_tx.send(()).unwrap();

tx1.send(()).unwrap();

loop {
rx2.recv().await.unwrap();
while running.load(Ordering::Relaxed) {
tx1.send(()).unwrap();
rx2.recv().await.unwrap();
}
});

task::spawn(async move {
loop {
rx1.recv().await.unwrap();
// Close the channel and wait for the other task to exit.
drop(tx1);
assert!(rx2.recv().await.is_none());
}));

tasks.push(task::spawn(async move {
while rx1.recv().await.is_some() {
tx2.send(()).unwrap();
}
});
}));
}

for _ in 0..NUM {
Expand All @@ -1068,6 +1074,10 @@ rt_test! {
}
});
handle.await.unwrap();
running.store(false, Ordering::Relaxed);
for t in tasks {
t.await.unwrap();
}
});
}
}