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" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "net/base/prioritized_dispatcher.h" | 12 #include "net/base/prioritized_dispatcher.h" |
13 #include "net/base/request_priority.h" | 13 #include "net/base/request_priority.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // We rely on the priority enum values being sequential having starting at 0, | 20 // We rely on the priority enum values being sequential having starting at 0, |
21 // and increasing for higher priorities. | 21 // and increasing for higher priorities. |
22 COMPILE_ASSERT(MINIMUM_PRIORITY == 0u && | 22 COMPILE_ASSERT(MINIMUM_PRIORITY == 0u && |
23 MINIMUM_PRIORITY == IDLE && | 23 MINIMUM_PRIORITY == |
24 IDLE < LOWEST && | 24 IDLE&& IDLE < LOWEST&& LOWEST < HIGHEST&& HIGHEST <= |
25 LOWEST < HIGHEST && | 25 MAXIMUM_PRIORITY, |
26 HIGHEST <= MAXIMUM_PRIORITY, | |
27 priority_indexes_incompatible); | 26 priority_indexes_incompatible); |
28 | 27 |
29 class PrioritizedDispatcherTest : public testing::Test { | 28 class PrioritizedDispatcherTest : public testing::Test { |
30 public: | 29 public: |
31 typedef PrioritizedDispatcher::Priority Priority; | 30 typedef PrioritizedDispatcher::Priority Priority; |
32 // A job that appends |tag| to |log| when started and '.' when finished. | 31 // A job that appends |tag| to |log| when started and '.' when finished. |
33 // This is intended to confirm the execution order of a sequence of jobs added | 32 // This is intended to confirm the execution order of a sequence of jobs added |
34 // to the dispatcher. Note that finishing order of jobs does not matter. | 33 // to the dispatcher. Note that finishing order of jobs does not matter. |
35 class TestJob : public PrioritizedDispatcher::Job { | 34 class TestJob : public PrioritizedDispatcher::Job { |
36 public: | 35 public: |
37 TestJob(PrioritizedDispatcher* dispatcher, | 36 TestJob(PrioritizedDispatcher* dispatcher, |
38 char tag, | 37 char tag, |
39 Priority priority, | 38 Priority priority, |
40 std::string* log) | 39 std::string* log) |
41 : dispatcher_(dispatcher), | 40 : dispatcher_(dispatcher), |
42 tag_(tag), | 41 tag_(tag), |
43 priority_(priority), | 42 priority_(priority), |
44 running_(false), | 43 running_(false), |
45 log_(log) {} | 44 log_(log) {} |
46 | 45 |
47 bool running() const { | 46 bool running() const { return running_; } |
48 return running_; | |
49 } | |
50 | 47 |
51 const PrioritizedDispatcher::Handle handle() const { | 48 const PrioritizedDispatcher::Handle handle() const { return handle_; } |
52 return handle_; | |
53 } | |
54 | 49 |
55 void Add(bool at_head) { | 50 void Add(bool at_head) { |
56 CHECK(handle_.is_null()); | 51 CHECK(handle_.is_null()); |
57 CHECK(!running_); | 52 CHECK(!running_); |
58 size_t num_queued = dispatcher_->num_queued_jobs(); | 53 size_t num_queued = dispatcher_->num_queued_jobs(); |
59 size_t num_running = dispatcher_->num_running_jobs(); | 54 size_t num_running = dispatcher_->num_running_jobs(); |
60 | 55 |
61 if (!at_head) { | 56 if (!at_head) { |
62 handle_ = dispatcher_->Add(this, priority_); | 57 handle_ = dispatcher_->Add(this, priority_); |
63 } else { | 58 } else { |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 } | 270 } |
276 | 271 |
277 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { | 272 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { |
278 // Reserve 2 for HIGHEST and 1 for LOW or higher. | 273 // Reserve 2 for HIGHEST and 1 for LOW or higher. |
279 // This leaves 2 for LOWEST or lower. | 274 // This leaves 2 for LOWEST or lower. |
280 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); | 275 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); |
281 limits.reserved_slots[HIGHEST] = 2; | 276 limits.reserved_slots[HIGHEST] = 2; |
282 limits.reserved_slots[LOW] = 1; | 277 limits.reserved_slots[LOW] = 1; |
283 Prepare(limits); | 278 Prepare(limits); |
284 | 279 |
285 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. | 280 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. |
286 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. | 281 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. |
287 TestJob* job_c = AddJob('c', LOWEST); // Must wait. | 282 TestJob* job_c = AddJob('c', LOWEST); // Must wait. |
288 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. | 283 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. |
289 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. | 284 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. |
290 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. | 285 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. |
291 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. | 286 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. |
292 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. | 287 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. |
293 | 288 |
294 EXPECT_EQ(5u, dispatcher_->num_running_jobs()); | 289 EXPECT_EQ(5u, dispatcher_->num_running_jobs()); |
295 EXPECT_EQ(3u, dispatcher_->num_queued_jobs()); | 290 EXPECT_EQ(3u, dispatcher_->num_queued_jobs()); |
296 | 291 |
297 ASSERT_TRUE(job_a->running()); | 292 ASSERT_TRUE(job_a->running()); |
298 ASSERT_TRUE(job_b->running()); | 293 ASSERT_TRUE(job_b->running()); |
299 ASSERT_TRUE(job_d->running()); | 294 ASSERT_TRUE(job_d->running()); |
300 ASSERT_TRUE(job_f->running()); | 295 ASSERT_TRUE(job_f->running()); |
301 ASSERT_TRUE(job_g->running()); | 296 ASSERT_TRUE(job_g->running()); |
302 // a, b, d, f, g are running. Finish them in any order. | 297 // a, b, d, f, g are running. Finish them in any order. |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 Prepare(limits); | 555 Prepare(limits); |
561 AddJob('a', IDLE); | 556 AddJob('a', IDLE); |
562 AddJob('b', IDLE); | 557 AddJob('b', IDLE); |
563 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); | 558 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); |
564 } | 559 } |
565 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 560 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
566 | 561 |
567 } // namespace | 562 } // namespace |
568 | 563 |
569 } // namespace net | 564 } // namespace net |
OLD | NEW |