blob: 4b89fb1f2565c30d664823ec7fdf4d0d4679659c [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) {
Victor Costan63c8b3d2018-09-01 01:34:1038 scoped_refptr<net::IOBuffer> buffer =
39 base::MakeRefCounted<net::IOBuffer>(buffer_size);
[email protected]b03507862012-05-23 17:11:5040 char *bufferp = buffer->data();
41 for (size_t i = 0; i < buffer_size; i++)
42 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
43 pointer_queue_.push_back(bufferp);
44 length_queue_.push_back(buffer_size);
45 ++producing_seed_key_;
46 return buffer;
47 }
48
49 // Create an IOBuffer of the appropriate size and add it to the
50 // ByteStream, returning the result of the ByteStream::Write.
51 // Separate function to avoid duplication of buffer_size in test
52 // calls.
[email protected]35869622012-10-26 23:23:5553 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5054 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
55 }
56
57 // Validate that we have the IOBuffer we expect. This routine must be
58 // called on buffers that were allocated from NewIOBuffer, and in the
59 // order that they were allocated. Calls to NewIOBuffer &&
60 // ValidateIOBuffer may be interleaved.
61 bool ValidateIOBuffer(
62 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
63 char *bufferp = buffer->data();
64
65 char *expected_ptr = pointer_queue_.front();
66 size_t expected_length = length_queue_.front();
67 pointer_queue_.pop_front();
68 length_queue_.pop_front();
69 ++consuming_seed_key_;
70
71 EXPECT_EQ(expected_ptr, bufferp);
72 if (expected_ptr != bufferp)
73 return false;
74
75 EXPECT_EQ(expected_length, buffer_size);
76 if (expected_length != buffer_size)
77 return false;
78
79 for (size_t i = 0; i < buffer_size; i++) {
80 // Already incremented, so subtract one from the key.
81 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
82 % (1 << sizeof(char))),
83 bufferp[i]);
84 if (static_cast<int>((i + consuming_seed_key_ - 1) %
85 (1 << sizeof(char))) != bufferp[i]) {
86 return false;
87 }
88 }
89 return true;
90 }
91
92 protected:
[email protected]dd32b1272013-05-04 14:17:1193 base::MessageLoop message_loop_;
[email protected]b03507862012-05-23 17:11:5094
95 private:
96 int producing_seed_key_;
97 int consuming_seed_key_;
Brett Wilsoncc8623d2017-09-12 03:28:1098 base::circular_deque<char*> pointer_queue_;
99 base::circular_deque<size_t> length_queue_;
[email protected]b03507862012-05-23 17:11:50100};
101
102ByteStreamTest::ByteStreamTest()
103 : producing_seed_key_(0),
104 consuming_seed_key_(0) { }
105
[email protected]d7db4f622012-06-04 18:20:56106// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50107// we get full triggers when we expect.
108TEST_F(ByteStreamTest, ByteStream_PushBack) {
dcheng59716272016-04-09 05:19:08109 std::unique_ptr<ByteStreamWriter> byte_stream_input;
110 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07111 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
112 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50113
114 // Push a series of IO buffers on; test pushback happening and
115 // that it's advisory.
116 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
117 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
118 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
119 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
120 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
121 // Flush
[email protected]8d0c23e2013-08-02 11:02:30122 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:24123 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
fdoraye716a902016-07-05 16:05:49124 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24125 // Data already sent to reader is also counted in.
126 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50127
128 // Pull the IO buffers out; do we get the same buffers and do they
129 // have the same contents?
130 scoped_refptr<net::IOBuffer> output_io_buffer;
131 size_t output_length;
[email protected]35869622012-10-26 23:23:55132 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50133 byte_stream_output->Read(&output_io_buffer, &output_length));
134 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
135
[email protected]35869622012-10-26 23:23:55136 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50137 byte_stream_output->Read(&output_io_buffer, &output_length));
138 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
139
[email protected]35869622012-10-26 23:23:55140 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50141 byte_stream_output->Read(&output_io_buffer, &output_length));
142 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
143
[email protected]35869622012-10-26 23:23:55144 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50145 byte_stream_output->Read(&output_io_buffer, &output_length));
146 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
147
[email protected]35869622012-10-26 23:23:55148 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50149 byte_stream_output->Read(&output_io_buffer, &output_length));
150 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
151
[email protected]35869622012-10-26 23:23:55152 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50153 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24154
fdoraye716a902016-07-05 16:05:49155 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24156 // Reader now knows that all data is read out.
157 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50158}
159
[email protected]566357e2013-07-31 03:59:36160// Confirm that Flush() method makes the writer to send written contents to
161// the reader.
162TEST_F(ByteStreamTest, ByteStream_Flush) {
dcheng59716272016-04-09 05:19:08163 std::unique_ptr<ByteStreamWriter> byte_stream_input;
164 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07165 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
166 1024, &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36167
168 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
fdoraye716a902016-07-05 16:05:49169 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36170
171 scoped_refptr<net::IOBuffer> output_io_buffer;
172 size_t output_length = 0;
173 // Check that data is not sent to the reader yet.
174 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
175 byte_stream_output->Read(&output_io_buffer, &output_length));
176
177 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49178 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36179
180 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
181 byte_stream_output->Read(&output_io_buffer, &output_length));
182 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
183
184 // Check that it's ok to Flush() an empty writer.
185 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49186 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36187
188 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
189 byte_stream_output->Read(&output_io_buffer, &output_length));
190
[email protected]8d0c23e2013-08-02 11:02:30191 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49192 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36193
194 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
195 byte_stream_output->Read(&output_io_buffer, &output_length));
196}
197
[email protected]b03507862012-05-23 17:11:50198// Same as above, only use knowledge of the internals to confirm
199// that we're getting pushback even when data's split across the two
200// objects
201TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
dcheng59716272016-04-09 05:19:08202 std::unique_ptr<ByteStreamWriter> byte_stream_input;
203 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07204 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
205 9 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50206
207 // Push a series of IO buffers on; test pushback happening and
208 // that it's advisory.
209 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_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_FALSE(Write(byte_stream_input.get(), 6 * 1024));
fdoraye716a902016-07-05 16:05:49218 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50219
220 // Pull the IO buffers out; do we get the same buffers and do they
221 // have the same contents?
222 scoped_refptr<net::IOBuffer> output_io_buffer;
223 size_t output_length;
[email protected]35869622012-10-26 23:23:55224 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50225 byte_stream_output->Read(&output_io_buffer, &output_length));
226 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
227
[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_EMPTY,
[email protected]b03507862012-05-23 17:11:50245 byte_stream_output->Read(&output_io_buffer, &output_length));
246}
247
248// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56249// with data on the stream.
[email protected]b03507862012-05-23 17:11:50250TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
dcheng59716272016-04-09 05:19:08251 std::unique_ptr<ByteStreamWriter> byte_stream_input;
252 std::unique_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50253
254 scoped_refptr<net::IOBuffer> output_io_buffer;
255 size_t output_length;
256
257 // Empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07258 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
259 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55260 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50261 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30262 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49263 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55264 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50265 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30266 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50267
268 // Non-empty stream, non-error case.
skyostil95082a62015-06-05 19:53:07269 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
270 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55271 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50272 byte_stream_output->Read(&output_io_buffer, &output_length));
273 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30274 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49275 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55276 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50277 byte_stream_output->Read(&output_io_buffer, &output_length));
278 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55279 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50280 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30281 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50282
[email protected]8d0c23e2013-08-02 11:02:30283 const int kFakeErrorCode = 22;
284
285 // Empty stream, error case.
skyostil95082a62015-06-05 19:53:07286 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
287 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55288 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50289 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30290 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49291 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55292 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50293 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30294 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50295
[email protected]8d0c23e2013-08-02 11:02:30296 // Non-empty stream, error case.
skyostil95082a62015-06-05 19:53:07297 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
298 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55299 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50300 byte_stream_output->Read(&output_io_buffer, &output_length));
301 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30302 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49303 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55304 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50305 byte_stream_output->Read(&output_io_buffer, &output_length));
306 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55307 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50308 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30309 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50310}
311
312// Confirm that callbacks on the sink side are triggered when they should be.
313TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29314 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
315 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50316
dcheng59716272016-04-09 05:19:08317 std::unique_ptr<ByteStreamWriter> byte_stream_input;
318 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07319 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
320 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50321
322 scoped_refptr<net::IOBuffer> output_io_buffer;
323 size_t output_length;
[email protected]b03507862012-05-23 17:11:50324
325 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56326 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50327 // of the interface contract. If it becomes part of the contract, the
328 // tests below should get much more precise.
329
330 // Confirm callback called when you add more than 33% of the buffer.
331
332 // Setup callback
333 int num_callbacks = 0;
334 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23335 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50336
337 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49338 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50339
[email protected]b03507862012-05-23 17:11:50340 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29341 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50342 EXPECT_EQ(1, num_callbacks);
343
344 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55345 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50346 byte_stream_output->Read(&output_io_buffer, &output_length));
347 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55348 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50349 byte_stream_output->Read(&output_io_buffer, &output_length));
350
351 // Confirm callback *isn't* called at less than 33% (by lack of
352 // unexpected call on task runner).
353 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
fdoraye716a902016-07-05 16:05:49354 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50355
356 // This reflects an implementation artifact that data goes with callbacks,
357 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55358 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50359 byte_stream_output->Read(&output_io_buffer, &output_length));
360}
361
362// Confirm that callbacks on the source side are triggered when they should
363// be.
364TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29365 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
366 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50367
dcheng59716272016-04-09 05:19:08368 std::unique_ptr<ByteStreamWriter> byte_stream_input;
369 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07370 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
371 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50372
373 scoped_refptr<net::IOBuffer> output_io_buffer;
374 size_t output_length;
[email protected]b03507862012-05-23 17:11:50375
376 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56377 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50378 // of the interface contract. If it becomes part of the contract, the
379 // tests below should get much more precise.
380
381 // Confirm callback called when about 33% space available, and not
382 // at other transitions.
383
[email protected]a8582b12012-12-19 22:18:29384 // Add data.
[email protected]b03507862012-05-23 17:11:50385 int num_callbacks = 0;
386 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23387 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50388 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
389 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
390 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
391
392 // Allow bytes to transition (needed for message passing implementation),
393 // and get and validate the data.
fdoraye716a902016-07-05 16:05:49394 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55395 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50396 byte_stream_output->Read(&output_io_buffer, &output_length));
397 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
398
[email protected]b03507862012-05-23 17:11:50399 // Grab data, triggering callback. Recorded on dispatch, but doesn't
400 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55401 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50402 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50403 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
404
405 // Confirm that the callback passed to the mock does what we expect.
406 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29407 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50408 EXPECT_EQ(1, num_callbacks);
409
410 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55411 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50412 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50413 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55414 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50415 byte_stream_output->Read(&output_io_buffer, &output_length));
416 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29417 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50418 // Should have updated the internal structures but not called the
419 // callback.
420 EXPECT_EQ(1, num_callbacks);
421}
422
423// Confirm that racing a change to a sink callback with a post results
424// in the new callback being called.
425TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29426 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
427 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50428
dcheng59716272016-04-09 05:19:08429 std::unique_ptr<ByteStreamWriter> byte_stream_input;
430 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07431 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
432 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50433
434 scoped_refptr<net::IOBuffer> output_io_buffer;
435 size_t output_length;
436 base::Closure intermediate_callback;
437
[email protected]a8582b12012-12-19 22:18:29438 // Record initial state.
[email protected]b03507862012-05-23 17:11:50439 int num_callbacks = 0;
440 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23441 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50442
443 // Add data, and pass it across.
444 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49445 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50446
447 // The task runner should have been hit, but the callback count
448 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50449 EXPECT_EQ(0, num_callbacks);
450
451 // If we change the callback now, the new one should be run
452 // (simulates race with post task).
453 int num_alt_callbacks = 0;
454 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23455 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29456 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50457 EXPECT_EQ(0, num_callbacks);
458 EXPECT_EQ(1, num_alt_callbacks);
459
460 // Final cleanup.
[email protected]35869622012-10-26 23:23:55461 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50462 byte_stream_output->Read(&output_io_buffer, &output_length));
463 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55464 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50465 byte_stream_output->Read(&output_io_buffer, &output_length));
466
467}
468
469// Confirm that racing a change to a source callback with a post results
470// in the new callback being called.
471TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29472 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
473 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50474
dcheng59716272016-04-09 05:19:08475 std::unique_ptr<ByteStreamWriter> byte_stream_input;
476 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07477 CreateByteStream(task_runner, message_loop_.task_runner(), 10000,
478 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50479
480 scoped_refptr<net::IOBuffer> output_io_buffer;
481 size_t output_length;
482 base::Closure intermediate_callback;
483
[email protected]a8582b12012-12-19 22:18:29484 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50485 int num_callbacks = 0;
486 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23487 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50488 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
489 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
490 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
fdoraye716a902016-07-05 16:05:49491 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50492
493 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55494 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50495 byte_stream_output->Read(&output_io_buffer, &output_length));
496 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
fdoraye716a902016-07-05 16:05:49497 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50498
[email protected]b03507862012-05-23 17:11:50499 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55500 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50501 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50502 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
503
504 // Which should do the right thing when it's run.
505 int num_alt_callbacks = 0;
506 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23507 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29508 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50509 EXPECT_EQ(0, num_callbacks);
510 EXPECT_EQ(1, num_alt_callbacks);
511
512 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55513 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50514 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50515 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55516 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50517 byte_stream_output->Read(&output_io_buffer, &output_length));
518}
519
520// Confirm that callback is called on zero data transfer but source
521// complete.
522TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29523 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
524 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50525
dcheng59716272016-04-09 05:19:08526 std::unique_ptr<ByteStreamWriter> byte_stream_input;
527 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07528 CreateByteStream(message_loop_.task_runner(), task_runner, 10000,
529 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50530
531 base::Closure intermediate_callback;
532
[email protected]a8582b12012-12-19 22:18:29533 // Record initial state.
[email protected]b03507862012-05-23 17:11:50534 int num_callbacks = 0;
535 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23536 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50537
538 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30539 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29540 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50541 EXPECT_EQ(1, num_callbacks);
542}
543
[email protected]6a14c192013-08-06 20:18:42544TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08545 std::unique_ptr<ByteStreamWriter> byte_stream_input;
546 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07547 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
548 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42549
550 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49551 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42552
553 scoped_refptr<net::IOBuffer> output_io_buffer;
554 size_t output_length;
555 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
556 byte_stream_output->Read(&output_io_buffer, &output_length));
557}
558
559TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08560 std::unique_ptr<ByteStreamWriter> byte_stream_input;
561 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07562 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
563 3 * 1024, &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42564
565 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49566 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42567
568 scoped_refptr<net::IOBuffer> output_io_buffer;
569 size_t output_length;
570 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
571 byte_stream_output->Read(&output_io_buffer, &output_length));
572
573 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49574 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42575
576 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
577 byte_stream_output->Read(&output_io_buffer, &output_length));
578}
579
[email protected]07516262013-08-22 07:43:24580TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
dcheng59716272016-04-09 05:19:08581 std::unique_ptr<ByteStreamWriter> byte_stream_input;
582 std::unique_ptr<ByteStreamReader> byte_stream_output;
skyostil95082a62015-06-05 19:53:07583 CreateByteStream(message_loop_.task_runner(), message_loop_.task_runner(),
584 std::numeric_limits<size_t>::max(), &byte_stream_input,
585 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24586
587 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
588 // 1 + size_t max -> Overflow.
589 scoped_refptr<net::IOBuffer> empty_io_buffer;
590 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
591 std::numeric_limits<size_t>::max()));
fdoraye716a902016-07-05 16:05:49592 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24593
594 // The first write is below PostToPeer threshold. We shouldn't get anything
595 // from the output.
596 scoped_refptr<net::IOBuffer> output_io_buffer;
597 size_t output_length;
598 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
599 byte_stream_output->Read(&output_io_buffer, &output_length));
600}
601
[email protected]35869622012-10-26 23:23:55602} // namespace content