blob: 0d617d3b1febbf1259e7b7f86b4df156654620dc [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"
fdoraye716a902016-07-05 16:05:4915#include "base/run_loop.h"
Alexander Timin58ee05f2018-10-05 11:44:3316#include "base/test/scoped_task_environment.h"
[email protected]a8582b12012-12-19 22:18:2917#include "base/test/test_simple_task_runner.h"
Alexander Timin58ee05f2018-10-05 11:44:3318#include "base/threading/thread_task_runner_handle.h"
[email protected]b03507862012-05-23 17:11:5019#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5020#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]35869622012-10-26 23:23:5522namespace content {
[email protected]b03507862012-05-23 17:11:5023namespace {
24
[email protected]b03507862012-05-23 17:11:5025void CountCallbacks(int* counter) {
26 ++*counter;
27}
28
29} // namespace
30
31class ByteStreamTest : public testing::Test {
32 public:
33 ByteStreamTest();
34
35 // Create a new IO buffer of the given |buffer_size|. Details of the
36 // contents of the created buffer will be kept, and can be validated
37 // by ValidateIOBuffer.
38 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
Victor Costan63c8b3d2018-09-01 01:34:1039 scoped_refptr<net::IOBuffer> buffer =
40 base::MakeRefCounted<net::IOBuffer>(buffer_size);
[email protected]b03507862012-05-23 17:11:5041 char *bufferp = buffer->data();
42 for (size_t i = 0; i < buffer_size; i++)
43 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
44 pointer_queue_.push_back(bufferp);
45 length_queue_.push_back(buffer_size);
46 ++producing_seed_key_;
47 return buffer;
48 }
49
50 // Create an IOBuffer of the appropriate size and add it to the
51 // ByteStream, returning the result of the ByteStream::Write.
52 // Separate function to avoid duplication of buffer_size in test
53 // calls.
[email protected]35869622012-10-26 23:23:5554 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5055 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
56 }
57
58 // Validate that we have the IOBuffer we expect. This routine must be
59 // called on buffers that were allocated from NewIOBuffer, and in the
60 // order that they were allocated. Calls to NewIOBuffer &&
61 // ValidateIOBuffer may be interleaved.
62 bool ValidateIOBuffer(
63 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
64 char *bufferp = buffer->data();
65
66 char *expected_ptr = pointer_queue_.front();
67 size_t expected_length = length_queue_.front();
68 pointer_queue_.pop_front();
69 length_queue_.pop_front();
70 ++consuming_seed_key_;
71
72 EXPECT_EQ(expected_ptr, bufferp);
73 if (expected_ptr != bufferp)
74 return false;
75
76 EXPECT_EQ(expected_length, buffer_size);
77 if (expected_length != buffer_size)
78 return false;
79
80 for (size_t i = 0; i < buffer_size; i++) {
81 // Already incremented, so subtract one from the key.
82 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
83 % (1 << sizeof(char))),
84 bufferp[i]);
85 if (static_cast<int>((i + consuming_seed_key_ - 1) %
86 (1 << sizeof(char))) != bufferp[i]) {
87 return false;
88 }
89 }
90 return true;
91 }
92
93 protected:
Alexander Timin58ee05f2018-10-05 11:44:3394 base::test::ScopedTaskEnvironment task_environment_;
[email protected]b03507862012-05-23 17:11:5095
96 private:
97 int producing_seed_key_;
98 int consuming_seed_key_;
Brett Wilsoncc8623d2017-09-12 03:28:1099 base::circular_deque<char*> pointer_queue_;
100 base::circular_deque<size_t> length_queue_;
[email protected]b03507862012-05-23 17:11:50101};
102
103ByteStreamTest::ByteStreamTest()
104 : producing_seed_key_(0),
105 consuming_seed_key_(0) { }
106
[email protected]d7db4f622012-06-04 18:20:56107// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50108// we get full triggers when we expect.
109TEST_F(ByteStreamTest, ByteStream_PushBack) {
dcheng59716272016-04-09 05:19:08110 std::unique_ptr<ByteStreamWriter> byte_stream_input;
111 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33112 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
113 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
114 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50115
116 // Push a series of IO buffers on; test pushback happening and
117 // that it's advisory.
118 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
119 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
120 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
121 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
122 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
123 // Flush
[email protected]8d0c23e2013-08-02 11:02:30124 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:24125 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
fdoraye716a902016-07-05 16:05:49126 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24127 // Data already sent to reader is also counted in.
128 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50129
130 // Pull the IO buffers out; do we get the same buffers and do they
131 // have the same contents?
132 scoped_refptr<net::IOBuffer> output_io_buffer;
133 size_t output_length;
[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_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50151 byte_stream_output->Read(&output_io_buffer, &output_length));
152 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
153
[email protected]35869622012-10-26 23:23:55154 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50155 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24156
fdoraye716a902016-07-05 16:05:49157 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24158 // Reader now knows that all data is read out.
159 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50160}
161
[email protected]566357e2013-07-31 03:59:36162// Confirm that Flush() method makes the writer to send written contents to
163// the reader.
164TEST_F(ByteStreamTest, ByteStream_Flush) {
dcheng59716272016-04-09 05:19:08165 std::unique_ptr<ByteStreamWriter> byte_stream_input;
166 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33167 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
168 base::ThreadTaskRunnerHandle::Get(), 1024,
169 &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36170
171 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
fdoraye716a902016-07-05 16:05:49172 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36173
174 scoped_refptr<net::IOBuffer> output_io_buffer;
175 size_t output_length = 0;
176 // Check that data is not sent to the reader yet.
177 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
178 byte_stream_output->Read(&output_io_buffer, &output_length));
179
180 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49181 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36182
183 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
184 byte_stream_output->Read(&output_io_buffer, &output_length));
185 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
186
187 // Check that it's ok to Flush() an empty writer.
188 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49189 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36190
191 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
192 byte_stream_output->Read(&output_io_buffer, &output_length));
193
[email protected]8d0c23e2013-08-02 11:02:30194 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49195 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36196
197 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
198 byte_stream_output->Read(&output_io_buffer, &output_length));
199}
200
[email protected]b03507862012-05-23 17:11:50201// Same as above, only use knowledge of the internals to confirm
202// that we're getting pushback even when data's split across the two
203// objects
204TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
dcheng59716272016-04-09 05:19:08205 std::unique_ptr<ByteStreamWriter> byte_stream_input;
206 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33207 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
208 base::ThreadTaskRunnerHandle::Get(), 9 * 1024,
209 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50210
211 // Push a series of IO buffers on; test pushback happening and
212 // that it's advisory.
213 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_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49216 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50217 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49218 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50219 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49220 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50221 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
fdoraye716a902016-07-05 16:05:49222 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50223
224 // Pull the IO buffers out; do we get the same buffers and do they
225 // have the same contents?
226 scoped_refptr<net::IOBuffer> output_io_buffer;
227 size_t output_length;
[email protected]35869622012-10-26 23:23:55228 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50229 byte_stream_output->Read(&output_io_buffer, &output_length));
230 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
231
[email protected]35869622012-10-26 23:23:55232 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50233 byte_stream_output->Read(&output_io_buffer, &output_length));
234 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
235
[email protected]35869622012-10-26 23:23:55236 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50237 byte_stream_output->Read(&output_io_buffer, &output_length));
238 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
239
[email protected]35869622012-10-26 23:23:55240 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50241 byte_stream_output->Read(&output_io_buffer, &output_length));
242 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
243
[email protected]35869622012-10-26 23:23:55244 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50245 byte_stream_output->Read(&output_io_buffer, &output_length));
246 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
247
[email protected]35869622012-10-26 23:23:55248 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50249 byte_stream_output->Read(&output_io_buffer, &output_length));
250}
251
252// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56253// with data on the stream.
[email protected]b03507862012-05-23 17:11:50254TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
dcheng59716272016-04-09 05:19:08255 std::unique_ptr<ByteStreamWriter> byte_stream_input;
256 std::unique_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50257
258 scoped_refptr<net::IOBuffer> output_io_buffer;
259 size_t output_length;
260
261 // Empty stream, non-error case.
Alexander Timin58ee05f2018-10-05 11:44:33262 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
263 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
264 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55265 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50266 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30267 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49268 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55269 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50270 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30271 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50272
273 // Non-empty stream, non-error case.
Alexander Timin58ee05f2018-10-05 11:44:33274 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
275 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
276 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55277 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50278 byte_stream_output->Read(&output_io_buffer, &output_length));
279 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30280 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49281 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55282 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50283 byte_stream_output->Read(&output_io_buffer, &output_length));
284 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55285 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[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 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50288
[email protected]8d0c23e2013-08-02 11:02:30289 const int kFakeErrorCode = 22;
290
291 // Empty stream, error case.
Alexander Timin58ee05f2018-10-05 11:44:33292 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
293 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
294 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55295 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50296 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30297 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49298 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55299 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50300 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30301 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50302
[email protected]8d0c23e2013-08-02 11:02:30303 // Non-empty stream, error case.
Alexander Timin58ee05f2018-10-05 11:44:33304 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
305 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
306 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55307 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50308 byte_stream_output->Read(&output_io_buffer, &output_length));
309 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30310 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49311 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55312 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50313 byte_stream_output->Read(&output_io_buffer, &output_length));
314 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55315 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50316 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30317 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50318}
319
320// Confirm that callbacks on the sink side are triggered when they should be.
321TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29322 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
323 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50324
dcheng59716272016-04-09 05:19:08325 std::unique_ptr<ByteStreamWriter> byte_stream_input;
326 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33327 CreateByteStream(base::ThreadTaskRunnerHandle::Get(), task_runner, 10000,
skyostil95082a62015-06-05 19:53:07328 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50329
330 scoped_refptr<net::IOBuffer> output_io_buffer;
331 size_t output_length;
[email protected]b03507862012-05-23 17:11:50332
333 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56334 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50335 // of the interface contract. If it becomes part of the contract, the
336 // tests below should get much more precise.
337
338 // Confirm callback called when you add more than 33% of the buffer.
339
340 // Setup callback
341 int num_callbacks = 0;
342 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23343 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50344
345 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49346 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50347
[email protected]b03507862012-05-23 17:11:50348 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29349 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50350 EXPECT_EQ(1, num_callbacks);
351
352 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55353 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50354 byte_stream_output->Read(&output_io_buffer, &output_length));
355 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[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 // Confirm callback *isn't* called at less than 33% (by lack of
360 // unexpected call on task runner).
361 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
fdoraye716a902016-07-05 16:05:49362 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50363
364 // This reflects an implementation artifact that data goes with callbacks,
365 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55366 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50367 byte_stream_output->Read(&output_io_buffer, &output_length));
368}
369
370// Confirm that callbacks on the source side are triggered when they should
371// be.
372TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29373 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
374 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50375
dcheng59716272016-04-09 05:19:08376 std::unique_ptr<ByteStreamWriter> byte_stream_input;
377 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33378 CreateByteStream(task_runner, base::ThreadTaskRunnerHandle::Get(), 10000,
skyostil95082a62015-06-05 19:53:07379 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50380
381 scoped_refptr<net::IOBuffer> output_io_buffer;
382 size_t output_length;
[email protected]b03507862012-05-23 17:11:50383
384 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56385 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50386 // of the interface contract. If it becomes part of the contract, the
387 // tests below should get much more precise.
388
389 // Confirm callback called when about 33% space available, and not
390 // at other transitions.
391
[email protected]a8582b12012-12-19 22:18:29392 // Add data.
[email protected]b03507862012-05-23 17:11:50393 int num_callbacks = 0;
394 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23395 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50396 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
397 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
398 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
399
400 // Allow bytes to transition (needed for message passing implementation),
401 // and get and validate the data.
fdoraye716a902016-07-05 16:05:49402 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55403 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50404 byte_stream_output->Read(&output_io_buffer, &output_length));
405 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
406
[email protected]b03507862012-05-23 17:11:50407 // Grab data, triggering callback. Recorded on dispatch, but doesn't
408 // happen because it's caught by the mock.
[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));
412
413 // Confirm that the callback passed to the mock does what we expect.
414 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29415 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50416 EXPECT_EQ(1, num_callbacks);
417
418 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55419 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50420 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50421 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55422 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50423 byte_stream_output->Read(&output_io_buffer, &output_length));
424 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29425 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50426 // Should have updated the internal structures but not called the
427 // callback.
428 EXPECT_EQ(1, num_callbacks);
429}
430
431// Confirm that racing a change to a sink callback with a post results
432// in the new callback being called.
433TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29434 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
435 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50436
dcheng59716272016-04-09 05:19:08437 std::unique_ptr<ByteStreamWriter> byte_stream_input;
438 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33439 CreateByteStream(base::ThreadTaskRunnerHandle::Get(), task_runner, 10000,
skyostil95082a62015-06-05 19:53:07440 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50441
442 scoped_refptr<net::IOBuffer> output_io_buffer;
443 size_t output_length;
444 base::Closure intermediate_callback;
445
[email protected]a8582b12012-12-19 22:18:29446 // Record initial state.
[email protected]b03507862012-05-23 17:11:50447 int num_callbacks = 0;
448 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23449 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50450
451 // Add data, and pass it across.
452 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49453 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50454
455 // The task runner should have been hit, but the callback count
456 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50457 EXPECT_EQ(0, num_callbacks);
458
459 // If we change the callback now, the new one should be run
460 // (simulates race with post task).
461 int num_alt_callbacks = 0;
462 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23463 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29464 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50465 EXPECT_EQ(0, num_callbacks);
466 EXPECT_EQ(1, num_alt_callbacks);
467
468 // Final cleanup.
[email protected]35869622012-10-26 23:23:55469 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50470 byte_stream_output->Read(&output_io_buffer, &output_length));
471 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55472 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50473 byte_stream_output->Read(&output_io_buffer, &output_length));
474
475}
476
477// Confirm that racing a change to a source callback with a post results
478// in the new callback being called.
479TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29480 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
481 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50482
dcheng59716272016-04-09 05:19:08483 std::unique_ptr<ByteStreamWriter> byte_stream_input;
484 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33485 CreateByteStream(task_runner, base::ThreadTaskRunnerHandle::Get(), 10000,
skyostil95082a62015-06-05 19:53:07486 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50487
488 scoped_refptr<net::IOBuffer> output_io_buffer;
489 size_t output_length;
490 base::Closure intermediate_callback;
491
[email protected]a8582b12012-12-19 22:18:29492 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50493 int num_callbacks = 0;
494 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23495 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50496 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
497 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
498 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
fdoraye716a902016-07-05 16:05:49499 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50500
501 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55502 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50503 byte_stream_output->Read(&output_io_buffer, &output_length));
504 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
fdoraye716a902016-07-05 16:05:49505 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50506
[email protected]b03507862012-05-23 17:11:50507 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55508 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50509 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50510 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
511
512 // Which should do the right thing when it's run.
513 int num_alt_callbacks = 0;
514 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23515 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29516 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50517 EXPECT_EQ(0, num_callbacks);
518 EXPECT_EQ(1, num_alt_callbacks);
519
520 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55521 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50522 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50523 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55524 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50525 byte_stream_output->Read(&output_io_buffer, &output_length));
526}
527
528// Confirm that callback is called on zero data transfer but source
529// complete.
530TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29531 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
532 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50533
dcheng59716272016-04-09 05:19:08534 std::unique_ptr<ByteStreamWriter> byte_stream_input;
535 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33536 CreateByteStream(base::ThreadTaskRunnerHandle::Get(), task_runner, 10000,
skyostil95082a62015-06-05 19:53:07537 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50538
539 base::Closure intermediate_callback;
540
[email protected]a8582b12012-12-19 22:18:29541 // Record initial state.
[email protected]b03507862012-05-23 17:11:50542 int num_callbacks = 0;
543 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23544 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50545
546 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30547 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29548 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50549 EXPECT_EQ(1, num_callbacks);
550}
551
[email protected]6a14c192013-08-06 20:18:42552TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08553 std::unique_ptr<ByteStreamWriter> byte_stream_input;
554 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33555 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
556 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
557 &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42558
559 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49560 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42561
562 scoped_refptr<net::IOBuffer> output_io_buffer;
563 size_t output_length;
564 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
565 byte_stream_output->Read(&output_io_buffer, &output_length));
566}
567
568TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08569 std::unique_ptr<ByteStreamWriter> byte_stream_input;
570 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33571 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
572 base::ThreadTaskRunnerHandle::Get(), 3 * 1024,
573 &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42574
575 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49576 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42577
578 scoped_refptr<net::IOBuffer> output_io_buffer;
579 size_t output_length;
580 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
581 byte_stream_output->Read(&output_io_buffer, &output_length));
582
583 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49584 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42585
586 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
587 byte_stream_output->Read(&output_io_buffer, &output_length));
588}
589
[email protected]07516262013-08-22 07:43:24590TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
dcheng59716272016-04-09 05:19:08591 std::unique_ptr<ByteStreamWriter> byte_stream_input;
592 std::unique_ptr<ByteStreamReader> byte_stream_output;
Alexander Timin58ee05f2018-10-05 11:44:33593 CreateByteStream(base::ThreadTaskRunnerHandle::Get(),
594 base::ThreadTaskRunnerHandle::Get(),
skyostil95082a62015-06-05 19:53:07595 std::numeric_limits<size_t>::max(), &byte_stream_input,
596 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24597
598 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
599 // 1 + size_t max -> Overflow.
600 scoped_refptr<net::IOBuffer> empty_io_buffer;
601 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
602 std::numeric_limits<size_t>::max()));
fdoraye716a902016-07-05 16:05:49603 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24604
605 // The first write is below PostToPeer threshold. We shouldn't get anything
606 // from the output.
607 scoped_refptr<net::IOBuffer> output_io_buffer;
608 size_t output_length;
609 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
610 byte_stream_output->Read(&output_io_buffer, &output_length));
611}
612
[email protected]35869622012-10-26 23:23:55613} // namespace content