Skip to content

Commit 35493f5

Browse files
committed
Enforce RFC 7540 rules about stream weights
1 parent 6c843cd commit 35493f5

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/priority/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"""
55
from .priority import ( # noqa
66
Stream, PriorityTree, DeadlockError, PriorityLoop, DuplicateStreamError,
7-
MissingStreamError, TooManyStreamsError
7+
MissingStreamError, TooManyStreamsError, BadWeightError
88
)

src/priority/priority.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ class TooManyStreamsError(Exception):
5353
pass
5454

5555

56+
class BadWeightError(Exception):
57+
"""
58+
An attempt was made to create a stream with an invalid weight.
59+
60+
.. versionadded:: 1.3.0
61+
"""
62+
pass
63+
64+
5665
class Stream(object):
5766
"""
5867
Priority information for a given stream.
@@ -70,6 +79,21 @@ def __init__(self, stream_id, weight=16):
7079
self.last_weight = 0
7180
self._deficit = 0
7281

82+
@property
83+
def weight(self):
84+
return self._weight
85+
86+
@weight.setter
87+
def weight(self, value):
88+
# RFC 7540 § 5.3.2: "All dependent streams are allocated an integer
89+
# weight between 1 and 256 (inclusive)."
90+
if not isinstance(value, int):
91+
raise BadWeightError("Stream weight should be an integer")
92+
elif not (1 <= value <= 256):
93+
raise BadWeightError(
94+
"Stream weight must be between 1 and 256 (inclusive)")
95+
self._weight = value
96+
7397
def add_child(self, child):
7498
"""
7599
Add a stream that depends on this one.

test/test_priority.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,39 @@ def test_can_insert_stream_with_exclusive_dependency_on_0(self,
391391
next_ten_ids = [next(p) for _ in range(0, 10)]
392392
assert next_ten_ids == [5] * 10
393393

394+
@pytest.mark.parametrize('weight', [
395+
None,
396+
0.5,
397+
float('inf'),
398+
'priority',
399+
object
400+
])
401+
def test_insert_stream_with_non_integer_weight_is_error(self, weight):
402+
"""
403+
Creating a stream with a non-integer weight is rejected.
404+
"""
405+
p = priority.PriorityTree()
406+
with pytest.raises(priority.BadWeightError) as err:
407+
p.insert_stream(stream_id=1, weight=weight)
408+
assert err.value.args[0] == 'Stream weight should be an integer'
409+
410+
@pytest.mark.parametrize('weight', [
411+
0,
412+
257,
413+
1000,
414+
-42,
415+
])
416+
def test_insert_stream_with_out_of_bounds_weight_is_error(self, weight):
417+
"""
418+
Creating a stream with an out-of-bounds integer weight is rejected.
419+
"""
420+
p = priority.PriorityTree()
421+
with pytest.raises(priority.BadWeightError) as err:
422+
p.insert_stream(stream_id=1, weight=weight)
423+
assert (
424+
err.value.args[0] ==
425+
'Stream weight must be between 1 and 256 (inclusive)')
426+
394427

395428
class TestPriorityTreeOutput(object):
396429
"""

0 commit comments

Comments
 (0)