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