[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f8e92b5d | 2013-03-21 18:35:46 | [diff] [blame] | 5 | #include "content/browser/byte_stream.h" |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 9 | #include <deque> |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 10 | #include <limits> |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 11 | |
| 12 | #include "base/bind.h" |
| 13 | #include "base/callback.h" |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 15 | #include "base/run_loop.h" |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 16 | #include "base/test/test_simple_task_runner.h" |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 17 | #include "net/base/io_buffer.h" |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 20 | namespace content { |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 21 | namespace { |
| 22 | |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 23 | void CountCallbacks(int* counter) { |
| 24 | ++*counter; |
| 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | class ByteStreamTest : public testing::Test { |
| 30 | public: |
| 31 | ByteStreamTest(); |
| 32 | |
| 33 | // Create a new IO buffer of the given |buffer_size|. Details of the |
| 34 | // contents of the created buffer will be kept, and can be validated |
| 35 | // by ValidateIOBuffer. |
| 36 | scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) { |
| 37 | scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size)); |
| 38 | char *bufferp = buffer->data(); |
| 39 | for (size_t i = 0; i < buffer_size; i++) |
| 40 | bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char)); |
| 41 | pointer_queue_.push_back(bufferp); |
| 42 | length_queue_.push_back(buffer_size); |
| 43 | ++producing_seed_key_; |
| 44 | return buffer; |
| 45 | } |
| 46 | |
| 47 | // Create an IOBuffer of the appropriate size and add it to the |
| 48 | // ByteStream, returning the result of the ByteStream::Write. |
| 49 | // Separate function to avoid duplication of buffer_size in test |
| 50 | // calls. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 51 | bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) { |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 52 | return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size); |
| 53 | } |
| 54 | |
| 55 | // Validate that we have the IOBuffer we expect. This routine must be |
| 56 | // called on buffers that were allocated from NewIOBuffer, and in the |
| 57 | // order that they were allocated. Calls to NewIOBuffer && |
| 58 | // ValidateIOBuffer may be interleaved. |
| 59 | bool ValidateIOBuffer( |
| 60 | scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) { |
| 61 | char *bufferp = buffer->data(); |
| 62 | |
| 63 | char *expected_ptr = pointer_queue_.front(); |
| 64 | size_t expected_length = length_queue_.front(); |
| 65 | pointer_queue_.pop_front(); |
| 66 | length_queue_.pop_front(); |
| 67 | ++consuming_seed_key_; |
| 68 | |
| 69 | EXPECT_EQ(expected_ptr, bufferp); |
| 70 | if (expected_ptr != bufferp) |
| 71 | return false; |
| 72 | |
| 73 | EXPECT_EQ(expected_length, buffer_size); |
| 74 | if (expected_length != buffer_size) |
| 75 | return false; |
| 76 | |
| 77 | for (size_t i = 0; i < buffer_size; i++) { |
| 78 | // Already incremented, so subtract one from the key. |
| 79 | EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1) |
| 80 | % (1 << sizeof(char))), |
| 81 | bufferp[i]); |
| 82 | if (static_cast<int>((i + consuming_seed_key_ - 1) % |
| 83 | (1 << sizeof(char))) != bufferp[i]) { |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | protected: |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 91 | base::MessageLoop message_loop_; |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 92 | |
| 93 | private: |
| 94 | int producing_seed_key_; |
| 95 | int consuming_seed_key_; |
| 96 | std::deque<char*> pointer_queue_; |
| 97 | std::deque<size_t> length_queue_; |
| 98 | }; |
| 99 | |
| 100 | ByteStreamTest::ByteStreamTest() |
| 101 | : producing_seed_key_(0), |
| 102 | consuming_seed_key_(0) { } |
| 103 | |
[email protected] | d7db4f62 | 2012-06-04 18:20:56 | [diff] [blame] | 104 | // Confirm that filling and emptying the stream works properly, and that |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 105 | // we get full triggers when we expect. |
| 106 | TEST_F(ByteStreamTest, ByteStream_PushBack) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 107 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 108 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 109 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 110 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 111 | |
| 112 | // Push a series of IO buffers on; test pushback happening and |
| 113 | // that it's advisory. |
| 114 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 115 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 116 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 117 | EXPECT_FALSE(Write(byte_stream_input.get(), 1)); |
| 118 | EXPECT_FALSE(Write(byte_stream_input.get(), 1024)); |
| 119 | // Flush |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 120 | byte_stream_input->Close(0); |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 121 | EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 122 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 123 | // Data already sent to reader is also counted in. |
| 124 | EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 125 | |
| 126 | // Pull the IO buffers out; do we get the same buffers and do they |
| 127 | // have the same contents? |
| 128 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 129 | size_t output_length; |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 130 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 131 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 132 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 133 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 134 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 135 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 136 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 137 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 138 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 139 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 140 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 141 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 142 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 143 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 144 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 145 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 146 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 147 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 148 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 149 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 150 | EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 151 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 152 | |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 153 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 154 | // Reader now knows that all data is read out. |
| 155 | EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 566357e | 2013-07-31 03:59:36 | [diff] [blame] | 158 | // Confirm that Flush() method makes the writer to send written contents to |
| 159 | // the reader. |
| 160 | TEST_F(ByteStreamTest, ByteStream_Flush) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 161 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 162 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 163 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 164 | 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 566357e | 2013-07-31 03:59:36 | [diff] [blame] | 165 | |
| 166 | EXPECT_TRUE(Write(byte_stream_input.get(), 1)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 167 | base::RunLoop().RunUntilIdle(); |
[email protected] | 566357e | 2013-07-31 03:59:36 | [diff] [blame] | 168 | |
| 169 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 170 | size_t output_length = 0; |
| 171 | // Check that data is not sent to the reader yet. |
| 172 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 173 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 174 | |
| 175 | byte_stream_input->Flush(); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 176 | base::RunLoop().RunUntilIdle(); |
[email protected] | 566357e | 2013-07-31 03:59:36 | [diff] [blame] | 177 | |
| 178 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 179 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 180 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 181 | |
| 182 | // Check that it's ok to Flush() an empty writer. |
| 183 | byte_stream_input->Flush(); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 184 | base::RunLoop().RunUntilIdle(); |
[email protected] | 566357e | 2013-07-31 03:59:36 | [diff] [blame] | 185 | |
| 186 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 187 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 188 | |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 189 | byte_stream_input->Close(0); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 190 | base::RunLoop().RunUntilIdle(); |
[email protected] | 566357e | 2013-07-31 03:59:36 | [diff] [blame] | 191 | |
| 192 | EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 193 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 194 | } |
| 195 | |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 196 | // Same as above, only use knowledge of the internals to confirm |
| 197 | // that we're getting pushback even when data's split across the two |
| 198 | // objects |
| 199 | TEST_F(ByteStreamTest, ByteStream_PushBackSplit) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 200 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 201 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 202 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 203 | 9 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 204 | |
| 205 | // Push a series of IO buffers on; test pushback happening and |
| 206 | // that it's advisory. |
| 207 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 208 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 209 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 210 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 211 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 212 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 213 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 214 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 215 | EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 216 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 217 | |
| 218 | // Pull the IO buffers out; do we get the same buffers and do they |
| 219 | // have the same contents? |
| 220 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 221 | size_t output_length; |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 222 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 223 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 224 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 225 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 226 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 227 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 228 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 229 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 230 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 231 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 232 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 233 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 234 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 235 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 236 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 237 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 238 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 239 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 240 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 241 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 242 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 243 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 244 | } |
| 245 | |
| 246 | // Confirm that a Close() notification transmits in-order |
[email protected] | d7db4f62 | 2012-06-04 18:20:56 | [diff] [blame] | 247 | // with data on the stream. |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 248 | TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 249 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 250 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 251 | |
| 252 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 253 | size_t output_length; |
| 254 | |
| 255 | // Empty stream, non-error case. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 256 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 257 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 258 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 259 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 260 | byte_stream_input->Close(0); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 261 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 262 | ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 263 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 264 | EXPECT_EQ(0, byte_stream_output->GetStatus()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 265 | |
| 266 | // Non-empty stream, non-error case. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 267 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 268 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 269 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 270 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 271 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 272 | byte_stream_input->Close(0); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 273 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 274 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 275 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 276 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 277 | ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 278 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 279 | EXPECT_EQ(0, byte_stream_output->GetStatus()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 280 | |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 281 | const int kFakeErrorCode = 22; |
| 282 | |
| 283 | // Empty stream, error case. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 284 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 285 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 286 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 287 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 288 | byte_stream_input->Close(kFakeErrorCode); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 289 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 290 | ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 291 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 292 | EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 293 | |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 294 | // Non-empty stream, error case. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 295 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 296 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 297 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 298 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 299 | EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 300 | byte_stream_input->Close(kFakeErrorCode); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 301 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 302 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 303 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 304 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 305 | ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 306 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 307 | EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Confirm that callbacks on the sink side are triggered when they should be. |
| 311 | TEST_F(ByteStreamTest, ByteStream_SinkCallback) { |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 312 | scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 313 | new base::TestSimpleTaskRunner()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 314 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 315 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 316 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 317 | CreateByteStream(message_loop_.task_runner(), task_runner, 10000, |
| 318 | &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 319 | |
| 320 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 321 | size_t output_length; |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 322 | |
| 323 | // Note that the specifics of when the callbacks are called with regard |
[email protected] | d7db4f62 | 2012-06-04 18:20:56 | [diff] [blame] | 324 | // to how much data is pushed onto the stream is not (currently) part |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 325 | // of the interface contract. If it becomes part of the contract, the |
| 326 | // tests below should get much more precise. |
| 327 | |
| 328 | // Confirm callback called when you add more than 33% of the buffer. |
| 329 | |
| 330 | // Setup callback |
| 331 | int num_callbacks = 0; |
| 332 | byte_stream_output->RegisterCallback( |
| 333 | base::Bind(CountCallbacks, &num_callbacks)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 334 | |
| 335 | EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 336 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 337 | |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 338 | EXPECT_EQ(0, num_callbacks); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 339 | task_runner->RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 340 | EXPECT_EQ(1, num_callbacks); |
| 341 | |
| 342 | // Check data and stream state. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 343 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 344 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 345 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 346 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 347 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 348 | |
| 349 | // Confirm callback *isn't* called at less than 33% (by lack of |
| 350 | // unexpected call on task runner). |
| 351 | EXPECT_TRUE(Write(byte_stream_input.get(), 3000)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 352 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 353 | |
| 354 | // This reflects an implementation artifact that data goes with callbacks, |
| 355 | // which should not be considered part of the interface guarantee. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 356 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 357 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 358 | } |
| 359 | |
| 360 | // Confirm that callbacks on the source side are triggered when they should |
| 361 | // be. |
| 362 | TEST_F(ByteStreamTest, ByteStream_SourceCallback) { |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 363 | scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 364 | new base::TestSimpleTaskRunner()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 365 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 366 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 367 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 368 | CreateByteStream(task_runner, message_loop_.task_runner(), 10000, |
| 369 | &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 370 | |
| 371 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 372 | size_t output_length; |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 373 | |
| 374 | // Note that the specifics of when the callbacks are called with regard |
[email protected] | d7db4f62 | 2012-06-04 18:20:56 | [diff] [blame] | 375 | // to how much data is pulled from the stream is not (currently) part |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 376 | // of the interface contract. If it becomes part of the contract, the |
| 377 | // tests below should get much more precise. |
| 378 | |
| 379 | // Confirm callback called when about 33% space available, and not |
| 380 | // at other transitions. |
| 381 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 382 | // Add data. |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 383 | int num_callbacks = 0; |
| 384 | byte_stream_input->RegisterCallback( |
| 385 | base::Bind(CountCallbacks, &num_callbacks)); |
| 386 | EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); |
| 387 | EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); |
| 388 | EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); |
| 389 | |
| 390 | // Allow bytes to transition (needed for message passing implementation), |
| 391 | // and get and validate the data. |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 392 | base::RunLoop().RunUntilIdle(); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 393 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 394 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 395 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 396 | |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 397 | // Grab data, triggering callback. Recorded on dispatch, but doesn't |
| 398 | // happen because it's caught by the mock. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 399 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 400 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 401 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 402 | |
| 403 | // Confirm that the callback passed to the mock does what we expect. |
| 404 | EXPECT_EQ(0, num_callbacks); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 405 | task_runner->RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 406 | EXPECT_EQ(1, num_callbacks); |
| 407 | |
| 408 | // Same drill with final buffer. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 409 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 410 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 411 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 412 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 413 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 414 | EXPECT_EQ(1, num_callbacks); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 415 | task_runner->RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 416 | // Should have updated the internal structures but not called the |
| 417 | // callback. |
| 418 | EXPECT_EQ(1, num_callbacks); |
| 419 | } |
| 420 | |
| 421 | // Confirm that racing a change to a sink callback with a post results |
| 422 | // in the new callback being called. |
| 423 | TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) { |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 424 | scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 425 | new base::TestSimpleTaskRunner()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 426 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 427 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 428 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 429 | CreateByteStream(message_loop_.task_runner(), task_runner, 10000, |
| 430 | &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 431 | |
| 432 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 433 | size_t output_length; |
| 434 | base::Closure intermediate_callback; |
| 435 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 436 | // Record initial state. |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 437 | int num_callbacks = 0; |
| 438 | byte_stream_output->RegisterCallback( |
| 439 | base::Bind(CountCallbacks, &num_callbacks)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 440 | |
| 441 | // Add data, and pass it across. |
| 442 | EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 443 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 444 | |
| 445 | // The task runner should have been hit, but the callback count |
| 446 | // isn't changed until we actually run the callback. |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 447 | EXPECT_EQ(0, num_callbacks); |
| 448 | |
| 449 | // If we change the callback now, the new one should be run |
| 450 | // (simulates race with post task). |
| 451 | int num_alt_callbacks = 0; |
| 452 | byte_stream_output->RegisterCallback( |
| 453 | base::Bind(CountCallbacks, &num_alt_callbacks)); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 454 | task_runner->RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 455 | EXPECT_EQ(0, num_callbacks); |
| 456 | EXPECT_EQ(1, num_alt_callbacks); |
| 457 | |
| 458 | // Final cleanup. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 459 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 460 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 461 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 462 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 463 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 464 | |
| 465 | } |
| 466 | |
| 467 | // Confirm that racing a change to a source callback with a post results |
| 468 | // in the new callback being called. |
| 469 | TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) { |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 470 | scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 471 | new base::TestSimpleTaskRunner()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 472 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 473 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 474 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 475 | CreateByteStream(task_runner, message_loop_.task_runner(), 10000, |
| 476 | &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 477 | |
| 478 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 479 | size_t output_length; |
| 480 | base::Closure intermediate_callback; |
| 481 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 482 | // Setup state for test. |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 483 | int num_callbacks = 0; |
| 484 | byte_stream_input->RegisterCallback( |
| 485 | base::Bind(CountCallbacks, &num_callbacks)); |
| 486 | EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); |
| 487 | EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); |
| 488 | EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 489 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 490 | |
| 491 | // Initial get should not trigger callback. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 492 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 493 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 494 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 495 | base::RunLoop().RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 496 | |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 497 | // Second get *should* trigger callback. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 498 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 499 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 500 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 501 | |
| 502 | // Which should do the right thing when it's run. |
| 503 | int num_alt_callbacks = 0; |
| 504 | byte_stream_input->RegisterCallback( |
| 505 | base::Bind(CountCallbacks, &num_alt_callbacks)); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 506 | task_runner->RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 507 | EXPECT_EQ(0, num_callbacks); |
| 508 | EXPECT_EQ(1, num_alt_callbacks); |
| 509 | |
| 510 | // Third get should also trigger callback. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 511 | EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 512 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 513 | EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 514 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 515 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 516 | } |
| 517 | |
| 518 | // Confirm that callback is called on zero data transfer but source |
| 519 | // complete. |
| 520 | TEST_F(ByteStreamTest, ByteStream_ZeroCallback) { |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 521 | scoped_refptr<base::TestSimpleTaskRunner> task_runner( |
| 522 | new base::TestSimpleTaskRunner()); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 523 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 524 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 525 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 526 | CreateByteStream(message_loop_.task_runner(), task_runner, 10000, |
| 527 | &byte_stream_input, &byte_stream_output); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 528 | |
| 529 | base::Closure intermediate_callback; |
| 530 | |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 531 | // Record initial state. |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 532 | int num_callbacks = 0; |
| 533 | byte_stream_output->RegisterCallback( |
| 534 | base::Bind(CountCallbacks, &num_callbacks)); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 535 | |
| 536 | // Immediately close the stream. |
[email protected] | 8d0c23e | 2013-08-02 11:02:30 | [diff] [blame] | 537 | byte_stream_input->Close(0); |
[email protected] | a8582b1 | 2012-12-19 22:18:29 | [diff] [blame] | 538 | task_runner->RunUntilIdle(); |
[email protected] | b0350786 | 2012-05-23 17:11:50 | [diff] [blame] | 539 | EXPECT_EQ(1, num_callbacks); |
| 540 | } |
| 541 | |
[email protected] | 6a14c19 | 2013-08-06 20:18:42 | [diff] [blame] | 542 | TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 543 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 544 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 545 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 546 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 6a14c19 | 2013-08-06 20:18:42 | [diff] [blame] | 547 | |
| 548 | byte_stream_input->Close(0); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 549 | base::RunLoop().RunUntilIdle(); |
[email protected] | 6a14c19 | 2013-08-06 20:18:42 | [diff] [blame] | 550 | |
| 551 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 552 | size_t output_length; |
| 553 | EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 554 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 555 | } |
| 556 | |
| 557 | TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 558 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 559 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 560 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 561 | 3 * 1024, &byte_stream_input, &byte_stream_output); |
[email protected] | 6a14c19 | 2013-08-06 20:18:42 | [diff] [blame] | 562 | |
| 563 | byte_stream_input->Flush(); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 564 | base::RunLoop().RunUntilIdle(); |
[email protected] | 6a14c19 | 2013-08-06 20:18:42 | [diff] [blame] | 565 | |
| 566 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 567 | size_t output_length; |
| 568 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 569 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 570 | |
| 571 | byte_stream_input->Close(0); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 572 | base::RunLoop().RunUntilIdle(); |
[email protected] | 6a14c19 | 2013-08-06 20:18:42 | [diff] [blame] | 573 | |
| 574 | EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 575 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 576 | } |
| 577 | |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 578 | TEST_F(ByteStreamTest, ByteStream_WriteOverflow) { |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 579 | std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 580 | std::unique_ptr<ByteStreamReader> byte_stream_output; |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 581 | CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 582 | std::numeric_limits<size_t>::max(), &byte_stream_input, |
| 583 | &byte_stream_output); |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 584 | |
| 585 | EXPECT_TRUE(Write(byte_stream_input.get(), 1)); |
| 586 | // 1 + size_t max -> Overflow. |
| 587 | scoped_refptr<net::IOBuffer> empty_io_buffer; |
| 588 | EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer, |
| 589 | std::numeric_limits<size_t>::max())); |
fdoray | e716a90 | 2016-07-05 16:05:49 | [diff] [blame^] | 590 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0751626 | 2013-08-22 07:43:24 | [diff] [blame] | 591 | |
| 592 | // The first write is below PostToPeer threshold. We shouldn't get anything |
| 593 | // from the output. |
| 594 | scoped_refptr<net::IOBuffer> output_io_buffer; |
| 595 | size_t output_length; |
| 596 | EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 597 | byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 598 | } |
| 599 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 600 | } // namespace content |