1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 #include "net/quic/quic_write_blocked_list.h"
6
7 #include "net/quic/test_tools/quic_test_utils.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace net {
11 namespace test {
12 namespace {
13
TEST(QuicWriteBlockedListTest,PriorityOrder)14 TEST(QuicWriteBlockedListTest, PriorityOrder) {
15 QuicWriteBlockedList write_blocked_list;
16
17 // Mark streams blocked in roughly reverse priority order, and
18 // verify that streams are sorted.
19 write_blocked_list.PushBack(40,
20 QuicWriteBlockedList::kLowestPriority);
21 write_blocked_list.PushBack(23,
22 QuicWriteBlockedList::kHighestPriority);
23 write_blocked_list.PushBack(17,
24 QuicWriteBlockedList::kHighestPriority);
25 write_blocked_list.PushBack(kHeadersStreamId,
26 QuicWriteBlockedList::kHighestPriority);
27 write_blocked_list.PushBack(kCryptoStreamId,
28 QuicWriteBlockedList::kHighestPriority);
29
30 EXPECT_EQ(5u, write_blocked_list.NumBlockedStreams());
31 EXPECT_TRUE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
32 EXPECT_TRUE(write_blocked_list.HasWriteBlockedDataStreams());
33 // The Crypto stream is highest priority.
34 EXPECT_EQ(kCryptoStreamId, write_blocked_list.PopFront());
35 // Followed by the Headers stream.
36 EXPECT_EQ(kHeadersStreamId, write_blocked_list.PopFront());
37 // Streams with same priority are popped in the order they were inserted.
38 EXPECT_EQ(23u, write_blocked_list.PopFront());
39 EXPECT_EQ(17u, write_blocked_list.PopFront());
40 // Low priority stream appears last.
41 EXPECT_EQ(40u, write_blocked_list.PopFront());
42
43 EXPECT_EQ(0u, write_blocked_list.NumBlockedStreams());
44 EXPECT_FALSE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
45 EXPECT_FALSE(write_blocked_list.HasWriteBlockedDataStreams());
46 }
47
TEST(QuicWriteBlockedListTest,CryptoStream)48 TEST(QuicWriteBlockedListTest, CryptoStream) {
49 QuicWriteBlockedList write_blocked_list;
50 write_blocked_list.PushBack(kCryptoStreamId,
51 QuicWriteBlockedList::kHighestPriority);
52
53 EXPECT_EQ(1u, write_blocked_list.NumBlockedStreams());
54 EXPECT_TRUE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
55 EXPECT_EQ(kCryptoStreamId, write_blocked_list.PopFront());
56 EXPECT_EQ(0u, write_blocked_list.NumBlockedStreams());
57 EXPECT_FALSE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
58 }
59
TEST(QuicWriteBlockedListTest,HeadersStream)60 TEST(QuicWriteBlockedListTest, HeadersStream) {
61 QuicWriteBlockedList write_blocked_list;
62 write_blocked_list.PushBack(kHeadersStreamId,
63 QuicWriteBlockedList::kHighestPriority);
64
65 EXPECT_EQ(1u, write_blocked_list.NumBlockedStreams());
66 EXPECT_TRUE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
67 EXPECT_EQ(kHeadersStreamId, write_blocked_list.PopFront());
68 EXPECT_EQ(0u, write_blocked_list.NumBlockedStreams());
69 EXPECT_FALSE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
70 }
71
TEST(QuicWriteBlockedListTest,VerifyHeadersStream)72 TEST(QuicWriteBlockedListTest, VerifyHeadersStream) {
73 QuicWriteBlockedList write_blocked_list;
74 write_blocked_list.PushBack(5,
75 QuicWriteBlockedList::kHighestPriority);
76 write_blocked_list.PushBack(kHeadersStreamId,
77 QuicWriteBlockedList::kHighestPriority);
78
79 EXPECT_EQ(2u, write_blocked_list.NumBlockedStreams());
80 EXPECT_TRUE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
81 EXPECT_TRUE(write_blocked_list.HasWriteBlockedDataStreams());
82 // In newer QUIC versions, there is a headers stream which is
83 // higher priority than data streams.
84 EXPECT_EQ(kHeadersStreamId, write_blocked_list.PopFront());
85 EXPECT_EQ(5u, write_blocked_list.PopFront());
86 EXPECT_EQ(0u, write_blocked_list.NumBlockedStreams());
87 EXPECT_FALSE(write_blocked_list.HasWriteBlockedCryptoOrHeadersStream());
88 EXPECT_FALSE(write_blocked_list.HasWriteBlockedDataStreams());
89 }
90
TEST(QuicWriteBlockedListTest,NoDuplicateEntries)91 TEST(QuicWriteBlockedListTest, NoDuplicateEntries) {
92 // Test that QuicWriteBlockedList doesn't allow duplicate entries.
93 QuicWriteBlockedList write_blocked_list;
94
95 // Try to add a stream to the write blocked list multiple times at the same
96 // priority.
97 const QuicStreamId kBlockedId = kClientDataStreamId1;
98 write_blocked_list.PushBack(kBlockedId,
99 QuicWriteBlockedList::kHighestPriority);
100 write_blocked_list.PushBack(kBlockedId,
101 QuicWriteBlockedList::kHighestPriority);
102 write_blocked_list.PushBack(kBlockedId,
103 QuicWriteBlockedList::kHighestPriority);
104
105 // This should only result in one blocked stream being added.
106 EXPECT_EQ(1u, write_blocked_list.NumBlockedStreams());
107 EXPECT_TRUE(write_blocked_list.HasWriteBlockedDataStreams());
108
109 // There should only be one stream to pop off the front.
110 EXPECT_EQ(kBlockedId, write_blocked_list.PopFront());
111 EXPECT_EQ(0u, write_blocked_list.NumBlockedStreams());
112 EXPECT_FALSE(write_blocked_list.HasWriteBlockedDataStreams());
113 }
114
115 } // namespace
116 } // namespace test
117 } // namespace net
118