Skip to content

Commit

Permalink
Add test for remote max streams update
Browse files Browse the repository at this point in the history
Improve testing for remote max streams update

This new test checks whether a change in the max streams setting is
properly handled. The max streams value is changed twice throughout the
mock stream. The first update increases the value while the second
update decreases it.
  • Loading branch information
dunkmann00 committed Feb 28, 2023
1 parent 851c303 commit bf604e0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/_async/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,55 @@ async def test_http2_request_to_incorrect_origin():
async with AsyncHTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(RuntimeError):
await conn.request("GET", "https://other.com/")


@pytest.mark.anyio
async def test_http2_remote_max_streams_update():
"""
If the remote server updates the maximum concurrent streams value, we should
be adjusting how many streams we will allow.
"""
origin = Origin(b"https", b"example.com", 443)
stream = AsyncMockStream(
[
hyperframe.frame.SettingsFrame(
settings={hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 1000}
).serialize(),
hyperframe.frame.HeadersFrame(
stream_id=1,
data=hpack.Encoder().encode(
[
(b":status", b"200"),
(b"content-type", b"plain/text"),
]
),
flags=["END_HEADERS"],
).serialize(),
hyperframe.frame.DataFrame(stream_id=1, data=b"Hello, world!").serialize(),
hyperframe.frame.SettingsFrame(
settings={hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 50}
).serialize(),
hyperframe.frame.DataFrame(
stream_id=1, data=b"Hello, world...again!", flags=["END_STREAM"]
).serialize(),
]
)
async with AsyncHTTP2Connection(origin=origin, stream=stream) as conn:
async with conn.stream("GET", "https://example.com/") as response:
i = 0
async for chunk in response.aiter_stream():
if i == 0:
assert chunk == b"Hello, world!"
assert conn._h2_state.remote_settings.max_concurrent_streams == 1000
assert conn._max_streams == min(
conn._h2_state.remote_settings.max_concurrent_streams,
conn._h2_state.local_settings.max_concurrent_streams,
)
elif i == 1:
assert chunk == b"Hello, world...again!"
assert conn._h2_state.remote_settings.max_concurrent_streams == 50
assert conn._max_streams == min(
conn._h2_state.remote_settings.max_concurrent_streams,
conn._h2_state.local_settings.max_concurrent_streams,
)
i += 1
52 changes: 52 additions & 0 deletions tests/_sync/test_http2.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,55 @@ def test_http2_request_to_incorrect_origin():
with HTTP2Connection(origin=origin, stream=stream) as conn:
with pytest.raises(RuntimeError):
conn.request("GET", "https://other.com/")



def test_http2_remote_max_streams_update():
"""
If the remote server updates the maximum concurrent streams value, we should
be adjusting how many streams we will allow.
"""
origin = Origin(b"https", b"example.com", 443)
stream = MockStream(
[
hyperframe.frame.SettingsFrame(
settings={hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 1000}
).serialize(),
hyperframe.frame.HeadersFrame(
stream_id=1,
data=hpack.Encoder().encode(
[
(b":status", b"200"),
(b"content-type", b"plain/text"),
]
),
flags=["END_HEADERS"],
).serialize(),
hyperframe.frame.DataFrame(stream_id=1, data=b"Hello, world!").serialize(),
hyperframe.frame.SettingsFrame(
settings={hyperframe.frame.SettingsFrame.MAX_CONCURRENT_STREAMS: 50}
).serialize(),
hyperframe.frame.DataFrame(
stream_id=1, data=b"Hello, world...again!", flags=["END_STREAM"]
).serialize(),
]
)
with HTTP2Connection(origin=origin, stream=stream) as conn:
with conn.stream("GET", "https://example.com/") as response:
i = 0
for chunk in response.iter_stream():
if i == 0:
assert chunk == b"Hello, world!"
assert conn._h2_state.remote_settings.max_concurrent_streams == 1000
assert conn._max_streams == min(
conn._h2_state.remote_settings.max_concurrent_streams,
conn._h2_state.local_settings.max_concurrent_streams,
)
elif i == 1:
assert chunk == b"Hello, world...again!"
assert conn._h2_state.remote_settings.max_concurrent_streams == 50
assert conn._max_streams == min(
conn._h2_state.remote_settings.max_concurrent_streams,
conn._h2_state.local_settings.max_concurrent_streams,
)
i += 1

0 comments on commit bf604e0

Please sign in to comment.