diff --git a/src/priority/priority.py b/src/priority/priority.py index 5352d02..40a8bb1 100644 --- a/src/priority/priority.py +++ b/src/priority/priority.py @@ -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) diff --git a/test/test_priority.py b/test/test_priority.py index a05e54c..ca4fcbf 100644 --- a/test/test_priority.py +++ b/test/test_priority.py @@ -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): """