Skip to content

Commit

Permalink
Add tests for new blocking code
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
  • Loading branch information
notgull committed Sep 20, 2023
1 parent 070d6a2 commit 7bb08b5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,42 @@ fn smoke() {
}
});
}

#[test]
fn smoke_blocking() {
future::block_on(async move {
const N: usize = 10;

let barrier = Arc::new(Barrier::new(N));

for _ in 0..10 {
let (tx, rx) = async_channel::unbounded();

for _ in 0..N - 1 {
let c = barrier.clone();
let tx = tx.clone();

thread::spawn(move || {
let res = c.wait_blocking();
tx.send_blocking(res.is_leader()).unwrap();
});
}

// At this point, all spawned threads should be blocked,
// so we shouldn't get anything from the cahnnel.
let res = rx.try_recv();
assert!(res.is_err());

let mut leader_found = barrier.wait_blocking().is_leader();

// Now, the barrier is cleared and we should get data.
for _ in 0..N - 1 {
if rx.recv().await.unwrap() {
assert!(!leader_found);
leader_found = true;
}
}
assert!(leader_found);
}
});
}
7 changes: 7 additions & 0 deletions tests/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ fn smoke() {
})
}

#[test]
fn smoke_blocking() {
let m = Mutex::new(());
drop(m.lock_blocking());
drop(m.lock_blocking());
}

#[test]
fn try_lock() {
let m = Mutex::new(());
Expand Down
9 changes: 9 additions & 0 deletions tests/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ fn smoke() {
});
}

#[test]
fn smoke_blocking() {
let lock = RwLock::new(());
drop(lock.read_blocking());
drop(lock.write_blocking());
drop((lock.read_blocking(), lock.read_blocking()));
drop(lock.write_blocking());
}

#[test]
fn try_write() {
future::block_on(async {
Expand Down
10 changes: 10 additions & 0 deletions tests/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ fn yields_when_contended() {
check_yields_when_contended(s.try_acquire_arc().unwrap(), s.acquire_arc());
}

#[test]
fn smoke_blocking() {
let s = Semaphore::new(2);
let g1 = s.acquire_blocking();
let _g2 = s.acquire_blocking();
assert!(s.try_acquire().is_none());
drop(g1);
assert!(s.try_acquire().is_some());
}

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

0 comments on commit 7bb08b5

Please sign in to comment.