blob: 5422a8bdc5faef61c5cf4e57ffa90f845d555029 [file] [log] [blame]
[email protected]b03507862012-05-23 17:11:501// 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]b03507862012-05-23 17:11:5011#include "base/memory/ref_counted.h"
12#include "base/message_loop.h"
[email protected]a8582b12012-12-19 22:18:2913#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5014#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5015#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]35869622012-10-26 23:23:5517namespace content {
[email protected]b03507862012-05-23 17:11:5018namespace {
19
[email protected]b03507862012-05-23 17:11:5020void CountCallbacks(int* counter) {
21 ++*counter;
22}
23
24} // namespace
25
26class 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]35869622012-10-26 23:23:5548 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5049 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
97ByteStreamTest::ByteStreamTest()
98 : producing_seed_key_(0),
99 consuming_seed_key_(0) { }
100
[email protected]d7db4f622012-06-04 18:20:56101// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50102// we get full triggers when we expect.
103TEST_F(ByteStreamTest, ByteStream_PushBack) {
[email protected]35869622012-10-26 23:23:55104 scoped_ptr<ByteStreamWriter> byte_stream_input;
105 scoped_ptr<ByteStreamReader> byte_stream_output;
106 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50107 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]35869622012-10-26 23:23:55118 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]f319ace2012-11-10 19:07:29119 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50120
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]35869622012-10-26 23:23:55125 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50126 byte_stream_output->Read(&output_io_buffer, &output_length));
127 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
128
[email protected]35869622012-10-26 23:23:55129 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50130 byte_stream_output->Read(&output_io_buffer, &output_length));
131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
132
[email protected]35869622012-10-26 23:23:55133 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50134 byte_stream_output->Read(&output_io_buffer, &output_length));
135 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
136
[email protected]35869622012-10-26 23:23:55137 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50138 byte_stream_output->Read(&output_io_buffer, &output_length));
139 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
140
[email protected]35869622012-10-26 23:23:55141 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50142 byte_stream_output->Read(&output_io_buffer, &output_length));
143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
144
[email protected]35869622012-10-26 23:23:55145 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50146 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
152TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
[email protected]35869622012-10-26 23:23:55153 scoped_ptr<ByteStreamWriter> byte_stream_input;
154 scoped_ptr<ByteStreamReader> byte_stream_output;
155 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50156 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]f319ace2012-11-10 19:07:29162 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50163 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29164 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50165 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29166 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50167 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29168 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50169 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
[email protected]f319ace2012-11-10 19:07:29170 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50171
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]35869622012-10-26 23:23:55176 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50177 byte_stream_output->Read(&output_io_buffer, &output_length));
178 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
179
[email protected]35869622012-10-26 23:23:55180 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50181 byte_stream_output->Read(&output_io_buffer, &output_length));
182 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
183
[email protected]35869622012-10-26 23:23:55184 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50185 byte_stream_output->Read(&output_io_buffer, &output_length));
186 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
187
[email protected]35869622012-10-26 23:23:55188 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50189 byte_stream_output->Read(&output_io_buffer, &output_length));
190 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
191
[email protected]35869622012-10-26 23:23:55192 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50193 byte_stream_output->Read(&output_io_buffer, &output_length));
194 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
195
[email protected]35869622012-10-26 23:23:55196 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50197 byte_stream_output->Read(&output_io_buffer, &output_length));
198}
199
200// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56201// with data on the stream.
[email protected]b03507862012-05-23 17:11:50202TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
[email protected]35869622012-10-26 23:23:55203 scoped_ptr<ByteStreamWriter> byte_stream_input;
204 scoped_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50205
206 scoped_refptr<net::IOBuffer> output_io_buffer;
207 size_t output_length;
208
209 // Empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55210 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50211 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
212 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55213 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50214 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]35869622012-10-26 23:23:55215 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]f319ace2012-11-10 19:07:29216 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55217 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50218 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]35869622012-10-26 23:23:55219 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
[email protected]b03507862012-05-23 17:11:50220 byte_stream_output->GetStatus());
221
222 // Non-empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55223 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50224 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
225 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55226 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50227 byte_stream_output->Read(&output_io_buffer, &output_length));
228 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]35869622012-10-26 23:23:55229 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]f319ace2012-11-10 19:07:29230 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55231 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50232 byte_stream_output->Read(&output_io_buffer, &output_length));
233 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55234 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50235 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]35869622012-10-26 23:23:55236 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE,
[email protected]b03507862012-05-23 17:11:50237 byte_stream_output->GetStatus());
238
239 // Empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55240 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50241 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
242 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55243 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50244 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]35869622012-10-26 23:23:55245 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED);
[email protected]f319ace2012-11-10 19:07:29246 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55247 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50248 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]35869622012-10-26 23:23:55249 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
[email protected]b03507862012-05-23 17:11:50250 byte_stream_output->GetStatus());
251
252 // Non-empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55253 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50254 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
255 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55256 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50257 byte_stream_output->Read(&output_io_buffer, &output_length));
258 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]35869622012-10-26 23:23:55259 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED);
[email protected]f319ace2012-11-10 19:07:29260 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55261 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50262 byte_stream_output->Read(&output_io_buffer, &output_length));
263 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55264 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50265 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]35869622012-10-26 23:23:55266 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED,
[email protected]b03507862012-05-23 17:11:50267 byte_stream_output->GetStatus());
268}
269
270// Confirm that callbacks on the sink side are triggered when they should be.
271TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29272 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
273 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50274
[email protected]35869622012-10-26 23:23:55275 scoped_ptr<ByteStreamWriter> byte_stream_input;
276 scoped_ptr<ByteStreamReader> byte_stream_output;
277 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50278 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]b03507862012-05-23 17:11:50283
284 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56285 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50286 // 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]b03507862012-05-23 17:11:50295
296 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29297 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50298
[email protected]b03507862012-05-23 17:11:50299 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29300 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50301 EXPECT_EQ(1, num_callbacks);
302
303 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55304 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50305 byte_stream_output->Read(&output_io_buffer, &output_length));
306 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55307 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50308 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]f319ace2012-11-10 19:07:29313 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50314
315 // This reflects an implementation artifact that data goes with callbacks,
316 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55317 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50318 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.
323TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29324 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
325 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50326
[email protected]35869622012-10-26 23:23:55327 scoped_ptr<ByteStreamWriter> byte_stream_input;
328 scoped_ptr<ByteStreamReader> byte_stream_output;
329 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50330 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]b03507862012-05-23 17:11:50335
336 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56337 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50338 // 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]a8582b12012-12-19 22:18:29344 // Add data.
[email protected]b03507862012-05-23 17:11:50345 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]f319ace2012-11-10 19:07:29354 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55355 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50356 byte_stream_output->Read(&output_io_buffer, &output_length));
357 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
358
[email protected]b03507862012-05-23 17:11:50359 // Grab data, triggering callback. Recorded on dispatch, but doesn't
360 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55361 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50362 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50363 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]a8582b12012-12-19 22:18:29367 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50368 EXPECT_EQ(1, num_callbacks);
369
370 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55371 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50372 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50373 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55374 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50375 byte_stream_output->Read(&output_io_buffer, &output_length));
376 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29377 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50378 // 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.
385TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29386 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
387 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50388
[email protected]35869622012-10-26 23:23:55389 scoped_ptr<ByteStreamWriter> byte_stream_input;
390 scoped_ptr<ByteStreamReader> byte_stream_output;
391 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50392 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]a8582b12012-12-19 22:18:29399 // Record initial state.
[email protected]b03507862012-05-23 17:11:50400 int num_callbacks = 0;
401 byte_stream_output->RegisterCallback(
402 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50403
404 // Add data, and pass it across.
405 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29406 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50407
408 // The task runner should have been hit, but the callback count
409 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50410 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]a8582b12012-12-19 22:18:29417 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50418 EXPECT_EQ(0, num_callbacks);
419 EXPECT_EQ(1, num_alt_callbacks);
420
421 // Final cleanup.
[email protected]35869622012-10-26 23:23:55422 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50423 byte_stream_output->Read(&output_io_buffer, &output_length));
424 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55425 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50426 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.
432TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29433 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
434 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50435
[email protected]35869622012-10-26 23:23:55436 scoped_ptr<ByteStreamWriter> byte_stream_input;
437 scoped_ptr<ByteStreamReader> byte_stream_output;
438 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50439 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]a8582b12012-12-19 22:18:29446 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50447 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]f319ace2012-11-10 19:07:29453 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50454
455 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55456 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50457 byte_stream_output->Read(&output_io_buffer, &output_length));
458 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]f319ace2012-11-10 19:07:29459 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50460
[email protected]b03507862012-05-23 17:11:50461 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55462 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50463 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50464 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]a8582b12012-12-19 22:18:29470 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50471 EXPECT_EQ(0, num_callbacks);
472 EXPECT_EQ(1, num_alt_callbacks);
473
474 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55475 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50476 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50477 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55478 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50479 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.
484TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29485 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
486 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50487
[email protected]35869622012-10-26 23:23:55488 scoped_ptr<ByteStreamWriter> byte_stream_input;
489 scoped_ptr<ByteStreamReader> byte_stream_output;
490 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50491 message_loop_.message_loop_proxy(), task_runner,
492 10000, &byte_stream_input, &byte_stream_output);
493
494 base::Closure intermediate_callback;
495
[email protected]a8582b12012-12-19 22:18:29496 // Record initial state.
[email protected]b03507862012-05-23 17:11:50497 int num_callbacks = 0;
498 byte_stream_output->RegisterCallback(
499 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50500
501 // Immediately close the stream.
[email protected]35869622012-10-26 23:23:55502 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE);
[email protected]a8582b12012-12-19 22:18:29503 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50504 EXPECT_EQ(1, num_callbacks);
505}
506
[email protected]35869622012-10-26 23:23:55507} // namespace content