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