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

Add is_closed, is_empty and len to mpsc::Receiver and mpsc::UnboundedReceiver #6348

Merged
merged 15 commits into from
Mar 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add missing rx len test scenarios
balliegojr committed Mar 23, 2024
commit 6537a807a522f6ac2aa5a626f2bf4eafcae2ff7a
58 changes: 58 additions & 0 deletions tokio/tests/sync_mpsc.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lot of tests! That's awesome.

Original file line number Diff line number Diff line change
@@ -1203,6 +1203,35 @@ async fn test_rx_len_when_consuming_all_messages() {
}
}

#[tokio::test]
async fn test_rx_len_when_close_is_called() {
let (tx, mut rx) = mpsc::channel(100);
tx.send(()).await.unwrap();
rx.close();

assert_eq!(rx.len(), 1);
}

#[tokio::test]
async fn test_rx_len_when_close_is_called_before_dropping_sender() {
let (tx, mut rx) = mpsc::channel(100);
tx.send(()).await.unwrap();
rx.close();
drop(tx);

assert_eq!(rx.len(), 1);
}

#[tokio::test]
async fn test_rx_len_when_close_is_called_after_dropping_sender() {
let (tx, mut rx) = mpsc::channel(100);
tx.send(()).await.unwrap();
drop(tx);
rx.close();

assert_eq!(rx.len(), 1);
}

#[tokio::test]
async fn test_rx_unbounded_is_closed_when_calling_close_with_sender() {
// is_closed should return true after calling close but still has a sender
@@ -1362,4 +1391,33 @@ async fn test_rx_unbounded_len_when_consuming_all_messages() {
}
}

#[tokio::test]
async fn test_rx_unbounded_len_when_close_is_called() {
let (tx, mut rx) = mpsc::unbounded_channel();
tx.send(()).unwrap();
rx.close();

assert_eq!(rx.len(), 1);
}

#[tokio::test]
async fn test_rx_unbounded_len_when_close_is_called_before_dropping_sender() {
let (tx, mut rx) = mpsc::unbounded_channel();
tx.send(()).unwrap();
rx.close();
drop(tx);

assert_eq!(rx.len(), 1);
}

#[tokio::test]
async fn test_rx_unbounded_len_when_close_is_called_after_dropping_sender() {
let (tx, mut rx) = mpsc::unbounded_channel();
tx.send(()).unwrap();
drop(tx);
rx.close();

assert_eq!(rx.len(), 1);
}

fn is_debug<T: fmt::Debug>(_: &T) {}