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