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