OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 multibuffer_.SetFileSize(10000); | 279 multibuffer_.SetFileSize(10000); |
280 multibuffer_.SetMustReadWholeFile(true); | 280 multibuffer_.SetMustReadWholeFile(true); |
281 MultiBufferReader reader(&multibuffer_, pos, end, | 281 MultiBufferReader reader(&multibuffer_, pos, end, |
282 base::Callback<void(int64_t, int64_t)>()); | 282 base::Callback<void(int64_t, int64_t)>()); |
283 reader.SetMaxBuffer(2000, 5000); | 283 reader.SetMaxBuffer(2000, 5000); |
284 reader.SetPreload(1000, 1000); | 284 reader.SetPreload(1000, 1000); |
285 while (pos < end) { | 285 while (pos < end) { |
286 unsigned char buffer[27]; | 286 unsigned char buffer[27]; |
287 buffer[17] = 17; | 287 buffer[17] = 17; |
288 size_t to_read = std::min<size_t>(end - pos, 17); | 288 size_t to_read = std::min<size_t>(end - pos, 17); |
289 while (AdvanceAll()) | 289 while (AdvanceAll()) { |
290 ; | 290 } |
291 int64_t bytes = reader.TryRead(buffer, to_read); | 291 int64_t bytes = reader.TryRead(buffer, to_read); |
292 EXPECT_GT(bytes, 0); | 292 EXPECT_GT(bytes, 0); |
293 EXPECT_EQ(buffer[17], 17); | 293 EXPECT_EQ(buffer[17], 17); |
294 for (int64_t i = 0; i < bytes; i++) { | 294 for (int64_t i = 0; i < bytes; i++) { |
295 uint8_t expected = static_cast<uint8_t>((pos * 15485863) >> 16); | 295 uint8_t expected = static_cast<uint8_t>((pos * 15485863) >> 16); |
296 EXPECT_EQ(expected, buffer[i]) << " pos = " << pos; | 296 EXPECT_EQ(expected, buffer[i]) << " pos = " << pos; |
297 pos++; | 297 pos++; |
298 } | 298 } |
299 } | 299 } |
300 } | 300 } |
301 | 301 |
302 // Checks that if the data provider provides too much data after we told it | 302 // Checks that if the data provider provides too much data after we told it |
303 // to defer, we kill it. | 303 // to defer, we kill it. |
304 TEST_F(MultiBufferTest, ReadAllAdvanceFirst_NeverDefer) { | 304 TEST_F(MultiBufferTest, ReadAllAdvanceFirst_NeverDefer) { |
305 multibuffer_.SetMaxWriters(1); | 305 multibuffer_.SetMaxWriters(1); |
306 size_t pos = 0; | 306 size_t pos = 0; |
307 size_t end = 10000; | 307 size_t end = 10000; |
308 multibuffer_.SetFileSize(10000); | 308 multibuffer_.SetFileSize(10000); |
309 multibuffer_.SetMaxBlocksAfterDefer(-10000); | 309 multibuffer_.SetMaxBlocksAfterDefer(-10000); |
310 multibuffer_.SetRangeSupported(true); | 310 multibuffer_.SetRangeSupported(true); |
311 MultiBufferReader reader(&multibuffer_, pos, end, | 311 MultiBufferReader reader(&multibuffer_, pos, end, |
312 base::Callback<void(int64_t, int64_t)>()); | 312 base::Callback<void(int64_t, int64_t)>()); |
313 reader.SetMaxBuffer(2000, 5000); | 313 reader.SetMaxBuffer(2000, 5000); |
314 reader.SetPreload(1000, 1000); | 314 reader.SetPreload(1000, 1000); |
315 while (pos < end) { | 315 while (pos < end) { |
316 unsigned char buffer[27]; | 316 unsigned char buffer[27]; |
317 buffer[17] = 17; | 317 buffer[17] = 17; |
318 size_t to_read = std::min<size_t>(end - pos, 17); | 318 size_t to_read = std::min<size_t>(end - pos, 17); |
319 while (AdvanceAll()) | 319 while (AdvanceAll()) { |
320 ; | 320 } |
321 int64_t bytes = reader.TryRead(buffer, to_read); | 321 int64_t bytes = reader.TryRead(buffer, to_read); |
322 EXPECT_GT(bytes, 0); | 322 EXPECT_GT(bytes, 0); |
323 EXPECT_EQ(buffer[17], 17); | 323 EXPECT_EQ(buffer[17], 17); |
324 for (int64_t i = 0; i < bytes; i++) { | 324 for (int64_t i = 0; i < bytes; i++) { |
325 uint8_t expected = static_cast<uint8_t>((pos * 15485863) >> 16); | 325 uint8_t expected = static_cast<uint8_t>((pos * 15485863) >> 16); |
326 EXPECT_EQ(expected, buffer[i]) << " pos = " << pos; | 326 EXPECT_EQ(expected, buffer[i]) << " pos = " << pos; |
327 pos++; | 327 pos++; |
328 } | 328 } |
329 } | 329 } |
330 EXPECT_GT(multibuffer_.writers_created(), 1); | 330 EXPECT_GT(multibuffer_.writers_created(), 1); |
331 } | 331 } |
332 | 332 |
333 // Same as ReadAllAdvanceFirst_NeverDefer, but the url doesn't support | 333 // Same as ReadAllAdvanceFirst_NeverDefer, but the url doesn't support |
334 // ranges, so we don't destroy it no matter how much data it provides. | 334 // ranges, so we don't destroy it no matter how much data it provides. |
335 TEST_F(MultiBufferTest, ReadAllAdvanceFirst_NeverDefer2) { | 335 TEST_F(MultiBufferTest, ReadAllAdvanceFirst_NeverDefer2) { |
336 multibuffer_.SetMaxWriters(1); | 336 multibuffer_.SetMaxWriters(1); |
337 size_t pos = 0; | 337 size_t pos = 0; |
338 size_t end = 10000; | 338 size_t end = 10000; |
339 multibuffer_.SetFileSize(10000); | 339 multibuffer_.SetFileSize(10000); |
340 multibuffer_.SetMustReadWholeFile(true); | 340 multibuffer_.SetMustReadWholeFile(true); |
341 multibuffer_.SetMaxBlocksAfterDefer(-10000); | 341 multibuffer_.SetMaxBlocksAfterDefer(-10000); |
342 MultiBufferReader reader(&multibuffer_, pos, end, | 342 MultiBufferReader reader(&multibuffer_, pos, end, |
343 base::Callback<void(int64_t, int64_t)>()); | 343 base::Callback<void(int64_t, int64_t)>()); |
344 reader.SetMaxBuffer(2000, 5000); | 344 reader.SetMaxBuffer(2000, 5000); |
345 reader.SetPreload(1000, 1000); | 345 reader.SetPreload(1000, 1000); |
346 while (pos < end) { | 346 while (pos < end) { |
347 unsigned char buffer[27]; | 347 unsigned char buffer[27]; |
348 buffer[17] = 17; | 348 buffer[17] = 17; |
349 size_t to_read = std::min<size_t>(end - pos, 17); | 349 size_t to_read = std::min<size_t>(end - pos, 17); |
350 while (AdvanceAll()) | 350 while (AdvanceAll()) { |
351 ; | 351 } |
352 int64_t bytes = reader.TryRead(buffer, to_read); | 352 int64_t bytes = reader.TryRead(buffer, to_read); |
353 EXPECT_GT(bytes, 0); | 353 EXPECT_GT(bytes, 0); |
354 EXPECT_EQ(buffer[17], 17); | 354 EXPECT_EQ(buffer[17], 17); |
355 for (int64_t i = 0; i < bytes; i++) { | 355 for (int64_t i = 0; i < bytes; i++) { |
356 uint8_t expected = static_cast<uint8_t>((pos * 15485863) >> 16); | 356 uint8_t expected = static_cast<uint8_t>((pos * 15485863) >> 16); |
357 EXPECT_EQ(expected, buffer[i]) << " pos = " << pos; | 357 EXPECT_EQ(expected, buffer[i]) << " pos = " << pos; |
358 pos++; | 358 pos++; |
359 } | 359 } |
360 } | 360 } |
361 } | 361 } |
362 | 362 |
363 TEST_F(MultiBufferTest, LRUTest) { | 363 TEST_F(MultiBufferTest, LRUTest) { |
364 int64_t max_size = 17; | 364 int64_t max_size = 17; |
365 int64_t current_size = 0; | 365 int64_t current_size = 0; |
366 lru_->IncrementMaxSize(max_size); | 366 lru_->IncrementMaxSize(max_size); |
367 | 367 |
368 multibuffer_.SetMaxWriters(1); | 368 multibuffer_.SetMaxWriters(1); |
369 size_t pos = 0; | 369 size_t pos = 0; |
370 size_t end = 10000; | 370 size_t end = 10000; |
371 multibuffer_.SetFileSize(10000); | 371 multibuffer_.SetFileSize(10000); |
372 MultiBufferReader reader(&multibuffer_, pos, end, | 372 MultiBufferReader reader(&multibuffer_, pos, end, |
373 base::Callback<void(int64_t, int64_t)>()); | 373 base::Callback<void(int64_t, int64_t)>()); |
374 reader.SetPreload(10000, 10000); | 374 reader.SetPreload(10000, 10000); |
375 // Note, no pinning, all data should end up in LRU. | 375 // Note, no pinning, all data should end up in LRU. |
376 EXPECT_EQ(current_size, lru_->Size()); | 376 EXPECT_EQ(current_size, lru_->Size()); |
377 current_size += max_size; | 377 current_size += max_size; |
378 while (AdvanceAll()) | 378 while (AdvanceAll()) { |
379 ; | 379 } |
380 EXPECT_EQ(current_size, lru_->Size()); | 380 EXPECT_EQ(current_size, lru_->Size()); |
381 lru_->IncrementMaxSize(-max_size); | 381 lru_->IncrementMaxSize(-max_size); |
382 lru_->Prune(3); | 382 lru_->Prune(3); |
383 current_size -= 3; | 383 current_size -= 3; |
384 EXPECT_EQ(current_size, lru_->Size()); | 384 EXPECT_EQ(current_size, lru_->Size()); |
385 lru_->Prune(3); | 385 lru_->Prune(3); |
386 current_size -= 3; | 386 current_size -= 3; |
387 EXPECT_EQ(current_size, lru_->Size()); | 387 EXPECT_EQ(current_size, lru_->Size()); |
388 lru_->Prune(1000); | 388 lru_->Prune(1000); |
389 EXPECT_EQ(0, lru_->Size()); | 389 EXPECT_EQ(0, lru_->Size()); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 multibuffer_.CheckLRUState(); | 508 multibuffer_.CheckLRUState(); |
509 } | 509 } |
510 multibuffer_.CheckPresentState(); | 510 multibuffer_.CheckPresentState(); |
511 while (!read_helpers.empty()) { | 511 while (!read_helpers.empty()) { |
512 delete read_helpers.back(); | 512 delete read_helpers.back(); |
513 read_helpers.pop_back(); | 513 read_helpers.pop_back(); |
514 } | 514 } |
515 } | 515 } |
516 | 516 |
517 } // namespace media | 517 } // namespace media |
OLD | NEW |