diff --git a/tokio/src/sync/mpsc/chan.rs b/tokio/src/sync/mpsc/chan.rs index 20e2c90d4..7f7f8b16d 100644 --- a/tokio/src/sync/mpsc/chan.rs +++ b/tokio/src/sync/mpsc/chan.rs @@ -306,13 +306,11 @@ impl Rx { return Ready(Some(value)); } Some(Read::Closed) => { - // TODO: This check may not be required as it most - // likely can only return `true` at this point. A - // channel is closed when all tx handles are + // A channel is closed when all tx handles are // dropped. Dropping a tx handle releases memory, // which ensures that if dropping the tx handle is // visible, then all messages sent are also visible. - assert!(self.inner.semaphore.is_idle()); + debug_assert!(self.inner.semaphore.is_idle()); coop.made_progress(); return Ready(None); } @@ -380,13 +378,11 @@ impl Rx { if number_added > 0 { self.inner.semaphore.add_permits(number_added); } - // TODO: This check may not be required as it most - // likely can only return `true` at this point. A - // channel is closed when all tx handles are + // A channel is closed when all tx handles are // dropped. Dropping a tx handle releases memory, // which ensures that if dropping the tx handle is // visible, then all messages sent are also visible. - assert!(self.inner.semaphore.is_idle()); + debug_assert!(self.inner.semaphore.is_idle()); coop.made_progress(); return Ready(number_added); } @@ -415,7 +411,7 @@ impl Rx { try_recv!(); if rx_fields.rx_closed && self.inner.semaphore.is_idle() { - assert!(buffer.is_empty()); + debug_assert_eq!(buffer.len(), initial_length); coop.made_progress(); Ready(0usize) } else { diff --git a/tokio/tests/sync_mpsc.rs b/tokio/tests/sync_mpsc.rs index 3ebac7390..048b94eb5 100644 --- a/tokio/tests/sync_mpsc.rs +++ b/tokio/tests/sync_mpsc.rs @@ -358,6 +358,30 @@ async fn send_recv_many_unbounded_capacity() { assert_eq!(expected, buffer); } +#[maybe_tokio_test] +async fn recv_many_with_non_empty_buffer_bounded_rx_closed_and_idle() { + let (_tx, mut rx) = mpsc::channel::(1); + + let mut buffer: Vec = vec![1]; + + rx.close(); + + assert_eq!(0, rx.recv_many(&mut buffer, 1).await); + assert_eq!(vec![1], buffer); +} + +#[maybe_tokio_test] +async fn recv_many_with_non_empty_buffer_unbounded_rx_closed_and_idle() { + let (_tx, mut rx) = mpsc::unbounded_channel::(); + + let mut buffer: Vec = vec![1]; + + rx.close(); + + assert_eq!(0, rx.recv_many(&mut buffer, 1).await); + assert_eq!(vec![1], buffer); +} + #[tokio::test] #[cfg(feature = "full")] async fn async_send_recv_unbounded() {