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