sync: make add_permits panic with usize::MAX >> 3 permits (#3188)

This commit is contained in:
Blas Rodriguez Irizar
2020-12-02 22:58:28 +01:00
committed by GitHub
parent a8e0f0a919
commit a6051a61ec
2 changed files with 16 additions and 2 deletions
+2 -2
View File
@@ -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
);
+14
View File
@@ -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);
}