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