Skip to content

Commit e9e0f27

Browse files
noxseanmonstar
authored andcommitted
Add test that would make wait_for_capacity hang if it doesn't loop
1 parent 87969c1 commit e9e0f27

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

tests/h2-support/src/util.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ impl Future for WaitForCapacity {
5454
type Output = h2::SendStream<Bytes>;
5555

5656
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
57-
let _ = ready!(self.stream().poll_capacity(cx)).unwrap();
57+
loop {
58+
let _ = ready!(self.stream().poll_capacity(cx)).unwrap();
5859

59-
let act = self.stream().capacity();
60+
let act = self.stream().capacity();
6061

61-
if act >= self.target {
62-
return Poll::Ready(self.stream.take().unwrap().into());
62+
if act >= self.target {
63+
return Poll::Ready(self.stream.take().unwrap().into());
64+
}
6365
}
64-
65-
Poll::Pending
6666
}
6767
}

tests/h2-tests/tests/flow_control.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,3 +1561,53 @@ async fn data_padding() {
15611561

15621562
join(srv, h2).await;
15631563
}
1564+
1565+
#[tokio::test]
1566+
async fn poll_capacity_after_send_data_and_reserve() {
1567+
h2_support::trace_init!();
1568+
let (io, mut srv) = mock::new();
1569+
1570+
let srv = async move {
1571+
let settings = srv
1572+
.assert_client_handshake_with_settings(frames::settings().initial_window_size(5))
1573+
.await;
1574+
assert_default_settings!(settings);
1575+
srv.recv_frame(frames::headers(1).request("POST", "https://www.example.com/"))
1576+
.await;
1577+
srv.send_frame(frames::headers(1).response(200)).await;
1578+
srv.recv_frame(frames::data(1, &b"abcde"[..])).await;
1579+
srv.send_frame(frames::window_update(1, 5)).await;
1580+
srv.recv_frame(frames::data(1, &b""[..]).eos()).await;
1581+
};
1582+
1583+
let h2 = async move {
1584+
let (mut client, mut h2) = client::handshake(io).await.unwrap();
1585+
let request = Request::builder()
1586+
.method(Method::POST)
1587+
.uri("https://www.example.com/")
1588+
.body(())
1589+
.unwrap();
1590+
1591+
let (response, mut stream) = client.send_request(request, false).unwrap();
1592+
1593+
let response = h2.drive(response).await.unwrap();
1594+
assert_eq!(response.status(), StatusCode::OK);
1595+
1596+
stream.send_data("abcde".into(), false).unwrap();
1597+
1598+
stream.reserve_capacity(5);
1599+
1600+
// Initial window size was 5 so current capacity is 0 even if we just reserved.
1601+
assert_eq!(stream.capacity(), 0);
1602+
1603+
// The first call to `poll_capacity` in `wait_for_capacity` will return 0.
1604+
let mut stream = h2.drive(util::wait_for_capacity(stream, 5)).await;
1605+
1606+
stream.send_data("".into(), true).unwrap();
1607+
1608+
// Wait for the connection to close
1609+
h2.await.unwrap();
1610+
};
1611+
1612+
join(srv, h2).await;
1613+
}

0 commit comments

Comments
 (0)