mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-25 00:00:18 +02:00
sync: make add_permits panic with usize::MAX >> 3 permits (#3188)
This commit is contained in:
@@ -253,9 +253,9 @@ impl Semaphore {
|
||||
}
|
||||
|
||||
if rem > 0 && is_empty {
|
||||
let permits = rem << Self::PERMIT_SHIFT;
|
||||
let permits = rem;
|
||||
assert!(
|
||||
permits < Self::MAX_PERMITS,
|
||||
permits <= Self::MAX_PERMITS,
|
||||
"cannot add more than MAX_PERMITS permits ({})",
|
||||
Self::MAX_PERMITS
|
||||
);
|
||||
|
||||
@@ -79,3 +79,17 @@ async fn stresstest() {
|
||||
let _p5 = sem.try_acquire().unwrap();
|
||||
assert!(sem.try_acquire().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_max_amount_permits() {
|
||||
let s = tokio::sync::Semaphore::new(0);
|
||||
s.add_permits(usize::MAX >> 3);
|
||||
assert_eq!(s.available_permits(), usize::MAX >> 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn add_more_than_max_amount_permits() {
|
||||
let s = tokio::sync::Semaphore::new(1);
|
||||
s.add_permits(usize::MAX >> 3);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user