Skip to content
This repository was archived by the owner on Mar 20, 2021. It is now read-only.

Commit 8fce630

Browse files
committed
Fix addChild method in Node.java
1 parent 57c98b2 commit 8fce630

File tree

2 files changed

+71
-14
lines changed

2 files changed

+71
-14
lines changed

modules/http2/src/main/java/org/glassfish/grizzly/http2/Node.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,39 @@ protected void addChild(final Node n) {
110110
}
111111

112112
/**
113-
* Add a new exclusive child. Any other children will be moved to
114-
* children of this exclusive child.
113+
* Add a new child. If the child is marked as exclusive, any
114+
* other children will be moved to become the children of this
115+
* exclusive child.
115116
*/
116-
protected void addChild(final Node n, final boolean exclusive) {
117+
protected void addChild(final Node nodeBeingAddedAsChild, final boolean exclusive) {
117118
writeLock.lock();
118119
try {
119120
if (exclusive) {
120-
n.exclusive = true;
121-
if (n.firstChild != null && firstChild != null) {
121+
nodeBeingAddedAsChild.exclusive = true;
122+
if (nodeBeingAddedAsChild.firstChild != null && firstChild != null) {
122123
Node tail = firstChild;
123124
while (tail.next != null) {
124125
tail = tail.next;
125126
}
126-
tail.next = n.firstChild;
127-
n.firstChild.prev = tail;
128-
127+
tail.next = nodeBeingAddedAsChild.firstChild;
128+
nodeBeingAddedAsChild.firstChild.prev = tail;
129+
nodeBeingAddedAsChild.firstChild = firstChild;
130+
} else if (nodeBeingAddedAsChild.firstChild == null && firstChild != null) {
131+
nodeBeingAddedAsChild.firstChild = firstChild;
129132
}
130-
n.firstChild = firstChild;
131133
firstChild = null;
132-
if (n.firstChild != null) {
133-
Node t = n.firstChild;
134+
if (nodeBeingAddedAsChild.firstChild != null) {
135+
Node t = nodeBeingAddedAsChild.firstChild;
134136
do {
135-
t.parent = n;
137+
t.parent = nodeBeingAddedAsChild;
136138
} while ((t = t.next) != null);
137139
}
138140
}
139141
if (firstChild == null) {
140-
firstChild = n;
142+
firstChild = nodeBeingAddedAsChild;
141143
firstChild.parent = this;
142144
} else {
143-
firstChild.addSibling(n);
145+
firstChild.addSibling(nodeBeingAddedAsChild);
144146
}
145147
} finally {
146148
writeLock.unlock();

modules/http2/src/test/java/org/glassfish/grizzly/http2/NaryTreeTest.java

+55
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,61 @@ public void addExclusiveChild2() {
115115
assertEquals(3, root.firstChild.firstChild.firstChild.parent.id);
116116
}
117117

118+
@Test
119+
public void addExclusiveChild3() {
120+
final Node root = new TestNode(0);
121+
final Node childToAdd = new TestNode(1);
122+
childToAdd.addChild(new TestNode(2));
123+
root.addChild(childToAdd, true);
124+
assertEquals(1, root.firstChild.id);
125+
assertTrue(root.firstChild.exclusive);
126+
assertEquals(0, root.firstChild.parent.id);
127+
assertEquals(2, root.firstChild.firstChild.id);
128+
assertEquals(1, root.firstChild.firstChild.parent.id);
129+
assertNull(root.firstChild.next);
130+
assertNull(root.firstChild.prev);
131+
}
132+
133+
@Test
134+
public void addExclusiveChild4() {
135+
final Node root = new TestNode(0);
136+
root.addChild(new TestNode(1));
137+
final Node childToAdd = new TestNode(2);
138+
root.addChild(childToAdd, true);
139+
assertEquals(2, root.firstChild.id);
140+
assertTrue(root.firstChild.exclusive);
141+
assertEquals(0, root.firstChild.parent.id);
142+
assertEquals(1, root.firstChild.firstChild.id);
143+
assertEquals(2, root.firstChild.firstChild.parent.id);
144+
assertNull(root.firstChild.next);
145+
assertNull(root.firstChild.prev);
146+
}
147+
148+
@Test
149+
public void addExclusiveChild5() {
150+
final Node root = new TestNode(0);
151+
root.addChild(new TestNode(1));
152+
root.addChild(new TestNode(2));
153+
final Node childToAdd = new TestNode(3);
154+
childToAdd.addChild(new TestNode(4));
155+
childToAdd.addChild(new TestNode(5));
156+
root.addChild(childToAdd, true);
157+
158+
assertEquals(0, root.id);
159+
assertEquals(3, root.firstChild.id);
160+
assertEquals(2, root.firstChild.firstChild.id);
161+
assertEquals(1, root.firstChild.firstChild.next.id);
162+
assertEquals(5, root.firstChild.firstChild.next.next.id);
163+
assertEquals(4, root.firstChild.firstChild.next.next.next.id);
164+
165+
assertTrue(root.firstChild.exclusive);
166+
assertEquals(0, root.firstChild.parent.id);
167+
assertEquals(3, root.firstChild.firstChild.parent.id);
168+
assertEquals(3, root.firstChild.firstChild.next.next.parent.id);
169+
assertNull(root.firstChild.next);
170+
assertNull(root.firstChild.prev);
171+
}
172+
118173
@Test
119174
public void find() {
120175
createAndValidate(); // No Exception thrown, find works.

0 commit comments

Comments
 (0)