Skip to content

Commit

Permalink
Merge pull request #54 from python-hyper/self-dependency
Browse files Browse the repository at this point in the history
Forbid stream self-dependency.
  • Loading branch information
Lukasa committed Jan 23, 2017
2 parents ad45291 + edfff54 commit 67b908e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Changelog

**API Changes**

- Throw ``PriorityLoop`` when inserting or reprioritising a stream that
depends on itself.
- Throw ``BadWeightError`` when creating or reprioritising a stream with a
weight that is not an integer between 1 and 256, inclusive.
- Throw ``PseudoStreamError`` when trying to reprioritise, remove, block or
Expand Down
9 changes: 9 additions & 0 deletions src/priority/priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ def insert_stream(self,

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

if exclusive:
parent_stream = self._get_or_insert_parent(depends_on)
Expand Down Expand Up @@ -389,6 +393,11 @@ def reprioritize(self,
# 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
25 changes: 25 additions & 0 deletions test/test_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,31 @@ def test_stream_with_out_of_bounds_weight_is_error(self, weight):
err.value.args[0] ==
'Stream weight must be between 1 and 256 (inclusive)')

@pytest.mark.parametrize('exclusive', (True, False))
@pytest.mark.parametrize('stream_id', (1, 5, 20, 32, 256))
def test_stream_depending_on_self_is_error(self, stream_id, exclusive):
"""
Inserting a stream that is dependent on itself is rejected.
"""
p = priority.PriorityTree()
with pytest.raises(priority.PriorityLoop):
p.insert_stream(
stream_id=stream_id, depends_on=stream_id, exclusive=exclusive
)

@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):
"""
Reprioritizing a stream to make it dependent on itself is an error.
"""
p = priority.PriorityTree()
p.insert_stream(stream_id=stream_id)
with pytest.raises(priority.PriorityLoop):
p.reprioritize(
stream_id=stream_id, depends_on=stream_id, exclusive=exclusive
)


class TestPriorityTreeOutput(object):
"""
Expand Down

0 comments on commit 67b908e

Please sign in to comment.