Add Mutex::try_lock and (Unbounded)Receiver::try_recv (#1939)

This commit is contained in:
Michael P. Jung
2019-12-10 08:01:23 -08:00
committed by Carl Lerche
parent 5d5755dca4
commit 975576952f
7 changed files with 164 additions and 4 deletions
+39 -1
View File
@@ -2,7 +2,7 @@
#![cfg(feature = "full")]
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::mpsc::error::{TryRecvError, TrySendError};
use tokio_test::task;
use tokio_test::{
assert_err, assert_ok, assert_pending, assert_ready, assert_ready_err, assert_ready_ok,
@@ -413,3 +413,41 @@ fn unconsumed_messages_are_dropped() {
assert_eq!(1, Arc::strong_count(&msg));
}
#[test]
fn try_recv() {
let (mut tx, mut rx) = mpsc::channel(1);
match rx.try_recv() {
Err(TryRecvError::Empty) => {}
_ => panic!(),
}
tx.try_send(42).unwrap();
match rx.try_recv() {
Ok(42) => {}
_ => panic!(),
}
drop(tx);
match rx.try_recv() {
Err(TryRecvError::Closed) => {}
_ => panic!(),
}
}
#[test]
fn try_recv_unbounded() {
let (tx, mut rx) = mpsc::unbounded_channel();
match rx.try_recv() {
Err(TryRecvError::Empty) => {}
_ => panic!(),
}
tx.send(42).unwrap();
match rx.try_recv() {
Ok(42) => {}
_ => panic!(),
}
drop(tx);
match rx.try_recv() {
Err(TryRecvError::Closed) => {}
_ => panic!(),
}
}
+13
View File
@@ -134,3 +134,16 @@ async fn aborted_future_2() {
.await
.expect("Mutex is locked");
}
#[test]
fn try_lock() {
let m: Mutex<usize> = Mutex::new(0);
{
let g1 = m.try_lock();
assert_eq!(g1.is_ok(), true);
let g2 = m.try_lock();
assert_eq!(g2.is_ok(), false);
}
let g3 = m.try_lock();
assert_eq!(g3.is_ok(), true);
}