OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <ctype.h> |
| 6 #include <string> |
| 7 |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" |
| 11 #include "net/base/prioritized_dispatcher.h" |
| 12 #include "net/base/request_priority.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace net { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // We rely on the priority enum values being sequential having starting at 0, |
| 20 // and increasing for lower priorities. |
| 21 COMPILE_ASSERT(HIGHEST == 0u && |
| 22 LOWEST > HIGHEST && |
| 23 IDLE > LOWEST && |
| 24 NUM_PRIORITIES > IDLE, |
| 25 priority_indexes_incompatible); |
| 26 |
| 27 class PrioritizedDispatcherTest : public testing::Test { |
| 28 public: |
| 29 // A job that appends |data| to |log_| when started and '.' when finished. |
| 30 // This is intended to confirm the execution order of a sequence of jobs added |
| 31 // to the dispatcher. |
| 32 class TestJob : public PrioritizedDispatcher::Job { |
| 33 public: |
| 34 TestJob(PrioritizedDispatcherTest* test, char data, size_t priority) |
| 35 : test_(test), data_(data), priority_(priority), running_(false) {} |
| 36 |
| 37 // MSVS does not accept EXPECT_EQ(this, ...) so wrap it up. |
| 38 PrioritizedDispatcher::Job* this_job() { |
| 39 return this; |
| 40 } |
| 41 |
| 42 void Add() { |
| 43 EXPECT_TRUE(handle_.is_null()); |
| 44 EXPECT_FALSE(running_); |
| 45 size_t num_queued = dispatch().num_queued_jobs(); |
| 46 size_t num_running = dispatch().num_running_jobs(); |
| 47 |
| 48 handle_ = dispatch().Add(this, priority_); |
| 49 |
| 50 if (handle_.is_null()) { |
| 51 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); |
| 52 EXPECT_TRUE(running_); |
| 53 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); |
| 54 } else { |
| 55 EXPECT_FALSE(running_); |
| 56 EXPECT_EQ(priority_, handle_.priority()); |
| 57 EXPECT_EQ(this_job(), handle_.value()); |
| 58 EXPECT_EQ(num_running, dispatch().num_running_jobs()); |
| 59 } |
| 60 } |
| 61 |
| 62 void Update(size_t priority) { |
| 63 EXPECT_FALSE(running_); |
| 64 ASSERT_FALSE(handle_.is_null()); |
| 65 size_t num_queued = dispatch().num_queued_jobs(); |
| 66 size_t num_running = dispatch().num_running_jobs(); |
| 67 |
| 68 handle_ = dispatch().Update(handle_, priority); |
| 69 |
| 70 if (handle_.is_null()) { |
| 71 EXPECT_TRUE(running_); |
| 72 EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs()); |
| 73 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); |
| 74 } else { |
| 75 EXPECT_FALSE(running_); |
| 76 EXPECT_EQ(priority, handle_.priority()); |
| 77 EXPECT_EQ(this_job(), handle_.value()); |
| 78 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); |
| 79 EXPECT_EQ(num_running, dispatch().num_running_jobs()); |
| 80 } |
| 81 } |
| 82 |
| 83 void Cancel() { |
| 84 EXPECT_FALSE(running_); |
| 85 ASSERT_FALSE(handle_.is_null()); |
| 86 size_t num_queued = dispatch().num_queued_jobs(); |
| 87 |
| 88 dispatch().Cancel(handle_); |
| 89 |
| 90 EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs()); |
| 91 handle_ = PrioritizedDispatcher::Handle(); |
| 92 } |
| 93 |
| 94 void Finish() { |
| 95 EXPECT_TRUE(running_); |
| 96 running_ = false; |
| 97 test_->log_.append(1u, '.'); |
| 98 |
| 99 dispatch().OnJobFinished(); |
| 100 } |
| 101 |
| 102 // PriorityDispatch::Job interface |
| 103 virtual void Start() OVERRIDE { |
| 104 EXPECT_FALSE(running_); |
| 105 handle_ = PrioritizedDispatcher::Handle(); |
| 106 running_ = true; |
| 107 test_->log_.append(1u, data_); |
| 108 } |
| 109 |
| 110 private: |
| 111 PrioritizedDispatcher& dispatch() { return *(test_->dispatch_); } |
| 112 |
| 113 PrioritizedDispatcherTest* test_; |
| 114 |
| 115 char data_; |
| 116 size_t priority_; |
| 117 |
| 118 PrioritizedDispatcher::Handle handle_; |
| 119 bool running_; |
| 120 }; |
| 121 |
| 122 protected: |
| 123 void Prepare(const PrioritizedDispatcher::Limits& limits) { |
| 124 dispatch_.reset(new PrioritizedDispatcher(NUM_PRIORITIES, limits)); |
| 125 } |
| 126 |
| 127 TestJob* AddJob(char data, size_t priority) { |
| 128 TestJob* job = new TestJob(this, data, priority); |
| 129 jobs_.push_back(job); |
| 130 job->Add(); |
| 131 return job; |
| 132 } |
| 133 |
| 134 void Expect(std::string log) { |
| 135 EXPECT_EQ(0u, dispatch_->num_queued_jobs()); |
| 136 EXPECT_EQ(0u, dispatch_->num_running_jobs()); |
| 137 EXPECT_EQ(log, log_); |
| 138 log_.clear(); |
| 139 } |
| 140 |
| 141 std::string log_; |
| 142 scoped_ptr<PrioritizedDispatcher> dispatch_; |
| 143 ScopedVector<TestJob> jobs_; |
| 144 }; |
| 145 |
| 146 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { |
| 147 // Allow only one running job. |
| 148 PrioritizedDispatcher::Limits limits = { 1 }; |
| 149 Prepare(limits); |
| 150 |
| 151 TestJob* job_a = AddJob('a', IDLE); |
| 152 TestJob* job_b = AddJob('b', IDLE); |
| 153 TestJob* job_c = AddJob('c', IDLE); |
| 154 TestJob* job_d = AddJob('d', IDLE); |
| 155 |
| 156 job_a->Finish(); |
| 157 job_b->Finish(); |
| 158 job_c->Finish(); |
| 159 job_d->Finish(); |
| 160 |
| 161 Expect("a.b.c.d."); |
| 162 } |
| 163 |
| 164 TEST_F(PrioritizedDispatcherTest, AddPriority) { |
| 165 PrioritizedDispatcher::Limits limits = { 1 }; |
| 166 Prepare(limits); |
| 167 |
| 168 TestJob* job_a = AddJob('a', IDLE); |
| 169 TestJob* job_b = AddJob('b', MEDIUM); |
| 170 TestJob* job_c = AddJob('c', HIGHEST); |
| 171 TestJob* job_d = AddJob('d', HIGHEST); |
| 172 TestJob* job_e = AddJob('e', MEDIUM); |
| 173 |
| 174 job_a->Finish(); |
| 175 job_c->Finish(); |
| 176 job_d->Finish(); |
| 177 job_b->Finish(); |
| 178 job_e->Finish(); |
| 179 |
| 180 Expect("a.c.d.b.e."); |
| 181 } |
| 182 |
| 183 TEST_F(PrioritizedDispatcherTest, EnforceLimits) { |
| 184 // Reserve 2 for HIGHEST and 1 for LOW or higher. |
| 185 // This leaves 2 for LOWEST or lower. |
| 186 std::vector<size_t> slots(NUM_PRIORITIES); |
| 187 slots[HIGHEST] = 2; |
| 188 slots[LOW] = 1; |
| 189 PrioritizedDispatcher::Limits limits = { 5, slots }; |
| 190 Prepare(limits); |
| 191 |
| 192 TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot. |
| 193 TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot. |
| 194 TestJob* job_c = AddJob('c', LOWEST); // Must wait. |
| 195 TestJob* job_d = AddJob('d', LOW); // Uses reserved slot. |
| 196 TestJob* job_e = AddJob('e', MEDIUM); // Must wait. |
| 197 TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot. |
| 198 TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot. |
| 199 TestJob* job_h = AddJob('h', HIGHEST); // Must wait. |
| 200 |
| 201 EXPECT_EQ(5u, dispatch_->num_running_jobs()); |
| 202 EXPECT_EQ(3u, dispatch_->num_queued_jobs()); |
| 203 |
| 204 job_a->Finish(); // Releases h. |
| 205 job_b->Finish(); |
| 206 job_d->Finish(); |
| 207 job_f->Finish(); // Releases e. |
| 208 job_g->Finish(); |
| 209 job_h->Finish(); // Releases c. |
| 210 job_e->Finish(); |
| 211 job_c->Finish(); |
| 212 |
| 213 Expect("abdfg.h...e..c.."); |
| 214 } |
| 215 |
| 216 TEST_F(PrioritizedDispatcherTest, Update) { |
| 217 PrioritizedDispatcher::Limits limits = { 1 }; |
| 218 Prepare(limits); |
| 219 |
| 220 TestJob* job_a = AddJob('a', IDLE); |
| 221 TestJob* job_b = AddJob('b', MEDIUM); |
| 222 TestJob* job_c = AddJob('c', HIGHEST); |
| 223 TestJob* job_d = AddJob('d', HIGHEST); |
| 224 |
| 225 job_b->Update(HIGHEST); |
| 226 job_c->Update(MEDIUM); |
| 227 |
| 228 job_a->Finish(); |
| 229 job_d->Finish(); |
| 230 job_b->Finish(); |
| 231 job_c->Finish(); |
| 232 |
| 233 Expect("a.d.b.c."); |
| 234 } |
| 235 |
| 236 TEST_F(PrioritizedDispatcherTest, Cancel) { |
| 237 PrioritizedDispatcher::Limits limits = { 1 }; |
| 238 Prepare(limits); |
| 239 |
| 240 TestJob* job_a = AddJob('a', IDLE); |
| 241 TestJob* job_b = AddJob('b', IDLE); |
| 242 TestJob* job_c = AddJob('c', IDLE); |
| 243 TestJob* job_d = AddJob('d', IDLE); |
| 244 TestJob* job_e = AddJob('e', IDLE); |
| 245 |
| 246 job_b->Cancel(); |
| 247 job_d->Cancel(); |
| 248 |
| 249 job_a->Finish(); |
| 250 job_c->Finish(); |
| 251 job_e->Finish(); |
| 252 |
| 253 Expect("a.c.e."); |
| 254 } |
| 255 |
| 256 TEST_F(PrioritizedDispatcherTest, Evict) { |
| 257 PrioritizedDispatcher::Limits limits = { 1 }; |
| 258 Prepare(limits); |
| 259 |
| 260 TestJob* job_a = AddJob('a', IDLE); |
| 261 TestJob* job_b = AddJob('b', LOW); |
| 262 TestJob* job_c = AddJob('c', HIGHEST); |
| 263 TestJob* job_d = AddJob('d', LOW); |
| 264 TestJob* job_e = AddJob('e', HIGHEST); |
| 265 |
| 266 EXPECT_EQ(job_b, dispatch_->EvictOldestLowest()); |
| 267 EXPECT_EQ(job_d, dispatch_->EvictOldestLowest()); |
| 268 |
| 269 job_a->Finish(); |
| 270 job_c->Finish(); |
| 271 job_e->Finish(); |
| 272 |
| 273 Expect("a.c.e."); |
| 274 } |
| 275 |
| 276 } // namespace |
| 277 |
| 278 } // namespace net |
| 279 |
OLD | NEW |