OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/http2_priority_dependencies.h" | 5 #include "net/spdy/http2_priority_dependencies.h" |
6 | 6 |
7 #include <algorithm> | |
8 | |
9 #include "testing/gmock/include/gmock/gmock.h" | |
7 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
8 | 11 |
12 using ::testing::ContainerEq; | |
13 | |
9 namespace net { | 14 namespace net { |
10 | 15 |
16 bool operator==(const Http2PriorityDependencies::DependencyUpdate& a, | |
17 const Http2PriorityDependencies::DependencyUpdate& b) { | |
18 return a.id == b.id && a.dependent_stream_id == b.dependent_stream_id && | |
19 a.exclusive == b.exclusive; | |
20 } | |
21 | |
22 std::ostream& operator<<( | |
23 std::ostream& os, | |
24 const std::vector<Http2PriorityDependencies::DependencyUpdate>& v) { | |
25 for (auto e : v) { | |
26 os << "{" << e.id << "," << e.dependent_stream_id << "," | |
27 << (e.exclusive ? "true" : "false") << "}"; | |
28 } | |
29 return os; | |
30 } | |
31 | |
11 class HttpPriorityDependencyTest : public PlatformTest { | 32 class HttpPriorityDependencyTest : public PlatformTest { |
12 public: | 33 public: |
13 HttpPriorityDependencyTest() : next_id_(0u) {} | 34 HttpPriorityDependencyTest() : next_id_(0u) {} |
14 | 35 |
15 // Fixed priority values to use for testing. | 36 // Fixed priority values to use for testing. |
16 enum { HIGHEST = 0, MEDIUM = 2, LOW = 4, LOWEST = 5 }; | 37 enum { |
38 HIGHEST = kV3HighestPriority, | |
39 MEDIUM = HIGHEST + 1, | |
40 LOW = MEDIUM + 1, | |
41 LOWEST = kV3LowestPriority, | |
42 }; | |
17 | 43 |
18 SpdyStreamId GetId() { return ++next_id_; } | 44 SpdyStreamId GetId() { return ++next_id_; } |
19 | 45 |
20 void TestStreamCreation(SpdyStreamId new_id, | 46 void TestStreamCreation(SpdyStreamId new_id, |
21 SpdyPriority priority, | 47 SpdyPriority priority, |
22 SpdyStreamId expected_dependent_id) { | 48 SpdyStreamId expected_dependent_id) { |
23 SpdyStreamId dependent_id = 999u; | 49 SpdyStreamId dependent_id = 999u; |
24 bool exclusive = false; | 50 bool exclusive = false; |
25 dependency_state.OnStreamSynSent(new_id, priority, &dependent_id, | 51 dependency_state_.OnStreamCreation(new_id, priority, &dependent_id, |
26 &exclusive); | 52 &exclusive); |
27 EXPECT_EQ(expected_dependent_id, dependent_id); | 53 if (expected_dependent_id != dependent_id || !exclusive) { |
28 EXPECT_TRUE(exclusive); | 54 ADD_FAILURE() << "OnStreamCreation(" << new_id << ", " << int(priority) |
55 << ")\n" | |
56 << " Got: (" << dependent_id << ", " << exclusive << ")\n" | |
57 << " Want: (" << expected_dependent_id << ", true)\n"; | |
58 } | |
59 } | |
60 | |
61 struct ExpectedDependencyUpdate { | |
62 SpdyStreamId id; | |
63 SpdyStreamId parent_id; | |
64 }; | |
65 | |
66 void TestStreamUpdate(SpdyStreamId id, | |
67 SpdyPriority new_priority, | |
68 std::vector<ExpectedDependencyUpdate> expected) { | |
69 auto got = dependency_state_.OnStreamUpdate(id, new_priority); | |
Bence
2017/01/05 17:14:45
Optional: I think the common gTest terminology is
Tom Bergan
2017/01/06 00:08:40
Done.
| |
70 std::vector<Http2PriorityDependencies::DependencyUpdate> want; | |
71 for (auto e : expected) { | |
72 want.push_back({e.id, e.parent_id, true /* exclusive */}); | |
73 } | |
74 if (got != want) { | |
75 ADD_FAILURE() << "OnStreamUpdate(" << id << ", " << int(new_priority) | |
76 << ")\n" | |
77 << " Got: " << got << "\n" | |
78 << " Want: " << want << "\n"; | |
79 } | |
29 } | 80 } |
30 | 81 |
31 void OnStreamDestruction(SpdyStreamId id) { | 82 void OnStreamDestruction(SpdyStreamId id) { |
32 dependency_state.OnStreamDestruction(id); | 83 dependency_state_.OnStreamDestruction(id); |
33 } | 84 } |
34 | 85 |
35 private: | 86 private: |
36 SpdyStreamId next_id_; | 87 SpdyStreamId next_id_; |
37 Http2PriorityDependencies dependency_state; | 88 Http2PriorityDependencies dependency_state_; |
38 }; | 89 }; |
39 | 90 |
40 // Confirm dependencies correct for entries at the same priority. | 91 // Confirm dependencies correct for entries at the same priority. |
41 TEST_F(HttpPriorityDependencyTest, SamePriority) { | 92 TEST_F(HttpPriorityDependencyTest, SamePriority) { |
42 Http2PriorityDependencies dependency_state; | |
43 | |
44 const SpdyStreamId first_id = GetId(); | 93 const SpdyStreamId first_id = GetId(); |
45 const SpdyStreamId second_id = GetId(); | 94 const SpdyStreamId second_id = GetId(); |
46 const SpdyStreamId third_id = GetId(); | 95 const SpdyStreamId third_id = GetId(); |
47 | 96 |
48 TestStreamCreation(first_id, MEDIUM, 0u); | 97 TestStreamCreation(first_id, MEDIUM, 0u); |
49 TestStreamCreation(second_id, MEDIUM, first_id); | 98 TestStreamCreation(second_id, MEDIUM, first_id); |
50 TestStreamCreation(third_id, MEDIUM, second_id); | 99 TestStreamCreation(third_id, MEDIUM, second_id); |
51 } | 100 } |
52 | 101 |
53 // Confirm dependencies correct for entries at different priorities, increasing. | 102 // Confirm dependencies correct for entries at different priorities, increasing. |
54 TEST_F(HttpPriorityDependencyTest, DifferentPriorityIncreasing) { | 103 TEST_F(HttpPriorityDependencyTest, DifferentPriorityIncreasing) { |
55 Http2PriorityDependencies dependency_state; | |
56 | |
57 const SpdyStreamId first_id = GetId(); | 104 const SpdyStreamId first_id = GetId(); |
58 const SpdyStreamId second_id = GetId(); | 105 const SpdyStreamId second_id = GetId(); |
59 const SpdyStreamId third_id = GetId(); | 106 const SpdyStreamId third_id = GetId(); |
60 | 107 |
61 TestStreamCreation(first_id, LOWEST, 0u); | 108 TestStreamCreation(first_id, LOWEST, 0u); |
62 TestStreamCreation(second_id, MEDIUM, 0u); | 109 TestStreamCreation(second_id, MEDIUM, 0u); |
63 TestStreamCreation(third_id, HIGHEST, 0u); | 110 TestStreamCreation(third_id, HIGHEST, 0u); |
64 } | 111 } |
65 | 112 |
66 // Confirm dependencies correct for entries at different priorities, increasing. | 113 // Confirm dependencies correct for entries at different priorities, increasing. |
67 TEST_F(HttpPriorityDependencyTest, DifferentPriorityDecreasing) { | 114 TEST_F(HttpPriorityDependencyTest, DifferentPriorityDecreasing) { |
68 Http2PriorityDependencies dependency_state; | |
69 | |
70 const SpdyStreamId first_id = GetId(); | 115 const SpdyStreamId first_id = GetId(); |
71 const SpdyStreamId second_id = GetId(); | 116 const SpdyStreamId second_id = GetId(); |
72 const SpdyStreamId third_id = GetId(); | 117 const SpdyStreamId third_id = GetId(); |
73 | 118 |
74 TestStreamCreation(first_id, HIGHEST, 0u); | 119 TestStreamCreation(first_id, HIGHEST, 0u); |
75 TestStreamCreation(second_id, MEDIUM, first_id); | 120 TestStreamCreation(second_id, MEDIUM, first_id); |
76 TestStreamCreation(third_id, LOWEST, second_id); | 121 TestStreamCreation(third_id, LOWEST, second_id); |
77 } | 122 } |
78 | 123 |
79 // Confirm dependencies correct if requests are completed between before | 124 // Confirm dependencies correct if requests are completed between before |
80 // next creation. | 125 // next creation. |
81 TEST_F(HttpPriorityDependencyTest, CompletionBeforeIssue) { | 126 TEST_F(HttpPriorityDependencyTest, CompletionBeforeIssue) { |
82 Http2PriorityDependencies dependency_state; | |
83 | |
84 const SpdyStreamId first_id = GetId(); | 127 const SpdyStreamId first_id = GetId(); |
85 const SpdyStreamId second_id = GetId(); | 128 const SpdyStreamId second_id = GetId(); |
86 const SpdyStreamId third_id = GetId(); | 129 const SpdyStreamId third_id = GetId(); |
87 | 130 |
88 TestStreamCreation(first_id, HIGHEST, 0u); | 131 TestStreamCreation(first_id, HIGHEST, 0u); |
89 OnStreamDestruction(first_id); | 132 OnStreamDestruction(first_id); |
90 TestStreamCreation(second_id, MEDIUM, 0u); | 133 TestStreamCreation(second_id, MEDIUM, 0u); |
91 OnStreamDestruction(second_id); | 134 OnStreamDestruction(second_id); |
92 TestStreamCreation(third_id, LOWEST, 0u); | 135 TestStreamCreation(third_id, LOWEST, 0u); |
93 } | 136 } |
94 | 137 |
95 // Confirm dependencies correct if some requests are completed between before | 138 // Confirm dependencies correct if some requests are completed between before |
96 // next creation. | 139 // next creation. |
97 TEST_F(HttpPriorityDependencyTest, SomeCompletions) { | 140 TEST_F(HttpPriorityDependencyTest, SomeCompletions) { |
98 Http2PriorityDependencies dependency_state; | |
99 | |
100 const SpdyStreamId first_id = GetId(); | 141 const SpdyStreamId first_id = GetId(); |
101 const SpdyStreamId second_id = GetId(); | 142 const SpdyStreamId second_id = GetId(); |
102 const SpdyStreamId third_id = GetId(); | 143 const SpdyStreamId third_id = GetId(); |
103 | 144 |
104 TestStreamCreation(first_id, HIGHEST, 0u); | 145 TestStreamCreation(first_id, HIGHEST, 0u); |
105 TestStreamCreation(second_id, MEDIUM, first_id); | 146 TestStreamCreation(second_id, MEDIUM, first_id); |
106 OnStreamDestruction(second_id); | 147 OnStreamDestruction(second_id); |
107 TestStreamCreation(third_id, LOWEST, first_id); | 148 TestStreamCreation(third_id, LOWEST, first_id); |
108 } | 149 } |
109 | 150 |
110 // A more complex example parallel to a simple web page. | 151 // A more complex example parallel to a simple web page. |
111 TEST_F(HttpPriorityDependencyTest, Complex) { | 152 TEST_F(HttpPriorityDependencyTest, Complex) { |
112 Http2PriorityDependencies dependency_state; | |
113 | |
114 const SpdyStreamId first_id = GetId(); | 153 const SpdyStreamId first_id = GetId(); |
115 const SpdyStreamId second_id = GetId(); | 154 const SpdyStreamId second_id = GetId(); |
116 const SpdyStreamId third_id = GetId(); | 155 const SpdyStreamId third_id = GetId(); |
117 const SpdyStreamId fourth_id = GetId(); | 156 const SpdyStreamId fourth_id = GetId(); |
118 const SpdyStreamId fifth_id = GetId(); | 157 const SpdyStreamId fifth_id = GetId(); |
119 const SpdyStreamId sixth_id = GetId(); | 158 const SpdyStreamId sixth_id = GetId(); |
120 const SpdyStreamId seventh_id = GetId(); | 159 const SpdyStreamId seventh_id = GetId(); |
121 const SpdyStreamId eighth_id = GetId(); | 160 const SpdyStreamId eighth_id = GetId(); |
122 const SpdyStreamId nineth_id = GetId(); | 161 const SpdyStreamId nineth_id = GetId(); |
123 const SpdyStreamId tenth_id = GetId(); | 162 const SpdyStreamId tenth_id = GetId(); |
(...skipping 10 matching lines...) Expand all Loading... | |
134 TestStreamCreation(eighth_id, LOW, seventh_id); | 173 TestStreamCreation(eighth_id, LOW, seventh_id); |
135 OnStreamDestruction(second_id); | 174 OnStreamDestruction(second_id); |
136 OnStreamDestruction(fourth_id); | 175 OnStreamDestruction(fourth_id); |
137 OnStreamDestruction(fifth_id); | 176 OnStreamDestruction(fifth_id); |
138 OnStreamDestruction(sixth_id); | 177 OnStreamDestruction(sixth_id); |
139 OnStreamDestruction(seventh_id); | 178 OnStreamDestruction(seventh_id); |
140 TestStreamCreation(nineth_id, MEDIUM, 0u); | 179 TestStreamCreation(nineth_id, MEDIUM, 0u); |
141 TestStreamCreation(tenth_id, HIGHEST, 0u); | 180 TestStreamCreation(tenth_id, HIGHEST, 0u); |
142 } | 181 } |
143 | 182 |
183 // Confirm dependencies correct after updates with just one stream. | |
184 // All updates are no-ops. | |
185 TEST_F(HttpPriorityDependencyTest, UpdateSingleStream) { | |
186 const SpdyStreamId id = GetId(); | |
187 | |
188 TestStreamCreation(id, HIGHEST, 0); | |
189 | |
190 std::vector<ExpectedDependencyUpdate> empty; | |
191 TestStreamUpdate(id, HIGHEST, empty); | |
192 TestStreamUpdate(id, MEDIUM, empty); | |
193 TestStreamUpdate(id, LOWEST, empty); | |
194 TestStreamUpdate(id, HIGHEST, empty); | |
195 } | |
196 | |
197 // Confirm dependencies correct after updates with three streams. | |
198 TEST_F(HttpPriorityDependencyTest, UpdateThreeStreams) { | |
199 const SpdyStreamId first_id = GetId(); | |
200 const SpdyStreamId second_id = GetId(); | |
201 const SpdyStreamId third_id = GetId(); | |
202 | |
203 TestStreamCreation(first_id, HIGHEST, 0); | |
204 TestStreamCreation(second_id, MEDIUM, first_id); | |
205 TestStreamCreation(third_id, LOWEST, second_id); | |
206 | |
207 std::vector<ExpectedDependencyUpdate> empty; | |
208 | |
209 // no-op: still at top. | |
210 TestStreamUpdate(first_id, HIGHEST, empty); | |
211 | |
212 // no-op: still below first. | |
213 TestStreamUpdate(second_id, MEDIUM, empty); | |
214 | |
215 // no-op: still below second. | |
216 TestStreamUpdate(third_id, LOWEST, empty); | |
217 | |
218 // second moves to top, first moves below second. | |
219 TestStreamUpdate(first_id, MEDIUM, {{second_id, 0}, {first_id, second_id}}); | |
220 | |
221 // third moves to top. | |
222 TestStreamUpdate(third_id, HIGHEST, {{third_id, 0}}); | |
223 | |
224 // third moves to bottom. | |
225 TestStreamUpdate(third_id, LOWEST, {{second_id, 0}, {third_id, first_id}}); | |
226 | |
227 // first moves to top. | |
228 TestStreamUpdate(first_id, HIGHEST, {{third_id, second_id}, {first_id, 0}}); | |
229 } | |
230 | |
231 // A more complex example parallel to a simple web page with pushed responses. | |
232 TEST_F(HttpPriorityDependencyTest, UpdateComplex) { | |
233 const SpdyStreamId first_id = GetId(); | |
234 const SpdyStreamId second_id = GetId(); // pushed | |
235 const SpdyStreamId third_id = GetId(); // pushed | |
236 const SpdyStreamId fourth_id = GetId(); | |
237 const SpdyStreamId fifth_id = GetId(); | |
238 const SpdyStreamId sixth_id = GetId(); | |
239 const SpdyStreamId seventh_id = GetId(); | |
240 | |
241 TestStreamCreation(first_id, HIGHEST, 0u); | |
242 TestStreamCreation(second_id, LOWEST, first_id); | |
243 TestStreamCreation(third_id, LOWEST, second_id); | |
244 TestStreamCreation(fourth_id, HIGHEST, first_id); | |
245 TestStreamCreation(fifth_id, MEDIUM, fourth_id); | |
246 TestStreamCreation(sixth_id, MEDIUM, fifth_id); | |
247 TestStreamCreation(seventh_id, LOW, sixth_id); | |
248 | |
249 // second matches a HIGHEST priority response. | |
250 // 3 moves under 7 | |
251 // 2 moves under 4 | |
252 TestStreamUpdate(second_id, HIGHEST, | |
253 {{third_id, seventh_id}, {second_id, fourth_id}}); | |
254 | |
255 // third matches a MEDIUM priority response. | |
256 // 3 moves under 6 | |
257 TestStreamUpdate(third_id, MEDIUM, {{third_id, sixth_id}}); | |
258 } | |
259 | |
144 } // namespace net | 260 } // namespace net |
OLD | NEW |