summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-util/tests/poll_semaphore.rs
blob: 50f36dd803ba01299c48db9bb7af67584f01da3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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(
    sem: &mut PollSemaphore,
) -> tokio_test::task::Spawn<impl Future<Output = SemRet> + '_> {
    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());
}