| 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, |
| 39 Priority priority, |
| 40 std::string* log) |
| 41 : dispatcher_(dispatcher), |
| 42 tag_(tag), |
| 43 priority_(priority), |
| 44 running_(false), |
| 45 log_(log) {} |
| 38 | 46 |
| 39 // MSVS does not accept EXPECT_EQ(this, ...) so wrap it up. | 47 bool running() const { |
| 40 PrioritizedDispatcher::Job* this_job() { | 48 return running_; |
| 41 return this; | 49 } |
| 50 |
| 51 const PrioritizedDispatcher::Handle handle() const { |
| 52 return handle_; |
| 42 } | 53 } |
| 43 | 54 |
| 44 void Add() { | 55 void Add() { |
| 45 EXPECT_TRUE(handle_.is_null()); | 56 CHECK(handle_.is_null()); |
| 46 EXPECT_FALSE(running_); | 57 CHECK(!running_); |
| 47 size_t num_queued = dispatch().num_queued_jobs(); | 58 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 48 size_t num_running = dispatch().num_running_jobs(); | 59 size_t num_running = dispatcher_->num_running_jobs(); |
| 49 | 60 |
| 50 handle_ = dispatch().Add(this, priority_); | 61 handle_ = dispatcher_->Add(this, priority_); |
| 51 | 62 |
| 52 if (handle_.is_null()) { | 63 if (handle_.is_null()) { |
| 53 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); | 64 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); |
| 54 EXPECT_TRUE(running_); | 65 EXPECT_TRUE(running_); |
| 55 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); | 66 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); |
| 56 } else { | 67 } else { |
| 57 EXPECT_FALSE(running_); | 68 EXPECT_FALSE(running_); |
| 58 EXPECT_EQ(priority_, handle_.priority()); | 69 EXPECT_EQ(priority_, handle_.priority()); |
| 59 EXPECT_EQ(this_job(), handle_.value()); | 70 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); |
| 60 EXPECT_EQ(num_running, dispatch().num_running_jobs()); | 71 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); |
| 61 } | 72 } |
| 62 } | 73 } |
| 63 | 74 |
| 64 void ChangePriority(Priority priority) { | 75 void ChangePriority(Priority priority) { |
| 65 EXPECT_FALSE(running_); | 76 CHECK(!handle_.is_null()); |
| 66 ASSERT_FALSE(handle_.is_null()); | 77 CHECK(!running_); |
| 67 size_t num_queued = dispatch().num_queued_jobs(); | 78 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 68 size_t num_running = dispatch().num_running_jobs(); | 79 size_t num_running = dispatcher_->num_running_jobs(); |
| 69 | 80 |
| 70 handle_ = dispatch().ChangePriority(handle_, priority); | 81 handle_ = dispatcher_->ChangePriority(handle_, priority); |
| 71 | 82 |
| 72 if (handle_.is_null()) { | 83 if (handle_.is_null()) { |
| 73 EXPECT_TRUE(running_); | 84 EXPECT_TRUE(running_); |
| 74 EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs()); | 85 EXPECT_EQ(num_queued - 1, dispatcher_->num_queued_jobs()); |
| 75 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); | 86 EXPECT_EQ(num_running + 1, dispatcher_->num_running_jobs()); |
| 76 } else { | 87 } else { |
| 77 EXPECT_FALSE(running_); | 88 EXPECT_FALSE(running_); |
| 78 EXPECT_EQ(priority, handle_.priority()); | 89 EXPECT_EQ(priority, handle_.priority()); |
| 79 EXPECT_EQ(this_job(), handle_.value()); | 90 EXPECT_EQ(tag_, reinterpret_cast<TestJob*>(handle_.value())->tag_); |
| 80 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); | 91 EXPECT_EQ(num_queued, dispatcher_->num_queued_jobs()); |
| 81 EXPECT_EQ(num_running, dispatch().num_running_jobs()); | 92 EXPECT_EQ(num_running, dispatcher_->num_running_jobs()); |
| 82 } | 93 } |
| 83 } | 94 } |
| 84 | 95 |
| 85 void Cancel() { | 96 void Cancel() { |
| 86 EXPECT_FALSE(running_); | 97 CHECK(!handle_.is_null()); |
| 87 ASSERT_FALSE(handle_.is_null()); | 98 CHECK(!running_); |
| 88 size_t num_queued = dispatch().num_queued_jobs(); | 99 size_t num_queued = dispatcher_->num_queued_jobs(); |
| 89 | 100 |
| 90 dispatch().Cancel(handle_); | 101 dispatcher_->Cancel(handle_); |
| 91 | 102 |
| 92 EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs()); | 103 EXPECT_EQ(num_queued - 1, dispatcher_->num_queued_jobs()); |
| 93 handle_ = PrioritizedDispatcher::Handle(); | 104 handle_ = PrioritizedDispatcher::Handle(); |
| 94 } | 105 } |
| 95 | 106 |
| 96 void Finish() { | 107 void Finish() { |
| 97 EXPECT_TRUE(running_); | 108 CHECK(running_); |
| 98 running_ = false; | 109 running_ = false; |
| 99 test_->log_.append(1u, '.'); | 110 log_->append(1u, '.'); |
| 100 | 111 |
| 101 dispatch().OnJobFinished(); | 112 dispatcher_->OnJobFinished(); |
| 102 } | 113 } |
| 103 | 114 |
| 104 // PriorityDispatch::Job interface | 115 // PriorityDispatch::Job interface |
| 105 virtual void Start() OVERRIDE { | 116 virtual void Start() OVERRIDE { |
| 106 EXPECT_FALSE(running_); | 117 EXPECT_FALSE(running_); |
| 107 handle_ = PrioritizedDispatcher::Handle(); | 118 handle_ = PrioritizedDispatcher::Handle(); |
| 108 running_ = true; | 119 running_ = true; |
| 109 test_->log_.append(1u, data_); | 120 log_->append(1u, tag_); |
| 110 } | 121 } |
| 111 | 122 |
| 112 private: | 123 private: |
| 113 PrioritizedDispatcher& dispatch() { return *(test_->dispatch_); } | 124 PrioritizedDispatcher* dispatcher_; |
| 114 | 125 |
| 115 PrioritizedDispatcherTest* test_; | 126 char tag_; |
| 116 | |
| 117 char data_; | |
| 118 Priority priority_; | 127 Priority priority_; |
| 119 | 128 |
| 120 PrioritizedDispatcher::Handle handle_; | 129 PrioritizedDispatcher::Handle handle_; |
| 121 bool running_; | 130 bool running_; |
| 131 |
| 132 std::string* log_; |
| 122 }; | 133 }; |
| 123 | 134 |
| 124 protected: | 135 protected: |
| 125 void Prepare(const PrioritizedDispatcher::Limits& limits) { | 136 void Prepare(const PrioritizedDispatcher::Limits& limits) { |
| 126 dispatch_.reset(new PrioritizedDispatcher(limits)); | 137 dispatcher_.reset(new PrioritizedDispatcher(limits)); |
| 127 } | 138 } |
| 128 | 139 |
| 129 TestJob* AddJob(char data, Priority priority) { | 140 TestJob* AddJob(char data, Priority priority) { |
| 130 TestJob* job = new TestJob(this, data, priority); | 141 TestJob* job = new TestJob(dispatcher_.get(), data, priority, &log_); |
| 131 jobs_.push_back(job); | 142 jobs_.push_back(job); |
| 132 job->Add(); | 143 job->Add(); |
| 133 return job; | 144 return job; |
| 134 } | 145 } |
| 135 | 146 |
| 136 void Expect(std::string log) { | 147 void Expect(std::string log) { |
| 137 EXPECT_EQ(0u, dispatch_->num_queued_jobs()); | 148 EXPECT_EQ(0u, dispatcher_->num_queued_jobs()); |
| 138 EXPECT_EQ(0u, dispatch_->num_running_jobs()); | 149 EXPECT_EQ(0u, dispatcher_->num_running_jobs()); |
| 139 EXPECT_EQ(log, log_); | 150 EXPECT_EQ(log, log_); |
| 140 log_.clear(); | 151 log_.clear(); |
| 141 } | 152 } |
| 142 | 153 |
| 143 std::string log_; | 154 std::string log_; |
| 144 scoped_ptr<PrioritizedDispatcher> dispatch_; | 155 scoped_ptr<PrioritizedDispatcher> dispatcher_; |
| 145 ScopedVector<TestJob> jobs_; | 156 ScopedVector<TestJob> jobs_; |
| 146 }; | 157 }; |
| 147 | 158 |
| 148 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { | 159 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { |
| 149 // Allow only one running job. | 160 // Allow only one running job. |
| 150 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 161 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 151 Prepare(limits); | 162 Prepare(limits); |
| 152 | 163 |
| 153 TestJob* job_a = AddJob('a', IDLE); | 164 TestJob* job_a = AddJob('a', IDLE); |
| 154 TestJob* job_b = AddJob('b', IDLE); | 165 TestJob* job_b = AddJob('b', IDLE); |
| 155 TestJob* job_c = AddJob('c', IDLE); | 166 TestJob* job_c = AddJob('c', IDLE); |
| 156 TestJob* job_d = AddJob('d', IDLE); | 167 TestJob* job_d = AddJob('d', IDLE); |
| 157 | 168 |
| 169 ASSERT_TRUE(job_a->running()); |
| 158 job_a->Finish(); | 170 job_a->Finish(); |
| 171 ASSERT_TRUE(job_b->running()); |
| 159 job_b->Finish(); | 172 job_b->Finish(); |
| 173 ASSERT_TRUE(job_c->running()); |
| 160 job_c->Finish(); | 174 job_c->Finish(); |
| 175 ASSERT_TRUE(job_d->running()); |
| 161 job_d->Finish(); | 176 job_d->Finish(); |
| 162 | 177 |
| 163 Expect("a.b.c.d."); | 178 Expect("a.b.c.d."); |
| 164 } | 179 } |
| 165 | 180 |
| 166 TEST_F(PrioritizedDispatcherTest, AddPriority) { | 181 TEST_F(PrioritizedDispatcherTest, AddPriority) { |
| 167 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 182 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 168 Prepare(limits); | 183 Prepare(limits); |
| 169 | 184 |
| 170 TestJob* job_a = AddJob('a', IDLE); | 185 TestJob* job_a = AddJob('a', IDLE); |
| 171 TestJob* job_b = AddJob('b', MEDIUM); | 186 TestJob* job_b = AddJob('b', MEDIUM); |
| 172 TestJob* job_c = AddJob('c', HIGHEST); | 187 TestJob* job_c = AddJob('c', HIGHEST); |
| 173 TestJob* job_d = AddJob('d', HIGHEST); | 188 TestJob* job_d = AddJob('d', HIGHEST); |
| 174 TestJob* job_e = AddJob('e', MEDIUM); | 189 TestJob* job_e = AddJob('e', MEDIUM); |
| 175 | 190 |
| 191 ASSERT_TRUE(job_a->running()); |
| 176 job_a->Finish(); | 192 job_a->Finish(); |
| 193 ASSERT_TRUE(job_c->running()); |
| 177 job_c->Finish(); | 194 job_c->Finish(); |
| 195 ASSERT_TRUE(job_d->running()); |
| 178 job_d->Finish(); | 196 job_d->Finish(); |
| 197 ASSERT_TRUE(job_b->running()); |
| 179 job_b->Finish(); | 198 job_b->Finish(); |
| 199 ASSERT_TRUE(job_e->running()); |
| 180 job_e->Finish(); | 200 job_e->Finish(); |
| 181 | 201 |
| 182 Expect("a.c.d.b.e."); | 202 Expect("a.c.d.b.e."); |
| 183 } | 203 } |
| 184 | 204 |
| 185 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { | 205 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { |
| 186 // Reserve 2 for HIGHEST and 1 for LOW or higher. | 206 // Reserve 2 for HIGHEST and 1 for LOW or higher. |
| 187 // This leaves 2 for LOWEST or lower. | 207 // This leaves 2 for LOWEST or lower. |
| 188 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); | 208 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5); |
| 189 limits.reserved_slots[HIGHEST] = 2; | 209 limits.reserved_slots[HIGHEST] = 2; |
| 190 limits.reserved_slots[LOW] = 1; | 210 limits.reserved_slots[LOW] = 1; |
| 191 Prepare(limits); | 211 Prepare(limits); |
| 192 | 212 |
| 193 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. | 213 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. |
| 194 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. | 214 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. |
| 195 TestJob* job_c = AddJob('c', LOWEST); // Must wait. | 215 TestJob* job_c = AddJob('c', LOWEST); // Must wait. |
| 196 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. | 216 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. |
| 197 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. | 217 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. |
| 198 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. | 218 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. |
| 199 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. | 219 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. |
| 200 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. | 220 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. |
| 201 | 221 |
| 202 EXPECT_EQ(5u, dispatch_->num_running_jobs()); | 222 EXPECT_EQ(5u, dispatcher_->num_running_jobs()); |
| 203 EXPECT_EQ(3u, dispatch_->num_queued_jobs()); | 223 EXPECT_EQ(3u, dispatcher_->num_queued_jobs()); |
| 204 | 224 |
| 205 job_a->Finish(); // Releases h. | 225 ASSERT_TRUE(job_a->running()); |
| 206 job_b->Finish(); | 226 ASSERT_TRUE(job_b->running()); |
| 227 ASSERT_TRUE(job_d->running()); |
| 228 ASSERT_TRUE(job_f->running()); |
| 229 ASSERT_TRUE(job_g->running()); |
| 230 // a, b, d, f, g are running. Finish them in any order. |
| 231 job_b->Finish(); // Releases h. |
| 232 job_f->Finish(); |
| 233 job_a->Finish(); |
| 234 job_g->Finish(); // Releases e. |
| 207 job_d->Finish(); | 235 job_d->Finish(); |
| 208 job_f->Finish(); // Releases e. | 236 ASSERT_TRUE(job_e->running()); |
| 209 job_g->Finish(); | 237 ASSERT_TRUE(job_h->running()); |
| 210 job_h->Finish(); // Releases c. | 238 // h, e are running. |
| 211 job_e->Finish(); | 239 job_e->Finish(); // Releases c. |
| 240 ASSERT_TRUE(job_c->running()); |
| 212 job_c->Finish(); | 241 job_c->Finish(); |
| 242 job_h->Finish(); |
| 213 | 243 |
| 214 Expect("abdfg.h...e..c.."); | 244 Expect("abdfg.h...e..c.."); |
| 215 } | 245 } |
| 216 | 246 |
| 217 TEST_F(PrioritizedDispatcherTest, ChangePriority) { | 247 TEST_F(PrioritizedDispatcherTest, ChangePriority) { |
| 218 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 248 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 219 Prepare(limits); | 249 Prepare(limits); |
| 220 | 250 |
| 221 TestJob* job_a = AddJob('a', IDLE); | 251 TestJob* job_a = AddJob('a', IDLE); |
| 222 TestJob* job_b = AddJob('b', MEDIUM); | 252 TestJob* job_b = AddJob('b', MEDIUM); |
| 223 TestJob* job_c = AddJob('c', HIGHEST); | 253 TestJob* job_c = AddJob('c', HIGHEST); |
| 224 TestJob* job_d = AddJob('d', HIGHEST); | 254 TestJob* job_d = AddJob('d', HIGHEST); |
| 225 | 255 |
| 256 ASSERT_FALSE(job_b->running()); |
| 257 ASSERT_FALSE(job_c->running()); |
| 226 job_b->ChangePriority(HIGHEST); | 258 job_b->ChangePriority(HIGHEST); |
| 227 job_c->ChangePriority(MEDIUM); | 259 job_c->ChangePriority(MEDIUM); |
| 228 | 260 |
| 261 ASSERT_TRUE(job_a->running()); |
| 229 job_a->Finish(); | 262 job_a->Finish(); |
| 263 ASSERT_TRUE(job_d->running()); |
| 230 job_d->Finish(); | 264 job_d->Finish(); |
| 265 ASSERT_TRUE(job_b->running()); |
| 231 job_b->Finish(); | 266 job_b->Finish(); |
| 267 ASSERT_TRUE(job_c->running()); |
| 232 job_c->Finish(); | 268 job_c->Finish(); |
| 233 | 269 |
| 234 Expect("a.d.b.c."); | 270 Expect("a.d.b.c."); |
| 235 } | 271 } |
| 236 | 272 |
| 237 TEST_F(PrioritizedDispatcherTest, Cancel) { | 273 TEST_F(PrioritizedDispatcherTest, Cancel) { |
| 238 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 274 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 239 Prepare(limits); | 275 Prepare(limits); |
| 240 | 276 |
| 241 TestJob* job_a = AddJob('a', IDLE); | 277 TestJob* job_a = AddJob('a', IDLE); |
| 242 TestJob* job_b = AddJob('b', IDLE); | 278 TestJob* job_b = AddJob('b', IDLE); |
| 243 TestJob* job_c = AddJob('c', IDLE); | 279 TestJob* job_c = AddJob('c', IDLE); |
| 244 TestJob* job_d = AddJob('d', IDLE); | 280 TestJob* job_d = AddJob('d', IDLE); |
| 245 TestJob* job_e = AddJob('e', IDLE); | 281 TestJob* job_e = AddJob('e', IDLE); |
| 246 | 282 |
| 283 ASSERT_FALSE(job_b->running()); |
| 284 ASSERT_FALSE(job_d->running()); |
| 247 job_b->Cancel(); | 285 job_b->Cancel(); |
| 248 job_d->Cancel(); | 286 job_d->Cancel(); |
| 249 | 287 |
| 288 ASSERT_TRUE(job_a->running()); |
| 250 job_a->Finish(); | 289 job_a->Finish(); |
| 290 ASSERT_TRUE(job_c->running()); |
| 251 job_c->Finish(); | 291 job_c->Finish(); |
| 292 ASSERT_TRUE(job_e->running()); |
| 252 job_e->Finish(); | 293 job_e->Finish(); |
| 253 | 294 |
| 254 Expect("a.c.e."); | 295 Expect("a.c.e."); |
| 255 } | 296 } |
| 256 | 297 |
| 257 TEST_F(PrioritizedDispatcherTest, Evict) { | 298 TEST_F(PrioritizedDispatcherTest, Evict) { |
| 258 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 299 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 259 Prepare(limits); | 300 Prepare(limits); |
| 260 | 301 |
| 261 TestJob* job_a = AddJob('a', IDLE); | 302 TestJob* job_a = AddJob('a', IDLE); |
| 262 TestJob* job_b = AddJob('b', LOW); | 303 TestJob* job_b = AddJob('b', LOW); |
| 263 TestJob* job_c = AddJob('c', HIGHEST); | 304 TestJob* job_c = AddJob('c', HIGHEST); |
| 264 TestJob* job_d = AddJob('d', LOW); | 305 TestJob* job_d = AddJob('d', LOW); |
| 265 TestJob* job_e = AddJob('e', HIGHEST); | 306 TestJob* job_e = AddJob('e', HIGHEST); |
| 266 | 307 |
| 267 EXPECT_EQ(job_b, dispatch_->EvictOldestLowest()); | 308 EXPECT_EQ(job_b, dispatcher_->EvictOldestLowest()); |
| 268 EXPECT_EQ(job_d, dispatch_->EvictOldestLowest()); | 309 EXPECT_EQ(job_d, dispatcher_->EvictOldestLowest()); |
| 269 | 310 |
| 311 ASSERT_TRUE(job_a->running()); |
| 270 job_a->Finish(); | 312 job_a->Finish(); |
| 313 ASSERT_TRUE(job_c->running()); |
| 271 job_c->Finish(); | 314 job_c->Finish(); |
| 315 ASSERT_TRUE(job_e->running()); |
| 272 job_e->Finish(); | 316 job_e->Finish(); |
| 273 | 317 |
| 274 Expect("a.c.e."); | 318 Expect("a.c.e."); |
| 275 } | 319 } |
| 276 | 320 |
| 321 TEST_F(PrioritizedDispatcherTest, EvictFromEmpty) { |
| 322 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 323 Prepare(limits); |
| 324 EXPECT_TRUE(dispatcher_->EvictOldestLowest() == NULL); |
| 325 } |
| 326 |
| 327 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 328 TEST_F(PrioritizedDispatcherTest, CancelNull) { |
| 329 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 330 Prepare(limits); |
| 331 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(PrioritizedDispatcher::Handle()), ""); |
| 332 } |
| 333 |
| 334 TEST_F(PrioritizedDispatcherTest, CancelMissing) { |
| 335 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 336 Prepare(limits); |
| 337 AddJob('a', IDLE); |
| 338 TestJob* job_b = AddJob('b', IDLE); |
| 339 PrioritizedDispatcher::Handle handle = job_b->handle(); |
| 340 ASSERT_FALSE(handle.is_null()); |
| 341 dispatcher_->Cancel(handle); |
| 342 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); |
| 343 } |
| 344 |
| 345 TEST_F(PrioritizedDispatcherTest, CancelIncompatible) { |
| 346 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 347 Prepare(limits); |
| 348 AddJob('a', IDLE); |
| 349 TestJob* job_b = AddJob('b', IDLE); |
| 350 PrioritizedDispatcher::Handle handle = job_b->handle(); |
| 351 ASSERT_FALSE(handle.is_null()); |
| 352 |
| 353 // New dispatcher. |
| 354 Prepare(limits); |
| 355 AddJob('a', IDLE); |
| 356 AddJob('b', IDLE); |
| 357 EXPECT_DEBUG_DEATH(dispatcher_->Cancel(handle), ""); |
| 358 } |
| 359 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG) |
| 360 |
| 277 } // namespace | 361 } // namespace |
| 278 | 362 |
| 279 } // namespace net | 363 } // namespace net |
| 280 | |
| OLD | NEW |