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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/prioritized_dispatcher.h"
#include <memory>
#include <string>
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/test/gtest_util.h"
#include "net/base/request_priority.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
// We rely on the priority enum values being sequential having starting at 0,
// and increasing for higher priorities.
static_assert(MINIMUM_PRIORITY == 0u && MINIMUM_PRIORITY == THROTTLED &&
THROTTLED < IDLE &&
IDLE < LOWEST &&
LOWEST < HIGHEST &&
HIGHEST <= MAXIMUM_PRIORITY,
"priority indexes incompatible");
class PrioritizedDispatcherTest : public testing::Test {
public:
typedef PrioritizedDispatcher::Priority Priority;
// A job that appends |tag| to |log| when started and '.' when finished.
// This is intended to confirm the execution order of a sequence of jobs added
// to the dispatcher. Note that finishing order of jobs does not matter.
class TestJob : public PrioritizedDispatcher::Job {
public:
TestJob(PrioritizedDispatcher* dispatcher,
char tag,
Priority priority,
std::string* log)
: dispatcher_(dispatcher), tag_(tag), priority_(priority), log_(log) {}
bool running() const {
return running_;
}
const PrioritizedDispatcher::Handle handle() const {
return handle_;
}
void Add(bool at_head) {
CHECK(handle_.is_null());
CHECK(!running_);
size_t num_queued = dispatcher_->num_queued_jobs();
size_t num_running = dispatcher_->num_running_jobs();
if (!at_head) {
handle_ = dispatcher_->Add(this, priority_);
} else {
handle_ = dispatcher_->AddAtHead(this, priority_);
}
if (handle_.is_null()) {
EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs());
EXPECT_TRUE(running_);
EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs());
} else {
EXPECT_FALSE(running_);
EXPECT_EQ(priority_, handle_.priority());
EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_);
EXPECT_EQ(num_running, dispatcher_->num_running_jobs());
}
}
void ChangePriority(Priority priority) {
CHECK(!handle_.is_null());
CHECK(!running_);
size_t num_queued = dispatcher_->num_queued_jobs();
size_t num_running = dispatcher_->num_running_jobs();
handle_ = dispatcher_->ChangePriority(handle_, priority);
if (handle_.is_null()) {
EXPECT_TRUE(running_);
EXPECT_EQ(num_queued - 1, dispatcher_->num_queued_jobs());
EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs());
} else {
EXPECT_FALSE(running_);
EXPECT_EQ(priority, handle_.priority());
EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_);
EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs());
EXPECT_EQ(num_running, dispatcher_->num_running_jobs());
}
}
void Cancel() {
CHECK(!handle_.is_null());
CHECK(!running_);
size_t num_queued = dispatcher_->num_queued_jobs();
dispatcher_->Cancel(handle_);
EXPECT_EQ(num_queued - 1, dispatcher_->num_queued_jobs());
handle_ = PrioritizedDispatcher::Handle();
}
void Finish() {
CHECK(running_);
running_ = false;
log_->append(1u, '.');
dispatcher_->OnJobFinished();
}
// PrioritizedDispatcher::Job interface
void Start() override {
EXPECT_FALSE(running_);
handle_ = PrioritizedDispatcher::Handle();
running_ = true;
log_->append(1u, tag_);
}
private:
raw_ptr<PrioritizedDispatcher> dispatcher_;
char tag_;
Priority priority_;
PrioritizedDispatcher::Handle handle_;
bool running_ = false;
raw_ptr<std::string> log_;
};
protected:
void Prepare(const PrioritizedDispatcher::Limits& limits) {
dispatcher_ = std::make_unique<PrioritizedDispatcher>(limits);
}
std::unique_ptr<TestJob> AddJob(char data, Priority priority) {
auto job =
std::make_unique<TestJob>(dispatcher_.get(), data, priority, &log_);
job->Add(false);
return job;
}
std::unique_ptr<TestJob> AddJobAtHead(char data, Priority priority) {
auto job =
std::make_unique<TestJob>(dispatcher_.get(), data, priority, &log_);
job->Add(true);
return job;
}
void Expect(const std::string& log) {
EXPECT_EQ(0u, dispatcher_->num_queued_jobs());
EXPECT_EQ(0u, dispatcher_->num_running_jobs());
EXPECT_EQ(log, log_);
log_.clear();
}
std::string log_;
std::unique_ptr<PrioritizedDispatcher> dispatcher_;
};
TEST_F(PrioritizedDispatcherTest, GetLimits) {
// Set non-trivial initial limits.
PrioritizedDispatcher::Limits original_limits(NUM_PRIORITIES, 5);
original_limits.reserved_slots[HIGHEST] = 1;
original_limits.reserved_slots[LOW] = 2;
Prepare(original_limits);
// Get current limits, make sure the original limits are returned.
PrioritizedDispatcher::Limits retrieved_limits = dispatcher_->GetLimits();
ASSERT_EQ(original_limits.total_jobs, retrieved_limits.total_jobs);
ASSERT_EQ(static_cast<size_t>(NUM_PRIORITIES),
retrieved_limits.reserved_slots.size());
for (size_t priority = MINIMUM_PRIORITY; priority <= MAXIMUM_PRIORITY;
++priority) {
EXPECT_EQ(original_limits.reserved_slots[priority],
retrieved_limits.reserved_slots[priority]);
}
// Set new limits.
PrioritizedDispatcher::Limits new_limits(NUM_PRIORITIES, 6);
new_limits.reserved_slots[MEDIUM] = 3;
new_limits.reserved_slots[LOWEST] = 1;
Prepare(new_limits);
// Get current limits, make sure the new limits are returned.
retrieved_limits = dispatcher_->GetLimits();
ASSERT_EQ(new_limits.total_jobs, retrieved_limits.total_jobs);
ASSERT_EQ(static_cast<size_t>(NUM_PRIORITIES),
retrieved_limits.reserved_slots.size());
for (size_t priority = MINIMUM_PRIORITY; priority <= MAXIMUM_PRIORITY;
++priority) {
EXPECT_EQ(new_limits.reserved_slots[priority],
retrieved_limits.reserved_slots[priority]);
}
}
TEST_F(PrioritizedDispatcherTest, AddAFIFO) {
// Allow only one running job.
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', IDLE);
std::unique_ptr<TestJob> job_c = AddJob('c', IDLE);
std::unique_ptr<TestJob> job_d = AddJob('d', IDLE);
ASSERT_TRUE(job_a->running());
job_a->Finish();
ASSERT_TRUE(job_b->running());
job_b->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_d->running());
job_d->Finish();
Expect("a.b.c.d.");
}
TEST_F(PrioritizedDispatcherTest, AddPriority) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', MEDIUM);
std::unique_ptr<TestJob> job_c = AddJob('c', HIGHEST);
std::unique_ptr<TestJob> job_d = AddJob('d', HIGHEST);
std::unique_ptr<TestJob> job_e = AddJob('e', MEDIUM);
ASSERT_TRUE(job_a->running());
job_a->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_d->running());
job_d->Finish();
ASSERT_TRUE(job_b->running());
job_b->Finish();
ASSERT_TRUE(job_e->running());
job_e->Finish();
Expect("a.c.d.b.e.");
}
TEST_F(PrioritizedDispatcherTest, AddAtHead) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', MEDIUM);
std::unique_ptr<TestJob> job_b = AddJobAtHead('b', MEDIUM);
std::unique_ptr<TestJob> job_c = AddJobAtHead('c', HIGHEST);
std::unique_ptr<TestJob> job_d = AddJobAtHead('d', HIGHEST);
std::unique_ptr<TestJob> job_e = AddJobAtHead('e', MEDIUM);
std::unique_ptr<TestJob> job_f = AddJob('f', MEDIUM);
ASSERT_TRUE(job_a->running());
job_a->Finish();
ASSERT_TRUE(job_d->running());
job_d->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_e->running());
job_e->Finish();
ASSERT_TRUE(job_b->running());
job_b->Finish();
ASSERT_TRUE(job_f->running());
job_f->Finish();
Expect("a.d.c.e.b.f.");
}
TEST_F(PrioritizedDispatcherTest, EnforceLimits) {
// Reserve 2 for HIGHEST and 1 for LOW or higher.
// This leaves 2 for LOWEST or lower.
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5);
limits.reserved_slots[HIGHEST] = 2;
limits.reserved_slots[LOW] = 1;
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE); // Uses unreserved slot.
std::unique_ptr<TestJob> job_b = AddJob('b', IDLE); // Uses unreserved slot.
std::unique_ptr<TestJob> job_c = AddJob('c', LOWEST); // Must wait.
std::unique_ptr<TestJob> job_d = AddJob('d', LOW); // Uses reserved slot.
std::unique_ptr<TestJob> job_e = AddJob('e', MEDIUM); // Must wait.
std::unique_ptr<TestJob> job_f = AddJob('f', HIGHEST); // Uses reserved slot.
std::unique_ptr<TestJob> job_g = AddJob('g', HIGHEST); // Uses reserved slot.
std::unique_ptr<TestJob> job_h = AddJob('h', HIGHEST); // Must wait.
EXPECT_EQ(5u, dispatcher_->num_running_jobs());
EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
ASSERT_TRUE(job_a->running());
ASSERT_TRUE(job_b->running());
ASSERT_TRUE(job_d->running());
ASSERT_TRUE(job_f->running());
ASSERT_TRUE(job_g->running());
// a, b, d, f, g are running. Finish them in any order.
job_b->Finish(); // Releases h.
job_f->Finish();
job_a->Finish();
job_g->Finish(); // Releases e.
job_d->Finish();
ASSERT_TRUE(job_e->running());
ASSERT_TRUE(job_h->running());
// h, e are running.
job_e->Finish(); // Releases c.
ASSERT_TRUE(job_c->running());
job_c->Finish();
job_h->Finish();
Expect("abdfg.h...e..c..");
}
TEST_F(PrioritizedDispatcherTest, ChangePriority) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2);
// Reserve one slot only for HIGHEST priority requests.
limits.reserved_slots[HIGHEST] = 1;
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', LOW);
std::unique_ptr<TestJob> job_c = AddJob('c', MEDIUM);
std::unique_ptr<TestJob> job_d = AddJob('d', MEDIUM);
std::unique_ptr<TestJob> job_e = AddJob('e', IDLE);
ASSERT_FALSE(job_b->running());
ASSERT_FALSE(job_c->running());
job_b->ChangePriority(MEDIUM);
job_c->ChangePriority(LOW);
ASSERT_TRUE(job_a->running());
job_a->Finish();
ASSERT_TRUE(job_d->running());
job_d->Finish();
EXPECT_FALSE(job_e->running());
// Increasing |job_e|'s priority to HIGHEST should result in it being
// started immediately.
job_e->ChangePriority(HIGHEST);
ASSERT_TRUE(job_e->running());
job_e->Finish();
ASSERT_TRUE(job_b->running());
job_b->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
Expect("a.d.be..c.");
}
TEST_F(PrioritizedDispatcherTest, Cancel) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', IDLE);
std::unique_ptr<TestJob> job_c = AddJob('c', IDLE);
std::unique_ptr<TestJob> job_d = AddJob('d', IDLE);
std::unique_ptr<TestJob> job_e = AddJob('e', IDLE);
ASSERT_FALSE(job_b->running());
ASSERT_FALSE(job_d->running());
job_b->Cancel();
job_d->Cancel();
ASSERT_TRUE(job_a->running());
job_a->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_e->running());
job_e->Finish();
Expect("a.c.e.");
}
TEST_F(PrioritizedDispatcherTest, Evict) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', LOW);
std::unique_ptr<TestJob> job_c = AddJob('c', HIGHEST);
std::unique_ptr<TestJob> job_d = AddJob('d', LOW);
std::unique_ptr<TestJob> job_e = AddJob('e', HIGHEST);
EXPECT_EQ(job_b.get(), dispatcher_->EvictOldestLowest());
EXPECT_EQ(job_d.get(), dispatcher_->EvictOldestLowest());
ASSERT_TRUE(job_a->running());
job_a->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_e->running());
job_e->Finish();
Expect("a.c.e.");
}
TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
EXPECT_TRUE(dispatcher_->EvictOldestLowest() == nullptr);
}
TEST_F(PrioritizedDispatcherTest, AddWhileZeroLimits) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2);
Prepare(limits);
dispatcher_->SetLimitsToZero();
std::unique_ptr<TestJob> job_a = AddJob('a', LOW);
std::unique_ptr<TestJob> job_b = AddJob('b', MEDIUM);
std::unique_ptr<TestJob> job_c = AddJobAtHead('c', MEDIUM);
EXPECT_EQ(0u, dispatcher_->num_running_jobs());
EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
dispatcher_->SetLimits(limits);
EXPECT_EQ(2u, dispatcher_->num_running_jobs());
EXPECT_EQ(1u, dispatcher_->num_queued_jobs());
ASSERT_TRUE(job_b->running());
job_b->Finish();
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_a->running());
job_a->Finish();
Expect("cb.a..");
}
TEST_F(PrioritizedDispatcherTest, ReduceLimitsWhileJobQueued) {
PrioritizedDispatcher::Limits initial_limits(NUM_PRIORITIES, 2);
Prepare(initial_limits);
std::unique_ptr<TestJob> job_a = AddJob('a', MEDIUM);
std::unique_ptr<TestJob> job_b = AddJob('b', MEDIUM);
std::unique_ptr<TestJob> job_c = AddJob('c', MEDIUM);
std::unique_ptr<TestJob> job_d = AddJob('d', MEDIUM);
std::unique_ptr<TestJob> job_e = AddJob('e', MEDIUM);
EXPECT_EQ(2u, dispatcher_->num_running_jobs());
EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
// Reduce limits to just allow one job at a time. Running jobs should not
// be affected.
dispatcher_->SetLimits(PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1));
EXPECT_EQ(2u, dispatcher_->num_running_jobs());
EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
// Finishing a job should not result in another job starting.
ASSERT_TRUE(job_a->running());
job_a->Finish();
EXPECT_EQ(1u, dispatcher_->num_running_jobs());
EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
ASSERT_TRUE(job_b->running());
job_b->Finish();
EXPECT_EQ(1u, dispatcher_->num_running_jobs());
EXPECT_EQ(2u, dispatcher_->num_queued_jobs());
// Increasing the limits again should let c start.
dispatcher_->SetLimits(initial_limits);
ASSERT_TRUE(job_c->running());
job_c->Finish();
ASSERT_TRUE(job_d->running());
job_d->Finish();
ASSERT_TRUE(job_e->running());
job_e->Finish();
Expect("ab..cd.e..");
}
TEST_F(PrioritizedDispatcherTest, ZeroLimitsThenCancel) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', IDLE);
std::unique_ptr<TestJob> job_c = AddJob('c', IDLE);
dispatcher_->SetLimitsToZero();
ASSERT_TRUE(job_a->running());
EXPECT_FALSE(job_b->running());
EXPECT_FALSE(job_c->running());
job_a->Finish();
EXPECT_FALSE(job_b->running());
EXPECT_FALSE(job_c->running());
// Cancelling b shouldn't start job c.
job_b->Cancel();
EXPECT_FALSE(job_c->running());
// Restoring the limits should start c.
dispatcher_->SetLimits(limits);
ASSERT_TRUE(job_c->running());
job_c->Finish();
Expect("a.c.");
}
TEST_F(PrioritizedDispatcherTest, ZeroLimitsThenIncreasePriority) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2);
limits.reserved_slots[HIGHEST] = 1;
Prepare(limits);
std::unique_ptr<TestJob> job_a = AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', IDLE);
EXPECT_TRUE(job_a->running());
EXPECT_FALSE(job_b->running());
dispatcher_->SetLimitsToZero();
job_b->ChangePriority(HIGHEST);
EXPECT_FALSE(job_b->running());
job_a->Finish();
EXPECT_FALSE(job_b->running());
job_b->Cancel();
Expect("a.");
}
#if GTEST_HAS_DEATH_TEST
TEST_F(PrioritizedDispatcherTest, CancelNull) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
EXPECT_DCHECK_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()));
}
TEST_F(PrioritizedDispatcherTest, CancelMissing) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
AddJob('a', IDLE);
std::unique_ptr<TestJob> job_b = AddJob('b', IDLE);
PrioritizedDispatcher::Handle handle = job_b->handle();
ASSERT_FALSE(handle.is_null());
dispatcher_->Cancel(handle);
EXPECT_DCHECK_DEATH(dispatcher_->Cancel(handle));
}
#endif // GTEST_HAS_DEATH_TEST
} // namespace
} // namespace net
|