Chromium Code Reviews| Index: net/spdy/http2_priority_dependencies_unittest.cc |
| diff --git a/net/spdy/http2_priority_dependencies_unittest.cc b/net/spdy/http2_priority_dependencies_unittest.cc |
| index 05c482bbfc5fec36a7d917655225fe11aa4c40b3..354dd5ec46248bcd8cb80fa7a9eef274274cb9fb 100644 |
| --- a/net/spdy/http2_priority_dependencies_unittest.cc |
| +++ b/net/spdy/http2_priority_dependencies_unittest.cc |
| @@ -4,16 +4,42 @@ |
| #include "net/spdy/http2_priority_dependencies.h" |
| +#include <algorithm> |
| + |
| +#include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/platform_test.h" |
| +using ::testing::ContainerEq; |
| + |
| namespace net { |
| +bool operator==(const Http2PriorityDependencies::DependencyUpdate& a, |
| + const Http2PriorityDependencies::DependencyUpdate& b) { |
| + return a.id == b.id && a.dependent_stream_id == b.dependent_stream_id && |
| + a.exclusive == b.exclusive; |
| +} |
| + |
| +std::ostream& operator<<( |
| + std::ostream& os, |
| + const std::vector<Http2PriorityDependencies::DependencyUpdate>& v) { |
| + for (auto e : v) { |
| + os << "{" << e.id << "," << e.dependent_stream_id << "," |
| + << (e.exclusive ? "true" : "false") << "}"; |
| + } |
| + return os; |
| +} |
| + |
| class HttpPriorityDependencyTest : public PlatformTest { |
| public: |
| HttpPriorityDependencyTest() : next_id_(0u) {} |
| // Fixed priority values to use for testing. |
| - enum { HIGHEST = 0, MEDIUM = 2, LOW = 4, LOWEST = 5 }; |
| + enum { |
| + HIGHEST = kV3HighestPriority, |
| + MEDIUM = HIGHEST + 1, |
| + LOW = MEDIUM + 1, |
| + LOWEST = kV3LowestPriority, |
| + }; |
| SpdyStreamId GetId() { return ++next_id_; } |
| @@ -22,25 +48,48 @@ class HttpPriorityDependencyTest : public PlatformTest { |
| SpdyStreamId expected_dependent_id) { |
| SpdyStreamId dependent_id = 999u; |
| bool exclusive = false; |
| - dependency_state.OnStreamSynSent(new_id, priority, &dependent_id, |
| - &exclusive); |
| - EXPECT_EQ(expected_dependent_id, dependent_id); |
| - EXPECT_TRUE(exclusive); |
| + dependency_state_.OnStreamCreation(new_id, priority, &dependent_id, |
| + &exclusive); |
| + if (expected_dependent_id != dependent_id || !exclusive) { |
| + ADD_FAILURE() << "OnStreamCreation(" << new_id << ", " << int(priority) |
| + << ")\n" |
| + << " Got: (" << dependent_id << ", " << exclusive << ")\n" |
| + << " Want: (" << expected_dependent_id << ", true)\n"; |
| + } |
| + } |
| + |
| + struct ExpectedDependencyUpdate { |
| + SpdyStreamId id; |
| + SpdyStreamId parent_id; |
| + }; |
| + |
| + void TestStreamUpdate(SpdyStreamId id, |
| + SpdyPriority new_priority, |
| + std::vector<ExpectedDependencyUpdate> expected) { |
| + 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.
|
| + std::vector<Http2PriorityDependencies::DependencyUpdate> want; |
| + for (auto e : expected) { |
| + want.push_back({e.id, e.parent_id, true /* exclusive */}); |
| + } |
| + if (got != want) { |
| + ADD_FAILURE() << "OnStreamUpdate(" << id << ", " << int(new_priority) |
| + << ")\n" |
| + << " Got: " << got << "\n" |
| + << " Want: " << want << "\n"; |
| + } |
| } |
| void OnStreamDestruction(SpdyStreamId id) { |
| - dependency_state.OnStreamDestruction(id); |
| + dependency_state_.OnStreamDestruction(id); |
| } |
| private: |
| SpdyStreamId next_id_; |
| - Http2PriorityDependencies dependency_state; |
| + Http2PriorityDependencies dependency_state_; |
| }; |
| // Confirm dependencies correct for entries at the same priority. |
| TEST_F(HttpPriorityDependencyTest, SamePriority) { |
| - Http2PriorityDependencies dependency_state; |
| - |
| const SpdyStreamId first_id = GetId(); |
| const SpdyStreamId second_id = GetId(); |
| const SpdyStreamId third_id = GetId(); |
| @@ -52,8 +101,6 @@ TEST_F(HttpPriorityDependencyTest, SamePriority) { |
| // Confirm dependencies correct for entries at different priorities, increasing. |
| TEST_F(HttpPriorityDependencyTest, DifferentPriorityIncreasing) { |
| - Http2PriorityDependencies dependency_state; |
| - |
| const SpdyStreamId first_id = GetId(); |
| const SpdyStreamId second_id = GetId(); |
| const SpdyStreamId third_id = GetId(); |
| @@ -65,8 +112,6 @@ TEST_F(HttpPriorityDependencyTest, DifferentPriorityIncreasing) { |
| // Confirm dependencies correct for entries at different priorities, increasing. |
| TEST_F(HttpPriorityDependencyTest, DifferentPriorityDecreasing) { |
| - Http2PriorityDependencies dependency_state; |
| - |
| const SpdyStreamId first_id = GetId(); |
| const SpdyStreamId second_id = GetId(); |
| const SpdyStreamId third_id = GetId(); |
| @@ -79,8 +124,6 @@ TEST_F(HttpPriorityDependencyTest, DifferentPriorityDecreasing) { |
| // Confirm dependencies correct if requests are completed between before |
| // next creation. |
| TEST_F(HttpPriorityDependencyTest, CompletionBeforeIssue) { |
| - Http2PriorityDependencies dependency_state; |
| - |
| const SpdyStreamId first_id = GetId(); |
| const SpdyStreamId second_id = GetId(); |
| const SpdyStreamId third_id = GetId(); |
| @@ -95,8 +138,6 @@ TEST_F(HttpPriorityDependencyTest, CompletionBeforeIssue) { |
| // Confirm dependencies correct if some requests are completed between before |
| // next creation. |
| TEST_F(HttpPriorityDependencyTest, SomeCompletions) { |
| - Http2PriorityDependencies dependency_state; |
| - |
| const SpdyStreamId first_id = GetId(); |
| const SpdyStreamId second_id = GetId(); |
| const SpdyStreamId third_id = GetId(); |
| @@ -109,8 +150,6 @@ TEST_F(HttpPriorityDependencyTest, SomeCompletions) { |
| // A more complex example parallel to a simple web page. |
| TEST_F(HttpPriorityDependencyTest, Complex) { |
| - Http2PriorityDependencies dependency_state; |
| - |
| const SpdyStreamId first_id = GetId(); |
| const SpdyStreamId second_id = GetId(); |
| const SpdyStreamId third_id = GetId(); |
| @@ -141,4 +180,81 @@ TEST_F(HttpPriorityDependencyTest, Complex) { |
| TestStreamCreation(tenth_id, HIGHEST, 0u); |
| } |
| +// Confirm dependencies correct after updates with just one stream. |
| +// All updates are no-ops. |
| +TEST_F(HttpPriorityDependencyTest, UpdateSingleStream) { |
| + const SpdyStreamId id = GetId(); |
| + |
| + TestStreamCreation(id, HIGHEST, 0); |
| + |
| + std::vector<ExpectedDependencyUpdate> empty; |
| + TestStreamUpdate(id, HIGHEST, empty); |
| + TestStreamUpdate(id, MEDIUM, empty); |
| + TestStreamUpdate(id, LOWEST, empty); |
| + TestStreamUpdate(id, HIGHEST, empty); |
| +} |
| + |
| +// Confirm dependencies correct after updates with three streams. |
| +TEST_F(HttpPriorityDependencyTest, UpdateThreeStreams) { |
| + const SpdyStreamId first_id = GetId(); |
| + const SpdyStreamId second_id = GetId(); |
| + const SpdyStreamId third_id = GetId(); |
| + |
| + TestStreamCreation(first_id, HIGHEST, 0); |
| + TestStreamCreation(second_id, MEDIUM, first_id); |
| + TestStreamCreation(third_id, LOWEST, second_id); |
| + |
| + std::vector<ExpectedDependencyUpdate> empty; |
| + |
| + // no-op: still at top. |
| + TestStreamUpdate(first_id, HIGHEST, empty); |
| + |
| + // no-op: still below first. |
| + TestStreamUpdate(second_id, MEDIUM, empty); |
| + |
| + // no-op: still below second. |
| + TestStreamUpdate(third_id, LOWEST, empty); |
| + |
| + // second moves to top, first moves below second. |
| + TestStreamUpdate(first_id, MEDIUM, {{second_id, 0}, {first_id, second_id}}); |
| + |
| + // third moves to top. |
| + TestStreamUpdate(third_id, HIGHEST, {{third_id, 0}}); |
| + |
| + // third moves to bottom. |
| + TestStreamUpdate(third_id, LOWEST, {{second_id, 0}, {third_id, first_id}}); |
| + |
| + // first moves to top. |
| + TestStreamUpdate(first_id, HIGHEST, {{third_id, second_id}, {first_id, 0}}); |
| +} |
| + |
| +// A more complex example parallel to a simple web page with pushed responses. |
| +TEST_F(HttpPriorityDependencyTest, UpdateComplex) { |
| + const SpdyStreamId first_id = GetId(); |
| + const SpdyStreamId second_id = GetId(); // pushed |
| + const SpdyStreamId third_id = GetId(); // pushed |
| + const SpdyStreamId fourth_id = GetId(); |
| + const SpdyStreamId fifth_id = GetId(); |
| + const SpdyStreamId sixth_id = GetId(); |
| + const SpdyStreamId seventh_id = GetId(); |
| + |
| + TestStreamCreation(first_id, HIGHEST, 0u); |
| + TestStreamCreation(second_id, LOWEST, first_id); |
| + TestStreamCreation(third_id, LOWEST, second_id); |
| + TestStreamCreation(fourth_id, HIGHEST, first_id); |
| + TestStreamCreation(fifth_id, MEDIUM, fourth_id); |
| + TestStreamCreation(sixth_id, MEDIUM, fifth_id); |
| + TestStreamCreation(seventh_id, LOW, sixth_id); |
| + |
| + // second matches a HIGHEST priority response. |
| + // 3 moves under 7 |
| + // 2 moves under 4 |
| + TestStreamUpdate(second_id, HIGHEST, |
| + {{third_id, seventh_id}, {second_id, fourth_id}}); |
| + |
| + // third matches a MEDIUM priority response. |
| + // 3 moves under 6 |
| + TestStreamUpdate(third_id, MEDIUM, {{third_id, sixth_id}}); |
| +} |
| + |
| } // namespace net |