blob: 4c348b72df3a6ae288674aa526532f4edfe71b82 [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
avib7348942015-12-25 20:57:107#include <stddef.h>
8
[email protected]b03507862012-05-23 17:11:509#include <deque>
[email protected]07516262013-08-22 07:43:2410#include <limits>
[email protected]b03507862012-05-23 17:11:5011
12#include "base/bind.h"
13#include "base/callback.h"
[email protected]b03507862012-05-23 17:11:5014#include "base/memory/ref_counted.h"
[email protected]a8582b12012-12-19 22:18:2915#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5016#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5017#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]35869622012-10-26 23:23:5519namespace content {
[email protected]b03507862012-05-23 17:11:5020namespace {
21
[email protected]b03507862012-05-23 17:11:5022void CountCallbacks(int* counter) {
23 ++*counter;
24}
25
26} // namespace
27
28class ByteStreamTest : public testing::Test {
29 public:
30 ByteStreamTest();
31
32 // Create a new IO buffer of the given |buffer_size|. Details of the
33 // contents of the created buffer will be kept, and can be validated
34 // by ValidateIOBuffer.
35 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
36 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size));
37 char *bufferp = buffer->data();
38 for (size_t i = 0; i < buffer_size; i++)
39 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
40 pointer_queue_.push_back(bufferp);
41 length_queue_.push_back(buffer_size);
42 ++producing_seed_key_;
43 return buffer;
44 }
45
46 // Create an IOBuffer of the appropriate size and add it to the
47 // ByteStream, returning the result of the ByteStream::Write.
48 // Separate function to avoid duplication of buffer_size in test
49 // calls.
[email protected]35869622012-10-26 23:23:5550 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5051 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
52 }
53
54 // Validate that we have the IOBuffer we expect. This routine must be
55 // called on buffers that were allocated from NewIOBuffer, and in the
56 // order that they were allocated. Calls to NewIOBuffer &&
57 // ValidateIOBuffer may be interleaved.
58 bool ValidateIOBuffer(
59 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
60 char *bufferp = buffer->data();
61
62 char *expected_ptr = pointer_queue_.front();
63 size_t expected_length = length_queue_.front();
64 pointer_queue_.pop_front();
65 length_queue_.pop_front();
66 ++consuming_seed_key_;
67
68 EXPECT_EQ(expected_ptr, bufferp);
69 if (expected_ptr != bufferp)
70 return false;
71
72 EXPECT_EQ(expected_length, buffer_size);
73 if (expected_length != buffer_size)
74 return false;
75
76 for (size_t i = 0; i < buffer_size; i++) {
77 // Already incremented, so subtract one from the key.
78 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
79 % (1 << sizeof(char))),
80 bufferp[i]);
81 if (static_cast<int>((i + consuming_seed_key_ - 1) %
82 (1 << sizeof(char))) != bufferp[i]) {
83 return false;
84 }
85 }
86 return true;
87 }
88
89 protected:
[email protected]dd32b1272013-05-04 14:17:1190 base::MessageLoop message_loop_;
[email protected]b03507862012-05-23 17:11:5091
92 private:
93 int producing_seed_key_;
94 int consuming_seed_key_;
95 std::deque<char*> pointer_queue_;
96 std::deque<size_t> length_queue_;
97};
98
99ByteStreamTest::ByteStreamTest()
100 : producing_seed_key_(0),
101 consuming_seed_key_(0) { }
102
[email protected]d7db4f622012-06-04 18:20:56103// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50104// we get full triggers when we expect.
105TEST_F(ByteStreamTest, ByteStream_PushBack) {
[email protected]35869622012-10-26 23:23:55106 scoped_ptr<ByteStreamWriter> byte_stream_input;
107 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07108 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
109 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50110
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;
skyostil95082a62015-06-05 19:53:07162 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
163 1024, &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36164
165 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
166 message_loop_.RunUntilIdle();
167
168 scoped_refptr<net::IOBuffer> output_io_buffer;
169 size_t output_length = 0;
170 // Check that data is not sent to the reader yet.
171 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
172 byte_stream_output->Read(&output_io_buffer, &output_length));
173
174 byte_stream_input->Flush();
175 message_loop_.RunUntilIdle();
176
177 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
178 byte_stream_output->Read(&output_io_buffer, &output_length));
179 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
180
181 // Check that it's ok to Flush() an empty writer.
182 byte_stream_input->Flush();
183 message_loop_.RunUntilIdle();
184
185 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
186 byte_stream_output->Read(&output_io_buffer, &output_length));
187
[email protected]8d0c23e2013-08-02 11:02:30188 byte_stream_input->Close(0);
[email protected]566357e2013-07-31 03:59:36189 message_loop_.RunUntilIdle();
190
191 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
192 byte_stream_output->Read(&output_io_buffer, &output_length));
193}
194
[email protected]b03507862012-05-23 17:11:50195// Same as above, only use knowledge of the internals to confirm
196// that we're getting pushback even when data's split across the two
197// objects
198TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
[email protected]35869622012-10-26 23:23:55199 scoped_ptr<ByteStreamWriter> byte_stream_input;
200 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07201 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
202 9 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50203
204 // Push a series of IO buffers on; test pushback happening and
205 // that it's advisory.
206 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_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_FALSE(Write(byte_stream_input.get(), 6 * 1024));
[email protected]f319ace2012-11-10 19:07:29215 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50216
217 // Pull the IO buffers out; do we get the same buffers and do they
218 // have the same contents?
219 scoped_refptr<net::IOBuffer> output_io_buffer;
220 size_t output_length;
[email protected]35869622012-10-26 23:23:55221 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50222 byte_stream_output->Read(&output_io_buffer, &output_length));
223 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
224
[email protected]35869622012-10-26 23:23:55225 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50226 byte_stream_output->Read(&output_io_buffer, &output_length));
227 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
228
[email protected]35869622012-10-26 23:23:55229 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50230 byte_stream_output->Read(&output_io_buffer, &output_length));
231 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
232
[email protected]35869622012-10-26 23:23:55233 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50234 byte_stream_output->Read(&output_io_buffer, &output_length));
235 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
236
[email protected]35869622012-10-26 23:23:55237 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50238 byte_stream_output->Read(&output_io_buffer, &output_length));
239 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
240
[email protected]35869622012-10-26 23:23:55241 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50242 byte_stream_output->Read(&output_io_buffer, &output_length));
243}
244
245// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56246// with data on the stream.
[email protected]b03507862012-05-23 17:11:50247TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
[email protected]35869622012-10-26 23:23:55248 scoped_ptr<ByteStreamWriter> byte_stream_input;
249 scoped_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50250
251 scoped_refptr<net::IOBuffer> output_io_buffer;
252 size_t output_length;
253
254 // Empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07255 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
256 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55257 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50258 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30259 byte_stream_input->Close(0);
[email protected]f319ace2012-11-10 19:07:29260 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55261 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50262 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30263 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50264
265 // Non-empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07266 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
267 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55268 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50269 byte_stream_output->Read(&output_io_buffer, &output_length));
270 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30271 byte_stream_input->Close(0);
[email protected]f319ace2012-11-10 19:07:29272 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55273 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50274 byte_stream_output->Read(&output_io_buffer, &output_length));
275 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55276 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50277 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30278 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50279
[email protected]8d0c23e2013-08-02 11:02:30280 const int kFakeErrorCode = 22;
281
282 // Empty stream, error case.
skyostil95082a62015-06-05 19:53:07283 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
284 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55285 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50286 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30287 byte_stream_input->Close(kFakeErrorCode);
[email protected]f319ace2012-11-10 19:07:29288 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55289 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50290 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30291 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50292
[email protected]8d0c23e2013-08-02 11:02:30293 // Non-empty stream, error case.
skyostil95082a62015-06-05 19:53:07294 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
295 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55296 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50297 byte_stream_output->Read(&output_io_buffer, &output_length));
298 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30299 byte_stream_input->Close(kFakeErrorCode);
[email protected]f319ace2012-11-10 19:07:29300 message_loop_.RunUntilIdle();
[email protected]35869622012-10-26 23:23:55301 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50302 byte_stream_output->Read(&output_io_buffer, &output_length));
303 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55304 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50305 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30306 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50307}
308
309// Confirm that callbacks on the sink side are triggered when they should be.
310TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29311 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
312 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50313
[email protected]35869622012-10-26 23:23:55314 scoped_ptr<ByteStreamWriter> byte_stream_input;
315 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07316 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
317 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50318
319 scoped_refptr<net::IOBuffer> output_io_buffer;
320 size_t output_length;
[email protected]b03507862012-05-23 17:11:50321
322 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56323 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50324 // of the interface contract. If it becomes part of the contract, the
325 // tests below should get much more precise.
326
327 // Confirm callback called when you add more than 33% of the buffer.
328
329 // Setup callback
330 int num_callbacks = 0;
331 byte_stream_output->RegisterCallback(
332 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50333
334 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29335 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50336
[email protected]b03507862012-05-23 17:11:50337 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29338 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50339 EXPECT_EQ(1, num_callbacks);
340
341 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55342 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50343 byte_stream_output->Read(&output_io_buffer, &output_length));
344 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55345 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50346 byte_stream_output->Read(&output_io_buffer, &output_length));
347
348 // Confirm callback *isn't* called at less than 33% (by lack of
349 // unexpected call on task runner).
350 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
[email protected]f319ace2012-11-10 19:07:29351 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50352
353 // This reflects an implementation artifact that data goes with callbacks,
354 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55355 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50356 byte_stream_output->Read(&output_io_buffer, &output_length));
357}
358
359// Confirm that callbacks on the source side are triggered when they should
360// be.
361TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29362 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
363 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50364
[email protected]35869622012-10-26 23:23:55365 scoped_ptr<ByteStreamWriter> byte_stream_input;
366 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07367 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
368 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50369
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;
skyostil95082a62015-06-05 19:53:07428 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
429 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50430
431 scoped_refptr<net::IOBuffer> output_io_buffer;
432 size_t output_length;
433 base::Closure intermediate_callback;
434
[email protected]a8582b12012-12-19 22:18:29435 // Record initial state.
[email protected]b03507862012-05-23 17:11:50436 int num_callbacks = 0;
437 byte_stream_output->RegisterCallback(
438 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50439
440 // Add data, and pass it across.
441 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
[email protected]f319ace2012-11-10 19:07:29442 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50443
444 // The task runner should have been hit, but the callback count
445 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50446 EXPECT_EQ(0, num_callbacks);
447
448 // If we change the callback now, the new one should be run
449 // (simulates race with post task).
450 int num_alt_callbacks = 0;
451 byte_stream_output->RegisterCallback(
452 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29453 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50454 EXPECT_EQ(0, num_callbacks);
455 EXPECT_EQ(1, num_alt_callbacks);
456
457 // Final cleanup.
[email protected]35869622012-10-26 23:23:55458 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50459 byte_stream_output->Read(&output_io_buffer, &output_length));
460 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55461 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50462 byte_stream_output->Read(&output_io_buffer, &output_length));
463
464}
465
466// Confirm that racing a change to a source callback with a post results
467// in the new callback being called.
468TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29469 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
470 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50471
[email protected]35869622012-10-26 23:23:55472 scoped_ptr<ByteStreamWriter> byte_stream_input;
473 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07474 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
475 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50476
477 scoped_refptr<net::IOBuffer> output_io_buffer;
478 size_t output_length;
479 base::Closure intermediate_callback;
480
[email protected]a8582b12012-12-19 22:18:29481 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50482 int num_callbacks = 0;
483 byte_stream_input->RegisterCallback(
484 base::Bind(CountCallbacks, &num_callbacks));
485 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
486 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
487 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
[email protected]f319ace2012-11-10 19:07:29488 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50489
490 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55491 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50492 byte_stream_output->Read(&output_io_buffer, &output_length));
493 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]f319ace2012-11-10 19:07:29494 message_loop_.RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50495
[email protected]b03507862012-05-23 17:11:50496 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55497 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50498 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50499 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
500
501 // Which should do the right thing when it's run.
502 int num_alt_callbacks = 0;
503 byte_stream_input->RegisterCallback(
504 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29505 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50506 EXPECT_EQ(0, num_callbacks);
507 EXPECT_EQ(1, num_alt_callbacks);
508
509 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55510 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50511 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50512 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55513 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50514 byte_stream_output->Read(&output_io_buffer, &output_length));
515}
516
517// Confirm that callback is called on zero data transfer but source
518// complete.
519TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29520 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
521 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50522
[email protected]35869622012-10-26 23:23:55523 scoped_ptr<ByteStreamWriter> byte_stream_input;
524 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07525 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
526 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50527
528 base::Closure intermediate_callback;
529
[email protected]a8582b12012-12-19 22:18:29530 // Record initial state.
[email protected]b03507862012-05-23 17:11:50531 int num_callbacks = 0;
532 byte_stream_output->RegisterCallback(
533 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50534
535 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30536 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29537 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50538 EXPECT_EQ(1, num_callbacks);
539}
540
[email protected]6a14c192013-08-06 20:18:42541TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
542 scoped_ptr<ByteStreamWriter> byte_stream_input;
543 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07544 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
545 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42546
547 byte_stream_input->Close(0);
548 message_loop_.RunUntilIdle();
549
550 scoped_refptr<net::IOBuffer> output_io_buffer;
551 size_t output_length;
552 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
553 byte_stream_output->Read(&output_io_buffer, &output_length));
554}
555
556TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
557 scoped_ptr<ByteStreamWriter> byte_stream_input;
558 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07559 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
560 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42561
562 byte_stream_input->Flush();
563 message_loop_.RunUntilIdle();
564
565 scoped_refptr<net::IOBuffer> output_io_buffer;
566 size_t output_length;
567 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
568 byte_stream_output->Read(&output_io_buffer, &output_length));
569
570 byte_stream_input->Close(0);
571 message_loop_.RunUntilIdle();
572
573 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
574 byte_stream_output->Read(&output_io_buffer, &output_length));
575}
576
[email protected]07516262013-08-22 07:43:24577TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
578 scoped_ptr<ByteStreamWriter> byte_stream_input;
579 scoped_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07580 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
581 std::numeric_limits<size_t>::max(), &byte_stream_input,
582 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24583
584 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
585 // 1 + size_t max -> Overflow.
586 scoped_refptr<net::IOBuffer> empty_io_buffer;
587 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
588 std::numeric_limits<size_t>::max()));
589 message_loop_.RunUntilIdle();
590
591 // The first write is below PostToPeer threshold. We shouldn't get anything
592 // from the output.
593 scoped_refptr<net::IOBuffer> output_io_buffer;
594 size_t output_length;
595 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
596 byte_stream_output->Read(&output_io_buffer, &output_length));
597}
598
[email protected]35869622012-10-26 23:23:55599} // namespace content