1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/cache_storage/cache_storage_scheduler.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace cache_storage_scheduler_unittest {
class TestTask {
public:
TestTask(CacheStorageScheduler* scheduler)
: scheduler_(scheduler),
id_(scheduler_->CreateId()),
callback_count_(0) {}
void Run() {
callback_count_++;
run_loop_.Quit();
}
void Done() { scheduler_->CompleteOperationAndRunNext(id_); }
int callback_count() const { return callback_count_; }
CacheStorageSchedulerId id() const { return id_; }
base::RunLoop& run_loop() { return run_loop_; }
protected:
raw_ptr<CacheStorageScheduler> scheduler_;
const CacheStorageSchedulerId id_;
base::RunLoop run_loop_;
int callback_count_;
};
class CacheStorageSchedulerTest : public testing::Test {
protected:
CacheStorageSchedulerTest()
: task_environment_(BrowserTaskEnvironment::IO_MAINLOOP),
task1_(&scheduler_),
task2_(&scheduler_),
task3_(&scheduler_) {}
BrowserTaskEnvironment task_environment_;
CacheStorageScheduler scheduler_{
CacheStorageSchedulerClient::kStorage,
base::SingleThreadTaskRunner::GetCurrentDefault()};
TestTask task1_;
TestTask task2_;
TestTask task3_;
};
TEST_F(CacheStorageSchedulerTest, ScheduleOne) {
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
// It's expected that the task will be executed synchronously.
EXPECT_TRUE(task1_.run_loop().AnyQuitCalled());
}
TEST_F(CacheStorageSchedulerTest, ScheduledOperations) {
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
EXPECT_TRUE(scheduler_.ScheduledOperations());
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_TRUE(scheduler_.ScheduledOperations());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
task1_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
}
TEST_F(CacheStorageSchedulerTest, ScheduleTwoExclusive) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
// Should only run the first exclusive op.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
// Should run the second exclusive op after the first completes.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
}
TEST_F(CacheStorageSchedulerTest, ScheduleTwoShared) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
// Should run both shared ops in paralle.
task1_.run_loop().Run();
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// Completing the first op should trigger a check for new ops
// which will not be present here.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// Completing the second op should result in the scheduler
// becoming idle.
task2_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
}
TEST_F(CacheStorageSchedulerTest, ScheduleOneExclusiveOneShared) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
// Should only run the first exclusive op.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
// Should run the second shared op after the first is completed.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
task2_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
}
TEST_F(CacheStorageSchedulerTest, ScheduleOneSharedOneExclusive) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
// Should only run the first shared op.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// Should run the second exclusive op after the first completes.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
task2_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
}
TEST_F(CacheStorageSchedulerTest, ScheduleTwoSharedOneExclusive) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
scheduler_.ScheduleOperation(
task3_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task3_)));
// Should run the two shared ops in parallel.
task1_.run_loop().Run();
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(0, task3_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// Completing the first shared op should not allow the exclusive op
// to run yet.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(0, task3_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// The third exclusive op should run after both the preceding shared ops
// complete.
task2_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task3_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(1, task3_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
task3_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
}
TEST_F(CacheStorageSchedulerTest, ScheduleOneExclusiveTwoShared) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
scheduler_.ScheduleOperation(
task3_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task3_)));
// Should only run the first exclusive op.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_EQ(0, task3_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
// Should run both the shared ops in parallel after the first exclusive
// op is completed.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
task3_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(1, task3_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
task2_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(1, task3_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
task3_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
}
TEST_F(CacheStorageSchedulerTest, ScheduleOneSharedOneExclusiveOneShared) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "3"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
scheduler_.ScheduleOperation(
task3_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task3_)));
// Should only run the first shared op.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_EQ(0, task3_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// Should run the exclusive op after the first op is completed.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(0, task3_.callback_count());
EXPECT_TRUE(scheduler_.IsRunningExclusiveOperation());
// Should run the last shared op after the preceding exclusive op
// is completed.
task2_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task3_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(1, task3_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
task3_.Done();
EXPECT_FALSE(scheduler_.ScheduledOperations());
}
TEST_F(CacheStorageSchedulerTest, ScheduleTwoSharedNotParallel) {
// Disable parallelism
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
kCacheStorageParallelOps, {{"max_shared_ops", "1"}});
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kShared,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
// Should only run one shared op since the max shared is set to 1.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
// Should run the next shared op after the first completes.
task1_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_FALSE(scheduler_.IsRunningExclusiveOperation());
}
TEST_F(CacheStorageSchedulerTest, ScheduleByPriorityTwoNormalOneHigh) {
scheduler_.ScheduleOperation(
task1_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task1_)));
scheduler_.ScheduleOperation(
task2_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kNormal,
base::BindOnce(&TestTask::Run, base::Unretained(&task2_)));
scheduler_.ScheduleOperation(
task3_.id(), CacheStorageSchedulerMode::kExclusive,
CacheStorageSchedulerOp::kTest, CacheStorageSchedulerPriority::kHigh,
base::BindOnce(&TestTask::Run, base::Unretained(&task3_)));
// Should run the first normal priority op because the queue was empty
// when it was added.
task1_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_EQ(0, task3_.callback_count());
// Should run the high priority op next.
task1_.Done();
task3_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(0, task2_.callback_count());
EXPECT_EQ(1, task3_.callback_count());
// Should run the final normal priority op after the high priority op
// completes.
task3_.Done();
EXPECT_TRUE(scheduler_.ScheduledOperations());
task2_.run_loop().Run();
EXPECT_EQ(1, task1_.callback_count());
EXPECT_EQ(1, task2_.callback_count());
EXPECT_EQ(1, task3_.callback_count());
}
// Regression test for crbug.com/370069678 --- not crashing under ASAN indicates
// success.
TEST_F(CacheStorageSchedulerTest, TaskDeletesScheduler) {
auto* scheduler = new CacheStorageScheduler(
CacheStorageSchedulerClient::kStorage,
base::SingleThreadTaskRunner::GetCurrentDefault());
scheduler->ScheduleOperation(
1, CacheStorageSchedulerMode::kExclusive, CacheStorageSchedulerOp::kTest,
CacheStorageSchedulerPriority::kNormal,
base::BindOnce([](CacheStorageScheduler* scheduler) { delete scheduler; },
scheduler));
}
} // namespace cache_storage_scheduler_unittest
} // namespace content
|