-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::future::Future; | ||
use std::sync::Arc; | ||
use std::task::Poll; | ||
use tokio::sync::{OwnedSemaphorePermit, Semaphore}; | ||
use tokio_util::sync::PollSemaphore; | ||
|
||
type SemRet = Option<OwnedSemaphorePermit>; | ||
|
||
fn semaphore_poll<'a>( | ||
sem: &'a mut PollSemaphore, | ||
) -> tokio_test::task::Spawn<impl Future<Output = SemRet> + 'a> { | ||
let fut = futures::future::poll_fn(move |cx| sem.poll_acquire(cx)); | ||
tokio_test::task::spawn(fut) | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_works() { | ||
let sem = Arc::new(Semaphore::new(1)); | ||
let mut poll_sem = PollSemaphore::new(sem.clone()); | ||
|
||
let permit = sem.acquire().await.unwrap(); | ||
let mut poll = semaphore_poll(&mut poll_sem); | ||
assert!(poll.poll().is_pending()); | ||
drop(permit); | ||
|
||
assert!(matches!(poll.poll(), Poll::Ready(Some(_)))); | ||
drop(poll); | ||
|
||
sem.close(); | ||
|
||
assert!(semaphore_poll(&mut poll_sem).await.is_none()); | ||
|
||
// Check that it is fused. | ||
assert!(semaphore_poll(&mut poll_sem).await.is_none()); | ||
assert!(semaphore_poll(&mut poll_sem).await.is_none()); | ||
} |