You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
async-std/tests/mutex.rs

85 lines
2.0 KiB
Rust

5 years ago
use std::sync::Arc;
use async_std::prelude::*;
5 years ago
use async_std::sync::Mutex;
use async_std::task;
use futures::channel::mpsc;
#[cfg(not(target_os = "unknown"))]
use async_std::task::spawn;
#[cfg(target_os = "unknown")]
use async_std::task::spawn_local as spawn;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
5 years ago
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
5 years ago
fn smoke() {
task::block_on(async {
let m = Mutex::new(());
drop(m.lock().await);
drop(m.lock().await);
})
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
5 years ago
fn try_lock() {
let m = Mutex::new(());
*m.try_lock().unwrap() = ();
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
5 years ago
fn into_inner() {
let m = Mutex::new(10);
assert_eq!(m.into_inner(), 10);
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
5 years ago
fn get_mut() {
let mut m = Mutex::new(10);
*m.get_mut() = 20;
assert_eq!(m.into_inner(), 20);
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
5 years ago
fn contention() {
task::block_on(async {
let (tx, mut rx) = mpsc::unbounded();
let tx = Arc::new(tx);
let mutex = Arc::new(Mutex::new(0));
let num_tasks = 10000;
5 years ago
let mut handles = Vec::new();
5 years ago
for _ in 0..num_tasks {
let tx = tx.clone();
let mutex = mutex.clone();
handles.push(spawn(async move {
5 years ago
let mut lock = mutex.lock().await;
*lock += 1;
tx.unbounded_send(()).unwrap();
drop(lock);
}));
5 years ago
}
for _ in 0..num_tasks {
5 years ago
rx.next().await.unwrap();
}
for handle in handles.into_iter() {
handle.await;
}
dbg!("wait");
5 years ago
let lock = mutex.lock().await;
assert_eq!(num_tasks, *lock);
});
}