Coverage Report

Created: 2025-07-11 06:53

/src/h2/tests/h2-support/src/util.rs
Line
Count
Source (jump to first uncovered line)
1
use bytes::{BufMut, Bytes};
2
use futures::ready;
3
use std::future::Future;
4
use std::pin::Pin;
5
use std::task::{Context, Poll};
6
7
pub fn byte_str(s: &str) -> h2::frame::BytesStr {
8
    h2::frame::BytesStr::try_from(Bytes::copy_from_slice(s.as_bytes())).unwrap()
9
}
10
11
0
pub async fn concat(mut body: h2::RecvStream) -> Result<Bytes, h2::Error> {
12
0
    let mut vec = Vec::new();
13
0
    while let Some(chunk) = body.data().await {
14
0
        vec.put(chunk?);
15
    }
16
0
    Ok(vec.into())
17
0
}
18
19
0
pub async fn yield_once() {
20
0
    let mut yielded = false;
21
0
    futures::future::poll_fn(move |cx| {
22
0
        if yielded {
23
0
            Poll::Ready(())
24
        } else {
25
0
            yielded = true;
26
0
            cx.waker().clone().wake();
27
0
            Poll::Pending
28
        }
29
0
    })
30
0
    .await;
31
0
}
32
33
/// Should only be called after a non-0 capacity was requested for the stream.
34
pub fn wait_for_capacity(stream: h2::SendStream<Bytes>, target: usize) -> WaitForCapacity {
35
    WaitForCapacity {
36
        stream: Some(stream),
37
        target,
38
    }
39
}
40
41
pub struct WaitForCapacity {
42
    stream: Option<h2::SendStream<Bytes>>,
43
    target: usize,
44
}
45
46
impl WaitForCapacity {
47
    fn stream(&mut self) -> &mut h2::SendStream<Bytes> {
48
        self.stream.as_mut().unwrap()
49
    }
50
}
51
52
impl Future for WaitForCapacity {
53
    type Output = h2::SendStream<Bytes>;
54
55
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
56
        loop {
57
            let _ = ready!(self.stream().poll_capacity(cx)).unwrap();
58
59
            let act = self.stream().capacity();
60
61
            // If a non-0 capacity was requested for the stream before calling
62
            // wait_for_capacity, then poll_capacity should return Pending
63
            // until there is a non-0 capacity.
64
            assert_ne!(act, 0);
65
66
            if act >= self.target {
67
                return Poll::Ready(self.stream.take().unwrap());
68
            }
69
        }
70
    }
71
}