blob: ab912573739ce23178bdb055ec3e2868c1052e27 [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"
fdoraye716a902016-07-05 16:05:4915#include "base/run_loop.h"
[email protected]a8582b12012-12-19 22:18:2916#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5017#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5018#include "testing/gtest/include/gtest/gtest.h"
19
[email protected]35869622012-10-26 23:23:5520namespace content {
[email protected]b03507862012-05-23 17:11:5021namespace {
22
[email protected]b03507862012-05-23 17:11:5023void CountCallbacks(int* counter) {
24 ++*counter;
25}
26
27} // namespace
28
29class ByteStreamTest : public testing::Test {
30 public:
31 ByteStreamTest();
32
33 // Create a new IO buffer of the given |buffer_size|. Details of the
34 // contents of the created buffer will be kept, and can be validated
35 // by ValidateIOBuffer.
36 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
37 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(buffer_size));
38 char *bufferp = buffer->data();
39 for (size_t i = 0; i < buffer_size; i++)
40 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
41 pointer_queue_.push_back(bufferp);
42 length_queue_.push_back(buffer_size);
43 ++producing_seed_key_;
44 return buffer;
45 }
46
47 // Create an IOBuffer of the appropriate size and add it to the
48 // ByteStream, returning the result of the ByteStream::Write.
49 // Separate function to avoid duplication of buffer_size in test
50 // calls.
[email protected]35869622012-10-26 23:23:5551 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5052 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
53 }
54
55 // Validate that we have the IOBuffer we expect. This routine must be
56 // called on buffers that were allocated from NewIOBuffer, and in the
57 // order that they were allocated. Calls to NewIOBuffer &&
58 // ValidateIOBuffer may be interleaved.
59 bool ValidateIOBuffer(
60 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
61 char *bufferp = buffer->data();
62
63 char *expected_ptr = pointer_queue_.front();
64 size_t expected_length = length_queue_.front();
65 pointer_queue_.pop_front();
66 length_queue_.pop_front();
67 ++consuming_seed_key_;
68
69 EXPECT_EQ(expected_ptr, bufferp);
70 if (expected_ptr != bufferp)
71 return false;
72
73 EXPECT_EQ(expected_length, buffer_size);
74 if (expected_length != buffer_size)
75 return false;
76
77 for (size_t i = 0; i < buffer_size; i++) {
78 // Already incremented, so subtract one from the key.
79 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
80 % (1 << sizeof(char))),
81 bufferp[i]);
82 if (static_cast<int>((i + consuming_seed_key_ - 1) %
83 (1 << sizeof(char))) != bufferp[i]) {
84 return false;
85 }
86 }
87 return true;
88 }
89
90 protected:
[email protected]dd32b1272013-05-04 14:17:1191 base::MessageLoop message_loop_;
[email protected]b03507862012-05-23 17:11:5092
93 private:
94 int producing_seed_key_;
95 int consuming_seed_key_;
96 std::deque<char*> pointer_queue_;
97 std::deque<size_t> length_queue_;
98};
99
100ByteStreamTest::ByteStreamTest()
101 : producing_seed_key_(0),
102 consuming_seed_key_(0) { }
103
[email protected]d7db4f622012-06-04 18:20:56104// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50105// we get full triggers when we expect.
106TEST_F(ByteStreamTest, ByteStream_PushBack) {
dcheng59716272016-04-09 05:19:08107 std::unique_ptr<ByteStreamWriter> byte_stream_input;
108 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07109 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
110 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50111
112 // Push a series of IO buffers on; test pushback happening and
113 // that it's advisory.
114 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
115 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
116 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
117 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
118 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
119 // Flush
[email protected]8d0c23e2013-08-02 11:02:30120 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:24121 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
fdoraye716a902016-07-05 16:05:49122 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24123 // Data already sent to reader is also counted in.
124 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50125
126 // Pull the IO buffers out; do we get the same buffers and do they
127 // have the same contents?
128 scoped_refptr<net::IOBuffer> output_io_buffer;
129 size_t output_length;
[email protected]35869622012-10-26 23:23:55130 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50131 byte_stream_output->Read(&output_io_buffer, &output_length));
132 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
133
[email protected]35869622012-10-26 23:23:55134 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50135 byte_stream_output->Read(&output_io_buffer, &output_length));
136 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
137
[email protected]35869622012-10-26 23:23:55138 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50139 byte_stream_output->Read(&output_io_buffer, &output_length));
140 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
141
[email protected]35869622012-10-26 23:23:55142 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50143 byte_stream_output->Read(&output_io_buffer, &output_length));
144 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
145
[email protected]35869622012-10-26 23:23:55146 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50147 byte_stream_output->Read(&output_io_buffer, &output_length));
148 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
149
[email protected]35869622012-10-26 23:23:55150 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50151 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24152
fdoraye716a902016-07-05 16:05:49153 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24154 // Reader now knows that all data is read out.
155 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50156}
157
[email protected]566357e2013-07-31 03:59:36158// Confirm that Flush() method makes the writer to send written contents to
159// the reader.
160TEST_F(ByteStreamTest, ByteStream_Flush) {
dcheng59716272016-04-09 05:19:08161 std::unique_ptr<ByteStreamWriter> byte_stream_input;
162 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07163 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
164 1024, &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36165
166 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
fdoraye716a902016-07-05 16:05:49167 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36168
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();
fdoraye716a902016-07-05 16:05:49176 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36177
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();
fdoraye716a902016-07-05 16:05:49184 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36185
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);
fdoraye716a902016-07-05 16:05:49190 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36191
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) {
dcheng59716272016-04-09 05:19:08200 std::unique_ptr<ByteStreamWriter> byte_stream_input;
201 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07202 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
203 9 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50204
205 // Push a series of IO buffers on; test pushback happening and
206 // that it's advisory.
207 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49208 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50209 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49210 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50211 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49212 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50213 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49214 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50215 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
fdoraye716a902016-07-05 16:05:49216 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50217
218 // Pull the IO buffers out; do we get the same buffers and do they
219 // have the same contents?
220 scoped_refptr<net::IOBuffer> output_io_buffer;
221 size_t output_length;
[email protected]35869622012-10-26 23:23:55222 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50223 byte_stream_output->Read(&output_io_buffer, &output_length));
224 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
225
[email protected]35869622012-10-26 23:23:55226 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50227 byte_stream_output->Read(&output_io_buffer, &output_length));
228 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
229
[email protected]35869622012-10-26 23:23:55230 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50231 byte_stream_output->Read(&output_io_buffer, &output_length));
232 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
233
[email protected]35869622012-10-26 23:23:55234 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50235 byte_stream_output->Read(&output_io_buffer, &output_length));
236 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
237
[email protected]35869622012-10-26 23:23:55238 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50239 byte_stream_output->Read(&output_io_buffer, &output_length));
240 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
241
[email protected]35869622012-10-26 23:23:55242 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50243 byte_stream_output->Read(&output_io_buffer, &output_length));
244}
245
246// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56247// with data on the stream.
[email protected]b03507862012-05-23 17:11:50248TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
dcheng59716272016-04-09 05:19:08249 std::unique_ptr<ByteStreamWriter> byte_stream_input;
250 std::unique_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50251
252 scoped_refptr<net::IOBuffer> output_io_buffer;
253 size_t output_length;
254
255 // Empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07256 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
257 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55258 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50259 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30260 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49261 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55262 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50263 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30264 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50265
266 // Non-empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07267 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
268 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55269 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50270 byte_stream_output->Read(&output_io_buffer, &output_length));
271 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30272 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49273 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55274 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50275 byte_stream_output->Read(&output_io_buffer, &output_length));
276 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55277 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50278 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30279 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50280
[email protected]8d0c23e2013-08-02 11:02:30281 const int kFakeErrorCode = 22;
282
283 // Empty stream, error case.
skyostil95082a62015-06-05 19:53:07284 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
285 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55286 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50287 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30288 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49289 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55290 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[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 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50293
[email protected]8d0c23e2013-08-02 11:02:30294 // Non-empty stream, error case.
skyostil95082a62015-06-05 19:53:07295 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
296 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55297 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50298 byte_stream_output->Read(&output_io_buffer, &output_length));
299 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30300 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49301 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55302 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50303 byte_stream_output->Read(&output_io_buffer, &output_length));
304 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55305 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50306 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30307 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50308}
309
310// Confirm that callbacks on the sink side are triggered when they should be.
311TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29312 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
313 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50314
dcheng59716272016-04-09 05:19:08315 std::unique_ptr<ByteStreamWriter> byte_stream_input;
316 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07317 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
318 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50319
320 scoped_refptr<net::IOBuffer> output_io_buffer;
321 size_t output_length;
[email protected]b03507862012-05-23 17:11:50322
323 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56324 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50325 // of the interface contract. If it becomes part of the contract, the
326 // tests below should get much more precise.
327
328 // Confirm callback called when you add more than 33% of the buffer.
329
330 // Setup callback
331 int num_callbacks = 0;
332 byte_stream_output->RegisterCallback(
333 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50334
335 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49336 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50337
[email protected]b03507862012-05-23 17:11:50338 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29339 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50340 EXPECT_EQ(1, num_callbacks);
341
342 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55343 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50344 byte_stream_output->Read(&output_io_buffer, &output_length));
345 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55346 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50347 byte_stream_output->Read(&output_io_buffer, &output_length));
348
349 // Confirm callback *isn't* called at less than 33% (by lack of
350 // unexpected call on task runner).
351 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
fdoraye716a902016-07-05 16:05:49352 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50353
354 // This reflects an implementation artifact that data goes with callbacks,
355 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55356 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50357 byte_stream_output->Read(&output_io_buffer, &output_length));
358}
359
360// Confirm that callbacks on the source side are triggered when they should
361// be.
362TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29363 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
364 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50365
dcheng59716272016-04-09 05:19:08366 std::unique_ptr<ByteStreamWriter> byte_stream_input;
367 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07368 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
369 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50370
371 scoped_refptr<net::IOBuffer> output_io_buffer;
372 size_t output_length;
[email protected]b03507862012-05-23 17:11:50373
374 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56375 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50376 // of the interface contract. If it becomes part of the contract, the
377 // tests below should get much more precise.
378
379 // Confirm callback called when about 33% space available, and not
380 // at other transitions.
381
[email protected]a8582b12012-12-19 22:18:29382 // Add data.
[email protected]b03507862012-05-23 17:11:50383 int num_callbacks = 0;
384 byte_stream_input->RegisterCallback(
385 base::Bind(CountCallbacks, &num_callbacks));
386 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
387 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
388 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
389
390 // Allow bytes to transition (needed for message passing implementation),
391 // and get and validate the data.
fdoraye716a902016-07-05 16:05:49392 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55393 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50394 byte_stream_output->Read(&output_io_buffer, &output_length));
395 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
396
[email protected]b03507862012-05-23 17:11:50397 // Grab data, triggering callback. Recorded on dispatch, but doesn't
398 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55399 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50400 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50401 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
402
403 // Confirm that the callback passed to the mock does what we expect.
404 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29405 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50406 EXPECT_EQ(1, num_callbacks);
407
408 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55409 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50410 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50411 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55412 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50413 byte_stream_output->Read(&output_io_buffer, &output_length));
414 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29415 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50416 // Should have updated the internal structures but not called the
417 // callback.
418 EXPECT_EQ(1, num_callbacks);
419}
420
421// Confirm that racing a change to a sink callback with a post results
422// in the new callback being called.
423TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29424 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
425 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50426
dcheng59716272016-04-09 05:19:08427 std::unique_ptr<ByteStreamWriter> byte_stream_input;
428 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07429 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
430 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50431
432 scoped_refptr<net::IOBuffer> output_io_buffer;
433 size_t output_length;
434 base::Closure intermediate_callback;
435
[email protected]a8582b12012-12-19 22:18:29436 // Record initial state.
[email protected]b03507862012-05-23 17:11:50437 int num_callbacks = 0;
438 byte_stream_output->RegisterCallback(
439 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50440
441 // Add data, and pass it across.
442 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49443 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50444
445 // The task runner should have been hit, but the callback count
446 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50447 EXPECT_EQ(0, num_callbacks);
448
449 // If we change the callback now, the new one should be run
450 // (simulates race with post task).
451 int num_alt_callbacks = 0;
452 byte_stream_output->RegisterCallback(
453 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29454 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50455 EXPECT_EQ(0, num_callbacks);
456 EXPECT_EQ(1, num_alt_callbacks);
457
458 // Final cleanup.
[email protected]35869622012-10-26 23:23:55459 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50460 byte_stream_output->Read(&output_io_buffer, &output_length));
461 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55462 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50463 byte_stream_output->Read(&output_io_buffer, &output_length));
464
465}
466
467// Confirm that racing a change to a source callback with a post results
468// in the new callback being called.
469TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29470 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
471 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50472
dcheng59716272016-04-09 05:19:08473 std::unique_ptr<ByteStreamWriter> byte_stream_input;
474 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07475 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
476 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50477
478 scoped_refptr<net::IOBuffer> output_io_buffer;
479 size_t output_length;
480 base::Closure intermediate_callback;
481
[email protected]a8582b12012-12-19 22:18:29482 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50483 int num_callbacks = 0;
484 byte_stream_input->RegisterCallback(
485 base::Bind(CountCallbacks, &num_callbacks));
486 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
487 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
488 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
fdoraye716a902016-07-05 16:05:49489 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50490
491 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55492 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50493 byte_stream_output->Read(&output_io_buffer, &output_length));
494 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
fdoraye716a902016-07-05 16:05:49495 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50496
[email protected]b03507862012-05-23 17:11:50497 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55498 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50499 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50500 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
501
502 // Which should do the right thing when it's run.
503 int num_alt_callbacks = 0;
504 byte_stream_input->RegisterCallback(
505 base::Bind(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29506 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50507 EXPECT_EQ(0, num_callbacks);
508 EXPECT_EQ(1, num_alt_callbacks);
509
510 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55511 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50512 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50513 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55514 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50515 byte_stream_output->Read(&output_io_buffer, &output_length));
516}
517
518// Confirm that callback is called on zero data transfer but source
519// complete.
520TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29521 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
522 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50523
dcheng59716272016-04-09 05:19:08524 std::unique_ptr<ByteStreamWriter> byte_stream_input;
525 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07526 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
527 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50528
529 base::Closure intermediate_callback;
530
[email protected]a8582b12012-12-19 22:18:29531 // Record initial state.
[email protected]b03507862012-05-23 17:11:50532 int num_callbacks = 0;
533 byte_stream_output->RegisterCallback(
534 base::Bind(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50535
536 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30537 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29538 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50539 EXPECT_EQ(1, num_callbacks);
540}
541
[email protected]6a14c192013-08-06 20:18:42542TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08543 std::unique_ptr<ByteStreamWriter> byte_stream_input;
544 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07545 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
546 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42547
548 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49549 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42550
551 scoped_refptr<net::IOBuffer> output_io_buffer;
552 size_t output_length;
553 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
554 byte_stream_output->Read(&output_io_buffer, &output_length));
555}
556
557TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08558 std::unique_ptr<ByteStreamWriter> byte_stream_input;
559 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07560 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
561 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42562
563 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49564 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42565
566 scoped_refptr<net::IOBuffer> output_io_buffer;
567 size_t output_length;
568 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
569 byte_stream_output->Read(&output_io_buffer, &output_length));
570
571 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49572 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42573
574 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
575 byte_stream_output->Read(&output_io_buffer, &output_length));
576}
577
[email protected]07516262013-08-22 07:43:24578TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
dcheng59716272016-04-09 05:19:08579 std::unique_ptr<ByteStreamWriter> byte_stream_input;
580 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07581 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
582 std::numeric_limits<size_t>::max(), &byte_stream_input,
583 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24584
585 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
586 // 1 + size_t max -> Overflow.
587 scoped_refptr<net::IOBuffer> empty_io_buffer;
588 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
589 std::numeric_limits<size_t>::max()));
fdoraye716a902016-07-05 16:05:49590 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24591
592 // The first write is below PostToPeer threshold. We shouldn't get anything
593 // from the output.
594 scoped_refptr<net::IOBuffer> output_io_buffer;
595 size_t output_length;
596 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
597 byte_stream_output->Read(&output_io_buffer, &output_length));
598}
599
[email protected]35869622012-10-26 23:23:55600} // namespace content