Skip to content

Commit

Permalink
Add test for add_permits
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet committed May 26, 2023
1 parent 216fd1b commit 64d7386
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion tests/semaphore.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::sync::{mpsc, Arc};
use std::mem::forget;
use std::sync::{
atomic::{AtomicUsize, Ordering},
mpsc, Arc,
};
use std::thread;

use async_lock::Semaphore;
Expand Down Expand Up @@ -92,3 +96,34 @@ fn lifetime() {
mutex.acquire_arc()
};
}

#[test]
fn add_permits() {
static COUNTER: AtomicUsize = AtomicUsize::new(0);

let s = Arc::new(Semaphore::new(0));
let (tx, rx) = mpsc::channel::<()>();

for _ in 0..50 {
let s = s.clone();
let tx = tx.clone();

thread::spawn(move || {
future::block_on(async {
let perm = s.acquire().await;
forget(perm);
COUNTER.fetch_add(1, Ordering::Relaxed);
drop(tx);
})
});
}

assert_eq!(COUNTER.load(Ordering::Relaxed), 0);

s.add_permits(50);

drop(tx);
let _ = rx.recv();

assert_eq!(COUNTER.load(Ordering::Relaxed), 50);
}

0 comments on commit 64d7386

Please sign in to comment.