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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 Prepare(limits); | 394 Prepare(limits); |
357 AddJob('a', IDLE); | 395 AddJob('a', IDLE); |
358 AddJob('b', IDLE); | 396 AddJob('b', IDLE); |
359 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); | 397 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); |
360 } | 398 } |
361 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) | 399 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
362 | 400 |
363 } // namespace | 401 } // namespace |
364 | 402 |
365 } // namespace net | 403 } // namespace net |
OLD | NEW |