| 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 "content/browser/cache_storage/cache_storage_scheduler.h" | 5 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 class JobStats { | 17 class TestTask { |
| 17 public: | 18 public: |
| 18 JobStats(CacheStorageScheduler* scheduler) | 19 TestTask(CacheStorageScheduler* scheduler) |
| 19 : scheduler_(scheduler), callback_count_(0) {} | 20 : scheduler_(scheduler), callback_count_(0) {} |
| 20 | 21 |
| 21 virtual void Run() = 0; | 22 virtual void Run() { callback_count_++; } |
| 23 void Done() { scheduler_->CompleteOperationAndRunNext(); } |
| 22 | 24 |
| 23 int callback_count() const { return callback_count_; } | 25 int callback_count() const { return callback_count_; } |
| 24 | 26 |
| 25 protected: | 27 protected: |
| 26 CacheStorageScheduler* scheduler_; | 28 CacheStorageScheduler* scheduler_; |
| 27 int callback_count_; | 29 int callback_count_; |
| 28 }; | 30 }; |
| 29 | 31 |
| 30 class SyncJob : public JobStats { | |
| 31 public: | |
| 32 SyncJob(CacheStorageScheduler* scheduler) : JobStats(scheduler) {} | |
| 33 | |
| 34 void Run() override { | |
| 35 callback_count_++; | |
| 36 scheduler_->CompleteOperationAndRunNext(); | |
| 37 } | |
| 38 }; | |
| 39 | |
| 40 class AsyncJob : public JobStats { | |
| 41 public: | |
| 42 AsyncJob(CacheStorageScheduler* scheduler) : JobStats(scheduler) {} | |
| 43 | |
| 44 void Run() override { callback_count_++; } | |
| 45 void Done() { scheduler_->CompleteOperationAndRunNext(); } | |
| 46 }; | |
| 47 | |
| 48 } // namespace | 32 } // namespace |
| 49 | 33 |
| 50 class CacheStorageSchedulerTest : public testing::Test { | 34 class CacheStorageSchedulerTest : public testing::Test { |
| 51 protected: | 35 protected: |
| 52 CacheStorageSchedulerTest() | 36 CacheStorageSchedulerTest() |
| 53 : async_job_(AsyncJob(&scheduler_)), sync_job_(SyncJob(&scheduler_)) {} | 37 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 38 task1_(TestTask(&scheduler_)), |
| 39 task2_(TestTask(&scheduler_)) {} |
| 54 | 40 |
| 41 TestBrowserThreadBundle browser_thread_bundle_; |
| 55 CacheStorageScheduler scheduler_; | 42 CacheStorageScheduler scheduler_; |
| 56 AsyncJob async_job_; | 43 TestTask task1_; |
| 57 SyncJob sync_job_; | 44 TestTask task2_; |
| 58 }; | 45 }; |
| 59 | 46 |
| 60 TEST_F(CacheStorageSchedulerTest, ScheduleOne) { | 47 TEST_F(CacheStorageSchedulerTest, ScheduleOne) { |
| 61 scheduler_.ScheduleOperation( | 48 scheduler_.ScheduleOperation( |
| 62 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 49 base::Bind(&TestTask::Run, base::Unretained(&task1_))); |
| 63 EXPECT_EQ(1, sync_job_.callback_count()); | 50 base::RunLoop().RunUntilIdle(); |
| 51 EXPECT_EQ(1, task1_.callback_count()); |
| 64 } | 52 } |
| 65 | 53 |
| 66 TEST_F(CacheStorageSchedulerTest, ScheduleTwo) { | 54 TEST_F(CacheStorageSchedulerTest, ScheduleTwo) { |
| 67 scheduler_.ScheduleOperation( | 55 scheduler_.ScheduleOperation( |
| 68 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 56 base::Bind(&TestTask::Run, base::Unretained(&task1_))); |
| 69 scheduler_.ScheduleOperation( | 57 scheduler_.ScheduleOperation( |
| 70 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 58 base::Bind(&TestTask::Run, base::Unretained(&task2_))); |
| 71 EXPECT_EQ(2, sync_job_.callback_count()); | 59 base::RunLoop().RunUntilIdle(); |
| 60 EXPECT_EQ(1, task1_.callback_count()); |
| 61 EXPECT_EQ(0, task2_.callback_count()); |
| 62 |
| 63 task1_.Done(); |
| 64 EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 65 base::RunLoop().RunUntilIdle(); |
| 66 EXPECT_EQ(1, task1_.callback_count()); |
| 67 EXPECT_EQ(1, task2_.callback_count()); |
| 72 } | 68 } |
| 73 | 69 |
| 74 TEST_F(CacheStorageSchedulerTest, Block) { | 70 TEST_F(CacheStorageSchedulerTest, ScheduledOperations) { |
| 75 scheduler_.ScheduleOperation( | 71 scheduler_.ScheduleOperation( |
| 76 base::Bind(&JobStats::Run, base::Unretained(&async_job_))); | 72 base::Bind(&TestTask::Run, base::Unretained(&task1_))); |
| 77 EXPECT_EQ(1, async_job_.callback_count()); | 73 EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 78 scheduler_.ScheduleOperation( | 74 base::RunLoop().RunUntilIdle(); |
| 79 base::Bind(&JobStats::Run, base::Unretained(&sync_job_))); | 75 EXPECT_EQ(1, task1_.callback_count()); |
| 80 EXPECT_EQ(0, sync_job_.callback_count()); | 76 EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 81 async_job_.Done(); | 77 task1_.Done(); |
| 82 EXPECT_EQ(1, sync_job_.callback_count()); | 78 EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 83 } | 79 } |
| 84 | 80 |
| 85 } // namespace content | 81 } // namespace content |
| OLD | NEW |