| 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 <memory> | 9 #include <memory> |
| 10 #include <string> | |
| 11 #include <utility> | 10 #include <utility> |
| 12 | 11 |
| 13 #include "base/logging.h" | 12 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 15 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 16 #include "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
| 17 #include "net/log/net_log_with_source.h" | 16 #include "net/log/net_log_with_source.h" |
| 17 #include "net/spdy/platform/api/spdy_string.h" |
| 18 #include "net/spdy/spdy_buffer_producer.h" | 18 #include "net/spdy/spdy_buffer_producer.h" |
| 19 #include "net/spdy/spdy_stream.h" | 19 #include "net/spdy/spdy_stream.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 using std::string; | |
| 28 | |
| 29 const char kOriginal[] = "original"; | 27 const char kOriginal[] = "original"; |
| 30 const char kRequeued[] = "requeued"; | 28 const char kRequeued[] = "requeued"; |
| 31 | 29 |
| 32 class SpdyWriteQueueTest : public ::testing::Test {}; | 30 class SpdyWriteQueueTest : public ::testing::Test {}; |
| 33 | 31 |
| 34 // Makes a SpdyFrameProducer producing a frame with the data in the | 32 // Makes a SpdyFrameProducer producing a frame with the data in the |
| 35 // given string. | 33 // given string. |
| 36 std::unique_ptr<SpdyBufferProducer> StringToProducer(const std::string& s) { | 34 std::unique_ptr<SpdyBufferProducer> StringToProducer(const SpdyString& s) { |
| 37 std::unique_ptr<char[]> data(new char[s.size()]); | 35 std::unique_ptr<char[]> data(new char[s.size()]); |
| 38 std::memcpy(data.get(), s.data(), s.size()); | 36 std::memcpy(data.get(), s.data(), s.size()); |
| 39 return std::unique_ptr<SpdyBufferProducer>( | 37 return std::unique_ptr<SpdyBufferProducer>( |
| 40 new SimpleBufferProducer(std::unique_ptr<SpdyBuffer>( | 38 new SimpleBufferProducer(std::unique_ptr<SpdyBuffer>( |
| 41 new SpdyBuffer(std::unique_ptr<SpdySerializedFrame>( | 39 new SpdyBuffer(std::unique_ptr<SpdySerializedFrame>( |
| 42 new SpdySerializedFrame(data.release(), s.size(), true)))))); | 40 new SpdySerializedFrame(data.release(), s.size(), true)))))); |
| 43 } | 41 } |
| 44 | 42 |
| 45 // Makes a SpdyBufferProducer producing a frame with the data in the | 43 // Makes a SpdyBufferProducer producing a frame with the data in the |
| 46 // given int (converted to a string). | 44 // given int (converted to a string). |
| (...skipping 30 matching lines...) Expand all Loading... |
| 77 queue->Enqueue(MEDIUM, SpdyFrameType::RST_STREAM, std::move(producer), | 75 queue->Enqueue(MEDIUM, SpdyFrameType::RST_STREAM, std::move(producer), |
| 78 base::WeakPtr<SpdyStream>()); | 76 base::WeakPtr<SpdyStream>()); |
| 79 } | 77 } |
| 80 | 78 |
| 81 private: | 79 private: |
| 82 std::unique_ptr<SpdyBuffer> buffer_; | 80 std::unique_ptr<SpdyBuffer> buffer_; |
| 83 }; | 81 }; |
| 84 | 82 |
| 85 // Produces a frame with the given producer and returns a copy of its | 83 // Produces a frame with the given producer and returns a copy of its |
| 86 // data as a string. | 84 // data as a string. |
| 87 std::string ProducerToString(std::unique_ptr<SpdyBufferProducer> producer) { | 85 SpdyString ProducerToString(std::unique_ptr<SpdyBufferProducer> producer) { |
| 88 std::unique_ptr<SpdyBuffer> buffer = producer->ProduceBuffer(); | 86 std::unique_ptr<SpdyBuffer> buffer = producer->ProduceBuffer(); |
| 89 return std::string(buffer->GetRemainingData(), buffer->GetRemainingSize()); | 87 return SpdyString(buffer->GetRemainingData(), buffer->GetRemainingSize()); |
| 90 } | 88 } |
| 91 | 89 |
| 92 // Produces a frame with the given producer and returns a copy of its | 90 // Produces a frame with the given producer and returns a copy of its |
| 93 // data as an int (converted from a string). | 91 // data as an int (converted from a string). |
| 94 int ProducerToInt(std::unique_ptr<SpdyBufferProducer> producer) { | 92 int ProducerToInt(std::unique_ptr<SpdyBufferProducer> producer) { |
| 95 int i = 0; | 93 int i = 0; |
| 96 EXPECT_TRUE(base::StringToInt(ProducerToString(std::move(producer)), &i)); | 94 EXPECT_TRUE(base::StringToInt(ProducerToString(std::move(producer)), &i)); |
| 97 return i; | 95 return i; |
| 98 } | 96 } |
| 99 | 97 |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, | 291 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, |
| 294 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), | 292 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), |
| 295 base::WeakPtr<SpdyStream>()); | 293 base::WeakPtr<SpdyStream>()); |
| 296 { | 294 { |
| 297 SpdyFrameType frame_type; | 295 SpdyFrameType frame_type; |
| 298 std::unique_ptr<SpdyBufferProducer> producer; | 296 std::unique_ptr<SpdyBufferProducer> producer; |
| 299 base::WeakPtr<SpdyStream> stream; | 297 base::WeakPtr<SpdyStream> stream; |
| 300 | 298 |
| 301 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream)); | 299 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream)); |
| 302 EXPECT_TRUE(queue.IsEmpty()); | 300 EXPECT_TRUE(queue.IsEmpty()); |
| 303 EXPECT_EQ(string(kOriginal), producer->ProduceBuffer()->GetRemainingData()); | 301 EXPECT_EQ(SpdyString(kOriginal), |
| 302 producer->ProduceBuffer()->GetRemainingData()); |
| 304 } | 303 } |
| 305 // |producer| was destroyed, and a buffer is re-queued. | 304 // |producer| was destroyed, and a buffer is re-queued. |
| 306 EXPECT_FALSE(queue.IsEmpty()); | 305 EXPECT_FALSE(queue.IsEmpty()); |
| 307 | 306 |
| 308 SpdyFrameType frame_type; | 307 SpdyFrameType frame_type; |
| 309 std::unique_ptr<SpdyBufferProducer> producer; | 308 std::unique_ptr<SpdyBufferProducer> producer; |
| 310 base::WeakPtr<SpdyStream> stream; | 309 base::WeakPtr<SpdyStream> stream; |
| 311 | 310 |
| 312 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream)); | 311 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream)); |
| 313 EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData()); | 312 EXPECT_EQ(SpdyString(kRequeued), |
| 313 producer->ProduceBuffer()->GetRemainingData()); |
| 314 } | 314 } |
| 315 | 315 |
| 316 TEST_F(SpdyWriteQueueTest, ReentranceOnClear) { | 316 TEST_F(SpdyWriteQueueTest, ReentranceOnClear) { |
| 317 SpdyWriteQueue queue; | 317 SpdyWriteQueue queue; |
| 318 queue.Enqueue( | 318 queue.Enqueue( |
| 319 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, | 319 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, |
| 320 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), | 320 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), |
| 321 base::WeakPtr<SpdyStream>()); | 321 base::WeakPtr<SpdyStream>()); |
| 322 | 322 |
| 323 queue.Clear(); | 323 queue.Clear(); |
| 324 EXPECT_FALSE(queue.IsEmpty()); | 324 EXPECT_FALSE(queue.IsEmpty()); |
| 325 | 325 |
| 326 SpdyFrameType frame_type; | 326 SpdyFrameType frame_type; |
| 327 std::unique_ptr<SpdyBufferProducer> producer; | 327 std::unique_ptr<SpdyBufferProducer> producer; |
| 328 base::WeakPtr<SpdyStream> stream; | 328 base::WeakPtr<SpdyStream> stream; |
| 329 | 329 |
| 330 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream)); | 330 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &stream)); |
| 331 EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData()); | 331 EXPECT_EQ(SpdyString(kRequeued), |
| 332 producer->ProduceBuffer()->GetRemainingData()); |
| 332 } | 333 } |
| 333 | 334 |
| 334 TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesAfter) { | 335 TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesAfter) { |
| 335 std::unique_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY)); | 336 std::unique_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY)); |
| 336 stream->set_stream_id(2); | 337 stream->set_stream_id(2); |
| 337 | 338 |
| 338 SpdyWriteQueue queue; | 339 SpdyWriteQueue queue; |
| 339 queue.Enqueue( | 340 queue.Enqueue( |
| 340 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, | 341 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, |
| 341 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), | 342 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), |
| 342 stream->GetWeakPtr()); | 343 stream->GetWeakPtr()); |
| 343 | 344 |
| 344 queue.RemovePendingWritesForStreamsAfter(1); | 345 queue.RemovePendingWritesForStreamsAfter(1); |
| 345 EXPECT_FALSE(queue.IsEmpty()); | 346 EXPECT_FALSE(queue.IsEmpty()); |
| 346 | 347 |
| 347 SpdyFrameType frame_type; | 348 SpdyFrameType frame_type; |
| 348 std::unique_ptr<SpdyBufferProducer> producer; | 349 std::unique_ptr<SpdyBufferProducer> producer; |
| 349 base::WeakPtr<SpdyStream> weak_stream; | 350 base::WeakPtr<SpdyStream> weak_stream; |
| 350 | 351 |
| 351 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream)); | 352 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream)); |
| 352 EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData()); | 353 EXPECT_EQ(SpdyString(kRequeued), |
| 354 producer->ProduceBuffer()->GetRemainingData()); |
| 353 } | 355 } |
| 354 | 356 |
| 355 TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesForStream) { | 357 TEST_F(SpdyWriteQueueTest, ReentranceOnRemovePendingWritesForStream) { |
| 356 std::unique_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY)); | 358 std::unique_ptr<SpdyStream> stream(MakeTestStream(DEFAULT_PRIORITY)); |
| 357 stream->set_stream_id(2); | 359 stream->set_stream_id(2); |
| 358 | 360 |
| 359 SpdyWriteQueue queue; | 361 SpdyWriteQueue queue; |
| 360 queue.Enqueue( | 362 queue.Enqueue( |
| 361 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, | 363 DEFAULT_PRIORITY, SpdyFrameType::HEADERS, |
| 362 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), | 364 std::unique_ptr<SpdyBufferProducer>(new RequeingBufferProducer(&queue)), |
| 363 stream->GetWeakPtr()); | 365 stream->GetWeakPtr()); |
| 364 | 366 |
| 365 queue.RemovePendingWritesForStream(stream->GetWeakPtr()); | 367 queue.RemovePendingWritesForStream(stream->GetWeakPtr()); |
| 366 EXPECT_FALSE(queue.IsEmpty()); | 368 EXPECT_FALSE(queue.IsEmpty()); |
| 367 | 369 |
| 368 SpdyFrameType frame_type; | 370 SpdyFrameType frame_type; |
| 369 std::unique_ptr<SpdyBufferProducer> producer; | 371 std::unique_ptr<SpdyBufferProducer> producer; |
| 370 base::WeakPtr<SpdyStream> weak_stream; | 372 base::WeakPtr<SpdyStream> weak_stream; |
| 371 | 373 |
| 372 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream)); | 374 EXPECT_TRUE(queue.Dequeue(&frame_type, &producer, &weak_stream)); |
| 373 EXPECT_EQ(string(kRequeued), producer->ProduceBuffer()->GetRemainingData()); | 375 EXPECT_EQ(SpdyString(kRequeued), |
| 376 producer->ProduceBuffer()->GetRemainingData()); |
| 374 } | 377 } |
| 375 | 378 |
| 376 } // namespace | 379 } // namespace |
| 377 | 380 |
| 378 } // namespace net | 381 } // namespace net |
| OLD | NEW |