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_write_queue.h" | 5 #include "net/spdy/spdy_write_queue.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 class SpdyWriteQueueTest : public ::testing::Test {}; | 26 class SpdyWriteQueueTest : public ::testing::Test {}; |
27 | 27 |
28 // Makes a SpdyFrameProducer producing a frame with the data in the | 28 // Makes a SpdyFrameProducer producing a frame with the data in the |
29 // given string. | 29 // given string. |
30 scoped_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) { | 30 scoped_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) { |
31 scoped_ptr<char[]> data(new char[s.size()]); | 31 scoped_ptr<char[]> data(new char[s.size()]); |
32 std::memcpy(data.get(), s.data(), s.size()); | 32 std::memcpy(data.get(), s.data(), s.size()); |
33 return scoped_ptr<SpdyBufferProducer>( | 33 return scoped_ptr<SpdyBufferProducer>(new SimpleBufferProducer( |
34 new SimpleBufferProducer( | 34 scoped_ptr<SpdyBuffer>(new SpdyBuffer(scoped_ptr<SpdyFrame>( |
35 scoped_ptr<SpdyBuffer>( | 35 new SpdyFrame(data.release(), s.size(), true)))))); |
36 new SpdyBuffer( | |
37 scoped_ptr<SpdyFrame>( | |
38 new SpdyFrame(data.release(), s.size(), true)))))); | |
39 } | 36 } |
40 | 37 |
41 // Makes a SpdyBufferProducer producing a frame with the data in the | 38 // Makes a SpdyBufferProducer producing a frame with the data in the |
42 // given int (converted to a string). | 39 // given int (converted to a string). |
43 scoped_ptr<SpdyBufferProducer> IntToProducer(int i) { | 40 scoped_ptr<SpdyBufferProducer> IntToProducer(int i) { |
44 return StringToProducer(base::IntToString(i)); | 41 return StringToProducer(base::IntToString(i)); |
45 } | 42 } |
46 | 43 |
47 // Produces a frame with the given producer and returns a copy of its | 44 // Produces a frame with the given producer and returns a copy of its |
48 // data as a string. | 45 // data as a string. |
49 std::string ProducerToString(scoped_ptr<SpdyBufferProducer> producer) { | 46 std::string ProducerToString(scoped_ptr<SpdyBufferProducer> producer) { |
50 scoped_ptr<SpdyBuffer> buffer = producer->ProduceBuffer(); | 47 scoped_ptr<SpdyBuffer> buffer = producer->ProduceBuffer(); |
51 return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize()); | 48 return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize()); |
52 } | 49 } |
53 | 50 |
54 // Produces a frame with the given producer and returns a copy of its | 51 // Produces a frame with the given producer and returns a copy of its |
55 // data as an int (converted from a string). | 52 // data as an int (converted from a string). |
56 int ProducerToInt(scoped_ptr<SpdyBufferProducer> producer) { | 53 int ProducerToInt(scoped_ptr<SpdyBufferProducer> producer) { |
57 int i = 0; | 54 int i = 0; |
58 EXPECT_TRUE(base::StringToInt(ProducerToString(producer.Pass()), &i)); | 55 EXPECT_TRUE(base::StringToInt(ProducerToString(producer.Pass()), &i)); |
59 return i; | 56 return i; |
60 } | 57 } |
61 | 58 |
62 // Makes a SpdyStream with the given priority and a NULL SpdySession | 59 // Makes a SpdyStream with the given priority and a NULL SpdySession |
63 // -- be careful to not call any functions that expect the session to | 60 // -- be careful to not call any functions that expect the session to |
64 // be there. | 61 // be there. |
65 SpdyStream* MakeTestStream(RequestPriority priority) { | 62 SpdyStream* MakeTestStream(RequestPriority priority) { |
66 return new SpdyStream( | 63 return new SpdyStream(SPDY_BIDIRECTIONAL_STREAM, |
67 SPDY_BIDIRECTIONAL_STREAM, base::WeakPtr<SpdySession>(), | 64 base::WeakPtr<SpdySession>(), |
68 GURL(), priority, 0, 0, BoundNetLog()); | 65 GURL(), |
| 66 priority, |
| 67 0, |
| 68 0, |
| 69 BoundNetLog()); |
69 } | 70 } |
70 | 71 |
71 // Add some frame producers of different priority. The producers | 72 // Add some frame producers of different priority. The producers |
72 // should be dequeued in priority order with their associated stream. | 73 // should be dequeued in priority order with their associated stream. |
73 TEST_F(SpdyWriteQueueTest, DequeuesByPriority) { | 74 TEST_F(SpdyWriteQueueTest, DequeuesByPriority) { |
74 SpdyWriteQueue write_queue; | 75 SpdyWriteQueue write_queue; |
75 | 76 |
76 scoped_ptr<SpdyBufferProducer> producer_low = StringToProducer("LOW"); | 77 scoped_ptr<SpdyBufferProducer> producer_low = StringToProducer("LOW"); |
77 scoped_ptr<SpdyBufferProducer> producer_medium = StringToProducer("MEDIUM"); | 78 scoped_ptr<SpdyBufferProducer> producer_medium = StringToProducer("MEDIUM"); |
78 scoped_ptr<SpdyBufferProducer> producer_highest = StringToProducer("HIGHEST"); | 79 scoped_ptr<SpdyBufferProducer> producer_highest = StringToProducer("HIGHEST"); |
79 | 80 |
80 scoped_ptr<SpdyStream> stream_medium(MakeTestStream(MEDIUM)); | 81 scoped_ptr<SpdyStream> stream_medium(MakeTestStream(MEDIUM)); |
81 scoped_ptr<SpdyStream> stream_highest(MakeTestStream(HIGHEST)); | 82 scoped_ptr<SpdyStream> stream_highest(MakeTestStream(HIGHEST)); |
82 | 83 |
83 // A NULL stream should still work. | 84 // A NULL stream should still work. |
84 write_queue.Enqueue( | 85 write_queue.Enqueue( |
85 LOW, SYN_STREAM, producer_low.Pass(), base::WeakPtr<SpdyStream>()); | 86 LOW, SYN_STREAM, producer_low.Pass(), base::WeakPtr<SpdyStream>()); |
86 write_queue.Enqueue( | 87 write_queue.Enqueue( |
87 MEDIUM, SYN_REPLY, producer_medium.Pass(), stream_medium->GetWeakPtr()); | 88 MEDIUM, SYN_REPLY, producer_medium.Pass(), stream_medium->GetWeakPtr()); |
88 write_queue.Enqueue( | 89 write_queue.Enqueue(HIGHEST, |
89 HIGHEST, RST_STREAM, producer_highest.Pass(), | 90 RST_STREAM, |
90 stream_highest->GetWeakPtr()); | 91 producer_highest.Pass(), |
| 92 stream_highest->GetWeakPtr()); |
91 | 93 |
92 SpdyFrameType frame_type = DATA; | 94 SpdyFrameType frame_type = DATA; |
93 scoped_ptr<SpdyBufferProducer> frame_producer; | 95 scoped_ptr<SpdyBufferProducer> frame_producer; |
94 base::WeakPtr<SpdyStream> stream; | 96 base::WeakPtr<SpdyStream> stream; |
95 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 97 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
96 EXPECT_EQ(RST_STREAM, frame_type); | 98 EXPECT_EQ(RST_STREAM, frame_type); |
97 EXPECT_EQ("HIGHEST", ProducerToString(frame_producer.Pass())); | 99 EXPECT_EQ("HIGHEST", ProducerToString(frame_producer.Pass())); |
98 EXPECT_EQ(stream_highest, stream.get()); | 100 EXPECT_EQ(stream_highest, stream.get()); |
99 | 101 |
100 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 102 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
(...skipping 15 matching lines...) Expand all Loading... |
116 SpdyWriteQueue write_queue; | 118 SpdyWriteQueue write_queue; |
117 | 119 |
118 scoped_ptr<SpdyBufferProducer> producer1 = IntToProducer(1); | 120 scoped_ptr<SpdyBufferProducer> producer1 = IntToProducer(1); |
119 scoped_ptr<SpdyBufferProducer> producer2 = IntToProducer(2); | 121 scoped_ptr<SpdyBufferProducer> producer2 = IntToProducer(2); |
120 scoped_ptr<SpdyBufferProducer> producer3 = IntToProducer(3); | 122 scoped_ptr<SpdyBufferProducer> producer3 = IntToProducer(3); |
121 | 123 |
122 scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY)); | 124 scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY)); |
123 scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY)); | 125 scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY)); |
124 scoped_ptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY)); | 126 scoped_ptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY)); |
125 | 127 |
126 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, producer1.Pass(), | 128 write_queue.Enqueue( |
127 stream1->GetWeakPtr()); | 129 DEFAULT_PRIORITY, SYN_STREAM, producer1.Pass(), stream1->GetWeakPtr()); |
128 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_REPLY, producer2.Pass(), | 130 write_queue.Enqueue( |
129 stream2->GetWeakPtr()); | 131 DEFAULT_PRIORITY, SYN_REPLY, producer2.Pass(), stream2->GetWeakPtr()); |
130 write_queue.Enqueue(DEFAULT_PRIORITY, RST_STREAM, producer3.Pass(), | 132 write_queue.Enqueue( |
131 stream3->GetWeakPtr()); | 133 DEFAULT_PRIORITY, RST_STREAM, producer3.Pass(), stream3->GetWeakPtr()); |
132 | 134 |
133 SpdyFrameType frame_type = DATA; | 135 SpdyFrameType frame_type = DATA; |
134 scoped_ptr<SpdyBufferProducer> frame_producer; | 136 scoped_ptr<SpdyBufferProducer> frame_producer; |
135 base::WeakPtr<SpdyStream> stream; | 137 base::WeakPtr<SpdyStream> stream; |
136 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 138 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
137 EXPECT_EQ(SYN_STREAM, frame_type); | 139 EXPECT_EQ(SYN_STREAM, frame_type); |
138 EXPECT_EQ(1, ProducerToInt(frame_producer.Pass())); | 140 EXPECT_EQ(1, ProducerToInt(frame_producer.Pass())); |
139 EXPECT_EQ(stream1, stream.get()); | 141 EXPECT_EQ(stream1, stream.get()); |
140 | 142 |
141 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 143 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 195 |
194 scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY)); | 196 scoped_ptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY)); |
195 stream1->set_stream_id(1); | 197 stream1->set_stream_id(1); |
196 scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY)); | 198 scoped_ptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY)); |
197 stream2->set_stream_id(3); | 199 stream2->set_stream_id(3); |
198 scoped_ptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY)); | 200 scoped_ptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY)); |
199 stream3->set_stream_id(5); | 201 stream3->set_stream_id(5); |
200 // No stream id assigned. | 202 // No stream id assigned. |
201 scoped_ptr<SpdyStream> stream4(MakeTestStream(DEFAULT_PRIORITY)); | 203 scoped_ptr<SpdyStream> stream4(MakeTestStream(DEFAULT_PRIORITY)); |
202 base::WeakPtr<SpdyStream> streams[] = { | 204 base::WeakPtr<SpdyStream> streams[] = { |
203 stream1->GetWeakPtr(), stream2->GetWeakPtr(), | 205 stream1->GetWeakPtr(), stream2->GetWeakPtr(), stream3->GetWeakPtr(), |
204 stream3->GetWeakPtr(), stream4->GetWeakPtr() | 206 stream4->GetWeakPtr()}; |
205 }; | |
206 | 207 |
207 for (int i = 0; i < 100; ++i) { | 208 for (int i = 0; i < 100; ++i) { |
208 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), | 209 write_queue.Enqueue(DEFAULT_PRIORITY, |
| 210 SYN_STREAM, |
| 211 IntToProducer(i), |
209 streams[i % arraysize(streams)]); | 212 streams[i % arraysize(streams)]); |
210 } | 213 } |
211 | 214 |
212 write_queue.RemovePendingWritesForStreamsAfter(stream1->stream_id()); | 215 write_queue.RemovePendingWritesForStreamsAfter(stream1->stream_id()); |
213 | 216 |
214 for (int i = 0; i < 100; i += arraysize(streams)) { | 217 for (int i = 0; i < 100; i += arraysize(streams)) { |
215 SpdyFrameType frame_type = DATA; | 218 SpdyFrameType frame_type = DATA; |
216 scoped_ptr<SpdyBufferProducer> frame_producer; | 219 scoped_ptr<SpdyBufferProducer> frame_producer; |
217 base::WeakPtr<SpdyStream> stream; | 220 base::WeakPtr<SpdyStream> stream; |
218 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)) | 221 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)) |
219 << "Unable to Dequeue i: " << i; | 222 << "Unable to Dequeue i: " << i; |
220 EXPECT_EQ(SYN_STREAM, frame_type); | 223 EXPECT_EQ(SYN_STREAM, frame_type); |
221 EXPECT_EQ(i, ProducerToInt(frame_producer.Pass())); | 224 EXPECT_EQ(i, ProducerToInt(frame_producer.Pass())); |
222 EXPECT_EQ(stream1, stream.get()); | 225 EXPECT_EQ(stream1, stream.get()); |
223 } | 226 } |
224 | 227 |
225 SpdyFrameType frame_type = DATA; | 228 SpdyFrameType frame_type = DATA; |
226 scoped_ptr<SpdyBufferProducer> frame_producer; | 229 scoped_ptr<SpdyBufferProducer> frame_producer; |
227 base::WeakPtr<SpdyStream> stream; | 230 base::WeakPtr<SpdyStream> stream; |
228 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 231 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
229 } | 232 } |
230 | 233 |
231 // Enqueue a bunch of writes and then call Clear(). The write queue | 234 // Enqueue a bunch of writes and then call Clear(). The write queue |
232 // should clean up the memory properly, and Dequeue() should return | 235 // should clean up the memory properly, and Dequeue() should return |
233 // false. | 236 // false. |
234 TEST_F(SpdyWriteQueueTest, Clear) { | 237 TEST_F(SpdyWriteQueueTest, Clear) { |
235 SpdyWriteQueue write_queue; | 238 SpdyWriteQueue write_queue; |
236 | 239 |
237 for (int i = 0; i < 100; ++i) { | 240 for (int i = 0; i < 100; ++i) { |
238 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), | 241 write_queue.Enqueue(DEFAULT_PRIORITY, |
| 242 SYN_STREAM, |
| 243 IntToProducer(i), |
239 base::WeakPtr<SpdyStream>()); | 244 base::WeakPtr<SpdyStream>()); |
240 } | 245 } |
241 | 246 |
242 write_queue.Clear(); | 247 write_queue.Clear(); |
243 | 248 |
244 SpdyFrameType frame_type = DATA; | 249 SpdyFrameType frame_type = DATA; |
245 scoped_ptr<SpdyBufferProducer> frame_producer; | 250 scoped_ptr<SpdyBufferProducer> frame_producer; |
246 base::WeakPtr<SpdyStream> stream; | 251 base::WeakPtr<SpdyStream> stream; |
247 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 252 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
248 } | 253 } |
249 | 254 |
250 } // namespace | 255 } // namespace |
251 | 256 |
252 } // namespace net | 257 } // namespace net |
OLD | NEW |