Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: content/browser/byte_stream_unittest.cc

Issue 18284005: Make ByteStream independent from DownloadInterruptReason (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove CONTENT_EXPORT all Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/byte_stream.h" 5 #include "content/browser/byte_stream.h"
6 6
7 #include <deque> 7 #include <deque>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
(...skipping 27 matching lines...) Expand all
38 pointer_queue_.push_back(bufferp); 38 pointer_queue_.push_back(bufferp);
39 length_queue_.push_back(buffer_size); 39 length_queue_.push_back(buffer_size);
40 ++producing_seed_key_; 40 ++producing_seed_key_;
41 return buffer; 41 return buffer;
42 } 42 }
43 43
44 // Create an IOBuffer of the appropriate size and add it to the 44 // Create an IOBuffer of the appropriate size and add it to the
45 // ByteStream, returning the result of the ByteStream::Write. 45 // ByteStream, returning the result of the ByteStream::Write.
46 // Separate function to avoid duplication of buffer_size in test 46 // Separate function to avoid duplication of buffer_size in test
47 // calls. 47 // calls.
48 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) { 48 bool Write(ByteStreamWriter<int>* byte_stream_input, size_t buffer_size) {
49 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size); 49 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
50 } 50 }
51 51
52 // Validate that we have the IOBuffer we expect. This routine must be 52 // Validate that we have the IOBuffer we expect. This routine must be
53 // called on buffers that were allocated from NewIOBuffer, and in the 53 // called on buffers that were allocated from NewIOBuffer, and in the
54 // order that they were allocated. Calls to NewIOBuffer && 54 // order that they were allocated. Calls to NewIOBuffer &&
55 // ValidateIOBuffer may be interleaved. 55 // ValidateIOBuffer may be interleaved.
56 bool ValidateIOBuffer( 56 bool ValidateIOBuffer(
57 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) { 57 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
58 char *bufferp = buffer->data(); 58 char *bufferp = buffer->data();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 std::deque<size_t> length_queue_; 94 std::deque<size_t> length_queue_;
95 }; 95 };
96 96
97 ByteStreamTest::ByteStreamTest() 97 ByteStreamTest::ByteStreamTest()
98 : producing_seed_key_(0), 98 : producing_seed_key_(0),
99 consuming_seed_key_(0) { } 99 consuming_seed_key_(0) { }
100 100
101 // Confirm that filling and emptying the stream works properly, and that 101 // Confirm that filling and emptying the stream works properly, and that
102 // we get full triggers when we expect. 102 // we get full triggers when we expect.
103 TEST_F(ByteStreamTest, ByteStream_PushBack) { 103 TEST_F(ByteStreamTest, ByteStream_PushBack) {
104 scoped_ptr<ByteStreamWriter> byte_stream_input; 104 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
105 scoped_ptr<ByteStreamReader> byte_stream_output; 105 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
106 CreateByteStream( 106 CreateByteStream<int>(
107 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), 107 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
108 3 * 1024, &byte_stream_input, &byte_stream_output); 108 3 * 1024, &byte_stream_input, &byte_stream_output);
109 109
110 // Push a series of IO buffers on; test pushback happening and 110 // Push a series of IO buffers on; test pushback happening and
111 // that it's advisory. 111 // that it's advisory.
112 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 112 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
113 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 113 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
114 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 114 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
115 EXPECT_FALSE(Write(byte_stream_input.get(), 1)); 115 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
116 EXPECT_FALSE(Write(byte_stream_input.get(), 1024)); 116 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
117 // Flush 117 // Flush
118 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE); 118 byte_stream_input->Close(0);
119 message_loop_.RunUntilIdle(); 119 message_loop_.RunUntilIdle();
120 120
121 // Pull the IO buffers out; do we get the same buffers and do they 121 // Pull the IO buffers out; do we get the same buffers and do they
122 // have the same contents? 122 // have the same contents?
123 scoped_refptr<net::IOBuffer> output_io_buffer; 123 scoped_refptr<net::IOBuffer> output_io_buffer;
124 size_t output_length; 124 size_t output_length;
125 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 125 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
126 byte_stream_output->Read(&output_io_buffer, &output_length)); 126 byte_stream_output->Read(&output_io_buffer, &output_length));
127 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 127 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
128 128
129 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 129 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
130 byte_stream_output->Read(&output_io_buffer, &output_length)); 130 byte_stream_output->Read(&output_io_buffer, &output_length));
131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 131 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
132 132
133 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 133 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
134 byte_stream_output->Read(&output_io_buffer, &output_length)); 134 byte_stream_output->Read(&output_io_buffer, &output_length));
135 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 135 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
136 136
137 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 137 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
138 byte_stream_output->Read(&output_io_buffer, &output_length)); 138 byte_stream_output->Read(&output_io_buffer, &output_length));
139 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 139 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
140 140
141 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 141 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
142 byte_stream_output->Read(&output_io_buffer, &output_length)); 142 byte_stream_output->Read(&output_io_buffer, &output_length));
143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 143 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
144 144
145 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE, 145 EXPECT_EQ(ByteStreamReader<int>::STREAM_COMPLETE,
146 byte_stream_output->Read(&output_io_buffer, &output_length)); 146 byte_stream_output->Read(&output_io_buffer, &output_length));
147 } 147 }
148 148
149 // Same as above, only use knowledge of the internals to confirm 149 // Same as above, only use knowledge of the internals to confirm
150 // that we're getting pushback even when data's split across the two 150 // that we're getting pushback even when data's split across the two
151 // objects 151 // objects
152 TEST_F(ByteStreamTest, ByteStream_PushBackSplit) { 152 TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
153 scoped_ptr<ByteStreamWriter> byte_stream_input; 153 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
154 scoped_ptr<ByteStreamReader> byte_stream_output; 154 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
155 CreateByteStream( 155 CreateByteStream<int>(
156 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), 156 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
157 9 * 1024, &byte_stream_input, &byte_stream_output); 157 9 * 1024, &byte_stream_input, &byte_stream_output);
158 158
159 // Push a series of IO buffers on; test pushback happening and 159 // Push a series of IO buffers on; test pushback happening and
160 // that it's advisory. 160 // that it's advisory.
161 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 161 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
162 message_loop_.RunUntilIdle(); 162 message_loop_.RunUntilIdle();
163 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 163 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
164 message_loop_.RunUntilIdle(); 164 message_loop_.RunUntilIdle();
165 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 165 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
166 message_loop_.RunUntilIdle(); 166 message_loop_.RunUntilIdle();
167 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 167 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
168 message_loop_.RunUntilIdle(); 168 message_loop_.RunUntilIdle();
169 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024)); 169 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
170 message_loop_.RunUntilIdle(); 170 message_loop_.RunUntilIdle();
171 171
172 // Pull the IO buffers out; do we get the same buffers and do they 172 // Pull the IO buffers out; do we get the same buffers and do they
173 // have the same contents? 173 // have the same contents?
174 scoped_refptr<net::IOBuffer> output_io_buffer; 174 scoped_refptr<net::IOBuffer> output_io_buffer;
175 size_t output_length; 175 size_t output_length;
176 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 176 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
177 byte_stream_output->Read(&output_io_buffer, &output_length)); 177 byte_stream_output->Read(&output_io_buffer, &output_length));
178 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 178 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
179 179
180 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 180 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
181 byte_stream_output->Read(&output_io_buffer, &output_length)); 181 byte_stream_output->Read(&output_io_buffer, &output_length));
182 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 182 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
183 183
184 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 184 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
185 byte_stream_output->Read(&output_io_buffer, &output_length)); 185 byte_stream_output->Read(&output_io_buffer, &output_length));
186 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 186 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
187 187
188 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 188 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
189 byte_stream_output->Read(&output_io_buffer, &output_length)); 189 byte_stream_output->Read(&output_io_buffer, &output_length));
190 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 190 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
191 191
192 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 192 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
193 byte_stream_output->Read(&output_io_buffer, &output_length)); 193 byte_stream_output->Read(&output_io_buffer, &output_length));
194 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 194 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
195 195
196 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 196 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
197 byte_stream_output->Read(&output_io_buffer, &output_length)); 197 byte_stream_output->Read(&output_io_buffer, &output_length));
198 } 198 }
199 199
200 // Confirm that a Close() notification transmits in-order 200 // Confirm that a Close() notification transmits in-order
201 // with data on the stream. 201 // with data on the stream.
202 TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) { 202 TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
203 scoped_ptr<ByteStreamWriter> byte_stream_input; 203 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
204 scoped_ptr<ByteStreamReader> byte_stream_output; 204 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
205 205
206 scoped_refptr<net::IOBuffer> output_io_buffer; 206 scoped_refptr<net::IOBuffer> output_io_buffer;
207 size_t output_length; 207 size_t output_length;
208 208
209 // Empty stream, non-error case. 209 // Empty stream, non-error case.
210 CreateByteStream( 210 CreateByteStream<int>(
211 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), 211 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
212 3 * 1024, &byte_stream_input, &byte_stream_output); 212 3 * 1024, &byte_stream_input, &byte_stream_output);
213 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 213 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
214 byte_stream_output->Read(&output_io_buffer, &output_length)); 214 byte_stream_output->Read(&output_io_buffer, &output_length));
215 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE); 215 byte_stream_input->Close(0);
216 message_loop_.RunUntilIdle(); 216 message_loop_.RunUntilIdle();
217 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, 217 ASSERT_EQ(ByteStreamReader<int>::STREAM_COMPLETE,
218 byte_stream_output->Read(&output_io_buffer, &output_length)); 218 byte_stream_output->Read(&output_io_buffer, &output_length));
219 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 219 EXPECT_EQ(0, byte_stream_output->GetStatus());
220 byte_stream_output->GetStatus());
221 220
222 // Non-empty stream, non-error case. 221 // Non-empty stream, non-error case.
223 CreateByteStream( 222 CreateByteStream<int>(
224 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), 223 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
225 3 * 1024, &byte_stream_input, &byte_stream_output); 224 3 * 1024, &byte_stream_input, &byte_stream_output);
226 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 225 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
227 byte_stream_output->Read(&output_io_buffer, &output_length)); 226 byte_stream_output->Read(&output_io_buffer, &output_length));
228 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 227 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
229 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE); 228 byte_stream_input->Close(0);
230 message_loop_.RunUntilIdle(); 229 message_loop_.RunUntilIdle();
231 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 230 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
232 byte_stream_output->Read(&output_io_buffer, &output_length)); 231 byte_stream_output->Read(&output_io_buffer, &output_length));
233 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 232 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
234 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, 233 ASSERT_EQ(ByteStreamReader<int>::STREAM_COMPLETE,
235 byte_stream_output->Read(&output_io_buffer, &output_length)); 234 byte_stream_output->Read(&output_io_buffer, &output_length));
236 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NONE, 235 EXPECT_EQ(0, byte_stream_output->GetStatus());
237 byte_stream_output->GetStatus());
238 236
239 // Empty stream, non-error case. 237 const int kFakeErrorCode = 22;
240 CreateByteStream( 238
239 // Empty stream, error case.
240 CreateByteStream<int>(
241 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), 241 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
242 3 * 1024, &byte_stream_input, &byte_stream_output); 242 3 * 1024, &byte_stream_input, &byte_stream_output);
243 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 243 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
244 byte_stream_output->Read(&output_io_buffer, &output_length)); 244 byte_stream_output->Read(&output_io_buffer, &output_length));
245 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED); 245 byte_stream_input->Close(kFakeErrorCode);
246 message_loop_.RunUntilIdle(); 246 message_loop_.RunUntilIdle();
247 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, 247 ASSERT_EQ(ByteStreamReader<int>::STREAM_COMPLETE,
248 byte_stream_output->Read(&output_io_buffer, &output_length)); 248 byte_stream_output->Read(&output_io_buffer, &output_length));
249 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 249 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
250 byte_stream_output->GetStatus());
251 250
252 // Non-empty stream, non-error case. 251 // Non-empty stream, error case.
253 CreateByteStream( 252 CreateByteStream<int>(
254 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(), 253 message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
255 3 * 1024, &byte_stream_input, &byte_stream_output); 254 3 * 1024, &byte_stream_input, &byte_stream_output);
256 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 255 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
257 byte_stream_output->Read(&output_io_buffer, &output_length)); 256 byte_stream_output->Read(&output_io_buffer, &output_length));
258 EXPECT_TRUE(Write(byte_stream_input.get(), 1024)); 257 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
259 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED); 258 byte_stream_input->Close(kFakeErrorCode);
260 message_loop_.RunUntilIdle(); 259 message_loop_.RunUntilIdle();
261 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 260 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
262 byte_stream_output->Read(&output_io_buffer, &output_length)); 261 byte_stream_output->Read(&output_io_buffer, &output_length));
263 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 262 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
264 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE, 263 ASSERT_EQ(ByteStreamReader<int>::STREAM_COMPLETE,
265 byte_stream_output->Read(&output_io_buffer, &output_length)); 264 byte_stream_output->Read(&output_io_buffer, &output_length));
266 EXPECT_EQ(DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED, 265 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
267 byte_stream_output->GetStatus());
268 } 266 }
269 267
270 // Confirm that callbacks on the sink side are triggered when they should be. 268 // Confirm that callbacks on the sink side are triggered when they should be.
271 TEST_F(ByteStreamTest, ByteStream_SinkCallback) { 269 TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
272 scoped_refptr<base::TestSimpleTaskRunner> task_runner( 270 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
273 new base::TestSimpleTaskRunner()); 271 new base::TestSimpleTaskRunner());
274 272
275 scoped_ptr<ByteStreamWriter> byte_stream_input; 273 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
276 scoped_ptr<ByteStreamReader> byte_stream_output; 274 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
277 CreateByteStream( 275 CreateByteStream<int>(
278 message_loop_.message_loop_proxy(), task_runner, 276 message_loop_.message_loop_proxy(), task_runner,
279 10000, &byte_stream_input, &byte_stream_output); 277 10000, &byte_stream_input, &byte_stream_output);
280 278
281 scoped_refptr<net::IOBuffer> output_io_buffer; 279 scoped_refptr<net::IOBuffer> output_io_buffer;
282 size_t output_length; 280 size_t output_length;
283 281
284 // Note that the specifics of when the callbacks are called with regard 282 // Note that the specifics of when the callbacks are called with regard
285 // to how much data is pushed onto the stream is not (currently) part 283 // to how much data is pushed onto the stream is not (currently) part
286 // of the interface contract. If it becomes part of the contract, the 284 // of the interface contract. If it becomes part of the contract, the
287 // tests below should get much more precise. 285 // tests below should get much more precise.
288 286
289 // Confirm callback called when you add more than 33% of the buffer. 287 // Confirm callback called when you add more than 33% of the buffer.
290 288
291 // Setup callback 289 // Setup callback
292 int num_callbacks = 0; 290 int num_callbacks = 0;
293 byte_stream_output->RegisterCallback( 291 byte_stream_output->RegisterCallback(
294 base::Bind(CountCallbacks, &num_callbacks)); 292 base::Bind(CountCallbacks, &num_callbacks));
295 293
296 EXPECT_TRUE(Write(byte_stream_input.get(), 4000)); 294 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
297 message_loop_.RunUntilIdle(); 295 message_loop_.RunUntilIdle();
298 296
299 EXPECT_EQ(0, num_callbacks); 297 EXPECT_EQ(0, num_callbacks);
300 task_runner->RunUntilIdle(); 298 task_runner->RunUntilIdle();
301 EXPECT_EQ(1, num_callbacks); 299 EXPECT_EQ(1, num_callbacks);
302 300
303 // Check data and stream state. 301 // Check data and stream state.
304 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 302 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
305 byte_stream_output->Read(&output_io_buffer, &output_length)); 303 byte_stream_output->Read(&output_io_buffer, &output_length));
306 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 304 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
307 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 305 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
308 byte_stream_output->Read(&output_io_buffer, &output_length)); 306 byte_stream_output->Read(&output_io_buffer, &output_length));
309 307
310 // Confirm callback *isn't* called at less than 33% (by lack of 308 // Confirm callback *isn't* called at less than 33% (by lack of
311 // unexpected call on task runner). 309 // unexpected call on task runner).
312 EXPECT_TRUE(Write(byte_stream_input.get(), 3000)); 310 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
313 message_loop_.RunUntilIdle(); 311 message_loop_.RunUntilIdle();
314 312
315 // This reflects an implementation artifact that data goes with callbacks, 313 // This reflects an implementation artifact that data goes with callbacks,
316 // which should not be considered part of the interface guarantee. 314 // which should not be considered part of the interface guarantee.
317 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 315 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
318 byte_stream_output->Read(&output_io_buffer, &output_length)); 316 byte_stream_output->Read(&output_io_buffer, &output_length));
319 } 317 }
320 318
321 // Confirm that callbacks on the source side are triggered when they should 319 // Confirm that callbacks on the source side are triggered when they should
322 // be. 320 // be.
323 TEST_F(ByteStreamTest, ByteStream_SourceCallback) { 321 TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
324 scoped_refptr<base::TestSimpleTaskRunner> task_runner( 322 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
325 new base::TestSimpleTaskRunner()); 323 new base::TestSimpleTaskRunner());
326 324
327 scoped_ptr<ByteStreamWriter> byte_stream_input; 325 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
328 scoped_ptr<ByteStreamReader> byte_stream_output; 326 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
329 CreateByteStream( 327 CreateByteStream<int>(
330 task_runner, message_loop_.message_loop_proxy(), 328 task_runner, message_loop_.message_loop_proxy(),
331 10000, &byte_stream_input, &byte_stream_output); 329 10000, &byte_stream_input, &byte_stream_output);
332 330
333 scoped_refptr<net::IOBuffer> output_io_buffer; 331 scoped_refptr<net::IOBuffer> output_io_buffer;
334 size_t output_length; 332 size_t output_length;
335 333
336 // Note that the specifics of when the callbacks are called with regard 334 // Note that the specifics of when the callbacks are called with regard
337 // to how much data is pulled from the stream is not (currently) part 335 // to how much data is pulled from the stream is not (currently) part
338 // of the interface contract. If it becomes part of the contract, the 336 // of the interface contract. If it becomes part of the contract, the
339 // tests below should get much more precise. 337 // tests below should get much more precise.
340 338
341 // Confirm callback called when about 33% space available, and not 339 // Confirm callback called when about 33% space available, and not
342 // at other transitions. 340 // at other transitions.
343 341
344 // Add data. 342 // Add data.
345 int num_callbacks = 0; 343 int num_callbacks = 0;
346 byte_stream_input->RegisterCallback( 344 byte_stream_input->RegisterCallback(
347 base::Bind(CountCallbacks, &num_callbacks)); 345 base::Bind(CountCallbacks, &num_callbacks));
348 EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); 346 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
349 EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); 347 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
350 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); 348 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
351 349
352 // Allow bytes to transition (needed for message passing implementation), 350 // Allow bytes to transition (needed for message passing implementation),
353 // and get and validate the data. 351 // and get and validate the data.
354 message_loop_.RunUntilIdle(); 352 message_loop_.RunUntilIdle();
355 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 353 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
356 byte_stream_output->Read(&output_io_buffer, &output_length)); 354 byte_stream_output->Read(&output_io_buffer, &output_length));
357 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 355 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
358 356
359 // Grab data, triggering callback. Recorded on dispatch, but doesn't 357 // Grab data, triggering callback. Recorded on dispatch, but doesn't
360 // happen because it's caught by the mock. 358 // happen because it's caught by the mock.
361 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 359 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
362 byte_stream_output->Read(&output_io_buffer, &output_length)); 360 byte_stream_output->Read(&output_io_buffer, &output_length));
363 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 361 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
364 362
365 // Confirm that the callback passed to the mock does what we expect. 363 // Confirm that the callback passed to the mock does what we expect.
366 EXPECT_EQ(0, num_callbacks); 364 EXPECT_EQ(0, num_callbacks);
367 task_runner->RunUntilIdle(); 365 task_runner->RunUntilIdle();
368 EXPECT_EQ(1, num_callbacks); 366 EXPECT_EQ(1, num_callbacks);
369 367
370 // Same drill with final buffer. 368 // Same drill with final buffer.
371 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 369 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
372 byte_stream_output->Read(&output_io_buffer, &output_length)); 370 byte_stream_output->Read(&output_io_buffer, &output_length));
373 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 371 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
374 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 372 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
375 byte_stream_output->Read(&output_io_buffer, &output_length)); 373 byte_stream_output->Read(&output_io_buffer, &output_length));
376 EXPECT_EQ(1, num_callbacks); 374 EXPECT_EQ(1, num_callbacks);
377 task_runner->RunUntilIdle(); 375 task_runner->RunUntilIdle();
378 // Should have updated the internal structures but not called the 376 // Should have updated the internal structures but not called the
379 // callback. 377 // callback.
380 EXPECT_EQ(1, num_callbacks); 378 EXPECT_EQ(1, num_callbacks);
381 } 379 }
382 380
383 // Confirm that racing a change to a sink callback with a post results 381 // Confirm that racing a change to a sink callback with a post results
384 // in the new callback being called. 382 // in the new callback being called.
385 TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) { 383 TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
386 scoped_refptr<base::TestSimpleTaskRunner> task_runner( 384 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
387 new base::TestSimpleTaskRunner()); 385 new base::TestSimpleTaskRunner());
388 386
389 scoped_ptr<ByteStreamWriter> byte_stream_input; 387 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
390 scoped_ptr<ByteStreamReader> byte_stream_output; 388 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
391 CreateByteStream( 389 CreateByteStream<int>(
392 message_loop_.message_loop_proxy(), task_runner, 390 message_loop_.message_loop_proxy(), task_runner,
393 10000, &byte_stream_input, &byte_stream_output); 391 10000, &byte_stream_input, &byte_stream_output);
394 392
395 scoped_refptr<net::IOBuffer> output_io_buffer; 393 scoped_refptr<net::IOBuffer> output_io_buffer;
396 size_t output_length; 394 size_t output_length;
397 base::Closure intermediate_callback; 395 base::Closure intermediate_callback;
398 396
399 // Record initial state. 397 // Record initial state.
400 int num_callbacks = 0; 398 int num_callbacks = 0;
401 byte_stream_output->RegisterCallback( 399 byte_stream_output->RegisterCallback(
(...skipping 10 matching lines...) Expand all
412 // If we change the callback now, the new one should be run 410 // If we change the callback now, the new one should be run
413 // (simulates race with post task). 411 // (simulates race with post task).
414 int num_alt_callbacks = 0; 412 int num_alt_callbacks = 0;
415 byte_stream_output->RegisterCallback( 413 byte_stream_output->RegisterCallback(
416 base::Bind(CountCallbacks, &num_alt_callbacks)); 414 base::Bind(CountCallbacks, &num_alt_callbacks));
417 task_runner->RunUntilIdle(); 415 task_runner->RunUntilIdle();
418 EXPECT_EQ(0, num_callbacks); 416 EXPECT_EQ(0, num_callbacks);
419 EXPECT_EQ(1, num_alt_callbacks); 417 EXPECT_EQ(1, num_alt_callbacks);
420 418
421 // Final cleanup. 419 // Final cleanup.
422 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 420 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
423 byte_stream_output->Read(&output_io_buffer, &output_length)); 421 byte_stream_output->Read(&output_io_buffer, &output_length));
424 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 422 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
425 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 423 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
426 byte_stream_output->Read(&output_io_buffer, &output_length)); 424 byte_stream_output->Read(&output_io_buffer, &output_length));
427 425
428 } 426 }
429 427
430 // Confirm that racing a change to a source callback with a post results 428 // Confirm that racing a change to a source callback with a post results
431 // in the new callback being called. 429 // in the new callback being called.
432 TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) { 430 TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
433 scoped_refptr<base::TestSimpleTaskRunner> task_runner( 431 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
434 new base::TestSimpleTaskRunner()); 432 new base::TestSimpleTaskRunner());
435 433
436 scoped_ptr<ByteStreamWriter> byte_stream_input; 434 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
437 scoped_ptr<ByteStreamReader> byte_stream_output; 435 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
438 CreateByteStream( 436 CreateByteStream<int>(
439 task_runner, message_loop_.message_loop_proxy(), 437 task_runner, message_loop_.message_loop_proxy(),
440 10000, &byte_stream_input, &byte_stream_output); 438 10000, &byte_stream_input, &byte_stream_output);
441 439
442 scoped_refptr<net::IOBuffer> output_io_buffer; 440 scoped_refptr<net::IOBuffer> output_io_buffer;
443 size_t output_length; 441 size_t output_length;
444 base::Closure intermediate_callback; 442 base::Closure intermediate_callback;
445 443
446 // Setup state for test. 444 // Setup state for test.
447 int num_callbacks = 0; 445 int num_callbacks = 0;
448 byte_stream_input->RegisterCallback( 446 byte_stream_input->RegisterCallback(
449 base::Bind(CountCallbacks, &num_callbacks)); 447 base::Bind(CountCallbacks, &num_callbacks));
450 EXPECT_TRUE(Write(byte_stream_input.get(), 2000)); 448 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
451 EXPECT_TRUE(Write(byte_stream_input.get(), 2001)); 449 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
452 EXPECT_FALSE(Write(byte_stream_input.get(), 6000)); 450 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
453 message_loop_.RunUntilIdle(); 451 message_loop_.RunUntilIdle();
454 452
455 // Initial get should not trigger callback. 453 // Initial get should not trigger callback.
456 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 454 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
457 byte_stream_output->Read(&output_io_buffer, &output_length)); 455 byte_stream_output->Read(&output_io_buffer, &output_length));
458 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 456 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
459 message_loop_.RunUntilIdle(); 457 message_loop_.RunUntilIdle();
460 458
461 // Second get *should* trigger callback. 459 // Second get *should* trigger callback.
462 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 460 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
463 byte_stream_output->Read(&output_io_buffer, &output_length)); 461 byte_stream_output->Read(&output_io_buffer, &output_length));
464 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 462 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
465 463
466 // Which should do the right thing when it's run. 464 // Which should do the right thing when it's run.
467 int num_alt_callbacks = 0; 465 int num_alt_callbacks = 0;
468 byte_stream_input->RegisterCallback( 466 byte_stream_input->RegisterCallback(
469 base::Bind(CountCallbacks, &num_alt_callbacks)); 467 base::Bind(CountCallbacks, &num_alt_callbacks));
470 task_runner->RunUntilIdle(); 468 task_runner->RunUntilIdle();
471 EXPECT_EQ(0, num_callbacks); 469 EXPECT_EQ(0, num_callbacks);
472 EXPECT_EQ(1, num_alt_callbacks); 470 EXPECT_EQ(1, num_alt_callbacks);
473 471
474 // Third get should also trigger callback. 472 // Third get should also trigger callback.
475 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA, 473 EXPECT_EQ(ByteStreamReader<int>::STREAM_HAS_DATA,
476 byte_stream_output->Read(&output_io_buffer, &output_length)); 474 byte_stream_output->Read(&output_io_buffer, &output_length));
477 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length)); 475 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
478 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY, 476 EXPECT_EQ(ByteStreamReader<int>::STREAM_EMPTY,
479 byte_stream_output->Read(&output_io_buffer, &output_length)); 477 byte_stream_output->Read(&output_io_buffer, &output_length));
480 } 478 }
481 479
482 // Confirm that callback is called on zero data transfer but source 480 // Confirm that callback is called on zero data transfer but source
483 // complete. 481 // complete.
484 TEST_F(ByteStreamTest, ByteStream_ZeroCallback) { 482 TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
485 scoped_refptr<base::TestSimpleTaskRunner> task_runner( 483 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
486 new base::TestSimpleTaskRunner()); 484 new base::TestSimpleTaskRunner());
487 485
488 scoped_ptr<ByteStreamWriter> byte_stream_input; 486 scoped_ptr<ByteStreamWriter<int> > byte_stream_input;
489 scoped_ptr<ByteStreamReader> byte_stream_output; 487 scoped_ptr<ByteStreamReader<int> > byte_stream_output;
490 CreateByteStream( 488 CreateByteStream<int>(
491 message_loop_.message_loop_proxy(), task_runner, 489 message_loop_.message_loop_proxy(), task_runner,
492 10000, &byte_stream_input, &byte_stream_output); 490 10000, &byte_stream_input, &byte_stream_output);
493 491
494 base::Closure intermediate_callback; 492 base::Closure intermediate_callback;
495 493
496 // Record initial state. 494 // Record initial state.
497 int num_callbacks = 0; 495 int num_callbacks = 0;
498 byte_stream_output->RegisterCallback( 496 byte_stream_output->RegisterCallback(
499 base::Bind(CountCallbacks, &num_callbacks)); 497 base::Bind(CountCallbacks, &num_callbacks));
500 498
501 // Immediately close the stream. 499 // Immediately close the stream.
502 byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE); 500 byte_stream_input->Close(0);
503 task_runner->RunUntilIdle(); 501 task_runner->RunUntilIdle();
504 EXPECT_EQ(1, num_callbacks); 502 EXPECT_EQ(1, num_callbacks);
505 } 503 }
506 504
507 } // namespace content 505 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698