Avi Drissman | 4e1b7bc | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jsbell | 279efb4 | 2015-03-31 17:02:46 | [diff] [blame] | 5 | #include "content/browser/cache_storage/cache_storage_scheduler.h" |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 6 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 7 | #include "base/functional/bind.h" |
| 8 | #include "base/functional/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 9 | #include "base/memory/raw_ptr.h" |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 10 | #include "base/run_loop.h" |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 11 | #include "base/task/single_thread_task_runner.h" |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 12 | #include "base/test/scoped_feature_list.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 13 | #include "content/public/test/browser_task_environment.h" |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace content { |
Daniel Bratell | 238244e | 2017-12-15 01:06:06 | [diff] [blame] | 17 | namespace cache_storage_scheduler_unittest { |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 18 | |
jkarlin | 9059304 | 2015-04-03 16:46:44 | [diff] [blame] | 19 | class TestTask { |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 20 | public: |
jkarlin | 9059304 | 2015-04-03 16:46:44 | [diff] [blame] | 21 | TestTask(CacheStorageScheduler* scheduler) |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 22 | : scheduler_(scheduler), |
| 23 | id_(scheduler_->CreateId()), |
| 24 | callback_count_(0) {} |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 25 | |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 26 | virtual void Run() { |
| 27 | callback_count_++; |
| 28 | run_loop_.Quit(); |
| 29 | } |
| 30 | void Done() { scheduler_->CompleteOperationAndRunNext(id_); } |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 31 | |
| 32 | int callback_count() const { return callback_count_; } |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 33 | CacheStorageSchedulerId id() const { return id_; } |
| 34 | base::RunLoop& run_loop() { return run_loop_; } |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 35 | |
| 36 | protected: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 37 | raw_ptr<CacheStorageScheduler> scheduler_; |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 38 | const CacheStorageSchedulerId id_; |
| 39 | base::RunLoop run_loop_; |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 40 | int callback_count_; |
| 41 | }; |
| 42 | |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 43 | class TestScheduler : public CacheStorageScheduler { |
| 44 | public: |
| 45 | TestScheduler() |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 46 | : CacheStorageScheduler( |
| 47 | CacheStorageSchedulerClient::kStorage, |
| 48 | base::SingleThreadTaskRunner::GetCurrentDefault()) {} |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 49 | |
| 50 | void SetDoneStartingClosure(base::OnceClosure done_closure) { |
| 51 | CHECK(!done_closure_); |
| 52 | done_closure_ = std::move(done_closure); |
| 53 | } |
| 54 | |
| 55 | protected: |
| 56 | void DoneStartingAvailableOperations() override { |
| 57 | if (done_closure_) { |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 58 | base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( |
| 59 | FROM_HERE, std::move(done_closure_)); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 60 | } |
| 61 | CacheStorageScheduler::DoneStartingAvailableOperations(); |
| 62 | } |
| 63 | |
| 64 | base::OnceClosure done_closure_; |
| 65 | }; |
| 66 | |
jsbell | 279efb4 | 2015-03-31 17:02:46 | [diff] [blame] | 67 | class CacheStorageSchedulerTest : public testing::Test { |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 68 | protected: |
jsbell | 279efb4 | 2015-03-31 17:02:46 | [diff] [blame] | 69 | CacheStorageSchedulerTest() |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 70 | : task_environment_(BrowserTaskEnvironment::IO_MAINLOOP), |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 71 | task1_(&scheduler_), |
| 72 | task2_(&scheduler_), |
| 73 | task3_(&scheduler_) {} |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 74 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 75 | BrowserTaskEnvironment task_environment_; |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 76 | TestScheduler scheduler_; |
jkarlin | 9059304 | 2015-04-03 16:46:44 | [diff] [blame] | 77 | TestTask task1_; |
| 78 | TestTask task2_; |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 79 | TestTask task3_; |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 80 | }; |
| 81 | |
jsbell | 279efb4 | 2015-03-31 17:02:46 | [diff] [blame] | 82 | TEST_F(CacheStorageSchedulerTest, ScheduleOne) { |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 83 | base::RunLoop done_loop; |
| 84 | scheduler_.SetDoneStartingClosure(done_loop.QuitClosure()); |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 85 | scheduler_.ScheduleOperation( |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 86 | task1_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 87 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
jsbell | f43e59e | 2017-06-21 21:12:25 | [diff] [blame] | 88 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 89 | task1_.run_loop().Run(); |
| 90 | done_loop.Run(); |
jkarlin | 9059304 | 2015-04-03 16:46:44 | [diff] [blame] | 91 | EXPECT_EQ(1, task1_.callback_count()); |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 92 | } |
| 93 | |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 94 | TEST_F(CacheStorageSchedulerTest, ScheduledOperations) { |
| 95 | base::RunLoop done_loop; |
| 96 | scheduler_.SetDoneStartingClosure(done_loop.QuitClosure()); |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 97 | scheduler_.ScheduleOperation( |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 98 | task1_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 99 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 100 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 101 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 102 | task1_.run_loop().Run(); |
| 103 | done_loop.Run(); |
| 104 | EXPECT_EQ(1, task1_.callback_count()); |
| 105 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 106 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 107 | task1_.Done(); |
| 108 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 109 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 110 | } |
| 111 | |
| 112 | TEST_F(CacheStorageSchedulerTest, ScheduleTwoExclusive) { |
| 113 | base::test::ScopedFeatureList scoped_feature_list; |
| 114 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 115 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 116 | |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 117 | scheduler_.ScheduleOperation( |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 118 | task1_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 119 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 120 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 121 | base::RunLoop done_loop1; |
| 122 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 123 | scheduler_.ScheduleOperation( |
| 124 | task2_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 125 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 126 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 127 | |
| 128 | // Should only run the first exclusive op. |
| 129 | task1_.run_loop().Run(); |
| 130 | done_loop1.Run(); |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 131 | EXPECT_EQ(1, task1_.callback_count()); |
| 132 | EXPECT_EQ(0, task2_.callback_count()); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 133 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 134 | |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 135 | base::RunLoop done_loop2; |
| 136 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 137 | |
| 138 | // Should run the second exclusive op after the first completes. |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 139 | task1_.Done(); |
| 140 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 141 | task2_.run_loop().Run(); |
| 142 | done_loop2.Run(); |
| 143 | EXPECT_EQ(1, task1_.callback_count()); |
| 144 | EXPECT_EQ(1, task2_.callback_count()); |
| 145 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(CacheStorageSchedulerTest, ScheduleTwoShared) { |
| 149 | base::test::ScopedFeatureList scoped_feature_list; |
| 150 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 151 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 152 | |
| 153 | scheduler_.ScheduleOperation( |
| 154 | task1_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 155 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 156 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 157 | base::RunLoop done_loop1; |
| 158 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 159 | scheduler_.ScheduleOperation( |
| 160 | task2_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 161 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 162 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 163 | |
| 164 | // Should run both shared ops in paralle. |
| 165 | task1_.run_loop().Run(); |
| 166 | task2_.run_loop().Run(); |
| 167 | done_loop1.Run(); |
| 168 | EXPECT_EQ(1, task1_.callback_count()); |
| 169 | EXPECT_EQ(1, task2_.callback_count()); |
| 170 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 171 | |
| 172 | base::RunLoop done_loop2; |
| 173 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 174 | |
| 175 | // Completing the first op should trigger a check for new ops |
| 176 | // which will not be present here. |
| 177 | task1_.Done(); |
| 178 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 179 | done_loop2.Run(); |
| 180 | EXPECT_EQ(1, task1_.callback_count()); |
| 181 | EXPECT_EQ(1, task2_.callback_count()); |
| 182 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 183 | |
| 184 | base::RunLoop done_loop3; |
| 185 | scheduler_.SetDoneStartingClosure(done_loop3.QuitClosure()); |
| 186 | |
| 187 | // Completing the second op should result in the scheduler |
| 188 | // becoming idle. |
| 189 | task2_.Done(); |
| 190 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 191 | done_loop3.Run(); |
Balazs Engedy | da57941 | 2019-08-06 14:16:56 | [diff] [blame] | 192 | EXPECT_EQ(1, task1_.callback_count()); |
| 193 | EXPECT_EQ(1, task2_.callback_count()); |
| 194 | } |
| 195 | |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 196 | TEST_F(CacheStorageSchedulerTest, ScheduleOneExclusiveOneShared) { |
| 197 | base::test::ScopedFeatureList scoped_feature_list; |
| 198 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 199 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 200 | |
Wez | 58282116 | 2019-07-31 18:36:03 | [diff] [blame] | 201 | scheduler_.ScheduleOperation( |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 202 | task1_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 203 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Wez | 58282116 | 2019-07-31 18:36:03 | [diff] [blame] | 204 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 205 | base::RunLoop done_loop1; |
| 206 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 207 | scheduler_.ScheduleOperation( |
| 208 | task2_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 209 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 210 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 211 | |
| 212 | // Should only run the first exclusive op. |
| 213 | task1_.run_loop().Run(); |
| 214 | done_loop1.Run(); |
Ben Kelly | 1d54425 | 2019-08-05 18:33:44 | [diff] [blame] | 215 | EXPECT_EQ(1, task1_.callback_count()); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 216 | EXPECT_EQ(0, task2_.callback_count()); |
| 217 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 218 | |
| 219 | base::RunLoop done_loop2; |
| 220 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 221 | |
| 222 | // Should run the second shared op after the first is completed. |
Ben Kelly | 1d54425 | 2019-08-05 18:33:44 | [diff] [blame] | 223 | task1_.Done(); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 224 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 225 | task2_.run_loop().Run(); |
| 226 | done_loop2.Run(); |
| 227 | EXPECT_EQ(1, task1_.callback_count()); |
| 228 | EXPECT_EQ(1, task2_.callback_count()); |
| 229 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 230 | |
| 231 | task2_.Done(); |
Ben Kelly | 1d54425 | 2019-08-05 18:33:44 | [diff] [blame] | 232 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
Ben Kelly | 1d54425 | 2019-08-05 18:33:44 | [diff] [blame] | 233 | } |
| 234 | |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 235 | TEST_F(CacheStorageSchedulerTest, ScheduleOneSharedOneExclusive) { |
| 236 | base::test::ScopedFeatureList scoped_feature_list; |
| 237 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 238 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 239 | |
| 240 | scheduler_.ScheduleOperation( |
| 241 | task1_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 242 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 243 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 244 | base::RunLoop done_loop1; |
| 245 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 246 | scheduler_.ScheduleOperation( |
| 247 | task2_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 248 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 249 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 250 | |
| 251 | // Should only run the first shared op. |
| 252 | task1_.run_loop().Run(); |
| 253 | done_loop1.Run(); |
| 254 | EXPECT_EQ(1, task1_.callback_count()); |
| 255 | EXPECT_EQ(0, task2_.callback_count()); |
| 256 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 257 | |
| 258 | base::RunLoop done_loop2; |
| 259 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 260 | |
| 261 | // Should run the second exclusive op after the first completes. |
| 262 | task1_.Done(); |
| 263 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 264 | task2_.run_loop().Run(); |
| 265 | done_loop2.Run(); |
| 266 | EXPECT_EQ(1, task1_.callback_count()); |
| 267 | EXPECT_EQ(1, task2_.callback_count()); |
| 268 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 269 | |
| 270 | task2_.Done(); |
| 271 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 272 | } |
| 273 | |
| 274 | TEST_F(CacheStorageSchedulerTest, ScheduleTwoSharedOneExclusive) { |
| 275 | base::test::ScopedFeatureList scoped_feature_list; |
| 276 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 277 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 278 | |
| 279 | scheduler_.ScheduleOperation( |
| 280 | task1_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 281 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 282 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 283 | scheduler_.ScheduleOperation( |
| 284 | task2_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 285 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 286 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 287 | base::RunLoop done_loop1; |
| 288 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 289 | scheduler_.ScheduleOperation( |
| 290 | task3_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 291 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 292 | base::BindOnce(&TestTask::Run, base::Unretained(&task3_))); |
| 293 | |
| 294 | // Should run the two shared ops in parallel. |
| 295 | task1_.run_loop().Run(); |
| 296 | task2_.run_loop().Run(); |
| 297 | done_loop1.Run(); |
| 298 | EXPECT_EQ(1, task1_.callback_count()); |
| 299 | EXPECT_EQ(1, task2_.callback_count()); |
| 300 | EXPECT_EQ(0, task3_.callback_count()); |
| 301 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 302 | |
| 303 | base::RunLoop done_loop2; |
| 304 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 305 | |
| 306 | // Completing the first shared op should not allow the exclusive op |
| 307 | // to run yet. |
| 308 | task1_.Done(); |
| 309 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 310 | done_loop2.Run(); |
| 311 | EXPECT_EQ(1, task1_.callback_count()); |
| 312 | EXPECT_EQ(1, task2_.callback_count()); |
| 313 | EXPECT_EQ(0, task3_.callback_count()); |
| 314 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 315 | |
| 316 | base::RunLoop done_loop3; |
| 317 | scheduler_.SetDoneStartingClosure(done_loop3.QuitClosure()); |
| 318 | |
| 319 | // The third exclusive op should run after both the preceding shared ops |
| 320 | // complete. |
| 321 | task2_.Done(); |
| 322 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 323 | task3_.run_loop().Run(); |
| 324 | done_loop3.Run(); |
| 325 | EXPECT_EQ(1, task1_.callback_count()); |
| 326 | EXPECT_EQ(1, task2_.callback_count()); |
| 327 | EXPECT_EQ(1, task3_.callback_count()); |
| 328 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 329 | |
| 330 | task3_.Done(); |
| 331 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 332 | } |
| 333 | |
| 334 | TEST_F(CacheStorageSchedulerTest, ScheduleOneExclusiveTwoShared) { |
| 335 | base::test::ScopedFeatureList scoped_feature_list; |
| 336 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 337 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 338 | |
| 339 | scheduler_.ScheduleOperation( |
| 340 | task1_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 341 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 342 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 343 | scheduler_.ScheduleOperation( |
| 344 | task2_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 345 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 346 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 347 | base::RunLoop done_loop1; |
| 348 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 349 | scheduler_.ScheduleOperation( |
| 350 | task3_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 351 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 352 | base::BindOnce(&TestTask::Run, base::Unretained(&task3_))); |
| 353 | |
| 354 | // Should only run the first exclusive op. |
| 355 | task1_.run_loop().Run(); |
| 356 | done_loop1.Run(); |
| 357 | EXPECT_EQ(1, task1_.callback_count()); |
| 358 | EXPECT_EQ(0, task2_.callback_count()); |
| 359 | EXPECT_EQ(0, task3_.callback_count()); |
| 360 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 361 | |
| 362 | base::RunLoop done_loop2; |
| 363 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 364 | |
| 365 | // Should run both the shared ops in parallel after the first exclusive |
| 366 | // op is completed. |
| 367 | task1_.Done(); |
| 368 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 369 | task2_.run_loop().Run(); |
| 370 | task3_.run_loop().Run(); |
| 371 | done_loop2.Run(); |
| 372 | EXPECT_EQ(1, task1_.callback_count()); |
| 373 | EXPECT_EQ(1, task2_.callback_count()); |
| 374 | EXPECT_EQ(1, task3_.callback_count()); |
| 375 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 376 | |
| 377 | base::RunLoop done_loop3; |
| 378 | scheduler_.SetDoneStartingClosure(done_loop3.QuitClosure()); |
| 379 | |
| 380 | task2_.Done(); |
| 381 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 382 | done_loop3.Run(); |
| 383 | EXPECT_EQ(1, task1_.callback_count()); |
| 384 | EXPECT_EQ(1, task2_.callback_count()); |
| 385 | EXPECT_EQ(1, task3_.callback_count()); |
| 386 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 387 | |
| 388 | task3_.Done(); |
| 389 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 390 | } |
| 391 | |
| 392 | TEST_F(CacheStorageSchedulerTest, ScheduleOneSharedOneExclusiveOneShared) { |
| 393 | base::test::ScopedFeatureList scoped_feature_list; |
| 394 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 395 | kCacheStorageParallelOps, {{"max_shared_ops", "3"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 396 | |
| 397 | scheduler_.ScheduleOperation( |
| 398 | task1_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 399 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 400 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 401 | scheduler_.ScheduleOperation( |
| 402 | task2_.id(), CacheStorageSchedulerMode::kExclusive, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 403 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 404 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 405 | base::RunLoop done_loop1; |
| 406 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 407 | scheduler_.ScheduleOperation( |
| 408 | task3_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 409 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 410 | base::BindOnce(&TestTask::Run, base::Unretained(&task3_))); |
| 411 | |
| 412 | // Should only run the first shared op. |
| 413 | task1_.run_loop().Run(); |
| 414 | done_loop1.Run(); |
| 415 | EXPECT_EQ(1, task1_.callback_count()); |
| 416 | EXPECT_EQ(0, task2_.callback_count()); |
| 417 | EXPECT_EQ(0, task3_.callback_count()); |
| 418 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 419 | |
| 420 | base::RunLoop done_loop2; |
| 421 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 422 | |
| 423 | // Should run the exclusive op after the first op is completed. |
| 424 | task1_.Done(); |
| 425 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 426 | task2_.run_loop().Run(); |
| 427 | done_loop2.Run(); |
| 428 | EXPECT_EQ(1, task1_.callback_count()); |
| 429 | EXPECT_EQ(1, task2_.callback_count()); |
| 430 | EXPECT_EQ(0, task3_.callback_count()); |
| 431 | EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation()); |
| 432 | |
| 433 | base::RunLoop done_loop3; |
| 434 | scheduler_.SetDoneStartingClosure(done_loop3.QuitClosure()); |
| 435 | |
| 436 | // Should run the last shared op after the preceding exclusive op |
| 437 | // is completed. |
| 438 | task2_.Done(); |
| 439 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 440 | task3_.run_loop().Run(); |
| 441 | done_loop3.Run(); |
| 442 | EXPECT_EQ(1, task1_.callback_count()); |
| 443 | EXPECT_EQ(1, task2_.callback_count()); |
| 444 | EXPECT_EQ(1, task3_.callback_count()); |
| 445 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 446 | |
| 447 | task3_.Done(); |
| 448 | EXPECT_FALSE(scheduler_.ScheduledOperations()); |
| 449 | } |
| 450 | |
| 451 | TEST_F(CacheStorageSchedulerTest, ScheduleTwoSharedNotParallel) { |
| 452 | // Disable parallelism |
| 453 | base::test::ScopedFeatureList scoped_feature_list; |
| 454 | scoped_feature_list.InitAndEnableFeatureWithParameters( |
Adrienne Walker | 12b7612 | 2021-01-28 02:49:57 | [diff] [blame] | 455 | kCacheStorageParallelOps, {{"max_shared_ops", "1"}}); |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 456 | |
| 457 | scheduler_.ScheduleOperation( |
| 458 | task1_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 459 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 460 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 461 | base::RunLoop done_loop1; |
| 462 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 463 | scheduler_.ScheduleOperation( |
| 464 | task2_.id(), CacheStorageSchedulerMode::kShared, |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 465 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
Ben Kelly | a984642 | 2019-08-06 15:57:50 | [diff] [blame] | 466 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 467 | |
| 468 | // Should only run one shared op since the max shared is set to 1. |
| 469 | task1_.run_loop().Run(); |
| 470 | done_loop1.Run(); |
| 471 | EXPECT_EQ(1, task1_.callback_count()); |
| 472 | EXPECT_EQ(0, task2_.callback_count()); |
| 473 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 474 | |
| 475 | base::RunLoop done_loop2; |
| 476 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 477 | |
| 478 | // Should run the next shared op after the first completes. |
| 479 | task1_.Done(); |
| 480 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 481 | task2_.run_loop().Run(); |
| 482 | done_loop2.Run(); |
| 483 | EXPECT_EQ(1, task1_.callback_count()); |
| 484 | EXPECT_EQ(1, task2_.callback_count()); |
| 485 | EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation()); |
| 486 | } |
| 487 | |
Ben Kelly | ab28fec6 | 2019-11-04 16:21:41 | [diff] [blame] | 488 | TEST_F(CacheStorageSchedulerTest, ScheduleByPriorityTwoNormalOneHigh) { |
| 489 | scheduler_.ScheduleOperation( |
| 490 | task1_.id(), CacheStorageSchedulerMode::kExclusive, |
| 491 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
| 492 | base::BindOnce(&TestTask::Run, base::Unretained(&task1_))); |
| 493 | base::RunLoop done_loop1; |
| 494 | scheduler_.SetDoneStartingClosure(done_loop1.QuitClosure()); |
| 495 | scheduler_.ScheduleOperation( |
| 496 | task2_.id(), CacheStorageSchedulerMode::kExclusive, |
| 497 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal, |
| 498 | base::BindOnce(&TestTask::Run, base::Unretained(&task2_))); |
| 499 | scheduler_.ScheduleOperation( |
| 500 | task3_.id(), CacheStorageSchedulerMode::kExclusive, |
| 501 | CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kHigh, |
| 502 | base::BindOnce(&TestTask::Run, base::Unretained(&task3_))); |
| 503 | |
| 504 | // Should run the first normal priority op because the queue was empty |
| 505 | // when it was added. |
| 506 | task1_.run_loop().Run(); |
| 507 | done_loop1.Run(); |
| 508 | EXPECT_EQ(1, task1_.callback_count()); |
| 509 | EXPECT_EQ(0, task2_.callback_count()); |
| 510 | EXPECT_EQ(0, task3_.callback_count()); |
| 511 | |
| 512 | base::RunLoop done_loop3; |
| 513 | scheduler_.SetDoneStartingClosure(done_loop3.QuitClosure()); |
| 514 | |
| 515 | // Should run the high priority op next. |
| 516 | task1_.Done(); |
| 517 | task3_.run_loop().Run(); |
| 518 | done_loop3.Run(); |
| 519 | EXPECT_EQ(1, task1_.callback_count()); |
| 520 | EXPECT_EQ(0, task2_.callback_count()); |
| 521 | EXPECT_EQ(1, task3_.callback_count()); |
| 522 | |
| 523 | base::RunLoop done_loop2; |
| 524 | scheduler_.SetDoneStartingClosure(done_loop2.QuitClosure()); |
| 525 | |
| 526 | // Should run the final normal priority op after the high priority op |
| 527 | // completes. |
| 528 | task3_.Done(); |
| 529 | EXPECT_TRUE(scheduler_.ScheduledOperations()); |
| 530 | task2_.run_loop().Run(); |
| 531 | done_loop2.Run(); |
| 532 | EXPECT_EQ(1, task1_.callback_count()); |
| 533 | EXPECT_EQ(1, task2_.callback_count()); |
| 534 | EXPECT_EQ(1, task3_.callback_count()); |
| 535 | } |
| 536 | |
Daniel Bratell | 238244e | 2017-12-15 01:06:06 | [diff] [blame] | 537 | } // namespace cache_storage_scheduler_unittest |
jkarlin | d1292e44 | 2015-02-06 13:54:43 | [diff] [blame] | 538 | } // namespace content |