Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: net/base/prioritized_dispatcher_unittest.cc

Issue 22909037: [net/dns] Reland of 218616 (Simultaneous A/AAAA queries). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fix printing unsigned Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/base/prioritized_dispatcher.cc ('k') | net/base/priority_queue.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
154 std::string log_; 165 std::string log_;
155 scoped_ptr<PrioritizedDispatcher> dispatcher_; 166 scoped_ptr<PrioritizedDispatcher> dispatcher_;
156 ScopedVector<TestJob> jobs_; 167 ScopedVector<TestJob> jobs_;
157 }; 168 };
158 169
170 TEST_F(PrioritizedDispatcherTest, GetLimits) {
171 // Set non-trivial initial limits.
172 PrioritizedDispatcher::Limits original_limits(NUM_PRIORITIES, 5);
173 original_limits.reserved_slots[HIGHEST] = 1;
174 original_limits.reserved_slots[LOW] = 2;
175 Prepare(original_limits);
176
177 // Get current limits, make sure the original limits are returned.
178 PrioritizedDispatcher::Limits retrieved_limits = dispatcher_->GetLimits();
179 ASSERT_EQ(original_limits.total_jobs, retrieved_limits.total_jobs);
180 ASSERT_EQ(NUM_PRIORITIES, retrieved_limits.reserved_slots.size());
181 for (size_t priority = 0; priority < NUM_PRIORITIES; ++priority) {
182 EXPECT_EQ(original_limits.reserved_slots[priority],
183 retrieved_limits.reserved_slots[priority]);
184 }
185
186 // Set new limits.
187 PrioritizedDispatcher::Limits new_limits(NUM_PRIORITIES, 6);
188 new_limits.reserved_slots[MEDIUM] = 3;
189 new_limits.reserved_slots[LOWEST] = 1;
190 Prepare(new_limits);
191
192 // Get current limits, make sure the new limits are returned.
193 retrieved_limits = dispatcher_->GetLimits();
194 ASSERT_EQ(new_limits.total_jobs, retrieved_limits.total_jobs);
195 ASSERT_EQ(NUM_PRIORITIES, retrieved_limits.reserved_slots.size());
196 for (size_t priority = 0; priority < NUM_PRIORITIES; ++priority) {
197 EXPECT_EQ(new_limits.reserved_slots[priority],
198 retrieved_limits.reserved_slots[priority]);
199 }
200 }
201
159 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { 202 TEST_F(PrioritizedDispatcherTest, AddAFIFO) {
160 // Allow only one running job. 203 // Allow only one running job.
161 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 204 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
162 Prepare(limits); 205 Prepare(limits);
163 206
164 TestJob* job_a = AddJob('a', IDLE); 207 TestJob* job_a = AddJob('a', IDLE);
165 TestJob* job_b = AddJob('b', IDLE); 208 TestJob* job_b = AddJob('b', IDLE);
166 TestJob* job_c = AddJob('c', IDLE); 209 TestJob* job_c = AddJob('c', IDLE);
167 TestJob* job_d = AddJob('d', IDLE); 210 TestJob* job_d = AddJob('d', IDLE);
168 211
(...skipping 26 matching lines...) Expand all
195 ASSERT_TRUE(job_d->running()); 238 ASSERT_TRUE(job_d->running());
196 job_d->Finish(); 239 job_d->Finish();
197 ASSERT_TRUE(job_b->running()); 240 ASSERT_TRUE(job_b->running());
198 job_b->Finish(); 241 job_b->Finish();
199 ASSERT_TRUE(job_e->running()); 242 ASSERT_TRUE(job_e->running());
200 job_e->Finish(); 243 job_e->Finish();
201 244
202 Expect("a.c.d.b.e."); 245 Expect("a.c.d.b.e.");
203 } 246 }
204 247
248 TEST_F(PrioritizedDispatcherTest, AddAtHead) {
249 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
250 Prepare(limits);
251
252 TestJob* job_a = AddJob('a', MEDIUM);
253 TestJob* job_b = AddJobAtHead('b', MEDIUM);
254 TestJob* job_c = AddJobAtHead('c', HIGHEST);
255 TestJob* job_d = AddJobAtHead('d', HIGHEST);
256 TestJob* job_e = AddJobAtHead('e', MEDIUM);
257 TestJob* job_f = AddJob('f', MEDIUM);
258
259 ASSERT_TRUE(job_a->running());
260 job_a->Finish();
261 ASSERT_TRUE(job_d->running());
262 job_d->Finish();
263 ASSERT_TRUE(job_c->running());
264 job_c->Finish();
265 ASSERT_TRUE(job_e->running());
266 job_e->Finish();
267 ASSERT_TRUE(job_b->running());
268 job_b->Finish();
269 ASSERT_TRUE(job_f->running());
270 job_f->Finish();
271
272 Expect("a.d.c.e.b.f.");
273 }
274
205 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { 275 TEST_F(PrioritizedDispatcherTest, EnforceLimits) {
206 // Reserve 2 for HIGHEST and 1 for LOW or higher. 276 // Reserve 2 for HIGHEST and 1 for LOW or higher.
207 // This leaves 2 for LOWEST or lower. 277 // This leaves 2 for LOWEST or lower.
208 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); 278 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5);
209 limits.reserved_slots[HIGHEST] = 2; 279 limits.reserved_slots[HIGHEST] = 2;
210 limits.reserved_slots[LOW] = 1; 280 limits.reserved_slots[LOW] = 1;
211 Prepare(limits); 281 Prepare(limits);
212 282
213 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. 283 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot.
214 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. 284 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot.
(...skipping 23 matching lines...) Expand all
238 // h, e are running. 308 // h, e are running.
239 job_e->Finish(); // Releases c. 309 job_e->Finish(); // Releases c.
240 ASSERT_TRUE(job_c->running()); 310 ASSERT_TRUE(job_c->running());
241 job_c->Finish(); 311 job_c->Finish();
242 job_h->Finish(); 312 job_h->Finish();
243 313
244 Expect("abdfg.h...e..c.."); 314 Expect("abdfg.h...e..c..");
245 } 315 }
246 316
247 TEST_F(PrioritizedDispatcherTest, ChangePriority) { 317 TEST_F(PrioritizedDispatcherTest, ChangePriority) {
248 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 318 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2);
319 // Reserve one slot only for HIGHEST priority requests.
320 limits.reserved_slots[HIGHEST] = 1;
249 Prepare(limits); 321 Prepare(limits);
250 322
251 TestJob* job_a = AddJob('a', IDLE); 323 TestJob* job_a = AddJob('a', IDLE);
252 TestJob* job_b = AddJob('b', MEDIUM); 324 TestJob* job_b = AddJob('b', LOW);
253 TestJob* job_c = AddJob('c', HIGHEST); 325 TestJob* job_c = AddJob('c', MEDIUM);
254 TestJob* job_d = AddJob('d', HIGHEST); 326 TestJob* job_d = AddJob('d', MEDIUM);
327 TestJob* job_e = AddJob('e', IDLE);
255 328
256 ASSERT_FALSE(job_b->running()); 329 ASSERT_FALSE(job_b->running());
257 ASSERT_FALSE(job_c->running()); 330 ASSERT_FALSE(job_c->running());
258 job_b->ChangePriority(HIGHEST); 331 job_b->ChangePriority(MEDIUM);
259 job_c->ChangePriority(MEDIUM); 332 job_c->ChangePriority(LOW);
260 333
261 ASSERT_TRUE(job_a->running()); 334 ASSERT_TRUE(job_a->running());
262 job_a->Finish(); 335 job_a->Finish();
263 ASSERT_TRUE(job_d->running()); 336 ASSERT_TRUE(job_d->running());
264 job_d->Finish(); 337 job_d->Finish();
338
339 EXPECT_FALSE(job_e->running());
340 // Increasing |job_e|'s priority to HIGHEST should result in it being
341 // started immediately.
342 job_e->ChangePriority(HIGHEST);
343 ASSERT_TRUE(job_e->running());
344 job_e->Finish();
345
265 ASSERT_TRUE(job_b->running()); 346 ASSERT_TRUE(job_b->running());
266 job_b->Finish(); 347 job_b->Finish();
267 ASSERT_TRUE(job_c->running()); 348 ASSERT_TRUE(job_c->running());
268 job_c->Finish(); 349 job_c->Finish();
269 350
270 Expect("a.d.b.c."); 351 Expect("a.d.be..c.");
271 } 352 }
272 353
273 TEST_F(PrioritizedDispatcherTest, Cancel) { 354 TEST_F(PrioritizedDispatcherTest, Cancel) {
274 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 355 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
275 Prepare(limits); 356 Prepare(limits);
276 357
277 TestJob* job_a = AddJob('a', IDLE); 358 TestJob* job_a = AddJob('a', IDLE);
278 TestJob* job_b = AddJob('b', IDLE); 359 TestJob* job_b = AddJob('b', IDLE);
279 TestJob* job_c = AddJob('c', IDLE); 360 TestJob* job_c = AddJob('c', IDLE);
280 TestJob* job_d = AddJob('d', IDLE); 361 TestJob* job_d = AddJob('d', IDLE);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 398
318 Expect("a.c.e."); 399 Expect("a.c.e.");
319 } 400 }
320 401
321 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { 402 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) {
322 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 403 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
323 Prepare(limits); 404 Prepare(limits);
324 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); 405 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL);
325 } 406 }
326 407
408 TEST_F(PrioritizedDispatcherTest, AddWhileZeroLimits) {
409 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2);
410 Prepare(limits);
411
412 dispatcher_->SetLimitsToZero();
413 TestJob* job_a = AddJob('a', LOW);
414 TestJob* job_b = AddJob('b', MEDIUM);
415 TestJob* job_c = AddJobAtHead('c', MEDIUM);
416
417 EXPECT_EQ(0u, dispatcher_->num_running_jobs());
418 EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
419
420 dispatcher_->SetLimits(limits);
421 EXPECT_EQ(2u, dispatcher_->num_running_jobs());
422 EXPECT_EQ(1u, dispatcher_->num_queued_jobs());
423
424 ASSERT_TRUE(job_b->running());
425 job_b->Finish();
426
427 ASSERT_TRUE(job_c->running());
428 job_c->Finish();
429
430 ASSERT_TRUE(job_a->running());
431 job_a->Finish();
432
433 Expect("cb.a..");
434 }
435
436 TEST_F(PrioritizedDispatcherTest, ReduceLimitsWhileJobQueued) {
437 PrioritizedDispatcher::Limits initial_limits(NUM_PRIORITIES, 2);
438 Prepare(initial_limits);
439
440 TestJob* job_a = AddJob('a', MEDIUM);
441 TestJob* job_b = AddJob('b', MEDIUM);
442 TestJob* job_c = AddJob('c', MEDIUM);
443 TestJob* job_d = AddJob('d', MEDIUM);
444 TestJob* job_e = AddJob('e', MEDIUM);
445
446 EXPECT_EQ(2u, dispatcher_->num_running_jobs());
447 EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
448
449 // Reduce limits to just allow one job at a time. Running jobs should not
450 // be affected.
451 dispatcher_->SetLimits(PrioritizedDispatcher::Limits(NUM_PRIORITIES, 1));
452
453 EXPECT_EQ(2u, dispatcher_->num_running_jobs());
454 EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
455
456 // Finishing a job should not result in another job starting.
457 ASSERT_TRUE(job_a->running());
458 job_a->Finish();
459 EXPECT_EQ(1u, dispatcher_->num_running_jobs());
460 EXPECT_EQ(3u, dispatcher_->num_queued_jobs());
461
462 ASSERT_TRUE(job_b->running());
463 job_b->Finish();
464 EXPECT_EQ(1u, dispatcher_->num_running_jobs());
465 EXPECT_EQ(2u, dispatcher_->num_queued_jobs());
466
467 // Increasing the limits again should let c start.
468 dispatcher_->SetLimits(initial_limits);
469
470 ASSERT_TRUE(job_c->running());
471 job_c->Finish();
472 ASSERT_TRUE(job_d->running());
473 job_d->Finish();
474 ASSERT_TRUE(job_e->running());
475 job_e->Finish();
476
477 Expect("ab..cd.e..");
478 }
479
480 TEST_F(PrioritizedDispatcherTest, ZeroLimitsThenCancel) {
481 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
482 Prepare(limits);
483
484 TestJob* job_a = AddJob('a', IDLE);
485 TestJob* job_b = AddJob('b', IDLE);
486 TestJob* job_c = AddJob('c', IDLE);
487 dispatcher_->SetLimitsToZero();
488
489 ASSERT_TRUE(job_a->running());
490 EXPECT_FALSE(job_b->running());
491 EXPECT_FALSE(job_c->running());
492 job_a->Finish();
493
494 EXPECT_FALSE(job_b->running());
495 EXPECT_FALSE(job_c->running());
496
497 // Cancelling b shouldn't start job c.
498 job_b->Cancel();
499 EXPECT_FALSE(job_c->running());
500
501 // Restoring the limits should start c.
502 dispatcher_->SetLimits(limits);
503 ASSERT_TRUE(job_c->running());
504 job_c->Finish();
505
506 Expect("a.c.");
507 }
508
509 TEST_F(PrioritizedDispatcherTest, ZeroLimitsThenIncreasePriority) {
510 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 2);
511 limits.reserved_slots[HIGHEST] = 1;
512 Prepare(limits);
513
514 TestJob* job_a = AddJob('a', IDLE);
515 TestJob* job_b = AddJob('b', IDLE);
516 EXPECT_TRUE(job_a->running());
517 EXPECT_FALSE(job_b->running());
518 dispatcher_->SetLimitsToZero();
519
520 job_b->ChangePriority(HIGHEST);
521 EXPECT_FALSE(job_b->running());
522 job_a->Finish();
523 EXPECT_FALSE(job_b->running());
524
525 job_b->Cancel();
526 Expect("a.");
527 }
528
327 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) 529 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
328 TEST_F(PrioritizedDispatcherTest, CancelNull) { 530 TEST_F(PrioritizedDispatcherTest, CancelNull) {
329 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 531 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
330 Prepare(limits); 532 Prepare(limits);
331 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), ""); 533 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), "");
332 } 534 }
333 535
334 TEST_F(PrioritizedDispatcherTest, CancelMissing) { 536 TEST_F(PrioritizedDispatcherTest, CancelMissing) {
335 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); 537 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
336 Prepare(limits); 538 Prepare(limits);
(...skipping 19 matching lines...) Expand all
356 Prepare(limits); 558 Prepare(limits);
357 AddJob('a', IDLE); 559 AddJob('a', IDLE);
358 AddJob('b', IDLE); 560 AddJob('b', IDLE);
359 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); 561 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), "");
360 } 562 }
361 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) 563 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
362 564
363 } // namespace 565 } // namespace
364 566
365 } // namespace net 567 } // namespace net
OLDNEW
« no previous file with comments | « net/base/prioritized_dispatcher.cc ('k') | net/base/priority_queue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698