blob: f814e2f5d083f4bd9fc1563634cfca58df352bcd [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>
[email protected]07516262013-08-22 07:43:248#include <limits>
[email protected]b03507862012-05-23 17:11:509
10#include "base/bind.h"
11#include "base/callback.h"
[email protected]b03507862012-05-23 17:11:5012#include "base/memory/ref_counted.h"
[email protected]95f861e2013-07-18 02:07:1713#include "base/message_loop/message_loop.h"
[email protected]a8582b12012-12-19 22:18:2914#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5015#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5016#include "testing/gtest/include/gtest/gtest.h"
17
[email protected]35869622012-10-26 23:23:5518namespace content {
[email protected]b03507862012-05-23 17:11:5019namespace {
20
[email protected]b03507862012-05-23 17:11:5021void CountCallbacks(int* counter) {
22 ++*counter;
23}
24
25} // namespace
26
27class ByteStreamTest : public testing::Test {
28 public:
29 ByteStreamTest();
30
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;
43 }
44
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.
[email protected]35869622012-10-26 23:23:5549 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5050 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
51 }
52
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();
60
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_;
66
67 EXPECT_EQ(expected_ptr, bufferp);
68 if (expected_ptr != bufferp)
69 return false;
70
71 EXPECT_EQ(expected_length, buffer_size);
72 if (expected_length != buffer_size)
73 return false;
74
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;
83 }
84 }
85 return true;
86 }
87
88 protected:
[email protected]dd32b1272013-05-04 14:17:1189 base::MessageLoop message_loop_;
[email protected]b03507862012-05-23 17:11:5090
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_;
96};
97
98ByteStreamTest::ByteStreamTest()
99 : producing_seed_key_(0),
100 consuming_seed_key_(0) { }
101
[email protected]d7db4f622012-06-04 18:20:56102// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50103// we get full triggers when we expect.
104TEST_F(ByteStreamTest, ByteStream_PushBack) {
[email protected]35869622012-10-26 23:23:55105 scoped_ptr<ByteStreamWriter> byte_stream_input;
106 scoped_ptr<ByteStreamReader> byte_stream_output;
107 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50108 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
109 3 * 1024, &byte_stream_input, &byte_stream_output);
110
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
[email protected]8d0c23e2013-08-02 11:02:30119 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:24120 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]f319ace2012-11-10 19:07:29121 message_loop_.RunUntilIdle();
[email protected]07516262013-08-22 07:43:24122 // Data already sent to reader is also counted in.
123 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50124
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;
[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_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50146 byte_stream_output->Read(&output_io_buffer, &output_length));
147 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
148
[email protected]35869622012-10-26 23:23:55149 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50150 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24151
152 message_loop_.RunUntilIdle();
153 // Reader now knows that all data is read out.
154 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50155}
156
[email protected]566357e2013-07-31 03:59:36157// Confirm that Flush() method makes the writer to send written contents to
158// the reader.
159TEST_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);
165
166 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
167 message_loop_.RunUntilIdle();
168
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));
174
175 byte_stream_input->Flush();
176 message_loop_.RunUntilIdle();
177
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));
181
182 // Check that it's ok to Flush() an empty writer.
183 byte_stream_input->Flush();
184 message_loop_.RunUntilIdle();
185
186 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
187 byte_stream_output->Read(&output_io_buffer, &output_length));
188
[email protected]8d0c23e2013-08-02 11:02:30189 byte_stream_input->Close(0);
[email protected]566357e2013-07-31 03:59:36190 message_loop_.RunUntilIdle();
191
192 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
193 byte_stream_output->Read(&output_io_buffer, &output_length));
194}
195
[email protected]b03507862012-05-23 17:11:50196// 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
199TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
[email protected]35869622012-10-26 23:23:55200 scoped_ptr<ByteStreamWriter> byte_stream_input;
201 scoped_ptr<ByteStreamReader> byte_stream_output;
202 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50203 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
204 9 * 1024, &byte_stream_input, &byte_stream_output);
205
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));
[email protected]f319ace2012-11-10 19:07:29209 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50210 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29211 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50212 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29213 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50214 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]f319ace2012-11-10 19:07:29215 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50216 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
[email protected]f319ace2012-11-10 19:07:29217 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50218
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;
[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_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50236 byte_stream_output->Read(&output_io_buffer, &output_length));
237 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
238
[email protected]35869622012-10-26 23:23:55239 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50240 byte_stream_output->Read(&output_io_buffer, &output_length));
241 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
242
[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));
245}
246
247// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56248// with data on the stream.
[email protected]b03507862012-05-23 17:11:50249TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
[email protected]35869622012-10-26 23:23:55250 scoped_ptr<ByteStreamWriter> byte_stream_input;
251 scoped_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50252
253 scoped_refptr<net::IOBuffer> output_io_buffer;
254 size_t output_length;
255
256 // Empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55257 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50258 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
259 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55260 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50261 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30262 byte_stream_input->Close(0);
[email protected]f319ace2012-11-10 19:07:29263 message_loop_.RunUntilIdle();
[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]8d0c23e2013-08-02 11:02:30266 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50267
268 // Non-empty stream, non-error case.
[email protected]35869622012-10-26 23:23:55269 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50270 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
271 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55272 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50273 byte_stream_output->Read(&output_io_buffer, &output_length));
274 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30275 byte_stream_input->Close(0);
[email protected]f319ace2012-11-10 19:07:29276 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55277 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50278 byte_stream_output->Read(&output_io_buffer, &output_length));
279 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55280 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50281 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30282 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50283
[email protected]8d0c23e2013-08-02 11:02:30284 const int kFakeErrorCode = 22;
285
286 // Empty stream, error case.
[email protected]35869622012-10-26 23:23:55287 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50288 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
289 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55290 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50291 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30292 byte_stream_input->Close(kFakeErrorCode);
[email protected]f319ace2012-11-10 19:07:29293 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55294 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50295 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30296 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50297
[email protected]8d0c23e2013-08-02 11:02:30298 // Non-empty stream, error case.
[email protected]35869622012-10-26 23:23:55299 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50300 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
301 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55302 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50303 byte_stream_output->Read(&output_io_buffer, &output_length));
304 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30305 byte_stream_input->Close(kFakeErrorCode);
[email protected]f319ace2012-11-10 19:07:29306 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55307 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50308 byte_stream_output->Read(&output_io_buffer, &output_length));
309 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55310 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50311 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30312 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50313}
314
315// Confirm that callbacks on the sink side are triggered when they should be.
316TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29317 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
318 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50319
[email protected]35869622012-10-26 23:23:55320 scoped_ptr<ByteStreamWriter> byte_stream_input;
321 scoped_ptr<ByteStreamReader> byte_stream_output;
322 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50323 message_loop_.message_loop_proxy(), task_runner,
324 10000, &byte_stream_input, &byte_stream_output);
325
326 scoped_refptr<net::IOBuffer> output_io_buffer;
327 size_t output_length;
[email protected]b03507862012-05-23 17:11:50328
329 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56330 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50331 // of the interface contract. If it becomes part of the contract, the
332 // tests below should get much more precise.
333
334 // Confirm callback called when you add more than 33% of the buffer.
335
336 // Setup callback
337 int num_callbacks = 0;
338 byte_stream_output->RegisterCallback(
339 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50340
341 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29342 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50343
[email protected]b03507862012-05-23 17:11:50344 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29345 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50346 EXPECT_EQ(1, num_callbacks);
347
348 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55349 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50350 byte_stream_output->Read(&output_io_buffer, &output_length));
351 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55352 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50353 byte_stream_output->Read(&output_io_buffer, &output_length));
354
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));
[email protected]f319ace2012-11-10 19:07:29358 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50359
360 // This reflects an implementation artifact that data goes with callbacks,
361 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55362 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50363 byte_stream_output->Read(&output_io_buffer, &output_length));
364}
365
366// Confirm that callbacks on the source side are triggered when they should
367// be.
368TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29369 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
370 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50371
[email protected]35869622012-10-26 23:23:55372 scoped_ptr<ByteStreamWriter> byte_stream_input;
373 scoped_ptr<ByteStreamReader> byte_stream_output;
374 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50375 task_runner, message_loop_.message_loop_proxy(),
376 10000, &byte_stream_input, &byte_stream_output);
377
378 scoped_refptr<net::IOBuffer> output_io_buffer;
379 size_t output_length;
[email protected]b03507862012-05-23 17:11:50380
381 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56382 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50383 // of the interface contract. If it becomes part of the contract, the
384 // tests below should get much more precise.
385
386 // Confirm callback called when about 33% space available, and not
387 // at other transitions.
388
[email protected]a8582b12012-12-19 22:18:29389 // Add data.
[email protected]b03507862012-05-23 17:11:50390 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));
396
397 // Allow bytes to transition (needed for message passing implementation),
398 // and get and validate the data.
[email protected]f319ace2012-11-10 19:07:29399 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55400 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50401 byte_stream_output->Read(&output_io_buffer, &output_length));
402 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
403
[email protected]b03507862012-05-23 17:11:50404 // Grab data, triggering callback. Recorded on dispatch, but doesn't
405 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55406 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50407 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50408 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
409
410 // Confirm that the callback passed to the mock does what we expect.
411 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29412 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50413 EXPECT_EQ(1, num_callbacks);
414
415 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55416 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50417 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50418 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55419 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50420 byte_stream_output->Read(&output_io_buffer, &output_length));
421 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29422 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50423 // Should have updated the internal structures but not called the
424 // callback.
425 EXPECT_EQ(1, num_callbacks);
426}
427
428// Confirm that racing a change to a sink callback with a post results
429// in the new callback being called.
430TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29431 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
432 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50433
[email protected]35869622012-10-26 23:23:55434 scoped_ptr<ByteStreamWriter> byte_stream_input;
435 scoped_ptr<ByteStreamReader> byte_stream_output;
436 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50437 message_loop_.message_loop_proxy(), task_runner,
438 10000, &byte_stream_input, &byte_stream_output);
439
440 scoped_refptr<net::IOBuffer> output_io_buffer;
441 size_t output_length;
442 base::Closure intermediate_callback;
443
[email protected]a8582b12012-12-19 22:18:29444 // Record initial state.
[email protected]b03507862012-05-23 17:11:50445 int num_callbacks = 0;
446 byte_stream_output->RegisterCallback(
447 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50448
449 // Add data, and pass it across.
450 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29451 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50452
453 // The task runner should have been hit, but the callback count
454 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50455 EXPECT_EQ(0, num_callbacks);
456
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));
[email protected]a8582b12012-12-19 22:18:29462 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50463 EXPECT_EQ(0, num_callbacks);
464 EXPECT_EQ(1, num_alt_callbacks);
465
466 // Final cleanup.
[email protected]35869622012-10-26 23:23:55467 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50468 byte_stream_output->Read(&output_io_buffer, &output_length));
469 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55470 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50471 byte_stream_output->Read(&output_io_buffer, &output_length));
472
473}
474
475// Confirm that racing a change to a source callback with a post results
476// in the new callback being called.
477TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29478 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
479 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50480
[email protected]35869622012-10-26 23:23:55481 scoped_ptr<ByteStreamWriter> byte_stream_input;
482 scoped_ptr<ByteStreamReader> byte_stream_output;
483 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50484 task_runner, message_loop_.message_loop_proxy(),
485 10000, &byte_stream_input, &byte_stream_output);
486
487 scoped_refptr<net::IOBuffer> output_io_buffer;
488 size_t output_length;
489 base::Closure intermediate_callback;
490
[email protected]a8582b12012-12-19 22:18:29491 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50492 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));
[email protected]f319ace2012-11-10 19:07:29498 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50499
500 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55501 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50502 byte_stream_output->Read(&output_io_buffer, &output_length));
503 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]f319ace2012-11-10 19:07:29504 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50505
[email protected]b03507862012-05-23 17:11:50506 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55507 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50508 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50509 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
510
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));
[email protected]a8582b12012-12-19 22:18:29515 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50516 EXPECT_EQ(0, num_callbacks);
517 EXPECT_EQ(1, num_alt_callbacks);
518
519 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55520 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50521 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50522 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55523 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50524 byte_stream_output->Read(&output_io_buffer, &output_length));
525}
526
527// Confirm that callback is called on zero data transfer but source
528// complete.
529TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29530 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
531 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50532
[email protected]35869622012-10-26 23:23:55533 scoped_ptr<ByteStreamWriter> byte_stream_input;
534 scoped_ptr<ByteStreamReader> byte_stream_output;
535 CreateByteStream(
[email protected]b03507862012-05-23 17:11:50536 message_loop_.message_loop_proxy(), task_runner,
537 10000, &byte_stream_input, &byte_stream_output);
538
539 base::Closure intermediate_callback;
540
[email protected]a8582b12012-12-19 22:18:29541 // Record initial state.
[email protected]b03507862012-05-23 17:11:50542 int num_callbacks = 0;
543 byte_stream_output->RegisterCallback(
544 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50545
546 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30547 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29548 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50549 EXPECT_EQ(1, num_callbacks);
550}
551
[email protected]6a14c192013-08-06 20:18:42552TEST_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);
558
559 byte_stream_input->Close(0);
560 message_loop_.RunUntilIdle();
561
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));
566}
567
568TEST_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);
574
575 byte_stream_input->Flush();
576 message_loop_.RunUntilIdle();
577
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));
582
583 byte_stream_input->Close(0);
584 message_loop_.RunUntilIdle();
585
586 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
587 byte_stream_output->Read(&output_io_buffer, &output_length));
588}
589
[email protected]07516262013-08-22 07:43:24590TEST_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);
597
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();
604
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));
611}
612
[email protected]35869622012-10-26 23:23:55613} // namespace content