blob: 04c5ae3753306b721a80dd7575309955b3156001 [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
[email protected]f8e92b5d2013-03-21 18:35:465#include "content/browser/byte_stream.h"
[email protected]b03507862012-05-23 17:11:506
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"
[email protected]95f861e2013-07-18 02:07:1712#include "base/message_loop/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:
[email protected]dd32b1272013-05-04 14:17:1188 base::MessageLoop message_loop_;
[email protected]b03507862012-05-23 17:11:5089
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]8d0c23e2013-08-02 11:02:30118 byte_stream_input->Close(0);
[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
[email protected]566357e2013-07-31 03:59:36149// Confirm that Flush() method makes the writer to send written contents to
150// the reader.
151TEST_F(ByteStreamTest, ByteStream_Flush) {
152 scoped_ptr<ByteStreamWriter> byte_stream_input;
153 scoped_ptr<ByteStreamReader> byte_stream_output;
154 CreateByteStream(
155 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
156 1024, &byte_stream_input, &byte_stream_output);
157
158 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
159 message_loop_.RunUntilIdle();
160
161 scoped_refptr<net::IOBuffer> output_io_buffer;
162 size_t output_length = 0;
163 // Check that data is not sent to the reader yet.
164 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
165 byte_stream_output->Read(&output_io_buffer, &output_length));
166
167 byte_stream_input->Flush();
168 message_loop_.RunUntilIdle();
169
170 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
171 byte_stream_output->Read(&output_io_buffer, &output_length));
172 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
173
174 // Check that it's ok to Flush() an empty writer.
175 byte_stream_input->Flush();
176 message_loop_.RunUntilIdle();
177
178 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
179 byte_stream_output->Read(&output_io_buffer, &output_length));
180
[email protected]8d0c23e2013-08-02 11:02:30181 byte_stream_input->Close(0);
[email protected]566357e2013-07-31 03:59:36182 message_loop_.RunUntilIdle();
183
184 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
185 byte_stream_output->Read(&output_io_buffer, &output_length));
186}
187
[email protected]b03507862012-05-23 17:11:50188// Same as above, only use knowledge of the internals to confirm
189// that we're getting pushback even when data's split across the two
190// objects
191TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
[email protected]35869622012-10-26 23:23:55192 scoped_ptr<ByteStreamWriter> byte_stream_input;
193 scoped_ptr<ByteStreamReader> byte_stream_output;
194 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50195 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
196 9 * 1024, &byte_stream_input, &byte_stream_output);
197
198 // Push a series of IO buffers on; test pushback happening and
199 // that it's advisory.
200 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29201 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50202 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29203 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50204 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29205 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50206 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29207 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50208 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
[email protected]f319ace2012-11-10 19:07:29209 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50210
211 // Pull the IO buffers out; do we get the same buffers and do they
212 // have the same contents?
213 scoped_refptr<net::IOBuffer> output_io_buffer;
214 size_t output_length;
[email protected]35869622012-10-26 23:23:55215 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50216 byte_stream_output->Read(&output_io_buffer, &output_length));
217 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
218
[email protected]35869622012-10-26 23:23:55219 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50220 byte_stream_output->Read(&output_io_buffer, &output_length));
221 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
222
[email protected]35869622012-10-26 23:23:55223 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50224 byte_stream_output->Read(&output_io_buffer, &output_length));
225 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
226
[email protected]35869622012-10-26 23:23:55227 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50228 byte_stream_output->Read(&output_io_buffer, &output_length));
229 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
230
[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));
234
[email protected]35869622012-10-26 23:23:55235 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50236 byte_stream_output->Read(&output_io_buffer, &output_length));
237}
238
239// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56240// with data on the stream.
[email protected]b03507862012-05-23 17:11:50241TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
[email protected]35869622012-10-26 23:23:55242 scoped_ptr<ByteStreamWriter> byte_stream_input;
243 scoped_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50244
245 scoped_refptr<net::IOBuffer> output_io_buffer;
246 size_t output_length;
247
248 // Empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55249 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50250 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
251 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55252 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50253 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30254 byte_stream_input->Close(0);
[email protected]f319ace2012-11-10 19:07:29255 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55256 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50257 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30258 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50259
260 // Non-empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55261 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50262 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
263 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55264 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50265 byte_stream_output->Read(&output_io_buffer, &output_length));
266 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30267 byte_stream_input->Close(0);
[email protected]f319ace2012-11-10 19:07:29268 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55269 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50270 byte_stream_output->Read(&output_io_buffer, &output_length));
271 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55272 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50273 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30274 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50275
[email protected]8d0c23e2013-08-02 11:02:30276 const int kFakeErrorCode = 22;
277
278 // Empty stream, error case.
[email protected]35869622012-10-26 23:23:55279 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50280 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
281 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55282 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50283 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30284 byte_stream_input->Close(kFakeErrorCode);
[email protected]f319ace2012-11-10 19:07:29285 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55286 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50287 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30288 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50289
[email protected]8d0c23e2013-08-02 11:02:30290 // Non-empty stream, error case.
[email protected]35869622012-10-26 23:23:55291 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50292 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
293 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55294 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50295 byte_stream_output->Read(&output_io_buffer, &output_length));
296 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30297 byte_stream_input->Close(kFakeErrorCode);
[email protected]f319ace2012-11-10 19:07:29298 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55299 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50300 byte_stream_output->Read(&output_io_buffer, &output_length));
301 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55302 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50303 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30304 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50305}
306
307// Confirm that callbacks on the sink side are triggered when they should be.
308TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29309 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
310 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50311
[email protected]35869622012-10-26 23:23:55312 scoped_ptr<ByteStreamWriter> byte_stream_input;
313 scoped_ptr<ByteStreamReader> byte_stream_output;
314 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50315 message_loop_.message_loop_proxy(), task_runner,
316 10000, &byte_stream_input, &byte_stream_output);
317
318 scoped_refptr<net::IOBuffer> output_io_buffer;
319 size_t output_length;
[email protected]b03507862012-05-23 17:11:50320
321 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56322 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50323 // of the interface contract. If it becomes part of the contract, the
324 // tests below should get much more precise.
325
326 // Confirm callback called when you add more than 33% of the buffer.
327
328 // Setup callback
329 int num_callbacks = 0;
330 byte_stream_output->RegisterCallback(
331 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50332
333 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29334 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50335
[email protected]b03507862012-05-23 17:11:50336 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29337 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50338 EXPECT_EQ(1, num_callbacks);
339
340 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55341 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50342 byte_stream_output->Read(&output_io_buffer, &output_length));
343 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55344 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50345 byte_stream_output->Read(&output_io_buffer, &output_length));
346
347 // Confirm callback *isn't* called at less than 33% (by lack of
348 // unexpected call on task runner).
349 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
[email protected]f319ace2012-11-10 19:07:29350 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50351
352 // This reflects an implementation artifact that data goes with callbacks,
353 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55354 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50355 byte_stream_output->Read(&output_io_buffer, &output_length));
356}
357
358// Confirm that callbacks on the source side are triggered when they should
359// be.
360TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29361 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
362 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50363
[email protected]35869622012-10-26 23:23:55364 scoped_ptr<ByteStreamWriter> byte_stream_input;
365 scoped_ptr<ByteStreamReader> byte_stream_output;
366 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50367 task_runner, message_loop_.message_loop_proxy(),
368 10000, &byte_stream_input, &byte_stream_output);
369
370 scoped_refptr<net::IOBuffer> output_io_buffer;
371 size_t output_length;
[email protected]b03507862012-05-23 17:11:50372
373 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56374 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50375 // of the interface contract. If it becomes part of the contract, the
376 // tests below should get much more precise.
377
378 // Confirm callback called when about 33% space available, and not
379 // at other transitions.
380
[email protected]a8582b12012-12-19 22:18:29381 // Add data.
[email protected]b03507862012-05-23 17:11:50382 int num_callbacks = 0;
383 byte_stream_input->RegisterCallback(
384 base::Bind(CountCallbacks, &num_callbacks));
385 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
386 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
387 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
388
389 // Allow bytes to transition (needed for message passing implementation),
390 // and get and validate the data.
[email protected]f319ace2012-11-10 19:07:29391 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55392 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50393 byte_stream_output->Read(&output_io_buffer, &output_length));
394 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
395
[email protected]b03507862012-05-23 17:11:50396 // Grab data, triggering callback. Recorded on dispatch, but doesn't
397 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55398 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50399 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50400 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
401
402 // Confirm that the callback passed to the mock does what we expect.
403 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29404 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50405 EXPECT_EQ(1, num_callbacks);
406
407 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55408 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50409 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50410 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55411 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50412 byte_stream_output->Read(&output_io_buffer, &output_length));
413 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29414 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50415 // Should have updated the internal structures but not called the
416 // callback.
417 EXPECT_EQ(1, num_callbacks);
418}
419
420// Confirm that racing a change to a sink callback with a post results
421// in the new callback being called.
422TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29423 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
424 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50425
[email protected]35869622012-10-26 23:23:55426 scoped_ptr<ByteStreamWriter> byte_stream_input;
427 scoped_ptr<ByteStreamReader> byte_stream_output;
428 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50429 message_loop_.message_loop_proxy(), task_runner,
430 10000, &byte_stream_input, &byte_stream_output);
431
432 scoped_refptr<net::IOBuffer> output_io_buffer;
433 size_t output_length;
434 base::Closure intermediate_callback;
435
[email protected]a8582b12012-12-19 22:18:29436 // Record initial state.
[email protected]b03507862012-05-23 17:11:50437 int num_callbacks = 0;
438 byte_stream_output->RegisterCallback(
439 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50440
441 // Add data, and pass it across.
442 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29443 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50444
445 // The task runner should have been hit, but the callback count
446 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50447 EXPECT_EQ(0, num_callbacks);
448
449 // If we change the callback now, the new one should be run
450 // (simulates race with post task).
451 int num_alt_callbacks = 0;
452 byte_stream_output->RegisterCallback(
453 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29454 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50455 EXPECT_EQ(0, num_callbacks);
456 EXPECT_EQ(1, num_alt_callbacks);
457
458 // Final cleanup.
[email protected]35869622012-10-26 23:23:55459 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50460 byte_stream_output->Read(&output_io_buffer, &output_length));
461 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55462 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50463 byte_stream_output->Read(&output_io_buffer, &output_length));
464
465}
466
467// Confirm that racing a change to a source callback with a post results
468// in the new callback being called.
469TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29470 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
471 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50472
[email protected]35869622012-10-26 23:23:55473 scoped_ptr<ByteStreamWriter> byte_stream_input;
474 scoped_ptr<ByteStreamReader> byte_stream_output;
475 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50476 task_runner, message_loop_.message_loop_proxy(),
477 10000, &byte_stream_input, &byte_stream_output);
478
479 scoped_refptr<net::IOBuffer> output_io_buffer;
480 size_t output_length;
481 base::Closure intermediate_callback;
482
[email protected]a8582b12012-12-19 22:18:29483 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50484 int num_callbacks = 0;
485 byte_stream_input->RegisterCallback(
486 base::Bind(CountCallbacks, &num_callbacks));
487 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
488 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
489 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
[email protected]f319ace2012-11-10 19:07:29490 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50491
492 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55493 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50494 byte_stream_output->Read(&output_io_buffer, &output_length));
495 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]f319ace2012-11-10 19:07:29496 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50497
[email protected]b03507862012-05-23 17:11:50498 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55499 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50500 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50501 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
502
503 // Which should do the right thing when it's run.
504 int num_alt_callbacks = 0;
505 byte_stream_input->RegisterCallback(
506 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29507 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50508 EXPECT_EQ(0, num_callbacks);
509 EXPECT_EQ(1, num_alt_callbacks);
510
511 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55512 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50513 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50514 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55515 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50516 byte_stream_output->Read(&output_io_buffer, &output_length));
517}
518
519// Confirm that callback is called on zero data transfer but source
520// complete.
521TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29522 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
523 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50524
[email protected]35869622012-10-26 23:23:55525 scoped_ptr<ByteStreamWriter> byte_stream_input;
526 scoped_ptr<ByteStreamReader> byte_stream_output;
527 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50528 message_loop_.message_loop_proxy(), task_runner,
529 10000, &byte_stream_input, &byte_stream_output);
530
531 base::Closure intermediate_callback;
532
[email protected]a8582b12012-12-19 22:18:29533 // Record initial state.
[email protected]b03507862012-05-23 17:11:50534 int num_callbacks = 0;
535 byte_stream_output->RegisterCallback(
536 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50537
538 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30539 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29540 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50541 EXPECT_EQ(1, num_callbacks);
542}
543
[email protected]6a14c192013-08-06 20:18:42544TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
545 scoped_ptr<ByteStreamWriter> byte_stream_input;
546 scoped_ptr<ByteStreamReader> byte_stream_output;
547 CreateByteStream(
548 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
549 3 * 1024, &byte_stream_input, &byte_stream_output);
550
551 byte_stream_input->Close(0);
552 message_loop_.RunUntilIdle();
553
554 scoped_refptr<net::IOBuffer> output_io_buffer;
555 size_t output_length;
556 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
557 byte_stream_output->Read(&output_io_buffer, &output_length));
558}
559
560TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
561 scoped_ptr<ByteStreamWriter> byte_stream_input;
562 scoped_ptr<ByteStreamReader> byte_stream_output;
563 CreateByteStream(
564 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
565 3 * 1024, &byte_stream_input, &byte_stream_output);
566
567 byte_stream_input->Flush();
568 message_loop_.RunUntilIdle();
569
570 scoped_refptr<net::IOBuffer> output_io_buffer;
571 size_t output_length;
572 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
573 byte_stream_output->Read(&output_io_buffer, &output_length));
574
575 byte_stream_input->Close(0);
576 message_loop_.RunUntilIdle();
577
578 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
579 byte_stream_output->Read(&output_io_buffer, &output_length));
580}
581
[email protected]35869622012-10-26 23:23:55582} // namespace content