mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
Add Mutex::try_lock and (Unbounded)Receiver::try_recv (#1939)
This commit is contained in:
committed by
Carl Lerche
parent
5d5755dca4
commit
975576952f
@@ -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!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user