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
4 changes: 3 additions & 1 deletion src/priority/priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,9 @@ def insert_stream(
if stream_id in self._streams:
raise DuplicateStreamError("Stream %d already in tree" % stream_id)

if (len(self._streams) + 1) > self._maximum_streams:
# A missing dependency is inserted too, so reserve room for both nodes.
new_streams = 1 + int(bool(depends_on) and depends_on not in self._streams)
if (len(self._streams) + new_streams) > self._maximum_streams:
raise TooManyStreamsError(
"Refusing to insert %d streams into priority tree at once"
% (self._maximum_streams + 1)
Expand Down
32 changes: 32 additions & 0 deletions test/test_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,38 @@ def test_can_insert_stream_with_exclusive_dependency_on_0(self, depends_on):
next_ten_ids = [next(p) for _ in range(0, 10)]
assert next_ten_ids == [5] * 10

@pytest.mark.parametrize("exclusive", (True, False))
def test_missing_parent_counts_toward_stream_limit(self, exclusive):
p = priority.PriorityTree(maximum_streams=3)
p.insert_stream(1)

with pytest.raises(priority.TooManyStreamsError):
p.insert_stream(5, depends_on=3, exclusive=exclusive)

# Neither the child nor its implicit parent may consume the last slot.
assert set(p._streams) == {0, 1}
assert [next(p) for _ in range(5)] == [1] * 5
p.insert_stream(3)
assert set(p._streams) == {0, 1, 3}

@pytest.mark.parametrize("exclusive", (True, False))
def test_missing_parent_can_fill_stream_limit(self, exclusive):
p = priority.PriorityTree(maximum_streams=3)
p.insert_stream(5, depends_on=3, exclusive=exclusive)

assert set(p._streams) == {0, 3, 5}
assert [next(p) for _ in range(5)] == [5] * 5

@pytest.mark.parametrize("exclusive", (True, False))
def test_existing_parent_does_not_consume_another_slot(self, exclusive):
p = priority.PriorityTree(maximum_streams=3)
p.insert_stream(3)
p.block(3)
p.insert_stream(5, depends_on=3, exclusive=exclusive)

assert set(p._streams) == {0, 3, 5}
assert [next(p) for _ in range(5)] == [5] * 5

@pytest.mark.parametrize("weight", [None, 0.5, float("inf"), "priority", object])
def test_stream_with_non_integer_weight_is_error(self, weight):
"""
Expand Down