1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-28 18:58:30 +00:00

cleanup on drop for utils::mutex_map.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-09 20:04:43 +00:00
parent 01b2928d55
commit 2d251eb19c
10 changed files with 131 additions and 54 deletions

View file

@ -81,3 +81,56 @@ fn checked_add_overflow() {
let res = checked!(a + 1).expect("overflow");
assert_eq!(res, 0);
}
#[tokio::test]
async fn mutex_map_cleanup() {
use crate::utils::MutexMap;
let map = MutexMap::<String, ()>::new();
let lock = map.lock("foo").await;
assert!(!map.is_empty(), "map must not be empty");
drop(lock);
assert!(map.is_empty(), "map must be empty");
}
#[tokio::test]
async fn mutex_map_contend() {
use std::sync::Arc;
use tokio::sync::Barrier;
use crate::utils::MutexMap;
let map = Arc::new(MutexMap::<String, ()>::new());
let seq = Arc::new([Barrier::new(2), Barrier::new(2)]);
let str = "foo".to_owned();
let seq_ = seq.clone();
let map_ = map.clone();
let str_ = str.clone();
let join_a = tokio::spawn(async move {
let _lock = map_.lock(&str_).await;
assert!(!map_.is_empty(), "A0 must not be empty");
seq_[0].wait().await;
assert!(map_.contains(&str_), "A1 must contain key");
});
let seq_ = seq.clone();
let map_ = map.clone();
let str_ = str.clone();
let join_b = tokio::spawn(async move {
let _lock = map_.lock(&str_).await;
assert!(!map_.is_empty(), "B0 must not be empty");
seq_[1].wait().await;
assert!(map_.contains(&str_), "B1 must contain key");
});
seq[0].wait().await;
assert!(map.contains(&str), "Must contain key");
seq[1].wait().await;
tokio::try_join!(join_b, join_a).expect("joined");
assert!(map.is_empty(), "Must be empty");
}