Skip to content

Commit 3ede149

Browse files
committed
Throw exceptions when inserting duplicate streams.
1 parent 9628621 commit 3ede149

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

docs/source/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ Exceptions
1313
.. autoclass:: priority.DeadlockError
1414

1515
.. autoclass:: priority.PriorityLoop
16+
17+
.. autoclass:: priority.DuplicateStreamError

src/priority/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
"""
33
priority: HTTP/2 priority implementation for Python
44
"""
5-
from .priority import Stream, PriorityTree, DeadlockError, PriorityLoop # noqa
5+
from .priority import ( # noqa
6+
Stream, PriorityTree, DeadlockError, PriorityLoop, DuplicateStreamError
7+
)

src/priority/priority.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class PriorityLoop(Exception):
3333
pass
3434

3535

36+
class DuplicateStreamError(Exception):
37+
"""
38+
An attempt was made to insert a stream that already exists.
39+
"""
40+
pass
41+
42+
3643
class Stream(object):
3744
"""
3845
Priority information for a given stream.
@@ -220,7 +227,9 @@ def insert_stream(self,
220227
:param exclusive: (optional) Whether this new stream should be an
221228
exclusive dependency of the parent.
222229
"""
223-
assert stream_id not in self._streams
230+
if stream_id in self._streams:
231+
raise DuplicateStreamError("Stream %d already in tree", stream_id)
232+
224233
stream = Stream(stream_id, weight)
225234

226235
if exclusive:

test/test_priority.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ def test_can_reprioritize_a_stream(self,
262262
actual_result = [next(t) for _ in range(len(result))]
263263
assert actual_result == result
264264

265+
def test_priority_tree_raises_error_inserting_duplicate(self):
266+
"""
267+
Attempting to insert a stream that is already in the tree raises a
268+
DuplicateStreamError
269+
"""
270+
p = priority.PriorityTree()
271+
p.insert_stream(1)
272+
273+
with pytest.raises(priority.DuplicateStreamError):
274+
p.insert_stream(1)
275+
265276

266277
class TestPriorityTreeOutput(object):
267278
"""

0 commit comments

Comments
 (0)