| 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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 11 #include "net/base/prioritized_dispatcher.h" | 12 #include "net/base/prioritized_dispatcher.h" |
| 12 #include "net/base/request_priority.h" | 13 #include "net/base/request_priority.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // 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, |
| 20 // and increasing for higher priorities. | 21 // and increasing for higher priorities. |
| 21 COMPILE_ASSERT(MINIMUM_PRIORITY == 0u && | 22 COMPILE_ASSERT(MINIMUM_PRIORITY == 0u && |
| 22 MINIMUM_PRIORITY == IDLE && | 23 MINIMUM_PRIORITY == IDLE && |
| 23 IDLE < LOWEST && | 24 IDLE < LOWEST && |
| 24 LOWEST < HIGHEST && | 25 LOWEST < HIGHEST && |
| 25 HIGHEST < NUM_PRIORITIES, | 26 HIGHEST < NUM_PRIORITIES, |
| 26 priority_indexes_incompatible); | 27 priority_indexes_incompatible); |
| 27 | 28 |
| 28 class PrioritizedDispatcherTest : public testing::Test { | 29 class PrioritizedDispatcherTest : public testing::Test { |
| 29 public: | 30 public: |
| 30 typedef PrioritizedDispatcher::Priority Priority; | 31 typedef PrioritizedDispatcher::Priority Priority; |
| 31 // A job that appends |data| to |log_| when started and '.' when finished. | 32 // A job that appends |tag| to |log| when started and '.' when finished. |
| 32 // This is intended to confirm the execution order of a sequence of jobs added | 33 // This is intended to confirm the execution order of a sequence of jobs added |
| 33 // to the dispatcher. | 34 // to the dispatcher. Note that finishing order of jobs does not matter. |
| 34 class TestJob : public PrioritizedDispatcher::Job { | 35 class TestJob : public PrioritizedDispatcher::Job { |
| 35 public: | 36 public: |
| 36 TestJob(PrioritizedDispatcherTest* test, char data, Priority priority) | 37 TestJob(PrioritizedDispatcher* dispatcher, |
| 37 : test_(test), data_(data), priority_(priority), running_(false) {} | 38 char tag, |
| 38 | 39 Priority priority, |
| 39 // MSVS does not accept EXPECT_EQ(this, ...) so wrap it up. | 40 std::string* log) |
| 40 PrioritizedDispatcher::Job* this_job() { | 41 : dispatcher_(dispatcher), |
| 41 return this; | 42 tag_(tag), |
| 42 } | 43 priority_(priority), |
| 44 running_(false), |
| 45 log_(log) {} |
| 43 | 46 |
| 44 void Add() { | 47 void Add() { |
| 45 EXPECT_TRUE(handle_.is_null()); | 48 DCHECK(handle_.is_null()); |
| 46 EXPECT_FALSE(running_); | 49 DCHECK(!running_); |
| 47 size_t num_queued = dispatch().num_queued_jobs(); | 50 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 48 size_t num_running = dispatch().num_running_jobs(); | 51 size_t num_running = dispatcher_->num_running_jobs(); |
| 49 | 52 |
| 50 handle_ = dispatch().Add(this, priority_); | 53 handle_ = dispatcher_->Add(this, priority_); |
| 51 | 54 |
| 52 if (handle_.is_null()) { | 55 if (handle_.is_null()) { |
| 53 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); | 56 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); |
| 54 EXPECT_TRUE(running_); | 57 EXPECT_TRUE(running_); |
| 55 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); | 58 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); |
| 56 } else { | 59 } else { |
| 57 EXPECT_FALSE(running_); | 60 EXPECT_FALSE(running_); |
| 58 EXPECT_EQ(priority_, handle_.priority()); | 61 EXPECT_EQ(priority_, handle_.priority()); |
| 59 EXPECT_EQ(this_job(), handle_.value()); | 62 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); |
| 60 EXPECT_EQ(num_running, dispatch().num_running_jobs()); | 63 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); |
| 61 } | 64 } |
| 62 } | 65 } |
| 63 | 66 |
| 64 void ChangePriority(Priority priority) { | 67 void ChangePriority(Priority priority) { |
| 65 EXPECT_FALSE(running_); | 68 EXPECT_FALSE(running_); |
| 66 ASSERT_FALSE(handle_.is_null()); | 69 ASSERT_FALSE(handle_.is_null()); |
| 67 size_t num_queued = dispatch().num_queued_jobs(); | 70 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 68 size_t num_running = dispatch().num_running_jobs(); | 71 size_t num_running = dispatcher_->num_running_jobs(); |
| 69 | 72 |
| 70 handle_ = dispatch().ChangePriority(handle_, priority); | 73 handle_ = dispatcher_->ChangePriority(handle_, priority); |
| 71 | 74 |
| 72 if (handle_.is_null()) { | 75 if (handle_.is_null()) { |
| 73 EXPECT_TRUE(running_); | 76 EXPECT_TRUE(running_); |
| 74 EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs()); | 77 EXPECT_EQ(num_queued - 1, dispatcher_->num_queued_jobs()); |
| 75 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); | 78 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); |
| 76 } else { | 79 } else { |
| 77 EXPECT_FALSE(running_); | 80 EXPECT_FALSE(running_); |
| 78 EXPECT_EQ(priority, handle_.priority()); | 81 EXPECT_EQ(priority, handle_.priority()); |
| 79 EXPECT_EQ(this_job(), handle_.value()); | 82 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); |
| 80 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); | 83 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); |
| 81 EXPECT_EQ(num_running, dispatch().num_running_jobs()); | 84 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); |
| 82 } | 85 } |
| 83 } | 86 } |
| 84 | 87 |
| 85 void Cancel() { | 88 void Cancel() { |
| 86 EXPECT_FALSE(running_); | 89 EXPECT_FALSE(running_); |
| 87 ASSERT_FALSE(handle_.is_null()); | 90 ASSERT_FALSE(handle_.is_null()); |
| 88 size_t num_queued = dispatch().num_queued_jobs(); | 91 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 89 | 92 |
| 90 dispatch().Cancel(handle_); | 93 dispatcher_->Cancel(handle_); |
| 91 | 94 |
| 92 EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs()); | 95 EXPECT_EQ(num_queued - 1, dispatcher_->num_queued_jobs()); |
| 93 handle_ = PrioritizedDispatcher::Handle(); | 96 handle_ = PrioritizedDispatcher::Handle(); |
| 94 } | 97 } |
| 95 | 98 |
| 96 void Finish() { | 99 void Finish() { |
| 97 EXPECT_TRUE(running_); | 100 EXPECT_TRUE(running_); |
| 98 running_ = false; | 101 running_ = false; |
| 99 test_->log_.append(1u, '.'); | 102 log_->append(1u, '.'); |
| 100 | 103 |
| 101 dispatch().OnJobFinished(); | 104 dispatcher_->OnJobFinished(); |
| 102 } | 105 } |
| 103 | 106 |
| 104 // PriorityDispatch::Job interface | 107 // PriorityDispatch::Job interface |
| 105 virtual void Start() OVERRIDE { | 108 virtual void Start() OVERRIDE { |
| 106 EXPECT_FALSE(running_); | 109 EXPECT_FALSE(running_); |
| 107 handle_ = PrioritizedDispatcher::Handle(); | 110 handle_ = PrioritizedDispatcher::Handle(); |
| 108 running_ = true; | 111 running_ = true; |
| 109 test_->log_.append(1u, data_); | 112 log_->append(1u, tag_); |
| 110 } | 113 } |
| 111 | 114 |
| 112 private: | 115 private: |
| 113 PrioritizedDispatcher& dispatch() { return *(test_->dispatch_); } | 116 PrioritizedDispatcher* dispatcher_; |
| 114 | 117 |
| 115 PrioritizedDispatcherTest* test_; | 118 char tag_; |
| 116 | |
| 117 char data_; | |
| 118 Priority priority_; | 119 Priority priority_; |
| 119 | 120 |
| 120 PrioritizedDispatcher::Handle handle_; | 121 PrioritizedDispatcher::Handle handle_; |
| 121 bool running_; | 122 bool running_; |
| 123 |
| 124 std::string* log_; |
| 122 }; | 125 }; |
| 123 | 126 |
| 124 protected: | 127 protected: |
| 125 void Prepare(const PrioritizedDispatcher::Limits& limits) { | 128 void Prepare(const PrioritizedDispatcher::Limits& limits) { |
| 126 dispatch_.reset(new PrioritizedDispatcher(limits)); | 129 dispatcher_.reset(new PrioritizedDispatcher(limits)); |
| 127 } | 130 } |
| 128 | 131 |
| 129 TestJob* AddJob(char data, Priority priority) { | 132 TestJob* AddJob(char data, Priority priority) { |
| 130 TestJob* job = new TestJob(this, data, priority); | 133 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); |
| 131 jobs_.push_back(job); | 134 jobs_.push_back(job); |
| 132 job->Add(); | 135 job->Add(); |
| 133 return job; | 136 return job; |
| 134 } | 137 } |
| 135 | 138 |
| 136 void Expect(std::string log) { | 139 void Expect(std::string log) { |
| 137 EXPECT_EQ(0u, dispatch_->num_queued_jobs()); | 140 EXPECT_EQ(0u, dispatcher_->num_queued_jobs()); |
| 138 EXPECT_EQ(0u, dispatch_->num_running_jobs()); | 141 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); |
| 139 EXPECT_EQ(log, log_); | 142 EXPECT_EQ(log, log_); |
| 140 log_.clear(); | 143 log_.clear(); |
| 141 } | 144 } |
| 142 | 145 |
| 143 std::string log_; | 146 std::string log_; |
| 144 scoped_ptr<PrioritizedDispatcher> dispatch_; | 147 scoped_ptr<PrioritizedDispatcher> dispatcher_; |
| 145 ScopedVector<TestJob> jobs_; | 148 ScopedVector<TestJob> jobs_; |
| 146 }; | 149 }; |
| 147 | 150 |
| 148 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { | 151 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { |
| 149 // Allow only one running job. | 152 // Allow only one running job. |
| 150 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 153 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 151 Prepare(limits); | 154 Prepare(limits); |
| 152 | 155 |
| 153 TestJob* job_a = AddJob('a', IDLE); | 156 TestJob* job_a = AddJob('a', IDLE); |
| 154 TestJob* job_b = AddJob('b', IDLE); | 157 TestJob* job_b = AddJob('b', IDLE); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 195 |
| 193 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. | 196 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. |
| 194 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. | 197 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. |
| 195 TestJob* job_c = AddJob('c', LOWEST); // Must wait. | 198 TestJob* job_c = AddJob('c', LOWEST); // Must wait. |
| 196 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. | 199 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. |
| 197 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. | 200 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. |
| 198 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. | 201 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. |
| 199 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. | 202 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. |
| 200 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. | 203 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. |
| 201 | 204 |
| 202 EXPECT_EQ(5u, dispatch_->num_running_jobs()); | 205 EXPECT_EQ(5u, dispatcher_->num_running_jobs()); |
| 203 EXPECT_EQ(3u, dispatch_->num_queued_jobs()); | 206 EXPECT_EQ(3u, dispatcher_->num_queued_jobs()); |
| 204 | 207 |
| 205 job_a->Finish(); // Releases h. | 208 // a, b, d, f, g are running. Finish them in any order. |
| 206 job_b->Finish(); | 209 job_b->Finish(); // Releases h. |
| 210 job_f->Finish(); |
| 211 job_a->Finish(); |
| 212 job_g->Finish(); // Releases e. |
| 207 job_d->Finish(); | 213 job_d->Finish(); |
| 208 job_f->Finish(); // Releases e. | 214 // h, e are running. |
| 209 job_g->Finish(); | 215 job_e->Finish(); // Releases c. |
| 210 job_h->Finish(); // Releases c. | |
| 211 job_e->Finish(); | |
| 212 job_c->Finish(); | 216 job_c->Finish(); |
| 217 job_h->Finish(); |
| 213 | 218 |
| 214 Expect("abdfg.h...e..c.."); | 219 Expect("abdfg.h...e..c.."); |
| 215 } | 220 } |
| 216 | 221 |
| 217 TEST_F(PrioritizedDispatcherTest, ChangePriority) { | 222 TEST_F(PrioritizedDispatcherTest, ChangePriority) { |
| 218 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 223 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 219 Prepare(limits); | 224 Prepare(limits); |
| 220 | 225 |
| 221 TestJob* job_a = AddJob('a', IDLE); | 226 TestJob* job_a = AddJob('a', IDLE); |
| 222 TestJob* job_b = AddJob('b', MEDIUM); | 227 TestJob* job_b = AddJob('b', MEDIUM); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 TEST_F(PrioritizedDispatcherTest, Evict) { | 262 TEST_F(PrioritizedDispatcherTest, Evict) { |
| 258 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 263 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 259 Prepare(limits); | 264 Prepare(limits); |
| 260 | 265 |
| 261 TestJob* job_a = AddJob('a', IDLE); | 266 TestJob* job_a = AddJob('a', IDLE); |
| 262 TestJob* job_b = AddJob('b', LOW); | 267 TestJob* job_b = AddJob('b', LOW); |
| 263 TestJob* job_c = AddJob('c', HIGHEST); | 268 TestJob* job_c = AddJob('c', HIGHEST); |
| 264 TestJob* job_d = AddJob('d', LOW); | 269 TestJob* job_d = AddJob('d', LOW); |
| 265 TestJob* job_e = AddJob('e', HIGHEST); | 270 TestJob* job_e = AddJob('e', HIGHEST); |
| 266 | 271 |
| 267 EXPECT_EQ(job_b, dispatch_->EvictOldestLowest()); | 272 EXPECT_EQ(job_b, dispatcher_->EvictOldestLowest()); |
| 268 EXPECT_EQ(job_d, dispatch_->EvictOldestLowest()); | 273 EXPECT_EQ(job_d, dispatcher_->EvictOldestLowest()); |
| 269 | 274 |
| 270 job_a->Finish(); | 275 job_a->Finish(); |
| 271 job_c->Finish(); | 276 job_c->Finish(); |
| 272 job_e->Finish(); | 277 job_e->Finish(); |
| 273 | 278 |
| 274 Expect("a.c.e."); | 279 Expect("a.c.e."); |
| 275 } | 280 } |
| 276 | 281 |
| 282 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { |
| 283 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 284 Prepare(limits); |
| 285 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); |
| 286 } |
| 287 |
| 277 } // namespace | 288 } // namespace |
| 278 | 289 |
| 279 } // namespace net | 290 } // namespace net |
| 280 | 291 |
| 292 |
| OLD | NEW |