Skip to content

Commit b83e71b

Browse files
authoredFeb 25, 2025··
test: acquire_then_drop (#40)
Signed-off-by: tison <[email protected]>
1 parent 4f2999f commit b83e71b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
 

‎mea/src/semaphore/tests.rs

+40
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::future::Future;
16+
use std::pin::pin;
1517
use std::sync::Arc;
18+
use std::task::Context;
19+
use std::task::RawWaker;
20+
use std::task::RawWakerVTable;
21+
use std::task::Waker;
1622
use std::vec::Vec;
1723

1824
use super::*;
@@ -137,6 +143,22 @@ fn try_acquire_concurrently() {
137143
assert_eq!(s.available_permits(), 1);
138144
}
139145

146+
#[test]
147+
fn acquire_then_drop() {
148+
let waker = noop_waker();
149+
let mut context = Context::from_waker(&waker);
150+
151+
let s = Semaphore::new(1);
152+
let p1 = s.try_acquire(1).unwrap();
153+
{
154+
let p2 = s.acquire(1);
155+
let poll = pin!(p2).poll(&mut context);
156+
assert!(poll.is_pending());
157+
}
158+
drop(p1);
159+
assert_eq!(s.available_permits(), 1);
160+
}
161+
140162
#[tokio::test]
141163
async fn acquire_then_forget_exact() {
142164
let s = Arc::new(Semaphore::new(5));
@@ -161,3 +183,21 @@ async fn acquire_then_forget_exact() {
161183
acquired.wait().await;
162184
assert_eq!(s.available_permits(), 3);
163185
}
186+
187+
fn noop_waker() -> Waker {
188+
const NOOP: RawWaker = {
189+
const VTABLE: RawWakerVTable = RawWakerVTable::new(
190+
// Cloning just returns a new no-op raw waker
191+
|_| NOOP,
192+
// `wake` does nothing
193+
|_| {},
194+
// `wake_by_ref` does nothing
195+
|_| {},
196+
// Dropping does nothing as we don't allocate anything
197+
|_| {},
198+
);
199+
RawWaker::new(std::ptr::null(), &VTABLE)
200+
};
201+
202+
unsafe { Waker::from_raw(NOOP) }
203+
}

0 commit comments

Comments
 (0)
Please sign in to comment.