mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-09 00:00:08 +02:00
tests: fix ping pong saturation (#3390)
This commit is contained in:
+20
-10
@@ -1021,39 +1021,45 @@ rt_test! {
|
|||||||
// other tasks.
|
// other tasks.
|
||||||
#[test]
|
#[test]
|
||||||
fn ping_pong_saturation() {
|
fn ping_pong_saturation() {
|
||||||
|
use std::sync::atomic::{Ordering, AtomicBool};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
const NUM: usize = 100;
|
const NUM: usize = 100;
|
||||||
|
|
||||||
let rt = rt();
|
let rt = rt();
|
||||||
|
|
||||||
|
let running = Arc::new(AtomicBool::new(true));
|
||||||
|
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
let (spawned_tx, mut spawned_rx) = mpsc::unbounded_channel();
|
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
|
// Spawn a bunch of tasks that ping ping between each other to
|
||||||
// saturate the runtime.
|
// saturate the runtime.
|
||||||
for _ in 0..NUM {
|
for _ in 0..NUM {
|
||||||
let (tx1, mut rx1) = mpsc::unbounded_channel();
|
let (tx1, mut rx1) = mpsc::unbounded_channel();
|
||||||
let (tx2, mut rx2) = mpsc::unbounded_channel();
|
let (tx2, mut rx2) = mpsc::unbounded_channel();
|
||||||
let spawned_tx = spawned_tx.clone();
|
let spawned_tx = spawned_tx.clone();
|
||||||
|
let running = running.clone();
|
||||||
task::spawn(async move {
|
tasks.push(task::spawn(async move {
|
||||||
spawned_tx.send(()).unwrap();
|
spawned_tx.send(()).unwrap();
|
||||||
|
|
||||||
tx1.send(()).unwrap();
|
|
||||||
|
|
||||||
loop {
|
while running.load(Ordering::Relaxed) {
|
||||||
rx2.recv().await.unwrap();
|
|
||||||
tx1.send(()).unwrap();
|
tx1.send(()).unwrap();
|
||||||
|
rx2.recv().await.unwrap();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
task::spawn(async move {
|
// Close the channel and wait for the other task to exit.
|
||||||
loop {
|
drop(tx1);
|
||||||
rx1.recv().await.unwrap();
|
assert!(rx2.recv().await.is_none());
|
||||||
|
}));
|
||||||
|
|
||||||
|
tasks.push(task::spawn(async move {
|
||||||
|
while rx1.recv().await.is_some() {
|
||||||
tx2.send(()).unwrap();
|
tx2.send(()).unwrap();
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
for _ in 0..NUM {
|
for _ in 0..NUM {
|
||||||
@@ -1068,6 +1074,10 @@ rt_test! {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
handle.await.unwrap();
|
handle.await.unwrap();
|
||||||
|
running.store(false, Ordering::Relaxed);
|
||||||
|
for t in tasks {
|
||||||
|
t.await.unwrap();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user