blob: e774da17afb268aed9b106c50e4609ad22711aca [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]07516262013-08-22 07:43:249#include <limits>
[email protected]b03507862012-05-23 17:11:5010
11#include "base/bind.h"
12#include "base/callback.h"
Brett Wilsoncc8623d2017-09-12 03:28:1013#include "base/containers/circular_deque.h"
[email protected]b03507862012-05-23 17:11:5014#include "base/memory/ref_counted.h"
gabf64a25e2017-05-12 19:42:5615#include "base/message_loop/message_loop.h"
fdoraye716a902016-07-05 16:05:4916#include "base/run_loop.h"
[email protected]a8582b12012-12-19 22:18:2917#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5018#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5019#include "testing/gtest/include/gtest/gtest.h"
20
[email protected]35869622012-10-26 23:23:5521namespace content {
[email protected]b03507862012-05-23 17:11:5022namespace {
23
[email protected]b03507862012-05-23 17:11:5024void CountCallbacks(int* counter) {
25 ++*counter;
26}
27
28} // namespace
29
30class ByteStreamTest : public testing::Test {
31 public:
32 ByteStreamTest();
33
34 // Create a new IO buffer of the given |buffer_size|. Details of the
35 // contents of the created buffer will be kept, and can be validated
36 // by ValidateIOBuffer.
37 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
38 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size));
39 char *bufferp = buffer->data();
40 for (size_t i = 0; i < buffer_size; i++)
41 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
42 pointer_queue_.push_back(bufferp);
43 length_queue_.push_back(buffer_size);
44 ++producing_seed_key_;
45 return buffer;
46 }
47
48 // Create an IOBuffer of the appropriate size and add it to the
49 // ByteStream, returning the result of the ByteStream::Write.
50 // Separate function to avoid duplication of buffer_size in test
51 // calls.
[email protected]35869622012-10-26 23:23:5552 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5053 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
54 }
55
56 // Validate that we have the IOBuffer we expect. This routine must be
57 // called on buffers that were allocated from NewIOBuffer, and in the
58 // order that they were allocated. Calls to NewIOBuffer &&
59 // ValidateIOBuffer may be interleaved.
60 bool ValidateIOBuffer(
61 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
62 char *bufferp = buffer->data();
63
64 char *expected_ptr = pointer_queue_.front();
65 size_t expected_length = length_queue_.front();
66 pointer_queue_.pop_front();
67 length_queue_.pop_front();
68 ++consuming_seed_key_;
69
70 EXPECT_EQ(expected_ptr, bufferp);
71 if (expected_ptr != bufferp)
72 return false;
73
74 EXPECT_EQ(expected_length, buffer_size);
75 if (expected_length != buffer_size)
76 return false;
77
78 for (size_t i = 0; i < buffer_size; i++) {
79 // Already incremented, so subtract one from the key.
80 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
81 % (1 << sizeof(char))),
82 bufferp[i]);
83 if (static_cast<int>((i + consuming_seed_key_ - 1) %
84 (1 << sizeof(char))) != bufferp[i]) {
85 return false;
86 }
87 }
88 return true;
89 }
90
91 protected:
[email protected]dd32b1272013-05-04 14:17:1192 base::MessageLoop message_loop_;
[email protected]b03507862012-05-23 17:11:5093
94 private:
95 int producing_seed_key_;
96 int consuming_seed_key_;
Brett Wilsoncc8623d2017-09-12 03:28:1097 base::circular_deque<char*> pointer_queue_;
98 base::circular_deque<size_t> length_queue_;
[email protected]b03507862012-05-23 17:11:5099};
100
101ByteStreamTest::ByteStreamTest()
102 : producing_seed_key_(0),
103 consuming_seed_key_(0) { }
104
[email protected]d7db4f622012-06-04 18:20:56105// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50106// we get full triggers when we expect.
107TEST_F(ByteStreamTest, ByteStream_PushBack) {
dcheng59716272016-04-09 05:19:08108 std::unique_ptr<ByteStreamWriter> byte_stream_input;
109 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07110 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
111 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50112
113 // Push a series of IO buffers on; test pushback happening and
114 // that it's advisory.
115 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
116 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
117 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
118 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
119 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
120 // Flush
[email protected]8d0c23e2013-08-02 11:02:30121 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:24122 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
fdoraye716a902016-07-05 16:05:49123 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24124 // Data already sent to reader is also counted in.
125 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50126
127 // Pull the IO buffers out; do we get the same buffers and do they
128 // have the same contents?
129 scoped_refptr<net::IOBuffer> output_io_buffer;
130 size_t output_length;
[email protected]35869622012-10-26 23:23:55131 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50132 byte_stream_output->Read(&output_io_buffer, &output_length));
133 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
134
[email protected]35869622012-10-26 23:23:55135 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50136 byte_stream_output->Read(&output_io_buffer, &output_length));
137 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
138
[email protected]35869622012-10-26 23:23:55139 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50140 byte_stream_output->Read(&output_io_buffer, &output_length));
141 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
142
[email protected]35869622012-10-26 23:23:55143 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50144 byte_stream_output->Read(&output_io_buffer, &output_length));
145 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
146
[email protected]35869622012-10-26 23:23:55147 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50148 byte_stream_output->Read(&output_io_buffer, &output_length));
149 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
150
[email protected]35869622012-10-26 23:23:55151 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50152 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24153
fdoraye716a902016-07-05 16:05:49154 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24155 // Reader now knows that all data is read out.
156 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50157}
158
[email protected]566357e2013-07-31 03:59:36159// Confirm that Flush() method makes the writer to send written contents to
160// the reader.
161TEST_F(ByteStreamTest, ByteStream_Flush) {
dcheng59716272016-04-09 05:19:08162 std::unique_ptr<ByteStreamWriter> byte_stream_input;
163 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07164 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
165 1024, &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36166
167 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
fdoraye716a902016-07-05 16:05:49168 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36169
170 scoped_refptr<net::IOBuffer> output_io_buffer;
171 size_t output_length = 0;
172 // Check that data is not sent to the reader yet.
173 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
174 byte_stream_output->Read(&output_io_buffer, &output_length));
175
176 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49177 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36178
179 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
180 byte_stream_output->Read(&output_io_buffer, &output_length));
181 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
182
183 // Check that it's ok to Flush() an empty writer.
184 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49185 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36186
187 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
188 byte_stream_output->Read(&output_io_buffer, &output_length));
189
[email protected]8d0c23e2013-08-02 11:02:30190 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49191 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36192
193 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
194 byte_stream_output->Read(&output_io_buffer, &output_length));
195}
196
[email protected]b03507862012-05-23 17:11:50197// Same as above, only use knowledge of the internals to confirm
198// that we're getting pushback even when data's split across the two
199// objects
200TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
dcheng59716272016-04-09 05:19:08201 std::unique_ptr<ByteStreamWriter> byte_stream_input;
202 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07203 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
204 9 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50205
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));
fdoraye716a902016-07-05 16:05:49209 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50210 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49211 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50212 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49213 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50214 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49215 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50216 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
fdoraye716a902016-07-05 16:05:49217 base::RunLoop().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) {
dcheng59716272016-04-09 05:19:08250 std::unique_ptr<ByteStreamWriter> byte_stream_input;
251 std::unique_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.
skyostil95082a62015-06-05 19:53:07257 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
258 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55259 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50260 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30261 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49262 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55263 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50264 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30265 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50266
267 // Non-empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07268 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
269 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55270 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50271 byte_stream_output->Read(&output_io_buffer, &output_length));
272 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30273 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49274 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55275 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50276 byte_stream_output->Read(&output_io_buffer, &output_length));
277 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55278 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50279 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30280 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50281
[email protected]8d0c23e2013-08-02 11:02:30282 const int kFakeErrorCode = 22;
283
284 // Empty stream, error case.
skyostil95082a62015-06-05 19:53:07285 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
286 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55287 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50288 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30289 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49290 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55291 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50292 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30293 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50294
[email protected]8d0c23e2013-08-02 11:02:30295 // Non-empty stream, error case.
skyostil95082a62015-06-05 19:53:07296 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
297 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55298 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50299 byte_stream_output->Read(&output_io_buffer, &output_length));
300 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30301 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49302 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55303 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50304 byte_stream_output->Read(&output_io_buffer, &output_length));
305 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55306 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50307 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30308 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50309}
310
311// Confirm that callbacks on the sink side are triggered when they should be.
312TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29313 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
314 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50315
dcheng59716272016-04-09 05:19:08316 std::unique_ptr<ByteStreamWriter> byte_stream_input;
317 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07318 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
319 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50320
321 scoped_refptr<net::IOBuffer> output_io_buffer;
322 size_t output_length;
[email protected]b03507862012-05-23 17:11:50323
324 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56325 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50326 // of the interface contract. If it becomes part of the contract, the
327 // tests below should get much more precise.
328
329 // Confirm callback called when you add more than 33% of the buffer.
330
331 // Setup callback
332 int num_callbacks = 0;
333 byte_stream_output->RegisterCallback(
334 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50335
336 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49337 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50338
[email protected]b03507862012-05-23 17:11:50339 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29340 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50341 EXPECT_EQ(1, num_callbacks);
342
343 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55344 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50345 byte_stream_output->Read(&output_io_buffer, &output_length));
346 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55347 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50348 byte_stream_output->Read(&output_io_buffer, &output_length));
349
350 // Confirm callback *isn't* called at less than 33% (by lack of
351 // unexpected call on task runner).
352 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
fdoraye716a902016-07-05 16:05:49353 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50354
355 // This reflects an implementation artifact that data goes with callbacks,
356 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55357 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50358 byte_stream_output->Read(&output_io_buffer, &output_length));
359}
360
361// Confirm that callbacks on the source side are triggered when they should
362// be.
363TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29364 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
365 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50366
dcheng59716272016-04-09 05:19:08367 std::unique_ptr<ByteStreamWriter> byte_stream_input;
368 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07369 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
370 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50371
372 scoped_refptr<net::IOBuffer> output_io_buffer;
373 size_t output_length;
[email protected]b03507862012-05-23 17:11:50374
375 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56376 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50377 // of the interface contract. If it becomes part of the contract, the
378 // tests below should get much more precise.
379
380 // Confirm callback called when about 33% space available, and not
381 // at other transitions.
382
[email protected]a8582b12012-12-19 22:18:29383 // Add data.
[email protected]b03507862012-05-23 17:11:50384 int num_callbacks = 0;
385 byte_stream_input->RegisterCallback(
386 base::Bind(CountCallbacks, &num_callbacks));
387 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
388 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
389 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
390
391 // Allow bytes to transition (needed for message passing implementation),
392 // and get and validate the data.
fdoraye716a902016-07-05 16:05:49393 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55394 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50395 byte_stream_output->Read(&output_io_buffer, &output_length));
396 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
397
[email protected]b03507862012-05-23 17:11:50398 // Grab data, triggering callback. Recorded on dispatch, but doesn't
399 // happen because it's caught by the mock.
[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));
[email protected]b03507862012-05-23 17:11:50402 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
403
404 // Confirm that the callback passed to the mock does what we expect.
405 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29406 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50407 EXPECT_EQ(1, num_callbacks);
408
409 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55410 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50411 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50412 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55413 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50414 byte_stream_output->Read(&output_io_buffer, &output_length));
415 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29416 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50417 // Should have updated the internal structures but not called the
418 // callback.
419 EXPECT_EQ(1, num_callbacks);
420}
421
422// Confirm that racing a change to a sink callback with a post results
423// in the new callback being called.
424TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29425 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
426 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50427
dcheng59716272016-04-09 05:19:08428 std::unique_ptr<ByteStreamWriter> byte_stream_input;
429 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07430 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
431 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50432
433 scoped_refptr<net::IOBuffer> output_io_buffer;
434 size_t output_length;
435 base::Closure intermediate_callback;
436
[email protected]a8582b12012-12-19 22:18:29437 // Record initial state.
[email protected]b03507862012-05-23 17:11:50438 int num_callbacks = 0;
439 byte_stream_output->RegisterCallback(
440 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50441
442 // Add data, and pass it across.
443 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49444 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50445
446 // The task runner should have been hit, but the callback count
447 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50448 EXPECT_EQ(0, num_callbacks);
449
450 // If we change the callback now, the new one should be run
451 // (simulates race with post task).
452 int num_alt_callbacks = 0;
453 byte_stream_output->RegisterCallback(
454 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29455 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50456 EXPECT_EQ(0, num_callbacks);
457 EXPECT_EQ(1, num_alt_callbacks);
458
459 // Final cleanup.
[email protected]35869622012-10-26 23:23:55460 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50461 byte_stream_output->Read(&output_io_buffer, &output_length));
462 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55463 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50464 byte_stream_output->Read(&output_io_buffer, &output_length));
465
466}
467
468// Confirm that racing a change to a source callback with a post results
469// in the new callback being called.
470TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29471 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
472 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50473
dcheng59716272016-04-09 05:19:08474 std::unique_ptr<ByteStreamWriter> byte_stream_input;
475 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07476 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
477 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50478
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));
fdoraye716a902016-07-05 16:05:49490 base::RunLoop().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));
fdoraye716a902016-07-05 16:05:49496 base::RunLoop().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
dcheng59716272016-04-09 05:19:08525 std::unique_ptr<ByteStreamWriter> byte_stream_input;
526 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07527 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
528 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50529
530 base::Closure intermediate_callback;
531
[email protected]a8582b12012-12-19 22:18:29532 // Record initial state.
[email protected]b03507862012-05-23 17:11:50533 int num_callbacks = 0;
534 byte_stream_output->RegisterCallback(
535 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50536
537 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30538 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29539 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50540 EXPECT_EQ(1, num_callbacks);
541}
542
[email protected]6a14c192013-08-06 20:18:42543TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08544 std::unique_ptr<ByteStreamWriter> byte_stream_input;
545 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07546 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
547 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42548
549 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49550 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42551
552 scoped_refptr<net::IOBuffer> output_io_buffer;
553 size_t output_length;
554 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
555 byte_stream_output->Read(&output_io_buffer, &output_length));
556}
557
558TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08559 std::unique_ptr<ByteStreamWriter> byte_stream_input;
560 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07561 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
562 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42563
564 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49565 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42566
567 scoped_refptr<net::IOBuffer> output_io_buffer;
568 size_t output_length;
569 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
570 byte_stream_output->Read(&output_io_buffer, &output_length));
571
572 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49573 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42574
575 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
576 byte_stream_output->Read(&output_io_buffer, &output_length));
577}
578
[email protected]07516262013-08-22 07:43:24579TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
dcheng59716272016-04-09 05:19:08580 std::unique_ptr<ByteStreamWriter> byte_stream_input;
581 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07582 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
583 std::numeric_limits<size_t>::max(), &byte_stream_input,
584 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24585
586 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
587 // 1 + size_t max -> Overflow.
588 scoped_refptr<net::IOBuffer> empty_io_buffer;
589 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
590 std::numeric_limits<size_t>::max()));
fdoraye716a902016-07-05 16:05:49591 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24592
593 // The first write is below PostToPeer threshold. We shouldn't get anything
594 // from the output.
595 scoped_refptr<net::IOBuffer> output_io_buffer;
596 size_t output_length;
597 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
598 byte_stream_output->Read(&output_io_buffer, &output_length));
599}
600
[email protected]35869622012-10-26 23:23:55601} // namespace content