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