| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/byte_stream.h" | 5 #include "content/browser/byte_stream.h" |
| 6 | 6 |
| 7 #include <deque> | 7 #include <deque> |
| 8 #include <limits> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/test/test_simple_task_runner.h" | 14 #include "base/test/test_simple_task_runner.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 110 |
| 110 // Push a series of IO buffers on; test pushback happening and | 111 // Push a series of IO buffers on; test pushback happening and |
| 111 // that it's advisory. | 112 // that it's advisory. |
| 112 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 113 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 113 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 114 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 114 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 115 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 115 EXPECT_FALSE(Write(byte_stream_input.get(), 1)); | 116 EXPECT_FALSE(Write(byte_stream_input.get(), 1)); |
| 116 EXPECT_FALSE(Write(byte_stream_input.get(), 1024)); | 117 EXPECT_FALSE(Write(byte_stream_input.get(), 1024)); |
| 117 // Flush | 118 // Flush |
| 118 byte_stream_input->Close(0); | 119 byte_stream_input->Close(0); |
| 120 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); |
| 119 message_loop_.RunUntilIdle(); | 121 message_loop_.RunUntilIdle(); |
| 122 // Data already sent to reader is also counted in. |
| 123 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); |
| 120 | 124 |
| 121 // Pull the IO buffers out; do we get the same buffers and do they | 125 // Pull the IO buffers out; do we get the same buffers and do they |
| 122 // have the same contents? | 126 // have the same contents? |
| 123 scoped_refptr<net::IOBuffer> output_io_buffer; | 127 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 124 size_t output_length; | 128 size_t output_length; |
| 125 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 129 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 126 byte_stream_output->Read(&output_io_buffer, &output_length)); | 130 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 127 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 128 | 132 |
| 129 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 133 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 130 byte_stream_output->Read(&output_io_buffer, &output_length)); | 134 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 135 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 132 | 136 |
| 133 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 137 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 134 byte_stream_output->Read(&output_io_buffer, &output_length)); | 138 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 135 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 139 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 136 | 140 |
| 137 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 141 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 138 byte_stream_output->Read(&output_io_buffer, &output_length)); | 142 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 139 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 140 | 144 |
| 141 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 145 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 142 byte_stream_output->Read(&output_io_buffer, &output_length)); | 146 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 147 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 144 | 148 |
| 145 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, | 149 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 146 byte_stream_output->Read(&output_io_buffer, &output_length)); | 150 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 151 |
| 152 message_loop_.RunUntilIdle(); |
| 153 // Reader now knows that all data is read out. |
| 154 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes()); |
| 147 } | 155 } |
| 148 | 156 |
| 149 // Confirm that Flush() method makes the writer to send written contents to | 157 // Confirm that Flush() method makes the writer to send written contents to |
| 150 // the reader. | 158 // the reader. |
| 151 TEST_F(ByteStreamTest, ByteStream_Flush) { | 159 TEST_F(ByteStreamTest, ByteStream_Flush) { |
| 152 scoped_ptr<ByteStreamWriter> byte_stream_input; | 160 scoped_ptr<ByteStreamWriter> byte_stream_input; |
| 153 scoped_ptr<ByteStreamReader> byte_stream_output; | 161 scoped_ptr<ByteStreamReader> byte_stream_output; |
| 154 CreateByteStream( | 162 CreateByteStream( |
| 155 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), | 163 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), |
| 156 1024, &byte_stream_input, &byte_stream_output); | 164 1024, &byte_stream_input, &byte_stream_output); |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 580 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 573 byte_stream_output->Read(&output_io_buffer, &output_length)); | 581 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 574 | 582 |
| 575 byte_stream_input->Close(0); | 583 byte_stream_input->Close(0); |
| 576 message_loop_.RunUntilIdle(); | 584 message_loop_.RunUntilIdle(); |
| 577 | 585 |
| 578 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, | 586 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 579 byte_stream_output->Read(&output_io_buffer, &output_length)); | 587 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 580 } | 588 } |
| 581 | 589 |
| 590 TEST_F(ByteStreamTest, ByteStream_WriteOverflow) { |
| 591 scoped_ptr<ByteStreamWriter> byte_stream_input; |
| 592 scoped_ptr<ByteStreamReader> byte_stream_output; |
| 593 CreateByteStream( |
| 594 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), |
| 595 std::numeric_limits<size_t>::max(), |
| 596 &byte_stream_input, &byte_stream_output); |
| 597 |
| 598 EXPECT_TRUE(Write(byte_stream_input.get(), 1)); |
| 599 // 1 + size_t max -> Overflow. |
| 600 scoped_refptr<net::IOBuffer> empty_io_buffer; |
| 601 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer, |
| 602 std::numeric_limits<size_t>::max())); |
| 603 message_loop_.RunUntilIdle(); |
| 604 |
| 605 // The first write is below PostToPeer threshold. We shouldn't get anything |
| 606 // from the output. |
| 607 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 608 size_t output_length; |
| 609 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 610 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 611 } |
| 612 |
| 582 } // namespace content | 613 } // namespace content |
| OLD | NEW |