| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <ctype.h> | 5 #include <ctype.h> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 log_(log) {} | 45 log_(log) {} |
| 46 | 46 |
| 47 bool running() const { | 47 bool running() const { |
| 48 return running_; | 48 return running_; |
| 49 } | 49 } |
| 50 | 50 |
| 51 const PrioritizedDispatcher::Handle handle() const { | 51 const PrioritizedDispatcher::Handle handle() const { |
| 52 return handle_; | 52 return handle_; |
| 53 } | 53 } |
| 54 | 54 |
| 55 void Add(bool at_head) { | 55 void Add() { |
| 56 CHECK(handle_.is_null()); | 56 CHECK(handle_.is_null()); |
| 57 CHECK(!running_); | 57 CHECK(!running_); |
| 58 size_t num_queued = dispatcher_->num_queued_jobs(); | 58 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 59 size_t num_running = dispatcher_->num_running_jobs(); | 59 size_t num_running = dispatcher_->num_running_jobs(); |
| 60 | 60 |
| 61 if (!at_head) { | 61 handle_ = dispatcher_->Add(this, priority_); |
| 62 handle_ = dispatcher_->Add(this, priority_); | |
| 63 } else { | |
| 64 handle_ = dispatcher_->AddAtHead(this, priority_); | |
| 65 } | |
| 66 | 62 |
| 67 if (handle_.is_null()) { | 63 if (handle_.is_null()) { |
| 68 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); | 64 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); |
| 69 EXPECT_TRUE(running_); | 65 EXPECT_TRUE(running_); |
| 70 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); | 66 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); |
| 71 } else { | 67 } else { |
| 72 EXPECT_FALSE(running_); | 68 EXPECT_FALSE(running_); |
| 73 EXPECT_EQ(priority_, handle_.priority()); | 69 EXPECT_EQ(priority_, handle_.priority()); |
| 74 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); | 70 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); |
| 75 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); | 71 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 }; | 133 }; |
| 138 | 134 |
| 139 protected: | 135 protected: |
| 140 void Prepare(const PrioritizedDispatcher::Limits& limits) { | 136 void Prepare(const PrioritizedDispatcher::Limits& limits) { |
| 141 dispatcher_.reset(new PrioritizedDispatcher(limits)); | 137 dispatcher_.reset(new PrioritizedDispatcher(limits)); |
| 142 } | 138 } |
| 143 | 139 |
| 144 TestJob* AddJob(char data, Priority priority) { | 140 TestJob* AddJob(char data, Priority priority) { |
| 145 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); | 141 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); |
| 146 jobs_.push_back(job); | 142 jobs_.push_back(job); |
| 147 job->Add(false); | 143 job->Add(); |
| 148 return job; | |
| 149 } | |
| 150 | |
| 151 TestJob* AddJobAtHead(char data, Priority priority) { | |
| 152 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); | |
| 153 jobs_.push_back(job); | |
| 154 job->Add(true); | |
| 155 return job; | 144 return job; |
| 156 } | 145 } |
| 157 | 146 |
| 158 void Expect(std::string log) { | 147 void Expect(std::string log) { |
| 159 EXPECT_EQ(0u, dispatcher_->num_queued_jobs()); | 148 EXPECT_EQ(0u, dispatcher_->num_queued_jobs()); |
| 160 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); | 149 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); |
| 161 EXPECT_EQ(log, log_); | 150 EXPECT_EQ(log, log_); |
| 162 log_.clear(); | 151 log_.clear(); |
| 163 } | 152 } |
| 164 | 153 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 ASSERT_TRUE(job_d->running()); | 195 ASSERT_TRUE(job_d->running()); |
| 207 job_d->Finish(); | 196 job_d->Finish(); |
| 208 ASSERT_TRUE(job_b->running()); | 197 ASSERT_TRUE(job_b->running()); |
| 209 job_b->Finish(); | 198 job_b->Finish(); |
| 210 ASSERT_TRUE(job_e->running()); | 199 ASSERT_TRUE(job_e->running()); |
| 211 job_e->Finish(); | 200 job_e->Finish(); |
| 212 | 201 |
| 213 Expect("a.c.d.b.e."); | 202 Expect("a.c.d.b.e."); |
| 214 } | 203 } |
| 215 | 204 |
| 216 TEST_F(PrioritizedDispatcherTest, AddAtHead) { | |
| 217 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | |
| 218 Prepare(limits); | |
| 219 | |
| 220 TestJob* job_a = AddJob('a', MEDIUM); | |
| 221 TestJob* job_b = AddJobAtHead('b', MEDIUM); | |
| 222 TestJob* job_c = AddJobAtHead('c', HIGHEST); | |
| 223 TestJob* job_d = AddJobAtHead('d', HIGHEST); | |
| 224 TestJob* job_e = AddJobAtHead('e', MEDIUM); | |
| 225 TestJob* job_f = AddJob('f', MEDIUM); | |
| 226 | |
| 227 ASSERT_TRUE(job_a->running()); | |
| 228 job_a->Finish(); | |
| 229 ASSERT_TRUE(job_d->running()); | |
| 230 job_d->Finish(); | |
| 231 ASSERT_TRUE(job_c->running()); | |
| 232 job_c->Finish(); | |
| 233 ASSERT_TRUE(job_e->running()); | |
| 234 job_e->Finish(); | |
| 235 ASSERT_TRUE(job_b->running()); | |
| 236 job_b->Finish(); | |
| 237 ASSERT_TRUE(job_f->running()); | |
| 238 job_f->Finish(); | |
| 239 | |
| 240 Expect("a.d.c.e.b.f."); | |
| 241 } | |
| 242 | |
| 243 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { | 205 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { |
| 244 // Reserve 2 for HIGHEST and 1 for LOW or higher. | 206 // Reserve 2 for HIGHEST and 1 for LOW or higher. |
| 245 // This leaves 2 for LOWEST or lower. | 207 // This leaves 2 for LOWEST or lower. |
| 246 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); | 208 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); |
| 247 limits.reserved_slots[HIGHEST] = 2; | 209 limits.reserved_slots[HIGHEST] = 2; |
| 248 limits.reserved_slots[LOW] = 1; | 210 limits.reserved_slots[LOW] = 1; |
| 249 Prepare(limits); | 211 Prepare(limits); |
| 250 | 212 |
| 251 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. | 213 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. |
| 252 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. | 214 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 276 // h, e are running. | 238 // h, e are running. |
| 277 job_e->Finish(); // Releases c. | 239 job_e->Finish(); // Releases c. |
| 278 ASSERT_TRUE(job_c->running()); | 240 ASSERT_TRUE(job_c->running()); |
| 279 job_c->Finish(); | 241 job_c->Finish(); |
| 280 job_h->Finish(); | 242 job_h->Finish(); |
| 281 | 243 |
| 282 Expect("abdfg.h...e..c.."); | 244 Expect("abdfg.h...e..c.."); |
| 283 } | 245 } |
| 284 | 246 |
| 285 TEST_F(PrioritizedDispatcherTest, ChangePriority) { | 247 TEST_F(PrioritizedDispatcherTest, ChangePriority) { |
| 286 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2); | 248 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 287 // Reserve one slot only for HIGHEST priority requests. | |
| 288 limits.reserved_slots[HIGHEST] = 1; | |
| 289 Prepare(limits); | 249 Prepare(limits); |
| 290 | 250 |
| 291 TestJob* job_a = AddJob('a', IDLE); | 251 TestJob* job_a = AddJob('a', IDLE); |
| 292 TestJob* job_b = AddJob('b', LOW); | 252 TestJob* job_b = AddJob('b', MEDIUM); |
| 293 TestJob* job_c = AddJob('c', MEDIUM); | 253 TestJob* job_c = AddJob('c', HIGHEST); |
| 294 TestJob* job_d = AddJob('d', MEDIUM); | 254 TestJob* job_d = AddJob('d', HIGHEST); |
| 295 TestJob* job_e = AddJob('e', IDLE); | |
| 296 | 255 |
| 297 ASSERT_FALSE(job_b->running()); | 256 ASSERT_FALSE(job_b->running()); |
| 298 ASSERT_FALSE(job_c->running()); | 257 ASSERT_FALSE(job_c->running()); |
| 299 job_b->ChangePriority(MEDIUM); | 258 job_b->ChangePriority(HIGHEST); |
| 300 job_c->ChangePriority(LOW); | 259 job_c->ChangePriority(MEDIUM); |
| 301 | 260 |
| 302 ASSERT_TRUE(job_a->running()); | 261 ASSERT_TRUE(job_a->running()); |
| 303 job_a->Finish(); | 262 job_a->Finish(); |
| 304 ASSERT_TRUE(job_d->running()); | 263 ASSERT_TRUE(job_d->running()); |
| 305 job_d->Finish(); | 264 job_d->Finish(); |
| 306 | |
| 307 EXPECT_FALSE(job_e->running()); | |
| 308 // Increasing |job_e|'s priority to HIGHEST should result in it being | |
| 309 // started immediately. | |
| 310 job_e->ChangePriority(HIGHEST); | |
| 311 ASSERT_TRUE(job_e->running()); | |
| 312 job_e->Finish(); | |
| 313 | |
| 314 ASSERT_TRUE(job_b->running()); | 265 ASSERT_TRUE(job_b->running()); |
| 315 job_b->Finish(); | 266 job_b->Finish(); |
| 316 ASSERT_TRUE(job_c->running()); | 267 ASSERT_TRUE(job_c->running()); |
| 317 job_c->Finish(); | 268 job_c->Finish(); |
| 318 | 269 |
| 319 Expect("a.d.be..c."); | 270 Expect("a.d.b.c."); |
| 320 } | 271 } |
| 321 | 272 |
| 322 TEST_F(PrioritizedDispatcherTest, Cancel) { | 273 TEST_F(PrioritizedDispatcherTest, Cancel) { |
| 323 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 274 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 324 Prepare(limits); | 275 Prepare(limits); |
| 325 | 276 |
| 326 TestJob* job_a = AddJob('a', IDLE); | 277 TestJob* job_a = AddJob('a', IDLE); |
| 327 TestJob* job_b = AddJob('b', IDLE); | 278 TestJob* job_b = AddJob('b', IDLE); |
| 328 TestJob* job_c = AddJob('c', IDLE); | 279 TestJob* job_c = AddJob('c', IDLE); |
| 329 TestJob* job_d = AddJob('d', IDLE); | 280 TestJob* job_d = AddJob('d', IDLE); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 | 317 |
| 367 Expect("a.c.e."); | 318 Expect("a.c.e."); |
| 368 } | 319 } |
| 369 | 320 |
| 370 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { | 321 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { |
| 371 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 322 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 372 Prepare(limits); | 323 Prepare(limits); |
| 373 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); | 324 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); |
| 374 } | 325 } |
| 375 | 326 |
| 376 TEST_F(PrioritizedDispatcherTest, AddWhileDisabled) { | |
| 377 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | |
| 378 Prepare(limits); | |
| 379 | |
| 380 dispatcher_->Disable(); | |
| 381 TestJob* job_a = AddJob('a', MEDIUM); | |
| 382 TestJob* job_b = AddJobAtHead('b', MEDIUM); | |
| 383 | |
| 384 EXPECT_FALSE(job_a->running()); | |
| 385 EXPECT_FALSE(job_b->running()); | |
| 386 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); | |
| 387 EXPECT_EQ(2u, dispatcher_->num_queued_jobs()); | |
| 388 } | |
| 389 | |
| 390 TEST_F(PrioritizedDispatcherTest, DisableThenCancel) { | |
| 391 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | |
| 392 Prepare(limits); | |
| 393 | |
| 394 TestJob* job_a = AddJob('a', IDLE); | |
| 395 TestJob* job_b = AddJob('b', IDLE); | |
| 396 TestJob* job_c = AddJob('c', IDLE); | |
| 397 dispatcher_->Disable(); | |
| 398 | |
| 399 EXPECT_TRUE(job_a->running()); | |
| 400 EXPECT_FALSE(job_b->running()); | |
| 401 EXPECT_FALSE(job_c->running()); | |
| 402 job_a->Finish(); | |
| 403 | |
| 404 EXPECT_FALSE(job_b->running()); | |
| 405 EXPECT_FALSE(job_c->running()); | |
| 406 | |
| 407 job_b->Cancel(); | |
| 408 EXPECT_FALSE(job_c->running()); | |
| 409 job_c->Cancel(); | |
| 410 | |
| 411 Expect("a."); | |
| 412 } | |
| 413 | |
| 414 TEST_F(PrioritizedDispatcherTest, DisableThenIncreatePriority) { | |
| 415 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2); | |
| 416 limits.reserved_slots[HIGHEST] = 1; | |
| 417 Prepare(limits); | |
| 418 | |
| 419 TestJob* job_a = AddJob('a', IDLE); | |
| 420 TestJob* job_b = AddJob('b', IDLE); | |
| 421 EXPECT_TRUE(job_a->running()); | |
| 422 EXPECT_FALSE(job_b->running()); | |
| 423 dispatcher_->Disable(); | |
| 424 | |
| 425 job_b->ChangePriority(HIGHEST); | |
| 426 EXPECT_FALSE(job_b->running()); | |
| 427 job_a->Finish(); | |
| 428 EXPECT_FALSE(job_b->running()); | |
| 429 | |
| 430 job_b->Cancel(); | |
| 431 Expect("a."); | |
| 432 } | |
| 433 | |
| 434 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 327 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 435 TEST_F(PrioritizedDispatcherTest, CancelNull) { | 328 TEST_F(PrioritizedDispatcherTest, CancelNull) { |
| 436 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 329 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 437 Prepare(limits); | 330 Prepare(limits); |
| 438 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), ""); | 331 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), ""); |
| 439 } | 332 } |
| 440 | 333 |
| 441 TEST_F(PrioritizedDispatcherTest, CancelMissing) { | 334 TEST_F(PrioritizedDispatcherTest, CancelMissing) { |
| 442 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 335 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 443 Prepare(limits); | 336 Prepare(limits); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 463 Prepare(limits); | 356 Prepare(limits); |
| 464 AddJob('a', IDLE); | 357 AddJob('a', IDLE); |
| 465 AddJob('b', IDLE); | 358 AddJob('b', IDLE); |
| 466 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); | 359 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); |
| 467 } | 360 } |
| 468 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 361 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 469 | 362 |
| 470 } // namespace | 363 } // namespace |
| 471 | 364 |
| 472 } // namespace net | 365 } // namespace net |
| OLD | NEW |