Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/priority/priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class PseudoStreamError(PriorityError):
pass


def _validate_weight(value: int) -> None:
# RFC 7540 § 5.3.2: "All dependent streams are allocated an integer
# weight between 1 and 256 (inclusive)."
if not isinstance(value, int):
raise BadWeightError("Stream weight should be an integer")
elif not (1 <= value <= 256):
raise BadWeightError("Stream weight must be between 1 and 256 (inclusive)")


class Stream:
"""
Priority information for a given stream.
Expand All @@ -105,12 +114,7 @@ def weight(self) -> int:

@weight.setter
def weight(self, value: int) -> None:
# RFC 7540 § 5.3.2: "All dependent streams are allocated an integer
# weight between 1 and 256 (inclusive)."
if not isinstance(value, int):
raise BadWeightError("Stream weight should be an integer")
elif not (1 <= value <= 256):
raise BadWeightError("Stream weight must be between 1 and 256 (inclusive)")
_validate_weight(value)
self._weight = value

def add_child(self, child: "Stream") -> None:
Expand Down Expand Up @@ -406,15 +410,17 @@ def reprioritize(
except KeyError:
raise MissingStreamError("Stream %d not in tree" % stream_id)

if depends_on == stream_id:
raise PriorityLoop("Stream %d must not depend on itself" % stream_id)

_validate_weight(weight)

# Update things in a specific order to make sure the calculation
# behaves properly. Specifically, we first update the weight. Then,
# behaves properly. Specifically, we first validate the weight. Then,
# we check whether this stream is being made dependent on one of its
# own dependents. Then, we remove this stream from its current parent
# and move it to its new parent, taking its children with it.
if depends_on:
if depends_on == stream_id:
raise PriorityLoop("Stream %d must not depend on itself" % stream_id)

new_parent = self._get_or_insert_parent(depends_on)
cycle = _stream_cycle(new_parent, current_stream)
else:
Expand Down
31 changes: 31 additions & 0 deletions test/test_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,37 @@ def test_stream_depending_on_self_is_error(self, stream_id, exclusive):
stream_id=stream_id, depends_on=stream_id, exclusive=exclusive
)

@pytest.mark.parametrize("weight", (0, 257, None, "invalid"))
@pytest.mark.parametrize("exclusive", (True, False))
def test_invalid_reprioritization_does_not_create_parent(self, weight, exclusive):
"""Rejected weights leave the streams and scheduling tree unchanged."""
tree = priority.PriorityTree()
tree.insert_stream(1, weight=32)
tree.insert_stream(5, depends_on=1)
tree.block(5)

with pytest.raises(priority.BadWeightError):
tree.reprioritize(1, depends_on=3, weight=weight, exclusive=exclusive)

assert set(tree._streams) == {0, 1, 5}
assert tree._streams[1].weight == 32
assert tree._streams[1].parent is tree._streams[0]
assert tree._streams[5].parent is tree._streams[1]
assert [next(tree) for _ in range(4)] == [1] * 4

@pytest.mark.parametrize("exclusive", (True, False))
def test_reprioritization_capacity_error_preserves_weight(self, exclusive):
"""A valid new weight is not applied when the parent cannot fit."""
tree = priority.PriorityTree(maximum_streams=2)
tree.insert_stream(1, weight=32)

with pytest.raises(priority.TooManyStreamsError):
tree.reprioritize(1, depends_on=3, weight=64, exclusive=exclusive)

assert set(tree._streams) == {0, 1}
assert tree._streams[1].weight == 32
assert tree._streams[1].parent is tree._streams[0]

@pytest.mark.parametrize("exclusive", (True, False))
@pytest.mark.parametrize("stream_id", (1, 5, 20, 32, 256))
def test_reprioritize_depend_on_self_is_error(self, stream_id, exclusive):
Expand Down