diff options
author | xd009642 <danielmckenna93@gmail.com> | 2020-08-26 20:39:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-26 21:39:06 +0200 |
commit | 347e18bc7700d0e93c5da494a49728d5b2cce500 (patch) | |
tree | 559901c07848769841d9430a59088df2ac25171c /tokio/tests | |
parent | 2e7e42bca78790e6b7a848445db47de77bfcd8af (diff) |
sync: add blocking_recv and blocking_send in mpsc (#2684)
Fixes: #2629
Diffstat (limited to 'tokio/tests')
-rw-r--r-- | tokio/tests/sync_mpsc.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tokio/tests/sync_mpsc.rs b/tokio/tests/sync_mpsc.rs index f02d90aa..f571a71c 100644 --- a/tokio/tests/sync_mpsc.rs +++ b/tokio/tests/sync_mpsc.rs @@ -2,6 +2,8 @@ #![warn(rust_2018_idioms)] #![cfg(feature = "full")] +use std::thread; +use tokio::runtime::Runtime; use tokio::sync::mpsc; use tokio::sync::mpsc::error::{TryRecvError, TrySendError}; use tokio_test::task; @@ -490,3 +492,45 @@ fn try_recv_unbounded() { _ => panic!(), } } + +#[test] +fn blocking_recv() { + let (mut tx, mut rx) = mpsc::channel::<u8>(1); + + let sync_code = thread::spawn(move || { + assert_eq!(Some(10), rx.blocking_recv()); + }); + + Runtime::new().unwrap().block_on(async move { + let _ = tx.send(10).await; + }); + sync_code.join().unwrap() +} + +#[tokio::test] +#[should_panic] +async fn blocking_recv_async() { + let (_tx, mut rx) = mpsc::channel::<()>(1); + let _ = rx.blocking_recv(); +} + +#[test] +fn blocking_send() { + let (mut tx, mut rx) = mpsc::channel::<u8>(1); + + let sync_code = thread::spawn(move || { + tx.blocking_send(10).unwrap(); + }); + + Runtime::new().unwrap().block_on(async move { + assert_eq!(Some(10), rx.recv().await); + }); + sync_code.join().unwrap() +} + +#[tokio::test] +#[should_panic] +async fn blocking_send_async() { + let (mut tx, _rx) = mpsc::channel::<()>(1); + let _ = tx.blocking_send(()); +} |