From 435e39001b04f5846d1e9db1b243e6b81982f654 Mon Sep 17 00:00:00 2001 From: Evan Rittenhouse Date: Sun, 12 Jan 2025 03:33:07 -0800 Subject: [PATCH] sync: fix `sync::broadcast::Sender::closed()` doctest (#7090) The test's previous iteration could sometimes flake since we didn't await the completion of the first task. Since the tasks only existed to `move` the relevant `rx`'s in, to force a drop, we can omit them entirely and drop the `rx`s via `drop()`. This prevents any scheduling-related flakes. --- tokio/src/sync/broadcast.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tokio/src/sync/broadcast.rs b/tokio/src/sync/broadcast.rs index d4ebad7d684..e48925b497e 100644 --- a/tokio/src/sync/broadcast.rs +++ b/tokio/src/sync/broadcast.rs @@ -825,17 +825,14 @@ impl Sender { /// let (tx, mut rx1) = broadcast::channel::(16); /// let mut rx2 = tx.subscribe(); /// - /// tokio::spawn(async move { - /// assert_eq!(rx1.recv().await.unwrap(), 10); - /// }); - /// /// let _ = tx.send(10); - /// assert!(tx.closed().now_or_never().is_none()); /// - /// let _ = tokio::spawn(async move { - /// assert_eq!(rx2.recv().await.unwrap(), 10); - /// }).await; + /// assert_eq!(rx1.recv().await.unwrap(), 10); + /// drop(rx1); + /// assert!(tx.closed().now_or_never().is_none()); /// + /// assert_eq!(rx2.recv().await.unwrap(), 10); + /// drop(rx2); /// assert!(tx.closed().now_or_never().is_some()); /// } /// ```