| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/byte_stream.h" | 5 #include "content/browser/byte_stream.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <deque> | 9 #include <deque> |
| 10 #include <limits> | 10 #include <limits> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/run_loop.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 void CountCallbacks(int* counter) { | 23 void CountCallbacks(int* counter) { |
| 23 ++*counter; | 24 ++*counter; |
| 24 } | 25 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // Push a series of IO buffers on; test pushback happening and | 112 // Push a series of IO buffers on; test pushback happening and |
| 112 // that it's advisory. | 113 // that it's advisory. |
| 113 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 114 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)); |
| 115 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 116 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(), 1)); |
| 117 EXPECT_FALSE(Write(byte_stream_input.get(), 1024)); | 118 EXPECT_FALSE(Write(byte_stream_input.get(), 1024)); |
| 118 // Flush | 119 // Flush |
| 119 byte_stream_input->Close(0); | 120 byte_stream_input->Close(0); |
| 120 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); | 121 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); |
| 121 message_loop_.RunUntilIdle(); | 122 base::RunLoop().RunUntilIdle(); |
| 122 // Data already sent to reader is also counted in. | 123 // Data already sent to reader is also counted in. |
| 123 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); | 124 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes()); |
| 124 | 125 |
| 125 // Pull the IO buffers out; do we get the same buffers and do they | 126 // Pull the IO buffers out; do we get the same buffers and do they |
| 126 // have the same contents? | 127 // have the same contents? |
| 127 scoped_refptr<net::IOBuffer> output_io_buffer; | 128 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 128 size_t output_length; | 129 size_t output_length; |
| 129 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 130 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 130 byte_stream_output->Read(&output_io_buffer, &output_length)); | 131 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 132 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 142 byte_stream_output->Read(&output_io_buffer, &output_length)); | 143 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 144 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 144 | 145 |
| 145 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 146 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 146 byte_stream_output->Read(&output_io_buffer, &output_length)); | 147 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 147 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 148 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 148 | 149 |
| 149 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, | 150 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 150 byte_stream_output->Read(&output_io_buffer, &output_length)); | 151 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 151 | 152 |
| 152 message_loop_.RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 153 // Reader now knows that all data is read out. | 154 // Reader now knows that all data is read out. |
| 154 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes()); | 155 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes()); |
| 155 } | 156 } |
| 156 | 157 |
| 157 // Confirm that Flush() method makes the writer to send written contents to | 158 // Confirm that Flush() method makes the writer to send written contents to |
| 158 // the reader. | 159 // the reader. |
| 159 TEST_F(ByteStreamTest, ByteStream_Flush) { | 160 TEST_F(ByteStreamTest, ByteStream_Flush) { |
| 160 std::unique_ptr<ByteStreamWriter> byte_stream_input; | 161 std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 161 std::unique_ptr<ByteStreamReader> byte_stream_output; | 162 std::unique_ptr<ByteStreamReader> byte_stream_output; |
| 162 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 163 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 163 1024, &byte_stream_input, &byte_stream_output); | 164 1024, &byte_stream_input, &byte_stream_output); |
| 164 | 165 |
| 165 EXPECT_TRUE(Write(byte_stream_input.get(), 1)); | 166 EXPECT_TRUE(Write(byte_stream_input.get(), 1)); |
| 166 message_loop_.RunUntilIdle(); | 167 base::RunLoop().RunUntilIdle(); |
| 167 | 168 |
| 168 scoped_refptr<net::IOBuffer> output_io_buffer; | 169 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 169 size_t output_length = 0; | 170 size_t output_length = 0; |
| 170 // Check that data is not sent to the reader yet. | 171 // Check that data is not sent to the reader yet. |
| 171 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 172 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 172 byte_stream_output->Read(&output_io_buffer, &output_length)); | 173 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 173 | 174 |
| 174 byte_stream_input->Flush(); | 175 byte_stream_input->Flush(); |
| 175 message_loop_.RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 176 | 177 |
| 177 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 178 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 178 byte_stream_output->Read(&output_io_buffer, &output_length)); | 179 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 179 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 180 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 180 | 181 |
| 181 // Check that it's ok to Flush() an empty writer. | 182 // Check that it's ok to Flush() an empty writer. |
| 182 byte_stream_input->Flush(); | 183 byte_stream_input->Flush(); |
| 183 message_loop_.RunUntilIdle(); | 184 base::RunLoop().RunUntilIdle(); |
| 184 | 185 |
| 185 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 186 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 186 byte_stream_output->Read(&output_io_buffer, &output_length)); | 187 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 187 | 188 |
| 188 byte_stream_input->Close(0); | 189 byte_stream_input->Close(0); |
| 189 message_loop_.RunUntilIdle(); | 190 base::RunLoop().RunUntilIdle(); |
| 190 | 191 |
| 191 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, | 192 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 192 byte_stream_output->Read(&output_io_buffer, &output_length)); | 193 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 193 } | 194 } |
| 194 | 195 |
| 195 // Same as above, only use knowledge of the internals to confirm | 196 // 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 // that we're getting pushback even when data's split across the two |
| 197 // objects | 198 // objects |
| 198 TEST_F(ByteStreamTest, ByteStream_PushBackSplit) { | 199 TEST_F(ByteStreamTest, ByteStream_PushBackSplit) { |
| 199 std::unique_ptr<ByteStreamWriter> byte_stream_input; | 200 std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 200 std::unique_ptr<ByteStreamReader> byte_stream_output; | 201 std::unique_ptr<ByteStreamReader> byte_stream_output; |
| 201 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 202 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 202 9 * 1024, &byte_stream_input, &byte_stream_output); | 203 9 * 1024, &byte_stream_input, &byte_stream_output); |
| 203 | 204 |
| 204 // Push a series of IO buffers on; test pushback happening and | 205 // Push a series of IO buffers on; test pushback happening and |
| 205 // that it's advisory. | 206 // that it's advisory. |
| 206 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 207 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 207 message_loop_.RunUntilIdle(); | 208 base::RunLoop().RunUntilIdle(); |
| 208 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 209 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 209 message_loop_.RunUntilIdle(); | 210 base::RunLoop().RunUntilIdle(); |
| 210 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 211 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 211 message_loop_.RunUntilIdle(); | 212 base::RunLoop().RunUntilIdle(); |
| 212 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 213 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 213 message_loop_.RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
| 214 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024)); | 215 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024)); |
| 215 message_loop_.RunUntilIdle(); | 216 base::RunLoop().RunUntilIdle(); |
| 216 | 217 |
| 217 // Pull the IO buffers out; do we get the same buffers and do they | 218 // Pull the IO buffers out; do we get the same buffers and do they |
| 218 // have the same contents? | 219 // have the same contents? |
| 219 scoped_refptr<net::IOBuffer> output_io_buffer; | 220 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 220 size_t output_length; | 221 size_t output_length; |
| 221 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 222 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 222 byte_stream_output->Read(&output_io_buffer, &output_length)); | 223 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 223 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 224 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 224 | 225 |
| 225 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 226 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 250 | 251 |
| 251 scoped_refptr<net::IOBuffer> output_io_buffer; | 252 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 252 size_t output_length; | 253 size_t output_length; |
| 253 | 254 |
| 254 // Empty stream, non-error case. | 255 // Empty stream, non-error case. |
| 255 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 256 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 256 3 * 1024, &byte_stream_input, &byte_stream_output); | 257 3 * 1024, &byte_stream_input, &byte_stream_output); |
| 257 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 258 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 258 byte_stream_output->Read(&output_io_buffer, &output_length)); | 259 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 259 byte_stream_input->Close(0); | 260 byte_stream_input->Close(0); |
| 260 message_loop_.RunUntilIdle(); | 261 base::RunLoop().RunUntilIdle(); |
| 261 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, | 262 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 262 byte_stream_output->Read(&output_io_buffer, &output_length)); | 263 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 263 EXPECT_EQ(0, byte_stream_output->GetStatus()); | 264 EXPECT_EQ(0, byte_stream_output->GetStatus()); |
| 264 | 265 |
| 265 // Non-empty stream, non-error case. | 266 // Non-empty stream, non-error case. |
| 266 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 267 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 267 3 * 1024, &byte_stream_input, &byte_stream_output); | 268 3 * 1024, &byte_stream_input, &byte_stream_output); |
| 268 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 269 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 269 byte_stream_output->Read(&output_io_buffer, &output_length)); | 270 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 270 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 271 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 271 byte_stream_input->Close(0); | 272 byte_stream_input->Close(0); |
| 272 message_loop_.RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
| 273 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 274 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 274 byte_stream_output->Read(&output_io_buffer, &output_length)); | 275 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 275 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 276 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 276 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, | 277 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 277 byte_stream_output->Read(&output_io_buffer, &output_length)); | 278 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 278 EXPECT_EQ(0, byte_stream_output->GetStatus()); | 279 EXPECT_EQ(0, byte_stream_output->GetStatus()); |
| 279 | 280 |
| 280 const int kFakeErrorCode = 22; | 281 const int kFakeErrorCode = 22; |
| 281 | 282 |
| 282 // Empty stream, error case. | 283 // Empty stream, error case. |
| 283 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 284 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 284 3 * 1024, &byte_stream_input, &byte_stream_output); | 285 3 * 1024, &byte_stream_input, &byte_stream_output); |
| 285 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 286 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 286 byte_stream_output->Read(&output_io_buffer, &output_length)); | 287 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 287 byte_stream_input->Close(kFakeErrorCode); | 288 byte_stream_input->Close(kFakeErrorCode); |
| 288 message_loop_.RunUntilIdle(); | 289 base::RunLoop().RunUntilIdle(); |
| 289 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, | 290 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 290 byte_stream_output->Read(&output_io_buffer, &output_length)); | 291 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 291 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus()); | 292 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus()); |
| 292 | 293 |
| 293 // Non-empty stream, error case. | 294 // Non-empty stream, error case. |
| 294 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 295 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 295 3 * 1024, &byte_stream_input, &byte_stream_output); | 296 3 * 1024, &byte_stream_input, &byte_stream_output); |
| 296 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 297 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 297 byte_stream_output->Read(&output_io_buffer, &output_length)); | 298 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 298 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); | 299 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); |
| 299 byte_stream_input->Close(kFakeErrorCode); | 300 byte_stream_input->Close(kFakeErrorCode); |
| 300 message_loop_.RunUntilIdle(); | 301 base::RunLoop().RunUntilIdle(); |
| 301 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 302 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 302 byte_stream_output->Read(&output_io_buffer, &output_length)); | 303 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 303 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 304 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 304 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, | 305 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 305 byte_stream_output->Read(&output_io_buffer, &output_length)); | 306 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 306 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus()); | 307 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus()); |
| 307 } | 308 } |
| 308 | 309 |
| 309 // Confirm that callbacks on the sink side are triggered when they should be. | 310 // Confirm that callbacks on the sink side are triggered when they should be. |
| 310 TEST_F(ByteStreamTest, ByteStream_SinkCallback) { | 311 TEST_F(ByteStreamTest, ByteStream_SinkCallback) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 325 // tests below should get much more precise. | 326 // tests below should get much more precise. |
| 326 | 327 |
| 327 // Confirm callback called when you add more than 33% of the buffer. | 328 // Confirm callback called when you add more than 33% of the buffer. |
| 328 | 329 |
| 329 // Setup callback | 330 // Setup callback |
| 330 int num_callbacks = 0; | 331 int num_callbacks = 0; |
| 331 byte_stream_output->RegisterCallback( | 332 byte_stream_output->RegisterCallback( |
| 332 base::Bind(CountCallbacks, &num_callbacks)); | 333 base::Bind(CountCallbacks, &num_callbacks)); |
| 333 | 334 |
| 334 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); | 335 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); |
| 335 message_loop_.RunUntilIdle(); | 336 base::RunLoop().RunUntilIdle(); |
| 336 | 337 |
| 337 EXPECT_EQ(0, num_callbacks); | 338 EXPECT_EQ(0, num_callbacks); |
| 338 task_runner->RunUntilIdle(); | 339 task_runner->RunUntilIdle(); |
| 339 EXPECT_EQ(1, num_callbacks); | 340 EXPECT_EQ(1, num_callbacks); |
| 340 | 341 |
| 341 // Check data and stream state. | 342 // Check data and stream state. |
| 342 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 343 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 343 byte_stream_output->Read(&output_io_buffer, &output_length)); | 344 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 344 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 345 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 345 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 346 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 346 byte_stream_output->Read(&output_io_buffer, &output_length)); | 347 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 347 | 348 |
| 348 // Confirm callback *isn't* called at less than 33% (by lack of | 349 // Confirm callback *isn't* called at less than 33% (by lack of |
| 349 // unexpected call on task runner). | 350 // unexpected call on task runner). |
| 350 EXPECT_TRUE(Write(byte_stream_input.get(), 3000)); | 351 EXPECT_TRUE(Write(byte_stream_input.get(), 3000)); |
| 351 message_loop_.RunUntilIdle(); | 352 base::RunLoop().RunUntilIdle(); |
| 352 | 353 |
| 353 // This reflects an implementation artifact that data goes with callbacks, | 354 // This reflects an implementation artifact that data goes with callbacks, |
| 354 // which should not be considered part of the interface guarantee. | 355 // which should not be considered part of the interface guarantee. |
| 355 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 356 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 356 byte_stream_output->Read(&output_io_buffer, &output_length)); | 357 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 357 } | 358 } |
| 358 | 359 |
| 359 // Confirm that callbacks on the source side are triggered when they should | 360 // Confirm that callbacks on the source side are triggered when they should |
| 360 // be. | 361 // be. |
| 361 TEST_F(ByteStreamTest, ByteStream_SourceCallback) { | 362 TEST_F(ByteStreamTest, ByteStream_SourceCallback) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 381 // Add data. | 382 // Add data. |
| 382 int num_callbacks = 0; | 383 int num_callbacks = 0; |
| 383 byte_stream_input->RegisterCallback( | 384 byte_stream_input->RegisterCallback( |
| 384 base::Bind(CountCallbacks, &num_callbacks)); | 385 base::Bind(CountCallbacks, &num_callbacks)); |
| 385 EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); | 386 EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); |
| 386 EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); | 387 EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); |
| 387 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); | 388 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); |
| 388 | 389 |
| 389 // Allow bytes to transition (needed for message passing implementation), | 390 // Allow bytes to transition (needed for message passing implementation), |
| 390 // and get and validate the data. | 391 // and get and validate the data. |
| 391 message_loop_.RunUntilIdle(); | 392 base::RunLoop().RunUntilIdle(); |
| 392 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 393 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 393 byte_stream_output->Read(&output_io_buffer, &output_length)); | 394 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 394 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 395 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 395 | 396 |
| 396 // Grab data, triggering callback. Recorded on dispatch, but doesn't | 397 // Grab data, triggering callback. Recorded on dispatch, but doesn't |
| 397 // happen because it's caught by the mock. | 398 // happen because it's caught by the mock. |
| 398 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 399 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 399 byte_stream_output->Read(&output_io_buffer, &output_length)); | 400 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 400 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 401 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 401 | 402 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 432 size_t output_length; | 433 size_t output_length; |
| 433 base::Closure intermediate_callback; | 434 base::Closure intermediate_callback; |
| 434 | 435 |
| 435 // Record initial state. | 436 // Record initial state. |
| 436 int num_callbacks = 0; | 437 int num_callbacks = 0; |
| 437 byte_stream_output->RegisterCallback( | 438 byte_stream_output->RegisterCallback( |
| 438 base::Bind(CountCallbacks, &num_callbacks)); | 439 base::Bind(CountCallbacks, &num_callbacks)); |
| 439 | 440 |
| 440 // Add data, and pass it across. | 441 // Add data, and pass it across. |
| 441 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); | 442 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); |
| 442 message_loop_.RunUntilIdle(); | 443 base::RunLoop().RunUntilIdle(); |
| 443 | 444 |
| 444 // The task runner should have been hit, but the callback count | 445 // The task runner should have been hit, but the callback count |
| 445 // isn't changed until we actually run the callback. | 446 // isn't changed until we actually run the callback. |
| 446 EXPECT_EQ(0, num_callbacks); | 447 EXPECT_EQ(0, num_callbacks); |
| 447 | 448 |
| 448 // If we change the callback now, the new one should be run | 449 // If we change the callback now, the new one should be run |
| 449 // (simulates race with post task). | 450 // (simulates race with post task). |
| 450 int num_alt_callbacks = 0; | 451 int num_alt_callbacks = 0; |
| 451 byte_stream_output->RegisterCallback( | 452 byte_stream_output->RegisterCallback( |
| 452 base::Bind(CountCallbacks, &num_alt_callbacks)); | 453 base::Bind(CountCallbacks, &num_alt_callbacks)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 478 size_t output_length; | 479 size_t output_length; |
| 479 base::Closure intermediate_callback; | 480 base::Closure intermediate_callback; |
| 480 | 481 |
| 481 // Setup state for test. | 482 // Setup state for test. |
| 482 int num_callbacks = 0; | 483 int num_callbacks = 0; |
| 483 byte_stream_input->RegisterCallback( | 484 byte_stream_input->RegisterCallback( |
| 484 base::Bind(CountCallbacks, &num_callbacks)); | 485 base::Bind(CountCallbacks, &num_callbacks)); |
| 485 EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); | 486 EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); |
| 486 EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); | 487 EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); |
| 487 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); | 488 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); |
| 488 message_loop_.RunUntilIdle(); | 489 base::RunLoop().RunUntilIdle(); |
| 489 | 490 |
| 490 // Initial get should not trigger callback. | 491 // Initial get should not trigger callback. |
| 491 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 492 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 492 byte_stream_output->Read(&output_io_buffer, &output_length)); | 493 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 493 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 494 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 494 message_loop_.RunUntilIdle(); | 495 base::RunLoop().RunUntilIdle(); |
| 495 | 496 |
| 496 // Second get *should* trigger callback. | 497 // Second get *should* trigger callback. |
| 497 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, | 498 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, |
| 498 byte_stream_output->Read(&output_io_buffer, &output_length)); | 499 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 499 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); | 500 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); |
| 500 | 501 |
| 501 // Which should do the right thing when it's run. | 502 // Which should do the right thing when it's run. |
| 502 int num_alt_callbacks = 0; | 503 int num_alt_callbacks = 0; |
| 503 byte_stream_input->RegisterCallback( | 504 byte_stream_input->RegisterCallback( |
| 504 base::Bind(CountCallbacks, &num_alt_callbacks)); | 505 base::Bind(CountCallbacks, &num_alt_callbacks)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 EXPECT_EQ(1, num_callbacks); | 539 EXPECT_EQ(1, num_callbacks); |
| 539 } | 540 } |
| 540 | 541 |
| 541 TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) { | 542 TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) { |
| 542 std::unique_ptr<ByteStreamWriter> byte_stream_input; | 543 std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 543 std::unique_ptr<ByteStreamReader> byte_stream_output; | 544 std::unique_ptr<ByteStreamReader> byte_stream_output; |
| 544 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 545 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 545 3 * 1024, &byte_stream_input, &byte_stream_output); | 546 3 * 1024, &byte_stream_input, &byte_stream_output); |
| 546 | 547 |
| 547 byte_stream_input->Close(0); | 548 byte_stream_input->Close(0); |
| 548 message_loop_.RunUntilIdle(); | 549 base::RunLoop().RunUntilIdle(); |
| 549 | 550 |
| 550 scoped_refptr<net::IOBuffer> output_io_buffer; | 551 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 551 size_t output_length; | 552 size_t output_length; |
| 552 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, | 553 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 553 byte_stream_output->Read(&output_io_buffer, &output_length)); | 554 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 554 } | 555 } |
| 555 | 556 |
| 556 TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) { | 557 TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) { |
| 557 std::unique_ptr<ByteStreamWriter> byte_stream_input; | 558 std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 558 std::unique_ptr<ByteStreamReader> byte_stream_output; | 559 std::unique_ptr<ByteStreamReader> byte_stream_output; |
| 559 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 560 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 560 3 * 1024, &byte_stream_input, &byte_stream_output); | 561 3 * 1024, &byte_stream_input, &byte_stream_output); |
| 561 | 562 |
| 562 byte_stream_input->Flush(); | 563 byte_stream_input->Flush(); |
| 563 message_loop_.RunUntilIdle(); | 564 base::RunLoop().RunUntilIdle(); |
| 564 | 565 |
| 565 scoped_refptr<net::IOBuffer> output_io_buffer; | 566 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 566 size_t output_length; | 567 size_t output_length; |
| 567 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 568 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 568 byte_stream_output->Read(&output_io_buffer, &output_length)); | 569 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 569 | 570 |
| 570 byte_stream_input->Close(0); | 571 byte_stream_input->Close(0); |
| 571 message_loop_.RunUntilIdle(); | 572 base::RunLoop().RunUntilIdle(); |
| 572 | 573 |
| 573 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, | 574 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, |
| 574 byte_stream_output->Read(&output_io_buffer, &output_length)); | 575 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 575 } | 576 } |
| 576 | 577 |
| 577 TEST_F(ByteStreamTest, ByteStream_WriteOverflow) { | 578 TEST_F(ByteStreamTest, ByteStream_WriteOverflow) { |
| 578 std::unique_ptr<ByteStreamWriter> byte_stream_input; | 579 std::unique_ptr<ByteStreamWriter> byte_stream_input; |
| 579 std::unique_ptr<ByteStreamReader> byte_stream_output; | 580 std::unique_ptr<ByteStreamReader> byte_stream_output; |
| 580 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), | 581 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(), |
| 581 std::numeric_limits<size_t>::max(), &byte_stream_input, | 582 std::numeric_limits<size_t>::max(), &byte_stream_input, |
| 582 &byte_stream_output); | 583 &byte_stream_output); |
| 583 | 584 |
| 584 EXPECT_TRUE(Write(byte_stream_input.get(), 1)); | 585 EXPECT_TRUE(Write(byte_stream_input.get(), 1)); |
| 585 // 1 + size_t max -> Overflow. | 586 // 1 + size_t max -> Overflow. |
| 586 scoped_refptr<net::IOBuffer> empty_io_buffer; | 587 scoped_refptr<net::IOBuffer> empty_io_buffer; |
| 587 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer, | 588 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer, |
| 588 std::numeric_limits<size_t>::max())); | 589 std::numeric_limits<size_t>::max())); |
| 589 message_loop_.RunUntilIdle(); | 590 base::RunLoop().RunUntilIdle(); |
| 590 | 591 |
| 591 // The first write is below PostToPeer threshold. We shouldn't get anything | 592 // The first write is below PostToPeer threshold. We shouldn't get anything |
| 592 // from the output. | 593 // from the output. |
| 593 scoped_refptr<net::IOBuffer> output_io_buffer; | 594 scoped_refptr<net::IOBuffer> output_io_buffer; |
| 594 size_t output_length; | 595 size_t output_length; |
| 595 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, | 596 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, |
| 596 byte_stream_output->Read(&output_io_buffer, &output_length)); | 597 byte_stream_output->Read(&output_io_buffer, &output_length)); |
| 597 } | 598 } |
| 598 | 599 |
| 599 } // namespace content | 600 } // namespace content |
| OLD | NEW |