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