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() { | 55 void Add(bool at_head) { |
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 handle_ = dispatcher_->Add(this, priority_); | 61 if (!at_head) { |
| 62 handle_ = dispatcher_->Add(this, priority_); |
| 63 } else { |
| 64 handle_ = dispatcher_->AddAtHead(this, priority_); |
| 65 } |
62 | 66 |
63 if (handle_.is_null()) { | 67 if (handle_.is_null()) { |
64 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); | 68 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); |
65 EXPECT_TRUE(running_); | 69 EXPECT_TRUE(running_); |
66 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); | 70 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); |
67 } else { | 71 } else { |
68 EXPECT_FALSE(running_); | 72 EXPECT_FALSE(running_); |
69 EXPECT_EQ(priority_, handle_.priority()); | 73 EXPECT_EQ(priority_, handle_.priority()); |
70 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); | 74 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); |
71 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); | 75 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 }; | 137 }; |
134 | 138 |
135 protected: | 139 protected: |
136 void Prepare(const PrioritizedDispatcher::Limits& limits) { | 140 void Prepare(const PrioritizedDispatcher::Limits& limits) { |
137 dispatcher_.reset(new PrioritizedDispatcher(limits)); | 141 dispatcher_.reset(new PrioritizedDispatcher(limits)); |
138 } | 142 } |
139 | 143 |
140 TestJob* AddJob(char data, Priority priority) { | 144 TestJob* AddJob(char data, Priority priority) { |
141 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); | 145 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); |
142 jobs_.push_back(job); | 146 jobs_.push_back(job); |
143 job->Add(); | 147 job->Add(false); |
| 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); |
144 return job; | 155 return job; |
145 } | 156 } |
146 | 157 |
147 void Expect(std::string log) { | 158 void Expect(std::string log) { |
148 EXPECT_EQ(0u, dispatcher_->num_queued_jobs()); | 159 EXPECT_EQ(0u, dispatcher_->num_queued_jobs()); |
149 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); | 160 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); |
150 EXPECT_EQ(log, log_); | 161 EXPECT_EQ(log, log_); |
151 log_.clear(); | 162 log_.clear(); |
152 } | 163 } |
153 | 164 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 ASSERT_TRUE(job_d->running()); | 206 ASSERT_TRUE(job_d->running()); |
196 job_d->Finish(); | 207 job_d->Finish(); |
197 ASSERT_TRUE(job_b->running()); | 208 ASSERT_TRUE(job_b->running()); |
198 job_b->Finish(); | 209 job_b->Finish(); |
199 ASSERT_TRUE(job_e->running()); | 210 ASSERT_TRUE(job_e->running()); |
200 job_e->Finish(); | 211 job_e->Finish(); |
201 | 212 |
202 Expect("a.c.d.b.e."); | 213 Expect("a.c.d.b.e."); |
203 } | 214 } |
204 | 215 |
| 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 |
205 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { | 243 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { |
206 // Reserve 2 for HIGHEST and 1 for LOW or higher. | 244 // Reserve 2 for HIGHEST and 1 for LOW or higher. |
207 // This leaves 2 for LOWEST or lower. | 245 // This leaves 2 for LOWEST or lower. |
208 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); | 246 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); |
209 limits.reserved_slots[HIGHEST] = 2; | 247 limits.reserved_slots[HIGHEST] = 2; |
210 limits.reserved_slots[LOW] = 1; | 248 limits.reserved_slots[LOW] = 1; |
211 Prepare(limits); | 249 Prepare(limits); |
212 | 250 |
213 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. | 251 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. |
214 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. | 252 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. |
(...skipping 23 matching lines...) Expand all Loading... |
238 // h, e are running. | 276 // h, e are running. |
239 job_e->Finish(); // Releases c. | 277 job_e->Finish(); // Releases c. |
240 ASSERT_TRUE(job_c->running()); | 278 ASSERT_TRUE(job_c->running()); |
241 job_c->Finish(); | 279 job_c->Finish(); |
242 job_h->Finish(); | 280 job_h->Finish(); |
243 | 281 |
244 Expect("abdfg.h...e..c.."); | 282 Expect("abdfg.h...e..c.."); |
245 } | 283 } |
246 | 284 |
247 TEST_F(PrioritizedDispatcherTest, ChangePriority) { | 285 TEST_F(PrioritizedDispatcherTest, ChangePriority) { |
248 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 286 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2); |
| 287 // Reserve one slot only for HIGHEST priority requests. |
| 288 limits.reserved_slots[HIGHEST] = 1; |
249 Prepare(limits); | 289 Prepare(limits); |
250 | 290 |
251 TestJob* job_a = AddJob('a', IDLE); | 291 TestJob* job_a = AddJob('a', IDLE); |
252 TestJob* job_b = AddJob('b', MEDIUM); | 292 TestJob* job_b = AddJob('b', LOW); |
253 TestJob* job_c = AddJob('c', HIGHEST); | 293 TestJob* job_c = AddJob('c', MEDIUM); |
254 TestJob* job_d = AddJob('d', HIGHEST); | 294 TestJob* job_d = AddJob('d', MEDIUM); |
| 295 TestJob* job_e = AddJob('e', IDLE); |
255 | 296 |
256 ASSERT_FALSE(job_b->running()); | 297 ASSERT_FALSE(job_b->running()); |
257 ASSERT_FALSE(job_c->running()); | 298 ASSERT_FALSE(job_c->running()); |
258 job_b->ChangePriority(HIGHEST); | 299 job_b->ChangePriority(MEDIUM); |
259 job_c->ChangePriority(MEDIUM); | 300 job_c->ChangePriority(LOW); |
260 | 301 |
261 ASSERT_TRUE(job_a->running()); | 302 ASSERT_TRUE(job_a->running()); |
262 job_a->Finish(); | 303 job_a->Finish(); |
263 ASSERT_TRUE(job_d->running()); | 304 ASSERT_TRUE(job_d->running()); |
264 job_d->Finish(); | 305 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 |
265 ASSERT_TRUE(job_b->running()); | 314 ASSERT_TRUE(job_b->running()); |
266 job_b->Finish(); | 315 job_b->Finish(); |
267 ASSERT_TRUE(job_c->running()); | 316 ASSERT_TRUE(job_c->running()); |
268 job_c->Finish(); | 317 job_c->Finish(); |
269 | 318 |
270 Expect("a.d.b.c."); | 319 Expect("a.d.be..c."); |
271 } | 320 } |
272 | 321 |
273 TEST_F(PrioritizedDispatcherTest, Cancel) { | 322 TEST_F(PrioritizedDispatcherTest, Cancel) { |
274 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 323 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
275 Prepare(limits); | 324 Prepare(limits); |
276 | 325 |
277 TestJob* job_a = AddJob('a', IDLE); | 326 TestJob* job_a = AddJob('a', IDLE); |
278 TestJob* job_b = AddJob('b', IDLE); | 327 TestJob* job_b = AddJob('b', IDLE); |
279 TestJob* job_c = AddJob('c', IDLE); | 328 TestJob* job_c = AddJob('c', IDLE); |
280 TestJob* job_d = AddJob('d', IDLE); | 329 TestJob* job_d = AddJob('d', IDLE); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 | 366 |
318 Expect("a.c.e."); | 367 Expect("a.c.e."); |
319 } | 368 } |
320 | 369 |
321 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { | 370 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { |
322 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 371 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
323 Prepare(limits); | 372 Prepare(limits); |
324 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); | 373 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); |
325 } | 374 } |
326 | 375 |
| 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 |
327 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 434 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
328 TEST_F(PrioritizedDispatcherTest, CancelNull) { | 435 TEST_F(PrioritizedDispatcherTest, CancelNull) { |
329 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 436 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
330 Prepare(limits); | 437 Prepare(limits); |
331 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), ""); | 438 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), ""); |
332 } | 439 } |
333 | 440 |
334 TEST_F(PrioritizedDispatcherTest, CancelMissing) { | 441 TEST_F(PrioritizedDispatcherTest, CancelMissing) { |
335 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 442 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
336 Prepare(limits); | 443 Prepare(limits); |
(...skipping 19 matching lines...) Expand all Loading... |
356 Prepare(limits); | 463 Prepare(limits); |
357 AddJob('a', IDLE); | 464 AddJob('a', IDLE); |
358 AddJob('b', IDLE); | 465 AddJob('b', IDLE); |
359 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); | 466 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); |
360 } | 467 } |
361 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 468 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
362 | 469 |
363 } // namespace | 470 } // namespace |
364 | 471 |
365 } // namespace net | 472 } // namespace net |
OLD | NEW |