#[cfg(all(target_family = "wasm", not(target_os = "wasi")))] use wasm_bindgen_test::wasm_bindgen_test as test; #[cfg(all(target_family = "wasm", not(target_os = "wasi")))] use wasm_bindgen_test::wasm_bindgen_test as maybe_tokio_test;
#[cfg(not(all(target_family = "wasm", not(target_os = "wasi"))))] use tokio::test as maybe_tokio_test;
use tokio::sync::Mutex; use tokio_test::task::spawn; use tokio_test::{assert_pending, assert_ready};
use std::sync::Arc;
#[test] fn straight_execution() { let l = Mutex::new(100);
{ letmut t = spawn(l.lock()); letmut g = assert_ready!(t.poll());
assert_eq!(&*g, &100);
*g = 99;
}
{ letmut t = spawn(l.lock()); letmut g = assert_ready!(t.poll());
assert_eq!(&*g, &99);
*g = 98;
}
{ letmut t = spawn(l.lock()); let g = assert_ready!(t.poll());
assert_eq!(&*g, &98);
}
}
#[test] fn readiness() { let l1 = Arc::new(Mutex::new(100)); let l2 = Arc::clone(&l1); letmut t1 = spawn(l1.lock()); letmut t2 = spawn(l2.lock());
let g = assert_ready!(t1.poll());
// We can't now acquire the lease since it's already held in g
assert_pending!(t2.poll());
// But once g unlocks, we can acquire it
drop(g);
assert!(t2.is_woken()); let _t2 = assert_ready!(t2.poll());
}
/// Ensure a mutex is unlocked if a future holding the lock /// is aborted prematurely. #[tokio::test] #[cfg(feature = "full")] asyncfn aborted_future_1() { use std::time::Duration; use tokio::time::{interval, timeout};
let m1: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
{ let m2 = m1.clone(); // Try to lock mutex in a future that is aborted prematurely
timeout(Duration::from_millis(1u64), asyncmove { let iv = interval(Duration::from_millis(1000));
tokio::pin!(iv); let _g = m2.lock().await;
iv.as_mut().tick().await;
iv.as_mut().tick().await;
})
.await
.unwrap_err();
} // This should succeed as there is no lock left for the mutex.
timeout(Duration::from_millis(1u64), asyncmove { let _g = m1.lock().await;
})
.await
.expect("Mutex is locked");
}
/// This test is similar to `aborted_future_1` but this time the /// aborted future is waiting for the lock. #[tokio::test] #[cfg(feature = "full")] asyncfn aborted_future_2() { use std::time::Duration; use tokio::time::timeout;
let m1: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
{ // Lock mutex let _lock = m1.lock().await;
{ let m2 = m1.clone(); // Try to lock mutex in a future that is aborted prematurely
timeout(Duration::from_millis(1u64), asyncmove { let _g = m2.lock().await;
})
.await
.unwrap_err();
}
} // This should succeed as there is no lock left for the mutex.
timeout(Duration::from_millis(1u64), asyncmove { let _g = m1.lock().await;
})
.await
.expect("Mutex is locked");
}
#[test] fn try_lock() { let m: Mutex<usize> = Mutex::new(0);
{ let g1 = m.try_lock();
assert!(g1.is_ok()); let g2 = m.try_lock();
assert!(g2.is_err());
} let g3 = m.try_lock();
assert!(g3.is_ok());
}
#[maybe_tokio_test] asyncfn debug_format() { let s = "debug"; let m = Mutex::new(s.to_string());
assert_eq!(format!("{:?}", s), format!("{:?}", m.lock().await));
}
#[maybe_tokio_test] asyncfn mutex_debug() { let s = "data"; let m = Mutex::new(s.to_string());
assert_eq!(format!("{:?}", m), r#"Mutex { data: "data" }"#); let _guard = m.lock().await;
assert_eq!(format!("{:?}", m), r#"Mutex { data: <locked> }"#)
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.0 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.