OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/spdy_priority_forest.h" | 5 #include "net/spdy/spdy_priority_forest.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 | 11 |
12 TEST(SpdyPriorityForestTest, AddAndRemoveNodes) { | 12 TEST(SpdyPriorityForestTest, AddAndRemoveNodes) { |
13 SpdyPriorityForest<uint32,int16> forest; | 13 SpdyPriorityForest<uint32, int16> forest; |
14 EXPECT_EQ(0, forest.num_nodes()); | 14 EXPECT_EQ(0, forest.num_nodes()); |
15 EXPECT_FALSE(forest.NodeExists(1)); | 15 EXPECT_FALSE(forest.NodeExists(1)); |
16 | 16 |
17 EXPECT_TRUE(forest.AddRootNode(1, 1000)); | 17 EXPECT_TRUE(forest.AddRootNode(1, 1000)); |
18 EXPECT_EQ(1, forest.num_nodes()); | 18 EXPECT_EQ(1, forest.num_nodes()); |
19 ASSERT_TRUE(forest.NodeExists(1)); | 19 ASSERT_TRUE(forest.NodeExists(1)); |
20 EXPECT_EQ(1000, forest.GetPriority(1)); | 20 EXPECT_EQ(1000, forest.GetPriority(1)); |
21 EXPECT_FALSE(forest.NodeExists(5)); | 21 EXPECT_FALSE(forest.NodeExists(5)); |
22 | 22 |
23 EXPECT_TRUE(forest.AddRootNode(5, 50)); | 23 EXPECT_TRUE(forest.AddRootNode(5, 50)); |
(...skipping 24 matching lines...) Expand all Loading... |
48 EXPECT_TRUE(forest.AddNonRootNode(7, 13, false)); | 48 EXPECT_TRUE(forest.AddNonRootNode(7, 13, false)); |
49 // Now node 7 already exists, so this should fail: | 49 // Now node 7 already exists, so this should fail: |
50 EXPECT_FALSE(forest.AddNonRootNode(7, 1, false)); | 50 EXPECT_FALSE(forest.AddNonRootNode(7, 1, false)); |
51 // Node 13 already has a child (7), so this should fail: | 51 // Node 13 already has a child (7), so this should fail: |
52 EXPECT_FALSE(forest.AddNonRootNode(17, 13, false)); | 52 EXPECT_FALSE(forest.AddNonRootNode(17, 13, false)); |
53 | 53 |
54 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 54 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
55 } | 55 } |
56 | 56 |
57 TEST(SpdyPriorityForestTest, SetParent) { | 57 TEST(SpdyPriorityForestTest, SetParent) { |
58 SpdyPriorityForest<uint32,int16> forest; | 58 SpdyPriorityForest<uint32, int16> forest; |
59 forest.AddRootNode(1, 1000); | 59 forest.AddRootNode(1, 1000); |
60 forest.AddNonRootNode(2, 1, false); | 60 forest.AddNonRootNode(2, 1, false); |
61 forest.AddNonRootNode(3, 2, false); | 61 forest.AddNonRootNode(3, 2, false); |
62 forest.AddNonRootNode(5, 3, false); | 62 forest.AddNonRootNode(5, 3, false); |
63 forest.AddNonRootNode(7, 5, false); | 63 forest.AddNonRootNode(7, 5, false); |
64 forest.AddNonRootNode(9, 7, false); | 64 forest.AddNonRootNode(9, 7, false); |
65 forest.AddRootNode(11, 2000); | 65 forest.AddRootNode(11, 2000); |
66 forest.AddNonRootNode(13, 11, false); | 66 forest.AddNonRootNode(13, 11, false); |
67 // We can't set the parent of a nonexistent node, or set the parent of an | 67 // We can't set the parent of a nonexistent node, or set the parent of an |
68 // existing node to a nonexistent node. | 68 // existing node to a nonexistent node. |
(...skipping 24 matching lines...) Expand all Loading... |
93 EXPECT_FALSE(forest.IsNodeUnordered(2)); | 93 EXPECT_FALSE(forest.IsNodeUnordered(2)); |
94 EXPECT_TRUE(forest.SetParent(2, 1, true)); | 94 EXPECT_TRUE(forest.SetParent(2, 1, true)); |
95 EXPECT_EQ(1u, forest.GetParent(2)); | 95 EXPECT_EQ(1u, forest.GetParent(2)); |
96 EXPECT_EQ(2u, forest.GetChild(1)); | 96 EXPECT_EQ(2u, forest.GetChild(1)); |
97 EXPECT_TRUE(forest.IsNodeUnordered(2)); | 97 EXPECT_TRUE(forest.IsNodeUnordered(2)); |
98 | 98 |
99 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 99 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
100 } | 100 } |
101 | 101 |
102 TEST(SpdyPriorityForestTest, RemoveNodesFromMiddleOfChain) { | 102 TEST(SpdyPriorityForestTest, RemoveNodesFromMiddleOfChain) { |
103 SpdyPriorityForest<uint32,int16> forest; | 103 SpdyPriorityForest<uint32, int16> forest; |
104 forest.AddRootNode(1, 1000); | 104 forest.AddRootNode(1, 1000); |
105 forest.AddNonRootNode(2, 1, false); | 105 forest.AddNonRootNode(2, 1, false); |
106 forest.AddNonRootNode(3, 2, true); | 106 forest.AddNonRootNode(3, 2, true); |
107 forest.AddNonRootNode(5, 3, false); | 107 forest.AddNonRootNode(5, 3, false); |
108 forest.AddNonRootNode(7, 5, true); | 108 forest.AddNonRootNode(7, 5, true); |
109 forest.AddNonRootNode(9, 7, true); | 109 forest.AddNonRootNode(9, 7, true); |
110 | 110 |
111 // Remove a node from the middle, with unordered links on both sides. The | 111 // Remove a node from the middle, with unordered links on both sides. The |
112 // new merged link should also be unordered. | 112 // new merged link should also be unordered. |
113 EXPECT_TRUE(forest.NodeExists(7)); | 113 EXPECT_TRUE(forest.NodeExists(7)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 EXPECT_TRUE(forest.NodeExists(9)); | 148 EXPECT_TRUE(forest.NodeExists(9)); |
149 EXPECT_EQ(9u, forest.GetChild(5)); | 149 EXPECT_EQ(9u, forest.GetChild(5)); |
150 EXPECT_TRUE(forest.RemoveNode(9)); | 150 EXPECT_TRUE(forest.RemoveNode(9)); |
151 EXPECT_FALSE(forest.NodeExists(9)); | 151 EXPECT_FALSE(forest.NodeExists(9)); |
152 EXPECT_EQ(0u, forest.GetChild(5)); | 152 EXPECT_EQ(0u, forest.GetChild(5)); |
153 | 153 |
154 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 154 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
155 } | 155 } |
156 | 156 |
157 TEST(SpdyPriorityForestTest, MergeOrderedAndUnorderedLinks1) { | 157 TEST(SpdyPriorityForestTest, MergeOrderedAndUnorderedLinks1) { |
158 SpdyPriorityForest<uint32,int16> forest; | 158 SpdyPriorityForest<uint32, int16> forest; |
159 forest.AddRootNode(1, 1000); | 159 forest.AddRootNode(1, 1000); |
160 forest.AddNonRootNode(2, 1, true); | 160 forest.AddNonRootNode(2, 1, true); |
161 forest.AddNonRootNode(3, 2, false); | 161 forest.AddNonRootNode(3, 2, false); |
162 | 162 |
163 EXPECT_EQ(2u, forest.GetChild(1)); | 163 EXPECT_EQ(2u, forest.GetChild(1)); |
164 EXPECT_EQ(3u, forest.GetChild(2)); | 164 EXPECT_EQ(3u, forest.GetChild(2)); |
165 EXPECT_EQ(1u, forest.GetParent(2)); | 165 EXPECT_EQ(1u, forest.GetParent(2)); |
166 EXPECT_EQ(2u, forest.GetParent(3)); | 166 EXPECT_EQ(2u, forest.GetParent(3)); |
167 EXPECT_TRUE(forest.IsNodeUnordered(2)); | 167 EXPECT_TRUE(forest.IsNodeUnordered(2)); |
168 EXPECT_FALSE(forest.IsNodeUnordered(3)); | 168 EXPECT_FALSE(forest.IsNodeUnordered(3)); |
169 EXPECT_TRUE(forest.RemoveNode(2)); | 169 EXPECT_TRUE(forest.RemoveNode(2)); |
170 EXPECT_FALSE(forest.NodeExists(2)); | 170 EXPECT_FALSE(forest.NodeExists(2)); |
171 EXPECT_EQ(3u, forest.GetChild(1)); | 171 EXPECT_EQ(3u, forest.GetChild(1)); |
172 EXPECT_EQ(1u, forest.GetParent(3)); | 172 EXPECT_EQ(1u, forest.GetParent(3)); |
173 EXPECT_FALSE(forest.IsNodeUnordered(3)); | 173 EXPECT_FALSE(forest.IsNodeUnordered(3)); |
174 | 174 |
175 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 175 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
176 } | 176 } |
177 | 177 |
178 TEST(SpdyPriorityForestTest, MergeOrderedAndUnorderedLinks2) { | 178 TEST(SpdyPriorityForestTest, MergeOrderedAndUnorderedLinks2) { |
179 SpdyPriorityForest<uint32,int16> forest; | 179 SpdyPriorityForest<uint32, int16> forest; |
180 forest.AddRootNode(1, 1000); | 180 forest.AddRootNode(1, 1000); |
181 forest.AddNonRootNode(2, 1, false); | 181 forest.AddNonRootNode(2, 1, false); |
182 forest.AddNonRootNode(3, 2, true); | 182 forest.AddNonRootNode(3, 2, true); |
183 | 183 |
184 EXPECT_EQ(2u, forest.GetChild(1)); | 184 EXPECT_EQ(2u, forest.GetChild(1)); |
185 EXPECT_EQ(3u, forest.GetChild(2)); | 185 EXPECT_EQ(3u, forest.GetChild(2)); |
186 EXPECT_EQ(1u, forest.GetParent(2)); | 186 EXPECT_EQ(1u, forest.GetParent(2)); |
187 EXPECT_EQ(2u, forest.GetParent(3)); | 187 EXPECT_EQ(2u, forest.GetParent(3)); |
188 EXPECT_FALSE(forest.IsNodeUnordered(2)); | 188 EXPECT_FALSE(forest.IsNodeUnordered(2)); |
189 EXPECT_TRUE(forest.IsNodeUnordered(3)); | 189 EXPECT_TRUE(forest.IsNodeUnordered(3)); |
190 EXPECT_TRUE(forest.RemoveNode(2)); | 190 EXPECT_TRUE(forest.RemoveNode(2)); |
191 EXPECT_FALSE(forest.NodeExists(2)); | 191 EXPECT_FALSE(forest.NodeExists(2)); |
192 EXPECT_EQ(3u, forest.GetChild(1)); | 192 EXPECT_EQ(3u, forest.GetChild(1)); |
193 EXPECT_EQ(1u, forest.GetParent(3)); | 193 EXPECT_EQ(1u, forest.GetParent(3)); |
194 EXPECT_FALSE(forest.IsNodeUnordered(3)); | 194 EXPECT_FALSE(forest.IsNodeUnordered(3)); |
195 | 195 |
196 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 196 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
197 } | 197 } |
198 | 198 |
199 TEST(SpdyPriorityForestTest, WeightedSelectionOfForests) { | 199 TEST(SpdyPriorityForestTest, WeightedSelectionOfForests) { |
200 SpdyPriorityForest<uint32,int16> forest; | 200 SpdyPriorityForest<uint32, int16> forest; |
201 forest.AddRootNode(1, 10); | 201 forest.AddRootNode(1, 10); |
202 forest.AddRootNode(3, 20); | 202 forest.AddRootNode(3, 20); |
203 forest.AddRootNode(5, 70); | 203 forest.AddRootNode(5, 70); |
204 EXPECT_EQ(70, forest.GetPriority(5)); | 204 EXPECT_EQ(70, forest.GetPriority(5)); |
205 EXPECT_TRUE(forest.SetPriority(5, 40)); | 205 EXPECT_TRUE(forest.SetPriority(5, 40)); |
206 EXPECT_FALSE(forest.SetPriority(7, 80)); | 206 EXPECT_FALSE(forest.SetPriority(7, 80)); |
207 EXPECT_EQ(40, forest.GetPriority(5)); | 207 EXPECT_EQ(40, forest.GetPriority(5)); |
208 forest.AddNonRootNode(7, 3, false); | 208 forest.AddNonRootNode(7, 3, false); |
209 EXPECT_FALSE(forest.IsMarkedReadyToRead(1)); | 209 EXPECT_FALSE(forest.IsMarkedReadyToRead(1)); |
210 EXPECT_TRUE(forest.MarkReadyToRead(1)); | 210 EXPECT_TRUE(forest.MarkReadyToRead(1)); |
211 EXPECT_TRUE(forest.IsMarkedReadyToRead(1)); | 211 EXPECT_TRUE(forest.IsMarkedReadyToRead(1)); |
212 EXPECT_TRUE(forest.MarkReadyToRead(5)); | 212 EXPECT_TRUE(forest.MarkReadyToRead(5)); |
213 EXPECT_TRUE(forest.MarkReadyToRead(7)); | 213 EXPECT_TRUE(forest.MarkReadyToRead(7)); |
214 EXPECT_FALSE(forest.MarkReadyToRead(99)); | 214 EXPECT_FALSE(forest.MarkReadyToRead(99)); |
215 | 215 |
216 int counts[8] = {0}; | 216 int counts[8] = {0}; |
217 for (int i = 0; i < 7000; ++i) { | 217 for (int i = 0; i < 7000; ++i) { |
218 const uint32 node_id = forest.NextNodeToRead(); | 218 const uint32 node_id = forest.NextNodeToRead(); |
219 ASSERT_TRUE(node_id == 1 || node_id == 5 || node_id == 7) | 219 ASSERT_TRUE(node_id == 1 || node_id == 5 || node_id == 7) << "node_id is " |
220 << "node_id is " << node_id; | 220 << node_id; |
221 ++counts[node_id]; | 221 ++counts[node_id]; |
222 } | 222 } |
223 | 223 |
224 // In theory, these could fail even if the weighted random selection is | 224 // In theory, these could fail even if the weighted random selection is |
225 // implemented correctly, but it's very unlikely. | 225 // implemented correctly, but it's very unlikely. |
226 EXPECT_GE(counts[1], 800); EXPECT_LE(counts[1], 1200); | 226 EXPECT_GE(counts[1], 800); |
227 EXPECT_GE(counts[7], 1600); EXPECT_LE(counts[7], 2400); | 227 EXPECT_LE(counts[1], 1200); |
228 EXPECT_GE(counts[5], 3200); EXPECT_LE(counts[5], 4800); | 228 EXPECT_GE(counts[7], 1600); |
| 229 EXPECT_LE(counts[7], 2400); |
| 230 EXPECT_GE(counts[5], 3200); |
| 231 EXPECT_LE(counts[5], 4800); |
229 | 232 |
230 // If we unmark all but one node, then we know for sure that that node will | 233 // If we unmark all but one node, then we know for sure that that node will |
231 // be selected next. | 234 // be selected next. |
232 EXPECT_TRUE(forest.MarkNoLongerReadyToRead(1)); | 235 EXPECT_TRUE(forest.MarkNoLongerReadyToRead(1)); |
233 EXPECT_TRUE(forest.MarkNoLongerReadyToRead(7)); | 236 EXPECT_TRUE(forest.MarkNoLongerReadyToRead(7)); |
234 EXPECT_FALSE(forest.MarkNoLongerReadyToRead(99)); | 237 EXPECT_FALSE(forest.MarkNoLongerReadyToRead(99)); |
235 EXPECT_EQ(5u, forest.NextNodeToRead()); | 238 EXPECT_EQ(5u, forest.NextNodeToRead()); |
236 | 239 |
237 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 240 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
238 } | 241 } |
239 | 242 |
240 TEST(SpdyPriorityForestTest, SelectionBetweenUnorderedNodes) { | 243 TEST(SpdyPriorityForestTest, SelectionBetweenUnorderedNodes) { |
241 SpdyPriorityForest<uint32,int16> forest; | 244 SpdyPriorityForest<uint32, int16> forest; |
242 forest.AddRootNode(1, 1000); | 245 forest.AddRootNode(1, 1000); |
243 forest.AddNonRootNode(2, 1, false); | 246 forest.AddNonRootNode(2, 1, false); |
244 forest.AddNonRootNode(3, 2, true); | 247 forest.AddNonRootNode(3, 2, true); |
245 forest.AddNonRootNode(4, 3, true); | 248 forest.AddNonRootNode(4, 3, true); |
246 forest.AddNonRootNode(5, 4, true); | 249 forest.AddNonRootNode(5, 4, true); |
247 forest.AddNonRootNode(6, 5, true); | 250 forest.AddNonRootNode(6, 5, true); |
248 forest.AddNonRootNode(7, 6, false); | 251 forest.AddNonRootNode(7, 6, false); |
249 EXPECT_FALSE(forest.IsMarkedReadyToWrite(2)); | 252 EXPECT_FALSE(forest.IsMarkedReadyToWrite(2)); |
250 EXPECT_TRUE(forest.MarkReadyToWrite(2)); | 253 EXPECT_TRUE(forest.MarkReadyToWrite(2)); |
251 EXPECT_TRUE(forest.IsMarkedReadyToWrite(2)); | 254 EXPECT_TRUE(forest.IsMarkedReadyToWrite(2)); |
252 EXPECT_TRUE(forest.MarkReadyToWrite(4)); | 255 EXPECT_TRUE(forest.MarkReadyToWrite(4)); |
253 EXPECT_TRUE(forest.MarkReadyToWrite(6)); | 256 EXPECT_TRUE(forest.MarkReadyToWrite(6)); |
254 EXPECT_TRUE(forest.MarkReadyToWrite(7)); | 257 EXPECT_TRUE(forest.MarkReadyToWrite(7)); |
255 EXPECT_FALSE(forest.MarkReadyToWrite(99)); | 258 EXPECT_FALSE(forest.MarkReadyToWrite(99)); |
256 | 259 |
257 int counts[8] = {0}; | 260 int counts[8] = {0}; |
258 for (int i = 0; i < 6000; ++i) { | 261 for (int i = 0; i < 6000; ++i) { |
259 const uint32 node_id = forest.NextNodeToWrite(); | 262 const uint32 node_id = forest.NextNodeToWrite(); |
260 ASSERT_TRUE(node_id == 2 || node_id == 4 || node_id == 6) | 263 ASSERT_TRUE(node_id == 2 || node_id == 4 || node_id == 6) << "node_id is " |
261 << "node_id is " << node_id; | 264 << node_id; |
262 ++counts[node_id]; | 265 ++counts[node_id]; |
263 } | 266 } |
264 | 267 |
265 // In theory, these could fail even if the random selection is implemented | 268 // In theory, these could fail even if the random selection is implemented |
266 // correctly, but it's very unlikely. | 269 // correctly, but it's very unlikely. |
267 EXPECT_GE(counts[2], 1600); EXPECT_LE(counts[2], 2400); | 270 EXPECT_GE(counts[2], 1600); |
268 EXPECT_GE(counts[4], 1600); EXPECT_LE(counts[4], 2400); | 271 EXPECT_LE(counts[2], 2400); |
269 EXPECT_GE(counts[6], 1600); EXPECT_LE(counts[6], 2400); | 272 EXPECT_GE(counts[4], 1600); |
| 273 EXPECT_LE(counts[4], 2400); |
| 274 EXPECT_GE(counts[6], 1600); |
| 275 EXPECT_LE(counts[6], 2400); |
270 | 276 |
271 // Once we unmark that group of nodes, the next node up should be node 7, | 277 // Once we unmark that group of nodes, the next node up should be node 7, |
272 // which has an ordered dependency on said group. | 278 // which has an ordered dependency on said group. |
273 EXPECT_TRUE(forest.MarkNoLongerReadyToWrite(2)); | 279 EXPECT_TRUE(forest.MarkNoLongerReadyToWrite(2)); |
274 EXPECT_TRUE(forest.MarkNoLongerReadyToWrite(4)); | 280 EXPECT_TRUE(forest.MarkNoLongerReadyToWrite(4)); |
275 EXPECT_TRUE(forest.MarkNoLongerReadyToWrite(6)); | 281 EXPECT_TRUE(forest.MarkNoLongerReadyToWrite(6)); |
276 EXPECT_FALSE(forest.MarkNoLongerReadyToWrite(99)); | 282 EXPECT_FALSE(forest.MarkNoLongerReadyToWrite(99)); |
277 EXPECT_EQ(7u, forest.NextNodeToWrite()); | 283 EXPECT_EQ(7u, forest.NextNodeToWrite()); |
278 | 284 |
279 ASSERT_TRUE(forest.ValidateInvariantsForTests()); | 285 ASSERT_TRUE(forest.ValidateInvariantsForTests()); |
280 } | 286 } |
281 | 287 |
282 } // namespace net | 288 } // namespace net |
OLD | NEW |