| 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 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
| 16 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
| 17 #include "net/spdy/spdy_frame_producer.h" | 17 #include "net/spdy/spdy_buffer_producer.h" |
| 18 #include "net/spdy/spdy_stream.h" | 18 #include "net/spdy/spdy_stream.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class SpdyWriteQueueTest : public ::testing::Test {}; | 25 class SpdyWriteQueueTest : public ::testing::Test {}; |
| 26 | 26 |
| 27 // Makes a SpdyFrameProducer producing a frame with the data in the | 27 // Makes a SpdyFrameProducer producing a frame with the data in the |
| 28 // given string. | 28 // given string. |
| 29 scoped_ptr<SpdyFrameProducer> StringToProducer(const std::string& s) { | 29 scoped_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) { |
| 30 scoped_ptr<char[]> data(new char[s.size()]); | 30 scoped_ptr<char[]> data(new char[s.size()]); |
| 31 std::memcpy(data.get(), s.data(), s.size()); | 31 std::memcpy(data.get(), s.data(), s.size()); |
| 32 return scoped_ptr<SpdyFrameProducer>( | 32 return scoped_ptr<SpdyBufferProducer>( |
| 33 new SimpleFrameProducer( | 33 new SimpleBufferProducer( |
| 34 scoped_ptr<SpdyFrame>( | 34 scoped_ptr<SpdyBuffer>( |
| 35 new SpdyFrame(data.release(), s.size(), true)))); | 35 new SpdyBuffer( |
| 36 scoped_ptr<SpdyFrame>( |
| 37 new SpdyFrame(data.release(), s.size(), true)))))); |
| 36 } | 38 } |
| 37 | 39 |
| 38 // Makes a SpdyFrameProducer producing a frame with the data in the | 40 // Makes a SpdyBufferProducer producing a frame with the data in the |
| 39 // given int (converted to a string). | 41 // given int (converted to a string). |
| 40 scoped_ptr<SpdyFrameProducer> IntToProducer(int i) { | 42 scoped_ptr<SpdyBufferProducer> IntToProducer(int i) { |
| 41 return StringToProducer(base::IntToString(i)); | 43 return StringToProducer(base::IntToString(i)); |
| 42 } | 44 } |
| 43 | 45 |
| 44 // Produces a frame with the given producer and returns a copy of its | 46 // Produces a frame with the given producer and returns a copy of its |
| 45 // data as a string. | 47 // data as a string. |
| 46 std::string ProducerToString(scoped_ptr<SpdyFrameProducer> producer) { | 48 std::string ProducerToString(scoped_ptr<SpdyBufferProducer> producer) { |
| 47 scoped_ptr<SpdyFrame> frame = producer->ProduceFrame(); | 49 scoped_ptr<SpdyBuffer> buffer = producer->ProduceBuffer(); |
| 48 return std::string(frame->data(), frame->size()); | 50 return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 // Produces a frame with the given producer and returns a copy of its | 53 // Produces a frame with the given producer and returns a copy of its |
| 52 // data as an int (converted from a string). | 54 // data as an int (converted from a string). |
| 53 int ProducerToInt(scoped_ptr<SpdyFrameProducer> producer) { | 55 int ProducerToInt(scoped_ptr<SpdyBufferProducer> producer) { |
| 54 int i = 0; | 56 int i = 0; |
| 55 EXPECT_TRUE(base::StringToInt(ProducerToString(producer.Pass()), &i)); | 57 EXPECT_TRUE(base::StringToInt(ProducerToString(producer.Pass()), &i)); |
| 56 return i; | 58 return i; |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Makes a SpdyStream with the given priority and a NULL SpdySession | 61 // Makes a SpdyStream with the given priority and a NULL SpdySession |
| 60 // -- be careful to not call any functions that expect the session to | 62 // -- be careful to not call any functions that expect the session to |
| 61 // be there. | 63 // be there. |
| 62 SpdyStream* MakeTestStream(RequestPriority priority) { | 64 SpdyStream* MakeTestStream(RequestPriority priority) { |
| 63 return new SpdyStream( | 65 return new SpdyStream( |
| 64 NULL, std::string(), priority, 0, 0, false, BoundNetLog()); | 66 NULL, std::string(), priority, 0, 0, false, BoundNetLog()); |
| 65 } | 67 } |
| 66 | 68 |
| 67 // Add some frame producers of different priority. The producers | 69 // Add some frame producers of different priority. The producers |
| 68 // should be dequeued in priority order with their associated stream. | 70 // should be dequeued in priority order with their associated stream. |
| 69 TEST_F(SpdyWriteQueueTest, DequeuesByPriority) { | 71 TEST_F(SpdyWriteQueueTest, DequeuesByPriority) { |
| 70 SpdyWriteQueue write_queue; | 72 SpdyWriteQueue write_queue; |
| 71 | 73 |
| 72 scoped_ptr<SpdyFrameProducer> producer_low = StringToProducer("LOW"); | 74 scoped_ptr<SpdyBufferProducer> producer_low = StringToProducer("LOW"); |
| 73 scoped_ptr<SpdyFrameProducer> producer_medium = StringToProducer("MEDIUM"); | 75 scoped_ptr<SpdyBufferProducer> producer_medium = StringToProducer("MEDIUM"); |
| 74 scoped_ptr<SpdyFrameProducer> producer_highest = StringToProducer("HIGHEST"); | 76 scoped_ptr<SpdyBufferProducer> producer_highest = StringToProducer("HIGHEST"); |
| 75 | 77 |
| 76 // A NULL stream should still work. | 78 // A NULL stream should still work. |
| 77 scoped_refptr<SpdyStream> stream_low(NULL); | 79 scoped_refptr<SpdyStream> stream_low(NULL); |
| 78 scoped_refptr<SpdyStream> stream_medium(MakeTestStream(MEDIUM)); | 80 scoped_refptr<SpdyStream> stream_medium(MakeTestStream(MEDIUM)); |
| 79 scoped_refptr<SpdyStream> stream_highest(MakeTestStream(HIGHEST)); | 81 scoped_refptr<SpdyStream> stream_highest(MakeTestStream(HIGHEST)); |
| 80 | 82 |
| 81 write_queue.Enqueue( | 83 write_queue.Enqueue( |
| 82 LOW, SYN_STREAM, producer_low.Pass(), stream_low); | 84 LOW, SYN_STREAM, producer_low.Pass(), stream_low); |
| 83 write_queue.Enqueue( | 85 write_queue.Enqueue( |
| 84 MEDIUM, SYN_REPLY, producer_medium.Pass(), stream_medium); | 86 MEDIUM, SYN_REPLY, producer_medium.Pass(), stream_medium); |
| 85 write_queue.Enqueue( | 87 write_queue.Enqueue( |
| 86 HIGHEST, RST_STREAM, producer_highest.Pass(), stream_highest); | 88 HIGHEST, RST_STREAM, producer_highest.Pass(), stream_highest); |
| 87 | 89 |
| 88 SpdyFrameType frame_type = DATA; | 90 SpdyFrameType frame_type = DATA; |
| 89 scoped_ptr<SpdyFrameProducer> frame_producer; | 91 scoped_ptr<SpdyBufferProducer> frame_producer; |
| 90 scoped_refptr<SpdyStream> stream; | 92 scoped_refptr<SpdyStream> stream; |
| 91 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 93 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 92 EXPECT_EQ(RST_STREAM, frame_type); | 94 EXPECT_EQ(RST_STREAM, frame_type); |
| 93 EXPECT_EQ("HIGHEST", ProducerToString(frame_producer.Pass())); | 95 EXPECT_EQ("HIGHEST", ProducerToString(frame_producer.Pass())); |
| 94 EXPECT_EQ(stream_highest, stream); | 96 EXPECT_EQ(stream_highest, stream); |
| 95 | 97 |
| 96 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 98 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 97 EXPECT_EQ(SYN_REPLY, frame_type); | 99 EXPECT_EQ(SYN_REPLY, frame_type); |
| 98 EXPECT_EQ("MEDIUM", ProducerToString(frame_producer.Pass())); | 100 EXPECT_EQ("MEDIUM", ProducerToString(frame_producer.Pass())); |
| 99 EXPECT_EQ(stream_medium, stream); | 101 EXPECT_EQ(stream_medium, stream); |
| 100 | 102 |
| 101 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 103 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 102 EXPECT_EQ(SYN_STREAM, frame_type); | 104 EXPECT_EQ(SYN_STREAM, frame_type); |
| 103 EXPECT_EQ("LOW", ProducerToString(frame_producer.Pass())); | 105 EXPECT_EQ("LOW", ProducerToString(frame_producer.Pass())); |
| 104 EXPECT_EQ(stream_low, stream); | 106 EXPECT_EQ(stream_low, stream); |
| 105 | 107 |
| 106 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 108 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 107 } | 109 } |
| 108 | 110 |
| 109 // Add some frame producers with the same priority. The producers | 111 // Add some frame producers with the same priority. The producers |
| 110 // should be dequeued in FIFO order with their associated stream. | 112 // should be dequeued in FIFO order with their associated stream. |
| 111 TEST_F(SpdyWriteQueueTest, DequeuesFIFO) { | 113 TEST_F(SpdyWriteQueueTest, DequeuesFIFO) { |
| 112 SpdyWriteQueue write_queue; | 114 SpdyWriteQueue write_queue; |
| 113 | 115 |
| 114 scoped_ptr<SpdyFrameProducer> producer1 = IntToProducer(1); | 116 scoped_ptr<SpdyBufferProducer> producer1 = IntToProducer(1); |
| 115 scoped_ptr<SpdyFrameProducer> producer2 = IntToProducer(2); | 117 scoped_ptr<SpdyBufferProducer> producer2 = IntToProducer(2); |
| 116 scoped_ptr<SpdyFrameProducer> producer3 = IntToProducer(3); | 118 scoped_ptr<SpdyBufferProducer> producer3 = IntToProducer(3); |
| 117 | 119 |
| 118 scoped_refptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY)); | 120 scoped_refptr<SpdyStream> stream1(MakeTestStream(DEFAULT_PRIORITY)); |
| 119 scoped_refptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY)); | 121 scoped_refptr<SpdyStream> stream2(MakeTestStream(DEFAULT_PRIORITY)); |
| 120 scoped_refptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY)); | 122 scoped_refptr<SpdyStream> stream3(MakeTestStream(DEFAULT_PRIORITY)); |
| 121 | 123 |
| 122 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, producer1.Pass(), stream1); | 124 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, producer1.Pass(), stream1); |
| 123 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_REPLY, producer2.Pass(), stream2); | 125 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_REPLY, producer2.Pass(), stream2); |
| 124 write_queue.Enqueue(DEFAULT_PRIORITY, RST_STREAM, producer3.Pass(), stream3); | 126 write_queue.Enqueue(DEFAULT_PRIORITY, RST_STREAM, producer3.Pass(), stream3); |
| 125 | 127 |
| 126 SpdyFrameType frame_type = DATA; | 128 SpdyFrameType frame_type = DATA; |
| 127 scoped_ptr<SpdyFrameProducer> frame_producer; | 129 scoped_ptr<SpdyBufferProducer> frame_producer; |
| 128 scoped_refptr<SpdyStream> stream; | 130 scoped_refptr<SpdyStream> stream; |
| 129 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 131 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 130 EXPECT_EQ(SYN_STREAM, frame_type); | 132 EXPECT_EQ(SYN_STREAM, frame_type); |
| 131 EXPECT_EQ(1, ProducerToInt(frame_producer.Pass())); | 133 EXPECT_EQ(1, ProducerToInt(frame_producer.Pass())); |
| 132 EXPECT_EQ(stream1, stream); | 134 EXPECT_EQ(stream1, stream); |
| 133 | 135 |
| 134 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 136 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 135 EXPECT_EQ(SYN_REPLY, frame_type); | 137 EXPECT_EQ(SYN_REPLY, frame_type); |
| 136 EXPECT_EQ(2, ProducerToInt(frame_producer.Pass())); | 138 EXPECT_EQ(2, ProducerToInt(frame_producer.Pass())); |
| 137 EXPECT_EQ(stream2, stream); | 139 EXPECT_EQ(stream2, stream); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 155 | 157 |
| 156 for (int i = 0; i < 100; ++i) { | 158 for (int i = 0; i < 100; ++i) { |
| 157 scoped_refptr<SpdyStream> stream = ((i % 3) == 0) ? stream1 : stream2; | 159 scoped_refptr<SpdyStream> stream = ((i % 3) == 0) ? stream1 : stream2; |
| 158 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), stream); | 160 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), stream); |
| 159 } | 161 } |
| 160 | 162 |
| 161 write_queue.RemovePendingWritesForStream(stream2); | 163 write_queue.RemovePendingWritesForStream(stream2); |
| 162 | 164 |
| 163 for (int i = 0; i < 100; i += 3) { | 165 for (int i = 0; i < 100; i += 3) { |
| 164 SpdyFrameType frame_type = DATA; | 166 SpdyFrameType frame_type = DATA; |
| 165 scoped_ptr<SpdyFrameProducer> frame_producer; | 167 scoped_ptr<SpdyBufferProducer> frame_producer; |
| 166 scoped_refptr<SpdyStream> stream; | 168 scoped_refptr<SpdyStream> stream; |
| 167 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 169 ASSERT_TRUE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 168 EXPECT_EQ(SYN_STREAM, frame_type); | 170 EXPECT_EQ(SYN_STREAM, frame_type); |
| 169 EXPECT_EQ(i, ProducerToInt(frame_producer.Pass())); | 171 EXPECT_EQ(i, ProducerToInt(frame_producer.Pass())); |
| 170 EXPECT_EQ(stream1, stream); | 172 EXPECT_EQ(stream1, stream); |
| 171 } | 173 } |
| 172 | 174 |
| 173 SpdyFrameType frame_type = DATA; | 175 SpdyFrameType frame_type = DATA; |
| 174 scoped_ptr<SpdyFrameProducer> frame_producer; | 176 scoped_ptr<SpdyBufferProducer> frame_producer; |
| 175 scoped_refptr<SpdyStream> stream; | 177 scoped_refptr<SpdyStream> stream; |
| 176 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 178 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 177 } | 179 } |
| 178 | 180 |
| 179 // Enqueue a bunch of writes and then call Clear(). The write queue | 181 // Enqueue a bunch of writes and then call Clear(). The write queue |
| 180 // should clean up the memory properly, and Dequeue() should return | 182 // should clean up the memory properly, and Dequeue() should return |
| 181 // false. | 183 // false. |
| 182 TEST_F(SpdyWriteQueueTest, Clear) { | 184 TEST_F(SpdyWriteQueueTest, Clear) { |
| 183 SpdyWriteQueue write_queue; | 185 SpdyWriteQueue write_queue; |
| 184 | 186 |
| 185 for (int i = 0; i < 100; ++i) { | 187 for (int i = 0; i < 100; ++i) { |
| 186 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), NULL); | 188 write_queue.Enqueue(DEFAULT_PRIORITY, SYN_STREAM, IntToProducer(i), NULL); |
| 187 } | 189 } |
| 188 | 190 |
| 189 write_queue.Clear(); | 191 write_queue.Clear(); |
| 190 | 192 |
| 191 SpdyFrameType frame_type = DATA; | 193 SpdyFrameType frame_type = DATA; |
| 192 scoped_ptr<SpdyFrameProducer> frame_producer; | 194 scoped_ptr<SpdyBufferProducer> frame_producer; |
| 193 scoped_refptr<SpdyStream> stream; | 195 scoped_refptr<SpdyStream> stream; |
| 194 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); | 196 EXPECT_FALSE(write_queue.Dequeue(&frame_type, &frame_producer, &stream)); |
| 195 } | 197 } |
| 196 | 198 |
| 197 } | 199 } // namespace |
| 198 | 200 |
| 199 } // namespace net | 201 } // namespace net |
| OLD | NEW |