From a0602f9c11fb649b2c13c89006e0ba1af049b00d Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Mon, 25 Jan 2016 11:15:04 -0500 Subject: [PATCH] priority: initialize children of exclusive Don't forget to set `parent` property of children when inserting exclusive node. Fix: https://github.com/indutny/node-spdy/issues/244 --- lib/spdy-transport/priority.js | 11 ++++++++--- test/base/priority-test.js | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/spdy-transport/priority.js b/lib/spdy-transport/priority.js index 43cb2f7..a6dc7b8 100644 --- a/lib/spdy-transport/priority.js +++ b/lib/spdy-transport/priority.js @@ -48,6 +48,7 @@ PriorityNode.prototype.getPriorityRange = function getPriorityRange() { }; PriorityNode.prototype.addChild = function addChild(child) { + child.parent = this; utils.binaryInsert(this.children.list, child, compareChildren); this.children.weight += child.weight; @@ -128,9 +129,11 @@ PriorityTree.prototype.add = function add(options) { if (parent === undefined) return this.addDefault(options.id); - debug('add node=%d parent=%d', + debug('add node=%d parent=%d weight=%d exclusive=%d', options.id, - options.parent === null ? -1 : options.parent); + options.parent === null ? -1 : options.parent, + options.weight || this.defaultWeight, + options.exclusive ? 1 : 0); var children; if (options.exclusive) @@ -149,8 +152,10 @@ PriorityTree.prototype.add = function add(options) { } this.count++; - if (this.count > this.maxCount) + if (this.count > this.maxCount) { + debug('hit maximum remove id=%d', this.list[0].id); this.list.shift().remove(); + } // Root node is not subject to removal if (node.parent !== null) diff --git a/test/base/priority-test.js b/test/base/priority-test.js index 6454795..46e5056 100644 --- a/test/base/priority-test.js +++ b/test/base/priority-test.js @@ -132,6 +132,11 @@ describe('Stream Priority tree', function() { assert.deepEqual([ 2, 3, 4, 5, 6 ].map(function(id) { return tree.get(id).priority; }), [ 0.5, 0.25, 0.125, 0.125, 0.5 ]); + + // This should not throw when removing ex-child of node swapped by + // exclusive one + tree.add({ id: 7, parent: 5, exclusive: false, weight: 2 }); + tree.add({ id: 8, parent: 5, exclusive: false, weight: 2 }); }); it('should use default weight', function() {