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