- remove mojo_view_manager_lib_unittests from chrome_tests.py
[chromium-blink-merge.git] / content / browser / byte_stream_unittest.cc
blobf814e2f5d083f4bd9fc1563634cfca58df352bcd
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.
5 #include "content/browser/byte_stream.h"
7 #include <deque>
8 #include <limits>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "net/base/io_buffer.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace content {
19 namespace {
21 void CountCallbacks(int* counter) {
22 ++*counter;
25 } // namespace
27 class ByteStreamTest : public testing::Test {
28 public:
29 ByteStreamTest();
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;
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.
49 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
50 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
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();
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_;
67 EXPECT_EQ(expected_ptr, bufferp);
68 if (expected_ptr != bufferp)
69 return false;
71 EXPECT_EQ(expected_length, buffer_size);
72 if (expected_length != buffer_size)
73 return false;
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;
85 return true;
88 protected:
89 base::MessageLoop message_loop_;
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_;
98 ByteStreamTest::ByteStreamTest()
99 : producing_seed_key_(0),
100 consuming_seed_key_(0) { }
102 // Confirm that filling and emptying the stream works properly, and that
103 // we get full triggers when we expect.
104 TEST_F(ByteStreamTest, ByteStream_PushBack) {
105 scoped_ptr<ByteStreamWriter> byte_stream_input;
106 scoped_ptr<ByteStreamReader> byte_stream_output;
107 CreateByteStream(
108 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
109 3 * 1024, &byte_stream_input, &byte_stream_output);
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
119 byte_stream_input->Close(0);
120 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
121 message_loop_.RunUntilIdle();
122 // Data already sent to reader is also counted in.
123 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
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;
129 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
130 byte_stream_output->Read(&output_io_buffer, &output_length));
131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
133 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
134 byte_stream_output->Read(&output_io_buffer, &output_length));
135 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
137 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
138 byte_stream_output->Read(&output_io_buffer, &output_length));
139 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
141 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
142 byte_stream_output->Read(&output_io_buffer, &output_length));
143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
145 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
146 byte_stream_output->Read(&output_io_buffer, &output_length));
147 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
149 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
150 byte_stream_output->Read(&output_io_buffer, &output_length));
152 message_loop_.RunUntilIdle();
153 // Reader now knows that all data is read out.
154 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
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);
166 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
167 message_loop_.RunUntilIdle();
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));
175 byte_stream_input->Flush();
176 message_loop_.RunUntilIdle();
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));
182 // Check that it's ok to Flush() an empty writer.
183 byte_stream_input->Flush();
184 message_loop_.RunUntilIdle();
186 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
187 byte_stream_output->Read(&output_io_buffer, &output_length));
189 byte_stream_input->Close(0);
190 message_loop_.RunUntilIdle();
192 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
193 byte_stream_output->Read(&output_io_buffer, &output_length));
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) {
200 scoped_ptr<ByteStreamWriter> byte_stream_input;
201 scoped_ptr<ByteStreamReader> byte_stream_output;
202 CreateByteStream(
203 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
204 9 * 1024, &byte_stream_input, &byte_stream_output);
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));
209 message_loop_.RunUntilIdle();
210 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
211 message_loop_.RunUntilIdle();
212 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
213 message_loop_.RunUntilIdle();
214 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
215 message_loop_.RunUntilIdle();
216 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
217 message_loop_.RunUntilIdle();
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;
223 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
224 byte_stream_output->Read(&output_io_buffer, &output_length));
225 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
227 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
228 byte_stream_output->Read(&output_io_buffer, &output_length));
229 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
231 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
232 byte_stream_output->Read(&output_io_buffer, &output_length));
233 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
235 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
236 byte_stream_output->Read(&output_io_buffer, &output_length));
237 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
239 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
240 byte_stream_output->Read(&output_io_buffer, &output_length));
241 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
243 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
244 byte_stream_output->Read(&output_io_buffer, &output_length));
247 // Confirm that a Close() notification transmits in-order
248 // with data on the stream.
249 TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
250 scoped_ptr<ByteStreamWriter> byte_stream_input;
251 scoped_ptr<ByteStreamReader> byte_stream_output;
253 scoped_refptr<net::IOBuffer> output_io_buffer;
254 size_t output_length;
256 // Empty stream, non-error case.
257 CreateByteStream(
258 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
259 3 * 1024, &byte_stream_input, &byte_stream_output);
260 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
261 byte_stream_output->Read(&output_io_buffer, &output_length));
262 byte_stream_input->Close(0);
263 message_loop_.RunUntilIdle();
264 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
265 byte_stream_output->Read(&output_io_buffer, &output_length));
266 EXPECT_EQ(0, byte_stream_output->GetStatus());
268 // Non-empty stream, non-error case.
269 CreateByteStream(
270 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
271 3 * 1024, &byte_stream_input, &byte_stream_output);
272 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
273 byte_stream_output->Read(&output_io_buffer, &output_length));
274 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
275 byte_stream_input->Close(0);
276 message_loop_.RunUntilIdle();
277 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
278 byte_stream_output->Read(&output_io_buffer, &output_length));
279 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
280 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
281 byte_stream_output->Read(&output_io_buffer, &output_length));
282 EXPECT_EQ(0, byte_stream_output->GetStatus());
284 const int kFakeErrorCode = 22;
286 // Empty stream, error case.
287 CreateByteStream(
288 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
289 3 * 1024, &byte_stream_input, &byte_stream_output);
290 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
291 byte_stream_output->Read(&output_io_buffer, &output_length));
292 byte_stream_input->Close(kFakeErrorCode);
293 message_loop_.RunUntilIdle();
294 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
295 byte_stream_output->Read(&output_io_buffer, &output_length));
296 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
298 // Non-empty stream, error case.
299 CreateByteStream(
300 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
301 3 * 1024, &byte_stream_input, &byte_stream_output);
302 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
303 byte_stream_output->Read(&output_io_buffer, &output_length));
304 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
305 byte_stream_input->Close(kFakeErrorCode);
306 message_loop_.RunUntilIdle();
307 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
308 byte_stream_output->Read(&output_io_buffer, &output_length));
309 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
310 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
311 byte_stream_output->Read(&output_io_buffer, &output_length));
312 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
315 // Confirm that callbacks on the sink side are triggered when they should be.
316 TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
317 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
318 new base::TestSimpleTaskRunner());
320 scoped_ptr<ByteStreamWriter> byte_stream_input;
321 scoped_ptr<ByteStreamReader> byte_stream_output;
322 CreateByteStream(
323 message_loop_.message_loop_proxy(), task_runner,
324 10000, &byte_stream_input, &byte_stream_output);
326 scoped_refptr<net::IOBuffer> output_io_buffer;
327 size_t output_length;
329 // Note that the specifics of when the callbacks are called with regard
330 // to how much data is pushed onto the stream is not (currently) part
331 // of the interface contract. If it becomes part of the contract, the
332 // tests below should get much more precise.
334 // Confirm callback called when you add more than 33% of the buffer.
336 // Setup callback
337 int num_callbacks = 0;
338 byte_stream_output->RegisterCallback(
339 base::Bind(CountCallbacks, &num_callbacks));
341 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
342 message_loop_.RunUntilIdle();
344 EXPECT_EQ(0, num_callbacks);
345 task_runner->RunUntilIdle();
346 EXPECT_EQ(1, num_callbacks);
348 // Check data and stream state.
349 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
350 byte_stream_output->Read(&output_io_buffer, &output_length));
351 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
352 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
353 byte_stream_output->Read(&output_io_buffer, &output_length));
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));
358 message_loop_.RunUntilIdle();
360 // This reflects an implementation artifact that data goes with callbacks,
361 // which should not be considered part of the interface guarantee.
362 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
363 byte_stream_output->Read(&output_io_buffer, &output_length));
366 // Confirm that callbacks on the source side are triggered when they should
367 // be.
368 TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
369 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
370 new base::TestSimpleTaskRunner());
372 scoped_ptr<ByteStreamWriter> byte_stream_input;
373 scoped_ptr<ByteStreamReader> byte_stream_output;
374 CreateByteStream(
375 task_runner, message_loop_.message_loop_proxy(),
376 10000, &byte_stream_input, &byte_stream_output);
378 scoped_refptr<net::IOBuffer> output_io_buffer;
379 size_t output_length;
381 // Note that the specifics of when the callbacks are called with regard
382 // to how much data is pulled from the stream is not (currently) part
383 // of the interface contract. If it becomes part of the contract, the
384 // tests below should get much more precise.
386 // Confirm callback called when about 33% space available, and not
387 // at other transitions.
389 // Add data.
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));
397 // Allow bytes to transition (needed for message passing implementation),
398 // and get and validate the data.
399 message_loop_.RunUntilIdle();
400 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
401 byte_stream_output->Read(&output_io_buffer, &output_length));
402 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
404 // Grab data, triggering callback. Recorded on dispatch, but doesn't
405 // happen because it's caught by the mock.
406 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
407 byte_stream_output->Read(&output_io_buffer, &output_length));
408 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
410 // Confirm that the callback passed to the mock does what we expect.
411 EXPECT_EQ(0, num_callbacks);
412 task_runner->RunUntilIdle();
413 EXPECT_EQ(1, num_callbacks);
415 // Same drill with final buffer.
416 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
417 byte_stream_output->Read(&output_io_buffer, &output_length));
418 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
419 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
420 byte_stream_output->Read(&output_io_buffer, &output_length));
421 EXPECT_EQ(1, num_callbacks);
422 task_runner->RunUntilIdle();
423 // Should have updated the internal structures but not called the
424 // callback.
425 EXPECT_EQ(1, num_callbacks);
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) {
431 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
432 new base::TestSimpleTaskRunner());
434 scoped_ptr<ByteStreamWriter> byte_stream_input;
435 scoped_ptr<ByteStreamReader> byte_stream_output;
436 CreateByteStream(
437 message_loop_.message_loop_proxy(), task_runner,
438 10000, &byte_stream_input, &byte_stream_output);
440 scoped_refptr<net::IOBuffer> output_io_buffer;
441 size_t output_length;
442 base::Closure intermediate_callback;
444 // Record initial state.
445 int num_callbacks = 0;
446 byte_stream_output->RegisterCallback(
447 base::Bind(CountCallbacks, &num_callbacks));
449 // Add data, and pass it across.
450 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
451 message_loop_.RunUntilIdle();
453 // The task runner should have been hit, but the callback count
454 // isn't changed until we actually run the callback.
455 EXPECT_EQ(0, num_callbacks);
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));
462 task_runner->RunUntilIdle();
463 EXPECT_EQ(0, num_callbacks);
464 EXPECT_EQ(1, num_alt_callbacks);
466 // Final cleanup.
467 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
468 byte_stream_output->Read(&output_io_buffer, &output_length));
469 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
470 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
471 byte_stream_output->Read(&output_io_buffer, &output_length));
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) {
478 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
479 new base::TestSimpleTaskRunner());
481 scoped_ptr<ByteStreamWriter> byte_stream_input;
482 scoped_ptr<ByteStreamReader> byte_stream_output;
483 CreateByteStream(
484 task_runner, message_loop_.message_loop_proxy(),
485 10000, &byte_stream_input, &byte_stream_output);
487 scoped_refptr<net::IOBuffer> output_io_buffer;
488 size_t output_length;
489 base::Closure intermediate_callback;
491 // Setup state for test.
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));
498 message_loop_.RunUntilIdle();
500 // Initial get should not trigger callback.
501 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
502 byte_stream_output->Read(&output_io_buffer, &output_length));
503 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
504 message_loop_.RunUntilIdle();
506 // Second get *should* trigger callback.
507 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
508 byte_stream_output->Read(&output_io_buffer, &output_length));
509 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
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));
515 task_runner->RunUntilIdle();
516 EXPECT_EQ(0, num_callbacks);
517 EXPECT_EQ(1, num_alt_callbacks);
519 // Third get should also trigger callback.
520 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
521 byte_stream_output->Read(&output_io_buffer, &output_length));
522 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
523 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
524 byte_stream_output->Read(&output_io_buffer, &output_length));
527 // Confirm that callback is called on zero data transfer but source
528 // complete.
529 TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
530 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
531 new base::TestSimpleTaskRunner());
533 scoped_ptr<ByteStreamWriter> byte_stream_input;
534 scoped_ptr<ByteStreamReader> byte_stream_output;
535 CreateByteStream(
536 message_loop_.message_loop_proxy(), task_runner,
537 10000, &byte_stream_input, &byte_stream_output);
539 base::Closure intermediate_callback;
541 // Record initial state.
542 int num_callbacks = 0;
543 byte_stream_output->RegisterCallback(
544 base::Bind(CountCallbacks, &num_callbacks));
546 // Immediately close the stream.
547 byte_stream_input->Close(0);
548 task_runner->RunUntilIdle();
549 EXPECT_EQ(1, num_callbacks);
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);
559 byte_stream_input->Close(0);
560 message_loop_.RunUntilIdle();
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));
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);
575 byte_stream_input->Flush();
576 message_loop_.RunUntilIdle();
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));
583 byte_stream_input->Close(0);
584 message_loop_.RunUntilIdle();
586 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
587 byte_stream_output->Read(&output_io_buffer, &output_length));
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);
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();
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));
613 } // namespace content