diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala index d495ce429..4a78be71c 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/PriorityTree.scala @@ -63,9 +63,11 @@ private[http2] object PriorityTree { private class PriorityTreeImpl(nodes: TreeMap[Int, PriorityInfo]) extends PriorityTree { def rootNode: PriorityNode = node(0) - def insertOrUpdate(streamId: Int, streamDependency: Int, weight: Int, exclusive: Boolean): PriorityTree = + def insertOrUpdate(streamId: Int, streamDependency: Int, weight: Int, exclusive: Boolean): PriorityTree = { + require(streamId != 0, "Cannot update root node") if (nodes.isDefinedAt(streamId)) update(streamId, streamDependency, weight, exclusive) else insert(streamId, streamDependency, weight, exclusive) + } private def insert(streamId: Int, streamDependency: Int, weight: Int, exclusive: Boolean): PriorityTreeImpl = { require(!nodes.isDefinedAt(streamId), s"Cannot insert node twice: $streamId") diff --git a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala index 5a09c93e9..60421ae20 100644 --- a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala +++ b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/PriorityTreeSpec.scala @@ -212,8 +212,25 @@ class PriorityTreeSpec extends AnyWordSpec with Matchers { "prune nodes" in pending - "fail when updating the root node" in pending - "fail when introducing a dependency on itself" in pending + "fail when updating the root node" in { + val tree = PriorityTree().insertOrUpdate(1, 0, 16, exclusive = false) + + intercept[IllegalArgumentException] { + tree.insertOrUpdate(0, 1, 16, exclusive = true) + } + } + + "fail when introducing a dependency on itself" in { + val empty = PriorityTree() + intercept[IllegalArgumentException] { + empty.insertOrUpdate(1, 1, 16, exclusive = false) + } + + val tree = empty.insertOrUpdate(1, 0, 16, exclusive = false) + intercept[IllegalArgumentException] { + tree.insertOrUpdate(1, 1, 16, exclusive = false) + } + } } def printLike(resultingTree: String): Matcher[PriorityTree] =