| 1 |
#![allow(clippy::redundant_clone)] |
| 2 |
#![warn(rust_2018_idioms)] |
| 3 |
#![cfg(feature = "sync")] |
| 4 |
|
| 5 |
#[cfg(all(target_family = "wasm", not(target_os = "wasi")))] |
| 6 |
use wasm_bindgen_test::wasm_bindgen_test as test; |
| 7 |
#[cfg(all(target_family = "wasm", not(target_os = "wasi")))] |
| 8 |
use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test; |
| 9 |
|
| 10 |
#[cfg(not(all(target_family = "wasm", not(target_os = "wasi"))))] |
| 11 |
use tokio::test as maybe_tokio_test; |
| 12 |
|
| 13 |
use std::fmt; |
| 14 |
use std::sync::Arc; |
| 15 |
use tokio::sync::mpsc; |
| 16 |
use tokio::sync::mpsc::error::{TryRecvError, TrySendError}; |
| 17 |
use tokio_test::*; |
| 18 |
|
| 19 |
#[cfg(not(target_family = "wasm"))] |
| 20 |
mod support { |
| 21 |
pub(crate) mod mpsc_stream; |
| 22 |
} |
| 23 |
|
| 24 |
#[allow(unused)] |
| 25 |
trait AssertSend: Send {} |
| 26 |
impl AssertSend for mpsc::Sender<i32> {} |
| 27 |
impl AssertSend for mpsc::Receiver<i32> {} |
| 28 |
|
| 29 |
#[maybe_tokio_test] |
| 30 |
async fn send_recv_with_buffer() { |
| 31 |
let (tx, mut rx) = mpsc::channel::<i32>(16); |
| 32 |
|
| 33 |
// Using poll_ready / try_send |
| 34 |
// let permit assert_ready_ok!(tx.reserve()); |
| 35 |
let permit = tx.reserve().await.unwrap(); |
| 36 |
permit.send(1); |
| 37 |
|
| 38 |
// Without poll_ready |
| 39 |
tx.try_send(2).unwrap(); |
| 40 |
|
| 41 |
drop(tx); |
| 42 |
|
| 43 |
let val = rx.recv().await; |
| 44 |
assert_eq!(val, Some(1)); |
| 45 |
|
| 46 |
let val = rx.recv().await; |
| 47 |
assert_eq!(val, Some(2)); |
| 48 |
|
| 49 |
let val = rx.recv().await; |
| 50 |
assert!(val.is_none()); |
| 51 |
} |
| 52 |
|
| 53 |
#[tokio::test] |
| 54 |
#[cfg(feature = "full")] |
| 55 |
async fn reserve_disarm() { |
| 56 |
let (tx, mut rx) = mpsc::channel::<i32>(2); |
| 57 |
let tx1 = tx.clone(); |
| 58 |
let tx2 = tx.clone(); |
| 59 |
let tx3 = tx.clone(); |
| 60 |
let tx4 = tx; |
| 61 |
|
| 62 |
// We should be able to `poll_ready` two handles without problem |
| 63 |
let permit1 = assert_ok!(tx1.reserve().await); |
| 64 |
let permit2 = assert_ok!(tx2.reserve().await); |
| 65 |
|
| 66 |
// But a third should not be ready |
| 67 |
let mut r3 = tokio_test::task::spawn(tx3.reserve()); |
| 68 |
assert_pending!(r3.poll()); |
| 69 |
|
| 70 |
let mut r4 = tokio_test::task::spawn(tx4.reserve()); |
| 71 |
assert_pending!(r4.poll()); |
| 72 |
|
| 73 |
// Using one of the reserved slots should allow a new handle to become ready |
| 74 |
permit1.send(1); |
| 75 |
|
| 76 |
// We also need to receive for the slot to be free |
| 77 |
assert!(!r3.is_woken()); |
| 78 |
rx.recv().await.unwrap(); |
| 79 |
// Now there's a free slot! |
| 80 |
assert!(r3.is_woken()); |
| 81 |
assert!(!r4.is_woken()); |
| 82 |
|
| 83 |
// Dropping a permit should also open up a slot |
| 84 |
drop(permit2); |
| 85 |
assert!(r4.is_woken()); |
| 86 |
|
| 87 |
let mut r1 = tokio_test::task::spawn(tx1.reserve()); |
| 88 |
assert_pending!(r1.poll()); |
| 89 |
} |
| 90 |
|
| 91 |
#[tokio::test] |
| 92 |
#[cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi doesn't support threads |
| 93 |
async fn send_recv_stream_with_buffer() { |
| 94 |
use tokio_stream::StreamExt; |
| 95 |
|
| 96 |
let (tx, rx) = support::mpsc_stream::channel_stream::<i32>(16); |
| 97 |
let mut rx = Box::pin(rx); |
| 98 |
|
| 99 |
tokio::spawn(async move { |
| 100 |
assert_ok!(tx.send(1).await); |
| 101 |
assert_ok!(tx.send(2).await); |
| 102 |
}); |
| 103 |
|
| 104 |
assert_eq!(Some(1), rx.next().await); |
| 105 |
assert_eq!(Some(2), rx.next().await); |
| 106 |
assert_eq!(None, rx.next().await); |
| 107 |
} |
| 108 |
|
| 109 |
#[tokio::test] |
| 110 |
#[cfg(feature = "full")] |
| 111 |
async fn async_send_recv_with_buffer() { |
| 112 |
let (tx, mut rx) = mpsc::channel(16); |
| 113 |
|
| 114 |
tokio::spawn(async move { |
| 115 |
assert_ok!(tx.send(1).await); |
| 116 |
assert_ok!(tx.send(2).await); |
| 117 |
}); |
| 118 |
|
| 119 |
assert_eq!(Some(1), rx.recv().await); |
| 120 |
assert_eq!(Some(2), rx.recv().await); |
| 121 |
assert_eq!(None, rx.recv().await); |
| 122 |
} |
| 123 |
|
| 124 |
#[tokio::test] |
| 125 |
#[cfg(feature = "full")] |
| 126 |
async fn async_send_recv_many_with_buffer() { |
| 127 |
let (tx, mut rx) = mpsc::channel(2); |
| 128 |
let mut buffer = Vec::<i32>::with_capacity(3); |
| 129 |
|
| 130 |
// With `limit=0` does not sleep, returns immediately |
| 131 |
assert_eq!(0, rx.recv_many(&mut buffer, 0).await); |
| 132 |
|
| 133 |
let handle = tokio::spawn(async move { |
| 134 |
assert_ok!(tx.send(1).await); |
| 135 |
assert_ok!(tx.send(2).await); |
| 136 |
assert_ok!(tx.send(7).await); |
| 137 |
assert_ok!(tx.send(0).await); |
| 138 |
}); |
| 139 |
|
| 140 |
let limit = 3; |
| 141 |
let mut recv_count = 0usize; |
| 142 |
while recv_count < 4 { |
| 143 |
recv_count += rx.recv_many(&mut buffer, limit).await; |
| 144 |
assert_eq!(buffer.len(), recv_count); |
| 145 |
} |
| 146 |
|
| 147 |
assert_eq!(vec![1, 2, 7, 0], buffer); |
| 148 |
assert_eq!(0, rx.recv_many(&mut buffer, limit).await); |
| 149 |
handle.await.unwrap(); |
| 150 |
} |
| 151 |
|
| 152 |
#[tokio::test] |
| 153 |
#[cfg(feature = "full")] |
| 154 |
async fn start_send_past_cap() { |
| 155 |
use std::future::Future; |
| 156 |
|
| 157 |
let mut t1 = tokio_test::task::spawn(()); |
| 158 |
|
| 159 |
let (tx1, mut rx) = mpsc::channel(1); |
| 160 |
let tx2 = tx1.clone(); |
| 161 |
|
| 162 |
assert_ok!(tx1.try_send(())); |
| 163 |
|
| 164 |
let mut r1 = Box::pin(tx1.reserve()); |
| 165 |
t1.enter(|cx, _| assert_pending!(r1.as_mut().poll(cx))); |
| 166 |
|
| 167 |
{ |
| 168 |
let mut r2 = tokio_test::task::spawn(tx2.reserve()); |
| 169 |
assert_pending!(r2.poll()); |
| 170 |
|
| 171 |
drop(r1); |
| 172 |
|
| 173 |
assert!(rx.recv().await.is_some()); |
| 174 |
|
| 175 |
assert!(r2.is_woken()); |
| 176 |
assert!(!t1.is_woken()); |
| 177 |
} |
| 178 |
|
| 179 |
drop(tx1); |
| 180 |
drop(tx2); |
| 181 |
|
| 182 |
assert!(rx.recv().await.is_none()); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
#[should_panic] |
| 187 |
#[cfg(not(target_family = "wasm"))] // wasm currently doesn't support unwinding |
| 188 |
fn buffer_gteq_one() { |
| 189 |
mpsc::channel::<i32>(0); |
| 190 |
} |
| 191 |
|
| 192 |
#[maybe_tokio_test] |
| 193 |
async fn send_recv_unbounded() { |
| 194 |
let (tx, mut rx) = mpsc::unbounded_channel::<i32>(); |
| 195 |
|
| 196 |
// Using `try_send` |
| 197 |
assert_ok!(tx.send(1)); |
| 198 |
assert_ok!(tx.send(2)); |
| 199 |
|
| 200 |
assert_eq!(rx.recv().await, Some(1)); |
| 201 |
assert_eq!(rx.recv().await, Some(2)); |
| 202 |
|
| 203 |
drop(tx); |
| 204 |
|
| 205 |
assert!(rx.recv().await.is_none()); |
| 206 |
} |
| 207 |
|
| 208 |
#[maybe_tokio_test] |
| 209 |
async fn send_recv_many_unbounded() { |
| 210 |
let (tx, mut rx) = mpsc::unbounded_channel::<i32>(); |
| 211 |
|
| 212 |
let mut buffer: Vec<i32> = Vec::new(); |
| 213 |
|
| 214 |
// With `limit=0` does not sleep, returns immediately |
| 215 |
rx.recv_many(&mut buffer, 0).await; |
| 216 |
assert_eq!(0, buffer.len()); |
| 217 |
|
| 218 |
assert_ok!(tx.send(7)); |
| 219 |
assert_ok!(tx.send(13)); |
| 220 |
assert_ok!(tx.send(100)); |
| 221 |
assert_ok!(tx.send(1002)); |
| 222 |
|
| 223 |
rx.recv_many(&mut buffer, 0).await; |
| 224 |
assert_eq!(0, buffer.len()); |
| 225 |
|
| 226 |
let mut count = 0; |
| 227 |
while count < 4 { |
| 228 |
count += rx.recv_many(&mut buffer, 1).await; |
| 229 |
} |
| 230 |
assert_eq!(count, 4); |
| 231 |
assert_eq!(vec![7, 13, 100, 1002], buffer); |
| 232 |
let final_capacity = buffer.capacity(); |
| 233 |
assert!(final_capacity > 0); |
| 234 |
|
| 235 |
buffer.clear(); |
| 236 |
|
| 237 |
assert_ok!(tx.send(5)); |
| 238 |
assert_ok!(tx.send(6)); |
| 239 |
assert_ok!(tx.send(7)); |
| 240 |
assert_ok!(tx.send(2)); |
| 241 |
|
| 242 |
// Re-use existing capacity |
| 243 |
count = rx.recv_many(&mut buffer, 32).await; |
| 244 |
|
| 245 |
assert_eq!(final_capacity, buffer.capacity()); |
| 246 |
assert_eq!(count, 4); |
| 247 |
assert_eq!(vec![5, 6, 7, 2], buffer); |
| 248 |
|
| 249 |
drop(tx); |
| 250 |
|
| 251 |
// recv_many will immediately return zero if the channel |
| 252 |
// is closed and no more messages are waiting |
| 253 |
assert_eq!(0, rx.recv_many(&mut buffer, 4).await); |
| 254 |
assert!(rx.recv().await.is_none()); |
| 255 |
} |
| 256 |
|
| 257 |
#[tokio::test] |
| 258 |
#[cfg(feature = "full")] |
| 259 |
async fn send_recv_many_bounded_capacity() { |
| 260 |
let mut buffer: Vec<String> = Vec::with_capacity(9); |
| 261 |
let limit = buffer.capacity(); |
| 262 |
let (tx, mut rx) = mpsc::channel(100); |
| 263 |
|
| 264 |
let mut expected: Vec<String> = (0..limit) |
| 265 |
.map(|x: usize| format!("{x}")) |
| 266 |
.collect::<Vec<_>>(); |
| 267 |
for x in expected.clone() { |
| 268 |
tx.send(x).await.unwrap() |
| 269 |
} |
| 270 |
tx.send("one more".to_string()).await.unwrap(); |
| 271 |
|
| 272 |
// Here `recv_many` receives all but the last value; |
| 273 |
// the initial capacity is adequate, so the buffer does |
| 274 |
// not increase in side. |
| 275 |
assert_eq!(buffer.capacity(), rx.recv_many(&mut buffer, limit).await); |
| 276 |
assert_eq!(expected, buffer); |
| 277 |
assert_eq!(limit, buffer.capacity()); |
| 278 |
|
| 279 |
// Receive up more values: |
| 280 |
assert_eq!(1, rx.recv_many(&mut buffer, limit).await); |
| 281 |
assert!(buffer.capacity() > limit); |
| 282 |
expected.push("one more".to_string()); |
| 283 |
assert_eq!(expected, buffer); |
| 284 |
|
| 285 |
tokio::spawn(async move { |
| 286 |
tx.send("final".to_string()).await.unwrap(); |
| 287 |
}); |
| 288 |
|
| 289 |
// 'tx' is dropped, but `recv_many` is guaranteed not |
| 290 |
// to return 0 as the channel has outstanding permits |
| 291 |
assert_eq!(1, rx.recv_many(&mut buffer, limit).await); |
| 292 |
expected.push("final".to_string()); |
| 293 |
assert_eq!(expected, buffer); |
| 294 |
// The channel is now closed and `recv_many` returns 0. |
| 295 |
assert_eq!(0, rx.recv_many(&mut buffer, limit).await); |
| 296 |
assert_eq!(expected, buffer); |
| 297 |
} |
| 298 |
|
| 299 |
#[tokio::test] |
| 300 |
#[cfg(feature = "full")] |
| 301 |
async fn send_recv_many_unbounded_capacity() { |
| 302 |
let mut buffer: Vec<String> = Vec::with_capacity(9); // capacity >= 9 |
| 303 |
let limit = buffer.capacity(); |
| 304 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 305 |
|
| 306 |
let mut expected: Vec<String> = (0..limit) |
| 307 |
.map(|x: usize| format!("{x}")) |
| 308 |
.collect::<Vec<_>>(); |
| 309 |
for x in expected.clone() { |
| 310 |
tx.send(x).unwrap() |
| 311 |
} |
| 312 |
tx.send("one more".to_string()).unwrap(); |
| 313 |
|
| 314 |
// Here `recv_many` receives all but the last value; |
| 315 |
// the initial capacity is adequate, so the buffer does |
| 316 |
// not increase in side. |
| 317 |
assert_eq!(buffer.capacity(), rx.recv_many(&mut buffer, limit).await); |
| 318 |
assert_eq!(expected, buffer); |
| 319 |
assert_eq!(limit, buffer.capacity()); |
| 320 |
|
| 321 |
// Receive up more values: |
| 322 |
assert_eq!(1, rx.recv_many(&mut buffer, limit).await); |
| 323 |
assert!(buffer.capacity() > limit); |
| 324 |
expected.push("one more".to_string()); |
| 325 |
assert_eq!(expected, buffer); |
| 326 |
|
| 327 |
tokio::spawn(async move { |
| 328 |
tx.send("final".to_string()).unwrap(); |
| 329 |
}); |
| 330 |
|
| 331 |
// 'tx' is dropped, but `recv_many` is guaranteed not |
| 332 |
// to return 0 as the channel has outstanding permits |
| 333 |
assert_eq!(1, rx.recv_many(&mut buffer, limit).await); |
| 334 |
expected.push("final".to_string()); |
| 335 |
assert_eq!(expected, buffer); |
| 336 |
// The channel is now closed and `recv_many` returns 0. |
| 337 |
assert_eq!(0, rx.recv_many(&mut buffer, limit).await); |
| 338 |
assert_eq!(expected, buffer); |
| 339 |
} |
| 340 |
|
| 341 |
#[tokio::test] |
| 342 |
#[cfg(feature = "full")] |
| 343 |
async fn async_send_recv_unbounded() { |
| 344 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 345 |
|
| 346 |
tokio::spawn(async move { |
| 347 |
assert_ok!(tx.send(1)); |
| 348 |
assert_ok!(tx.send(2)); |
| 349 |
}); |
| 350 |
|
| 351 |
assert_eq!(Some(1), rx.recv().await); |
| 352 |
assert_eq!(Some(2), rx.recv().await); |
| 353 |
assert_eq!(None, rx.recv().await); |
| 354 |
} |
| 355 |
|
| 356 |
#[tokio::test] |
| 357 |
#[cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi doesn't support threads |
| 358 |
async fn send_recv_stream_unbounded() { |
| 359 |
use tokio_stream::StreamExt; |
| 360 |
|
| 361 |
let (tx, rx) = support::mpsc_stream::unbounded_channel_stream::<i32>(); |
| 362 |
|
| 363 |
let mut rx = Box::pin(rx); |
| 364 |
|
| 365 |
tokio::spawn(async move { |
| 366 |
assert_ok!(tx.send(1)); |
| 367 |
assert_ok!(tx.send(2)); |
| 368 |
}); |
| 369 |
|
| 370 |
assert_eq!(Some(1), rx.next().await); |
| 371 |
assert_eq!(Some(2), rx.next().await); |
| 372 |
assert_eq!(None, rx.next().await); |
| 373 |
} |
| 374 |
|
| 375 |
#[maybe_tokio_test] |
| 376 |
async fn no_t_bounds_buffer() { |
| 377 |
struct NoImpls; |
| 378 |
|
| 379 |
let (tx, mut rx) = mpsc::channel(100); |
| 380 |
|
| 381 |
// sender should be Debug even though T isn't Debug |
| 382 |
is_debug(&tx); |
| 383 |
// same with Receiver |
| 384 |
is_debug(&rx); |
| 385 |
// and sender should be Clone even though T isn't Clone |
| 386 |
assert!(tx.clone().try_send(NoImpls).is_ok()); |
| 387 |
|
| 388 |
assert!(rx.recv().await.is_some()); |
| 389 |
} |
| 390 |
|
| 391 |
#[maybe_tokio_test] |
| 392 |
async fn no_t_bounds_unbounded() { |
| 393 |
struct NoImpls; |
| 394 |
|
| 395 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 396 |
|
| 397 |
// sender should be Debug even though T isn't Debug |
| 398 |
is_debug(&tx); |
| 399 |
// same with Receiver |
| 400 |
is_debug(&rx); |
| 401 |
// and sender should be Clone even though T isn't Clone |
| 402 |
assert!(tx.clone().send(NoImpls).is_ok()); |
| 403 |
|
| 404 |
assert!(rx.recv().await.is_some()); |
| 405 |
} |
| 406 |
|
| 407 |
#[tokio::test] |
| 408 |
#[cfg(feature = "full")] |
| 409 |
async fn send_recv_buffer_limited() { |
| 410 |
let (tx, mut rx) = mpsc::channel::<i32>(1); |
| 411 |
|
| 412 |
// Reserve capacity |
| 413 |
let p1 = assert_ok!(tx.reserve().await); |
| 414 |
|
| 415 |
// Send first message |
| 416 |
p1.send(1); |
| 417 |
|
| 418 |
// Not ready |
| 419 |
let mut p2 = tokio_test::task::spawn(tx.reserve()); |
| 420 |
assert_pending!(p2.poll()); |
| 421 |
|
| 422 |
// Take the value |
| 423 |
assert!(rx.recv().await.is_some()); |
| 424 |
|
| 425 |
// Notified |
| 426 |
assert!(p2.is_woken()); |
| 427 |
|
| 428 |
// Trying to send fails |
| 429 |
assert_err!(tx.try_send(1337)); |
| 430 |
|
| 431 |
// Send second |
| 432 |
let permit = assert_ready_ok!(p2.poll()); |
| 433 |
permit.send(2); |
| 434 |
|
| 435 |
assert!(rx.recv().await.is_some()); |
| 436 |
} |
| 437 |
|
| 438 |
#[maybe_tokio_test] |
| 439 |
async fn recv_close_gets_none_idle() { |
| 440 |
let (tx, mut rx) = mpsc::channel::<i32>(10); |
| 441 |
|
| 442 |
rx.close(); |
| 443 |
|
| 444 |
assert!(rx.recv().await.is_none()); |
| 445 |
|
| 446 |
assert_err!(tx.send(1).await); |
| 447 |
} |
| 448 |
|
| 449 |
#[tokio::test] |
| 450 |
#[cfg(feature = "full")] |
| 451 |
async fn recv_close_gets_none_reserved() { |
| 452 |
let (tx1, mut rx) = mpsc::channel::<i32>(1); |
| 453 |
let tx2 = tx1.clone(); |
| 454 |
|
| 455 |
let permit1 = assert_ok!(tx1.reserve().await); |
| 456 |
let mut permit2 = tokio_test::task::spawn(tx2.reserve()); |
| 457 |
assert_pending!(permit2.poll()); |
| 458 |
|
| 459 |
rx.close(); |
| 460 |
|
| 461 |
assert!(permit2.is_woken()); |
| 462 |
assert_ready_err!(permit2.poll()); |
| 463 |
|
| 464 |
{ |
| 465 |
let mut recv = tokio_test::task::spawn(rx.recv()); |
| 466 |
assert_pending!(recv.poll()); |
| 467 |
|
| 468 |
permit1.send(123); |
| 469 |
assert!(recv.is_woken()); |
| 470 |
|
| 471 |
let v = assert_ready!(recv.poll()); |
| 472 |
assert_eq!(v, Some(123)); |
| 473 |
} |
| 474 |
|
| 475 |
assert!(rx.recv().await.is_none()); |
| 476 |
} |
| 477 |
|
| 478 |
#[maybe_tokio_test] |
| 479 |
async fn tx_close_gets_none() { |
| 480 |
let (_, mut rx) = mpsc::channel::<i32>(10); |
| 481 |
assert!(rx.recv().await.is_none()); |
| 482 |
} |
| 483 |
|
| 484 |
#[maybe_tokio_test] |
| 485 |
async fn try_send_fail() { |
| 486 |
let (tx, mut rx) = mpsc::channel(1); |
| 487 |
|
| 488 |
tx.try_send("hello").unwrap(); |
| 489 |
|
| 490 |
// This should fail |
| 491 |
match assert_err!(tx.try_send("fail")) { |
| 492 |
TrySendError::Full(..) => {} |
| 493 |
_ => panic!(), |
| 494 |
} |
| 495 |
|
| 496 |
assert_eq!(rx.recv().await, Some("hello")); |
| 497 |
|
| 498 |
assert_ok!(tx.try_send("goodbye")); |
| 499 |
drop(tx); |
| 500 |
|
| 501 |
assert_eq!(rx.recv().await, Some("goodbye")); |
| 502 |
assert!(rx.recv().await.is_none()); |
| 503 |
} |
| 504 |
|
| 505 |
#[maybe_tokio_test] |
| 506 |
async fn try_send_fail_with_try_recv() { |
| 507 |
let (tx, mut rx) = mpsc::channel(1); |
| 508 |
|
| 509 |
tx.try_send("hello").unwrap(); |
| 510 |
|
| 511 |
// This should fail |
| 512 |
match assert_err!(tx.try_send("fail")) { |
| 513 |
TrySendError::Full(..) => {} |
| 514 |
_ => panic!(), |
| 515 |
} |
| 516 |
|
| 517 |
assert_eq!(rx.try_recv(), Ok("hello")); |
| 518 |
|
| 519 |
assert_ok!(tx.try_send("goodbye")); |
| 520 |
drop(tx); |
| 521 |
|
| 522 |
assert_eq!(rx.try_recv(), Ok("goodbye")); |
| 523 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 524 |
} |
| 525 |
|
| 526 |
#[maybe_tokio_test] |
| 527 |
async fn reserve_many_above_cap() { |
| 528 |
const MAX_PERMITS: usize = tokio::sync::Semaphore::MAX_PERMITS; |
| 529 |
let (tx, _rx) = mpsc::channel::<()>(1); |
| 530 |
|
| 531 |
assert_err!(tx.reserve_many(2).await); |
| 532 |
assert_err!(tx.reserve_many(MAX_PERMITS + 1).await); |
| 533 |
assert_err!(tx.reserve_many(usize::MAX).await); |
| 534 |
} |
| 535 |
|
| 536 |
#[test] |
| 537 |
fn try_reserve_many_zero() { |
| 538 |
let (tx, rx) = mpsc::channel::<()>(1); |
| 539 |
|
| 540 |
// Succeeds when not closed. |
| 541 |
assert!(assert_ok!(tx.try_reserve_many(0)).next().is_none()); |
| 542 |
|
| 543 |
// Even when channel is full. |
| 544 |
tx.try_send(()).unwrap(); |
| 545 |
assert!(assert_ok!(tx.try_reserve_many(0)).next().is_none()); |
| 546 |
|
| 547 |
drop(rx); |
| 548 |
|
| 549 |
// Closed error when closed. |
| 550 |
assert_eq!( |
| 551 |
assert_err!(tx.try_reserve_many(0)), |
| 552 |
TrySendError::Closed(()) |
| 553 |
); |
| 554 |
} |
| 555 |
|
| 556 |
#[maybe_tokio_test] |
| 557 |
async fn reserve_many_zero() { |
| 558 |
let (tx, rx) = mpsc::channel::<()>(1); |
| 559 |
|
| 560 |
// Succeeds when not closed. |
| 561 |
assert!(assert_ok!(tx.reserve_many(0).await).next().is_none()); |
| 562 |
|
| 563 |
// Even when channel is full. |
| 564 |
tx.send(()).await.unwrap(); |
| 565 |
assert!(assert_ok!(tx.reserve_many(0).await).next().is_none()); |
| 566 |
|
| 567 |
drop(rx); |
| 568 |
|
| 569 |
// Closed error when closed. |
| 570 |
assert_err!(tx.reserve_many(0).await); |
| 571 |
} |
| 572 |
|
| 573 |
#[maybe_tokio_test] |
| 574 |
async fn try_reserve_many_edge_cases() { |
| 575 |
const MAX_PERMITS: usize = tokio::sync::Semaphore::MAX_PERMITS; |
| 576 |
|
| 577 |
let (tx, rx) = mpsc::channel::<()>(1); |
| 578 |
|
| 579 |
let mut permit = assert_ok!(tx.try_reserve_many(0)); |
| 580 |
assert!(permit.next().is_none()); |
| 581 |
|
| 582 |
let permit = tx.try_reserve_many(MAX_PERMITS + 1); |
| 583 |
match assert_err!(permit) { |
| 584 |
TrySendError::Full(..) => {} |
| 585 |
_ => panic!(), |
| 586 |
} |
| 587 |
|
| 588 |
let permit = tx.try_reserve_many(usize::MAX); |
| 589 |
match assert_err!(permit) { |
| 590 |
TrySendError::Full(..) => {} |
| 591 |
_ => panic!(), |
| 592 |
} |
| 593 |
|
| 594 |
// Dropping the receiver should close the channel |
| 595 |
drop(rx); |
| 596 |
assert_err!(tx.reserve_many(0).await); |
| 597 |
} |
| 598 |
|
| 599 |
#[maybe_tokio_test] |
| 600 |
async fn try_reserve_fails() { |
| 601 |
let (tx, mut rx) = mpsc::channel(1); |
| 602 |
|
| 603 |
let permit = tx.try_reserve().unwrap(); |
| 604 |
|
| 605 |
// This should fail |
| 606 |
match assert_err!(tx.try_reserve()) { |
| 607 |
TrySendError::Full(()) => {} |
| 608 |
_ => panic!(), |
| 609 |
} |
| 610 |
|
| 611 |
permit.send("foo"); |
| 612 |
|
| 613 |
assert_eq!(rx.recv().await, Some("foo")); |
| 614 |
|
| 615 |
// Dropping permit releases the slot. |
| 616 |
let permit = tx.try_reserve().unwrap(); |
| 617 |
drop(permit); |
| 618 |
|
| 619 |
let _permit = tx.try_reserve().unwrap(); |
| 620 |
} |
| 621 |
|
| 622 |
#[maybe_tokio_test] |
| 623 |
async fn reserve_many_and_send() { |
| 624 |
let (tx, mut rx) = mpsc::channel(100); |
| 625 |
for i in 0..100 { |
| 626 |
for permit in assert_ok!(tx.reserve_many(i).await) { |
| 627 |
permit.send("foo"); |
| 628 |
assert_eq!(rx.recv().await, Some("foo")); |
| 629 |
} |
| 630 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 631 |
} |
| 632 |
} |
| 633 |
#[maybe_tokio_test] |
| 634 |
async fn try_reserve_many_and_send() { |
| 635 |
let (tx, mut rx) = mpsc::channel(100); |
| 636 |
for i in 0..100 { |
| 637 |
for permit in assert_ok!(tx.try_reserve_many(i)) { |
| 638 |
permit.send("foo"); |
| 639 |
assert_eq!(rx.recv().await, Some("foo")); |
| 640 |
} |
| 641 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
#[maybe_tokio_test] |
| 646 |
async fn reserve_many_on_closed_channel() { |
| 647 |
let (tx, rx) = mpsc::channel::<()>(100); |
| 648 |
drop(rx); |
| 649 |
assert_err!(tx.reserve_many(10).await); |
| 650 |
} |
| 651 |
|
| 652 |
#[maybe_tokio_test] |
| 653 |
async fn try_reserve_many_on_closed_channel() { |
| 654 |
let (tx, rx) = mpsc::channel::<usize>(100); |
| 655 |
drop(rx); |
| 656 |
match assert_err!(tx.try_reserve_many(10)) { |
| 657 |
TrySendError::Closed(()) => {} |
| 658 |
_ => panic!(), |
| 659 |
}; |
| 660 |
} |
| 661 |
|
| 662 |
#[maybe_tokio_test] |
| 663 |
async fn try_reserve_many_full() { |
| 664 |
// Reserve n capacity and send k messages |
| 665 |
for n in 1..100 { |
| 666 |
for k in 0..n { |
| 667 |
let (tx, mut rx) = mpsc::channel::<usize>(n); |
| 668 |
let permits = assert_ok!(tx.try_reserve_many(n)); |
| 669 |
|
| 670 |
assert_eq!(permits.len(), n); |
| 671 |
assert_eq!(tx.capacity(), 0); |
| 672 |
|
| 673 |
match assert_err!(tx.try_reserve_many(1)) { |
| 674 |
TrySendError::Full(..) => {} |
| 675 |
_ => panic!(), |
| 676 |
}; |
| 677 |
|
| 678 |
for permit in permits.take(k) { |
| 679 |
permit.send(0); |
| 680 |
} |
| 681 |
// We only used k permits on the n reserved |
| 682 |
assert_eq!(tx.capacity(), n - k); |
| 683 |
|
| 684 |
// We can reserve more permits |
| 685 |
assert_ok!(tx.try_reserve_many(1)); |
| 686 |
|
| 687 |
// But not more than the current capacity |
| 688 |
match assert_err!(tx.try_reserve_many(n - k + 1)) { |
| 689 |
TrySendError::Full(..) => {} |
| 690 |
_ => panic!(), |
| 691 |
}; |
| 692 |
|
| 693 |
for _i in 0..k { |
| 694 |
assert_eq!(rx.recv().await, Some(0)); |
| 695 |
} |
| 696 |
|
| 697 |
// Now that we've received everything, capacity should be back to n |
| 698 |
assert_eq!(tx.capacity(), n); |
| 699 |
} |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
#[tokio::test] |
| 704 |
#[cfg(feature = "full")] |
| 705 |
async fn drop_permit_releases_permit() { |
| 706 |
// poll_ready reserves capacity, ensure that the capacity is released if tx |
| 707 |
// is dropped w/o sending a value. |
| 708 |
let (tx1, _rx) = mpsc::channel::<i32>(1); |
| 709 |
let tx2 = tx1.clone(); |
| 710 |
|
| 711 |
let permit = assert_ok!(tx1.reserve().await); |
| 712 |
|
| 713 |
let mut reserve2 = tokio_test::task::spawn(tx2.reserve()); |
| 714 |
assert_pending!(reserve2.poll()); |
| 715 |
|
| 716 |
drop(permit); |
| 717 |
|
| 718 |
assert!(reserve2.is_woken()); |
| 719 |
assert_ready_ok!(reserve2.poll()); |
| 720 |
} |
| 721 |
|
| 722 |
#[maybe_tokio_test] |
| 723 |
async fn drop_permit_iterator_releases_permits() { |
| 724 |
// poll_ready reserves capacity, ensure that the capacity is released if tx |
| 725 |
// is dropped w/o sending a value. |
| 726 |
for n in 1..100 { |
| 727 |
let (tx1, _rx) = mpsc::channel::<i32>(n); |
| 728 |
let tx2 = tx1.clone(); |
| 729 |
|
| 730 |
let permits = assert_ok!(tx1.reserve_many(n).await); |
| 731 |
|
| 732 |
let mut reserve2 = tokio_test::task::spawn(tx2.reserve_many(n)); |
| 733 |
assert_pending!(reserve2.poll()); |
| 734 |
|
| 735 |
drop(permits); |
| 736 |
|
| 737 |
assert!(reserve2.is_woken()); |
| 738 |
|
| 739 |
let permits = assert_ready_ok!(reserve2.poll()); |
| 740 |
drop(permits); |
| 741 |
|
| 742 |
assert_eq!(tx1.capacity(), n); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
#[maybe_tokio_test] |
| 747 |
async fn dropping_rx_closes_channel() { |
| 748 |
let (tx, rx) = mpsc::channel(100); |
| 749 |
|
| 750 |
let msg = Arc::new(()); |
| 751 |
assert_ok!(tx.try_send(msg.clone())); |
| 752 |
|
| 753 |
drop(rx); |
| 754 |
assert_err!(tx.reserve().await); |
| 755 |
assert_err!(tx.reserve_many(10).await); |
| 756 |
assert_eq!(1, Arc::strong_count(&msg)); |
| 757 |
} |
| 758 |
|
| 759 |
#[test] |
| 760 |
fn dropping_rx_closes_channel_for_try() { |
| 761 |
let (tx, rx) = mpsc::channel(100); |
| 762 |
|
| 763 |
let msg = Arc::new(()); |
| 764 |
tx.try_send(msg.clone()).unwrap(); |
| 765 |
|
| 766 |
drop(rx); |
| 767 |
|
| 768 |
assert!(matches!( |
| 769 |
tx.try_send(msg.clone()), |
| 770 |
Err(TrySendError::Closed(_)) |
| 771 |
)); |
| 772 |
assert!(matches!(tx.try_reserve(), Err(TrySendError::Closed(_)))); |
| 773 |
assert!(matches!( |
| 774 |
tx.try_reserve_owned(), |
| 775 |
Err(TrySendError::Closed(_)) |
| 776 |
)); |
| 777 |
|
| 778 |
assert_eq!(1, Arc::strong_count(&msg)); |
| 779 |
} |
| 780 |
|
| 781 |
#[test] |
| 782 |
fn unconsumed_messages_are_dropped() { |
| 783 |
let msg = Arc::new(()); |
| 784 |
|
| 785 |
let (tx, rx) = mpsc::channel(100); |
| 786 |
|
| 787 |
tx.try_send(msg.clone()).unwrap(); |
| 788 |
|
| 789 |
assert_eq!(2, Arc::strong_count(&msg)); |
| 790 |
|
| 791 |
drop((tx, rx)); |
| 792 |
|
| 793 |
assert_eq!(1, Arc::strong_count(&msg)); |
| 794 |
} |
| 795 |
|
| 796 |
#[test] |
| 797 |
#[cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi doesn't support threads |
| 798 |
fn blocking_recv() { |
| 799 |
let (tx, mut rx) = mpsc::channel::<u8>(1); |
| 800 |
|
| 801 |
let sync_code = std::thread::spawn(move || { |
| 802 |
assert_eq!(Some(10), rx.blocking_recv()); |
| 803 |
}); |
| 804 |
|
| 805 |
tokio::runtime::Runtime::new() |
| 806 |
.unwrap() |
| 807 |
.block_on(async move { |
| 808 |
let _ = tx.send(10).await; |
| 809 |
}); |
| 810 |
sync_code.join().unwrap() |
| 811 |
} |
| 812 |
|
| 813 |
#[tokio::test] |
| 814 |
#[should_panic] |
| 815 |
#[cfg(not(target_family = "wasm"))] // wasm currently doesn't support unwinding |
| 816 |
async fn blocking_recv_async() { |
| 817 |
let (_tx, mut rx) = mpsc::channel::<()>(1); |
| 818 |
let _ = rx.blocking_recv(); |
| 819 |
} |
| 820 |
|
| 821 |
#[test] |
| 822 |
#[cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi doesn't support threads |
| 823 |
fn blocking_send() { |
| 824 |
let (tx, mut rx) = mpsc::channel::<u8>(1); |
| 825 |
|
| 826 |
let sync_code = std::thread::spawn(move || { |
| 827 |
tx.blocking_send(10).unwrap(); |
| 828 |
}); |
| 829 |
|
| 830 |
tokio::runtime::Runtime::new() |
| 831 |
.unwrap() |
| 832 |
.block_on(async move { |
| 833 |
assert_eq!(Some(10), rx.recv().await); |
| 834 |
}); |
| 835 |
sync_code.join().unwrap() |
| 836 |
} |
| 837 |
|
| 838 |
#[tokio::test] |
| 839 |
#[should_panic] |
| 840 |
#[cfg(not(target_family = "wasm"))] // wasm currently doesn't support unwinding |
| 841 |
async fn blocking_send_async() { |
| 842 |
let (tx, _rx) = mpsc::channel::<()>(1); |
| 843 |
let _ = tx.blocking_send(()); |
| 844 |
} |
| 845 |
|
| 846 |
#[tokio::test] |
| 847 |
#[cfg(feature = "full")] |
| 848 |
async fn ready_close_cancel_bounded() { |
| 849 |
let (tx, mut rx) = mpsc::channel::<()>(100); |
| 850 |
let _tx2 = tx.clone(); |
| 851 |
|
| 852 |
let permit = assert_ok!(tx.reserve().await); |
| 853 |
|
| 854 |
rx.close(); |
| 855 |
|
| 856 |
let mut recv = tokio_test::task::spawn(rx.recv()); |
| 857 |
assert_pending!(recv.poll()); |
| 858 |
|
| 859 |
drop(permit); |
| 860 |
|
| 861 |
assert!(recv.is_woken()); |
| 862 |
let val = assert_ready!(recv.poll()); |
| 863 |
assert!(val.is_none()); |
| 864 |
} |
| 865 |
|
| 866 |
#[tokio::test] |
| 867 |
#[cfg(feature = "full")] |
| 868 |
async fn permit_available_not_acquired_close() { |
| 869 |
let (tx1, mut rx) = mpsc::channel::<()>(1); |
| 870 |
let tx2 = tx1.clone(); |
| 871 |
|
| 872 |
let permit1 = assert_ok!(tx1.reserve().await); |
| 873 |
|
| 874 |
let mut permit2 = tokio_test::task::spawn(tx2.reserve()); |
| 875 |
assert_pending!(permit2.poll()); |
| 876 |
|
| 877 |
rx.close(); |
| 878 |
|
| 879 |
drop(permit1); |
| 880 |
assert!(permit2.is_woken()); |
| 881 |
|
| 882 |
drop(permit2); |
| 883 |
assert!(rx.recv().await.is_none()); |
| 884 |
} |
| 885 |
|
| 886 |
#[test] |
| 887 |
fn try_recv_bounded() { |
| 888 |
let (tx, mut rx) = mpsc::channel(5); |
| 889 |
|
| 890 |
tx.try_send("hello").unwrap(); |
| 891 |
tx.try_send("hello").unwrap(); |
| 892 |
tx.try_send("hello").unwrap(); |
| 893 |
tx.try_send("hello").unwrap(); |
| 894 |
tx.try_send("hello").unwrap(); |
| 895 |
assert!(tx.try_send("hello").is_err()); |
| 896 |
|
| 897 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 898 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 899 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 900 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 901 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 902 |
assert_eq!(Err(TryRecvError::Empty), rx.try_recv()); |
| 903 |
|
| 904 |
tx.try_send("hello").unwrap(); |
| 905 |
tx.try_send("hello").unwrap(); |
| 906 |
tx.try_send("hello").unwrap(); |
| 907 |
tx.try_send("hello").unwrap(); |
| 908 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 909 |
tx.try_send("hello").unwrap(); |
| 910 |
tx.try_send("hello").unwrap(); |
| 911 |
assert!(tx.try_send("hello").is_err()); |
| 912 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 913 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 914 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 915 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 916 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 917 |
assert_eq!(Err(TryRecvError::Empty), rx.try_recv()); |
| 918 |
|
| 919 |
tx.try_send("hello").unwrap(); |
| 920 |
tx.try_send("hello").unwrap(); |
| 921 |
tx.try_send("hello").unwrap(); |
| 922 |
drop(tx); |
| 923 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 924 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 925 |
assert_eq!(Ok("hello"), rx.try_recv()); |
| 926 |
assert_eq!(Err(TryRecvError::Disconnected), rx.try_recv()); |
| 927 |
} |
| 928 |
|
| 929 |
#[test] |
| 930 |
fn try_recv_unbounded() { |
| 931 |
for num in 0..100 { |
| 932 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 933 |
|
| 934 |
for i in 0..num { |
| 935 |
tx.send(i).unwrap(); |
| 936 |
} |
| 937 |
|
| 938 |
for i in 0..num { |
| 939 |
assert_eq!(rx.try_recv(), Ok(i)); |
| 940 |
} |
| 941 |
|
| 942 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 943 |
drop(tx); |
| 944 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
#[test] |
| 949 |
fn try_recv_close_while_empty_bounded() { |
| 950 |
let (tx, mut rx) = mpsc::channel::<()>(5); |
| 951 |
|
| 952 |
assert_eq!(Err(TryRecvError::Empty), rx.try_recv()); |
| 953 |
drop(tx); |
| 954 |
assert_eq!(Err(TryRecvError::Disconnected), rx.try_recv()); |
| 955 |
} |
| 956 |
|
| 957 |
#[test] |
| 958 |
fn try_recv_close_while_empty_unbounded() { |
| 959 |
let (tx, mut rx) = mpsc::unbounded_channel::<()>(); |
| 960 |
|
| 961 |
assert_eq!(Err(TryRecvError::Empty), rx.try_recv()); |
| 962 |
drop(tx); |
| 963 |
assert_eq!(Err(TryRecvError::Disconnected), rx.try_recv()); |
| 964 |
} |
| 965 |
|
| 966 |
#[tokio::test(start_paused = true)] |
| 967 |
#[cfg(feature = "full")] |
| 968 |
async fn recv_timeout() { |
| 969 |
use tokio::sync::mpsc::error::SendTimeoutError::{Closed, Timeout}; |
| 970 |
use tokio::time::Duration; |
| 971 |
|
| 972 |
let (tx, rx) = mpsc::channel(5); |
| 973 |
|
| 974 |
assert_eq!(tx.send_timeout(10, Duration::from_secs(1)).await, Ok(())); |
| 975 |
assert_eq!(tx.send_timeout(20, Duration::from_secs(1)).await, Ok(())); |
| 976 |
assert_eq!(tx.send_timeout(30, Duration::from_secs(1)).await, Ok(())); |
| 977 |
assert_eq!(tx.send_timeout(40, Duration::from_secs(1)).await, Ok(())); |
| 978 |
assert_eq!(tx.send_timeout(50, Duration::from_secs(1)).await, Ok(())); |
| 979 |
assert_eq!( |
| 980 |
tx.send_timeout(60, Duration::from_secs(1)).await, |
| 981 |
Err(Timeout(60)) |
| 982 |
); |
| 983 |
|
| 984 |
drop(rx); |
| 985 |
assert_eq!( |
| 986 |
tx.send_timeout(70, Duration::from_secs(1)).await, |
| 987 |
Err(Closed(70)) |
| 988 |
); |
| 989 |
} |
| 990 |
|
| 991 |
#[test] |
| 992 |
#[should_panic = "there is no reactor running, must be called from the context of a Tokio 1.x runtime"] |
| 993 |
#[cfg(not(target_family = "wasm"))] // wasm currently doesn't support unwinding |
| 994 |
fn recv_timeout_panic() { |
| 995 |
use futures::future::FutureExt; |
| 996 |
use tokio::time::Duration; |
| 997 |
|
| 998 |
let (tx, _rx) = mpsc::channel(5); |
| 999 |
tx.send_timeout(10, Duration::from_secs(1)).now_or_never(); |
| 1000 |
} |
| 1001 |
|
| 1002 |
// Tests that channel `capacity` changes and `max_capacity` stays the same |
| 1003 |
#[tokio::test] |
| 1004 |
async fn test_tx_capacity() { |
| 1005 |
let (tx, _rx) = mpsc::channel::<()>(10); |
| 1006 |
// both capacities are same before |
| 1007 |
assert_eq!(tx.capacity(), 10); |
| 1008 |
assert_eq!(tx.max_capacity(), 10); |
| 1009 |
|
| 1010 |
let _permit = tx.reserve().await.unwrap(); |
| 1011 |
// after reserve, only capacity should drop by one |
| 1012 |
assert_eq!(tx.capacity(), 9); |
| 1013 |
assert_eq!(tx.max_capacity(), 10); |
| 1014 |
|
| 1015 |
tx.send(()).await.unwrap(); |
| 1016 |
// after send, capacity should drop by one again |
| 1017 |
assert_eq!(tx.capacity(), 8); |
| 1018 |
assert_eq!(tx.max_capacity(), 10); |
| 1019 |
} |
| 1020 |
|
| 1021 |
#[tokio::test] |
| 1022 |
async fn test_rx_is_closed_when_calling_close_with_sender() { |
| 1023 |
// is_closed should return true after calling close but still has a sender |
| 1024 |
let (_tx, mut rx) = mpsc::channel::<()>(10); |
| 1025 |
rx.close(); |
| 1026 |
|
| 1027 |
assert!(rx.is_closed()); |
| 1028 |
} |
| 1029 |
|
| 1030 |
#[tokio::test] |
| 1031 |
async fn test_rx_is_closed_when_dropping_all_senders() { |
| 1032 |
// is_closed should return true after dropping all senders |
| 1033 |
let (tx, rx) = mpsc::channel::<()>(10); |
| 1034 |
let another_tx = tx.clone(); |
| 1035 |
let task = tokio::spawn(async move { |
| 1036 |
drop(another_tx); |
| 1037 |
}); |
| 1038 |
|
| 1039 |
drop(tx); |
| 1040 |
let _ = task.await; |
| 1041 |
|
| 1042 |
assert!(rx.is_closed()); |
| 1043 |
} |
| 1044 |
|
| 1045 |
#[tokio::test] |
| 1046 |
async fn test_rx_is_not_closed_when_there_are_senders() { |
| 1047 |
// is_closed should return false when there is a sender |
| 1048 |
let (_tx, rx) = mpsc::channel::<()>(10); |
| 1049 |
assert!(!rx.is_closed()); |
| 1050 |
} |
| 1051 |
|
| 1052 |
#[tokio::test] |
| 1053 |
async fn test_rx_is_not_closed_when_there_are_senders_and_buffer_filled() { |
| 1054 |
// is_closed should return false when there is a sender, even if enough messages have been sent to fill the channel |
| 1055 |
let (tx, rx) = mpsc::channel(10); |
| 1056 |
for i in 0..10 { |
| 1057 |
assert!(tx.send(i).await.is_ok()); |
| 1058 |
} |
| 1059 |
assert!(!rx.is_closed()); |
| 1060 |
} |
| 1061 |
|
| 1062 |
#[tokio::test] |
| 1063 |
async fn test_rx_is_closed_when_there_are_no_senders_and_there_are_messages() { |
| 1064 |
// is_closed should return true when there are messages in the buffer, but no senders |
| 1065 |
let (tx, rx) = mpsc::channel(10); |
| 1066 |
for i in 0..10 { |
| 1067 |
assert!(tx.send(i).await.is_ok()); |
| 1068 |
} |
| 1069 |
drop(tx); |
| 1070 |
assert!(rx.is_closed()); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#[tokio::test] |
| 1074 |
async fn test_rx_is_closed_when_there_are_messages_and_close_is_called() { |
| 1075 |
// is_closed should return true when there are messages in the buffer, and close is called |
| 1076 |
let (tx, mut rx) = mpsc::channel(10); |
| 1077 |
for i in 0..10 { |
| 1078 |
assert!(tx.send(i).await.is_ok()); |
| 1079 |
} |
| 1080 |
rx.close(); |
| 1081 |
assert!(rx.is_closed()); |
| 1082 |
} |
| 1083 |
|
| 1084 |
#[tokio::test] |
| 1085 |
async fn test_rx_is_not_closed_when_there_are_permits_but_not_senders() { |
| 1086 |
// is_closed should return false when there is a permit (but no senders) |
| 1087 |
let (tx, rx) = mpsc::channel::<()>(10); |
| 1088 |
let _permit = tx.reserve_owned().await.expect("Failed to reserve permit"); |
| 1089 |
assert!(!rx.is_closed()); |
| 1090 |
} |
| 1091 |
|
| 1092 |
#[tokio::test] |
| 1093 |
async fn test_rx_is_empty_when_no_messages_were_sent() { |
| 1094 |
let (_tx, rx) = mpsc::channel::<()>(10); |
| 1095 |
assert!(rx.is_empty()) |
| 1096 |
} |
| 1097 |
|
| 1098 |
#[tokio::test] |
| 1099 |
async fn test_rx_is_not_empty_when_there_are_messages_in_the_buffer() { |
| 1100 |
let (tx, rx) = mpsc::channel::<()>(10); |
| 1101 |
assert!(tx.send(()).await.is_ok()); |
| 1102 |
assert!(!rx.is_empty()) |
| 1103 |
} |
| 1104 |
|
| 1105 |
#[tokio::test] |
| 1106 |
async fn test_rx_is_not_empty_when_the_buffer_is_full() { |
| 1107 |
let (tx, rx) = mpsc::channel(10); |
| 1108 |
for i in 0..10 { |
| 1109 |
assert!(tx.send(i).await.is_ok()); |
| 1110 |
} |
| 1111 |
assert!(!rx.is_empty()) |
| 1112 |
} |
| 1113 |
|
| 1114 |
#[tokio::test] |
| 1115 |
async fn test_rx_is_not_empty_when_all_but_one_messages_are_consumed() { |
| 1116 |
let (tx, mut rx) = mpsc::channel(10); |
| 1117 |
for i in 0..10 { |
| 1118 |
assert!(tx.send(i).await.is_ok()); |
| 1119 |
} |
| 1120 |
|
| 1121 |
for _ in 0..9 { |
| 1122 |
assert!(rx.recv().await.is_some()); |
| 1123 |
} |
| 1124 |
|
| 1125 |
assert!(!rx.is_empty()) |
| 1126 |
} |
| 1127 |
|
| 1128 |
#[tokio::test] |
| 1129 |
async fn test_rx_is_empty_when_all_messages_are_consumed() { |
| 1130 |
let (tx, mut rx) = mpsc::channel(10); |
| 1131 |
for i in 0..10 { |
| 1132 |
assert!(tx.send(i).await.is_ok()); |
| 1133 |
} |
| 1134 |
while rx.try_recv().is_ok() {} |
| 1135 |
assert!(rx.is_empty()) |
| 1136 |
} |
| 1137 |
|
| 1138 |
#[tokio::test] |
| 1139 |
async fn test_rx_is_empty_all_senders_are_dropped_and_messages_consumed() { |
| 1140 |
let (tx, mut rx) = mpsc::channel(10); |
| 1141 |
for i in 0..10 { |
| 1142 |
assert!(tx.send(i).await.is_ok()); |
| 1143 |
} |
| 1144 |
drop(tx); |
| 1145 |
|
| 1146 |
for _ in 0..10 { |
| 1147 |
assert!(rx.recv().await.is_some()); |
| 1148 |
} |
| 1149 |
|
| 1150 |
assert!(rx.is_empty()) |
| 1151 |
} |
| 1152 |
|
| 1153 |
#[tokio::test] |
| 1154 |
async fn test_rx_len_on_empty_channel() { |
| 1155 |
let (_tx, rx) = mpsc::channel::<()>(100); |
| 1156 |
assert_eq!(rx.len(), 0); |
| 1157 |
} |
| 1158 |
|
| 1159 |
#[tokio::test] |
| 1160 |
async fn test_rx_len_on_empty_channel_without_senders() { |
| 1161 |
// when all senders are dropped, a "closed" value is added to the end of the linked list. |
| 1162 |
// here we test that the "closed" value does not change the len of the channel. |
| 1163 |
|
| 1164 |
let (tx, rx) = mpsc::channel::<()>(100); |
| 1165 |
drop(tx); |
| 1166 |
assert_eq!(rx.len(), 0); |
| 1167 |
} |
| 1168 |
|
| 1169 |
#[tokio::test] |
| 1170 |
async fn test_rx_len_on_filled_channel() { |
| 1171 |
let (tx, rx) = mpsc::channel(100); |
| 1172 |
|
| 1173 |
for i in 0..100 { |
| 1174 |
assert!(tx.send(i).await.is_ok()); |
| 1175 |
} |
| 1176 |
assert_eq!(rx.len(), 100); |
| 1177 |
} |
| 1178 |
|
| 1179 |
#[tokio::test] |
| 1180 |
async fn test_rx_len_on_filled_channel_without_senders() { |
| 1181 |
let (tx, rx) = mpsc::channel(100); |
| 1182 |
|
| 1183 |
for i in 0..100 { |
| 1184 |
assert!(tx.send(i).await.is_ok()); |
| 1185 |
} |
| 1186 |
drop(tx); |
| 1187 |
assert_eq!(rx.len(), 100); |
| 1188 |
} |
| 1189 |
|
| 1190 |
#[tokio::test] |
| 1191 |
async fn test_rx_len_when_consuming_all_messages() { |
| 1192 |
let (tx, mut rx) = mpsc::channel(100); |
| 1193 |
|
| 1194 |
for i in 0..100 { |
| 1195 |
assert!(tx.send(i).await.is_ok()); |
| 1196 |
assert_eq!(rx.len(), i + 1); |
| 1197 |
} |
| 1198 |
|
| 1199 |
drop(tx); |
| 1200 |
|
| 1201 |
for i in (0..100).rev() { |
| 1202 |
assert!(rx.recv().await.is_some()); |
| 1203 |
assert_eq!(rx.len(), i); |
| 1204 |
} |
| 1205 |
} |
| 1206 |
|
| 1207 |
#[tokio::test] |
| 1208 |
async fn test_rx_len_when_close_is_called() { |
| 1209 |
let (tx, mut rx) = mpsc::channel(100); |
| 1210 |
tx.send(()).await.unwrap(); |
| 1211 |
rx.close(); |
| 1212 |
|
| 1213 |
assert_eq!(rx.len(), 1); |
| 1214 |
} |
| 1215 |
|
| 1216 |
#[tokio::test] |
| 1217 |
async fn test_rx_len_when_close_is_called_before_dropping_sender() { |
| 1218 |
let (tx, mut rx) = mpsc::channel(100); |
| 1219 |
tx.send(()).await.unwrap(); |
| 1220 |
rx.close(); |
| 1221 |
drop(tx); |
| 1222 |
|
| 1223 |
assert_eq!(rx.len(), 1); |
| 1224 |
} |
| 1225 |
|
| 1226 |
#[tokio::test] |
| 1227 |
async fn test_rx_len_when_close_is_called_after_dropping_sender() { |
| 1228 |
let (tx, mut rx) = mpsc::channel(100); |
| 1229 |
tx.send(()).await.unwrap(); |
| 1230 |
drop(tx); |
| 1231 |
rx.close(); |
| 1232 |
|
| 1233 |
assert_eq!(rx.len(), 1); |
| 1234 |
} |
| 1235 |
|
| 1236 |
#[tokio::test] |
| 1237 |
async fn test_rx_unbounded_is_closed_when_calling_close_with_sender() { |
| 1238 |
// is_closed should return true after calling close but still has a sender |
| 1239 |
let (_tx, mut rx) = mpsc::unbounded_channel::<()>(); |
| 1240 |
rx.close(); |
| 1241 |
|
| 1242 |
assert!(rx.is_closed()); |
| 1243 |
} |
| 1244 |
|
| 1245 |
#[tokio::test] |
| 1246 |
async fn test_rx_unbounded_is_closed_when_dropping_all_senders() { |
| 1247 |
// is_closed should return true after dropping all senders |
| 1248 |
let (tx, rx) = mpsc::unbounded_channel::<()>(); |
| 1249 |
let another_tx = tx.clone(); |
| 1250 |
let task = tokio::spawn(async move { |
| 1251 |
drop(another_tx); |
| 1252 |
}); |
| 1253 |
|
| 1254 |
drop(tx); |
| 1255 |
let _ = task.await; |
| 1256 |
|
| 1257 |
assert!(rx.is_closed()); |
| 1258 |
} |
| 1259 |
|
| 1260 |
#[tokio::test] |
| 1261 |
async fn test_rx_unbounded_is_not_closed_when_there_are_senders() { |
| 1262 |
// is_closed should return false when there is a sender |
| 1263 |
let (_tx, rx) = mpsc::unbounded_channel::<()>(); |
| 1264 |
assert!(!rx.is_closed()); |
| 1265 |
} |
| 1266 |
|
| 1267 |
#[tokio::test] |
| 1268 |
async fn test_rx_unbounded_is_closed_when_there_are_no_senders_and_there_are_messages() { |
| 1269 |
// is_closed should return true when there are messages in the buffer, but no senders |
| 1270 |
let (tx, rx) = mpsc::unbounded_channel(); |
| 1271 |
for i in 0..10 { |
| 1272 |
assert!(tx.send(i).is_ok()); |
| 1273 |
} |
| 1274 |
drop(tx); |
| 1275 |
assert!(rx.is_closed()); |
| 1276 |
} |
| 1277 |
|
| 1278 |
#[tokio::test] |
| 1279 |
async fn test_rx_unbounded_is_closed_when_there_are_messages_and_close_is_called() { |
| 1280 |
// is_closed should return true when there are messages in the buffer, and close is called |
| 1281 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1282 |
for i in 0..10 { |
| 1283 |
assert!(tx.send(i).is_ok()); |
| 1284 |
} |
| 1285 |
rx.close(); |
| 1286 |
assert!(rx.is_closed()); |
| 1287 |
} |
| 1288 |
|
| 1289 |
#[tokio::test] |
| 1290 |
async fn test_rx_unbounded_is_empty_when_no_messages_were_sent() { |
| 1291 |
let (_tx, rx) = mpsc::unbounded_channel::<()>(); |
| 1292 |
assert!(rx.is_empty()) |
| 1293 |
} |
| 1294 |
|
| 1295 |
#[tokio::test] |
| 1296 |
async fn test_rx_unbounded_is_not_empty_when_there_are_messages_in_the_buffer() { |
| 1297 |
let (tx, rx) = mpsc::unbounded_channel(); |
| 1298 |
assert!(tx.send(()).is_ok()); |
| 1299 |
assert!(!rx.is_empty()) |
| 1300 |
} |
| 1301 |
|
| 1302 |
#[tokio::test] |
| 1303 |
async fn test_rx_unbounded_is_not_empty_when_all_but_one_messages_are_consumed() { |
| 1304 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1305 |
for i in 0..10 { |
| 1306 |
assert!(tx.send(i).is_ok()); |
| 1307 |
} |
| 1308 |
|
| 1309 |
for _ in 0..9 { |
| 1310 |
assert!(rx.recv().await.is_some()); |
| 1311 |
} |
| 1312 |
|
| 1313 |
assert!(!rx.is_empty()) |
| 1314 |
} |
| 1315 |
|
| 1316 |
#[tokio::test] |
| 1317 |
async fn test_rx_unbounded_is_empty_when_all_messages_are_consumed() { |
| 1318 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1319 |
for i in 0..10 { |
| 1320 |
assert!(tx.send(i).is_ok()); |
| 1321 |
} |
| 1322 |
while rx.try_recv().is_ok() {} |
| 1323 |
assert!(rx.is_empty()) |
| 1324 |
} |
| 1325 |
|
| 1326 |
#[tokio::test] |
| 1327 |
async fn test_rx_unbounded_is_empty_all_senders_are_dropped_and_messages_consumed() { |
| 1328 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1329 |
for i in 0..10 { |
| 1330 |
assert!(tx.send(i).is_ok()); |
| 1331 |
} |
| 1332 |
drop(tx); |
| 1333 |
|
| 1334 |
for _ in 0..10 { |
| 1335 |
assert!(rx.recv().await.is_some()); |
| 1336 |
} |
| 1337 |
|
| 1338 |
assert!(rx.is_empty()) |
| 1339 |
} |
| 1340 |
|
| 1341 |
#[tokio::test] |
| 1342 |
async fn test_rx_unbounded_len_on_empty_channel() { |
| 1343 |
let (_tx, rx) = mpsc::unbounded_channel::<()>(); |
| 1344 |
assert_eq!(rx.len(), 0); |
| 1345 |
} |
| 1346 |
|
| 1347 |
#[tokio::test] |
| 1348 |
async fn test_rx_unbounded_len_on_empty_channel_without_senders() { |
| 1349 |
// when all senders are dropped, a "closed" value is added to the end of the linked list. |
| 1350 |
// here we test that the "closed" value does not change the len of the channel. |
| 1351 |
|
| 1352 |
let (tx, rx) = mpsc::unbounded_channel::<()>(); |
| 1353 |
drop(tx); |
| 1354 |
assert_eq!(rx.len(), 0); |
| 1355 |
} |
| 1356 |
|
| 1357 |
#[tokio::test] |
| 1358 |
async fn test_rx_unbounded_len_with_multiple_messages() { |
| 1359 |
let (tx, rx) = mpsc::unbounded_channel(); |
| 1360 |
|
| 1361 |
for i in 0..100 { |
| 1362 |
assert!(tx.send(i).is_ok()); |
| 1363 |
} |
| 1364 |
assert_eq!(rx.len(), 100); |
| 1365 |
} |
| 1366 |
|
| 1367 |
#[tokio::test] |
| 1368 |
async fn test_rx_unbounded_len_with_multiple_messages_and_dropped_senders() { |
| 1369 |
let (tx, rx) = mpsc::unbounded_channel(); |
| 1370 |
|
| 1371 |
for i in 0..100 { |
| 1372 |
assert!(tx.send(i).is_ok()); |
| 1373 |
} |
| 1374 |
drop(tx); |
| 1375 |
assert_eq!(rx.len(), 100); |
| 1376 |
} |
| 1377 |
|
| 1378 |
#[tokio::test] |
| 1379 |
async fn test_rx_unbounded_len_when_consuming_all_messages() { |
| 1380 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1381 |
|
| 1382 |
for i in 0..100 { |
| 1383 |
assert!(tx.send(i).is_ok()); |
| 1384 |
assert_eq!(rx.len(), i + 1); |
| 1385 |
} |
| 1386 |
|
| 1387 |
drop(tx); |
| 1388 |
|
| 1389 |
for i in (0..100).rev() { |
| 1390 |
assert!(rx.recv().await.is_some()); |
| 1391 |
assert_eq!(rx.len(), i); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
#[tokio::test] |
| 1396 |
async fn test_rx_unbounded_len_when_close_is_called() { |
| 1397 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1398 |
tx.send(()).unwrap(); |
| 1399 |
rx.close(); |
| 1400 |
|
| 1401 |
assert_eq!(rx.len(), 1); |
| 1402 |
} |
| 1403 |
|
| 1404 |
#[tokio::test] |
| 1405 |
async fn test_rx_unbounded_len_when_close_is_called_before_dropping_sender() { |
| 1406 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1407 |
tx.send(()).unwrap(); |
| 1408 |
rx.close(); |
| 1409 |
drop(tx); |
| 1410 |
|
| 1411 |
assert_eq!(rx.len(), 1); |
| 1412 |
} |
| 1413 |
|
| 1414 |
#[tokio::test] |
| 1415 |
async fn test_rx_unbounded_len_when_close_is_called_after_dropping_sender() { |
| 1416 |
let (tx, mut rx) = mpsc::unbounded_channel(); |
| 1417 |
tx.send(()).unwrap(); |
| 1418 |
drop(tx); |
| 1419 |
rx.close(); |
| 1420 |
|
| 1421 |
assert_eq!(rx.len(), 1); |
| 1422 |
} |
| 1423 |
|
| 1424 |
// Regression test for https://github.com/tokio-rs/tokio/issues/6602 |
| 1425 |
#[tokio::test] |
| 1426 |
async fn test_is_empty_32_msgs() { |
| 1427 |
let (sender, mut receiver) = mpsc::channel(33); |
| 1428 |
|
| 1429 |
for value in 1..257 { |
| 1430 |
sender.send(value).await.unwrap(); |
| 1431 |
receiver.recv().await.unwrap(); |
| 1432 |
assert!(receiver.is_empty(), "{value}. len: {}", receiver.len()); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
fn is_debug<T: fmt::Debug>(_: &T) {} |