Chromium Code Reviews| 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 #include "net/base/prioritized_dispatcher.h" | 11 #include "net/base/prioritized_dispatcher.h" |
| 12 #include "net/base/request_priority.h" | 12 #include "net/base/request_priority.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // We rely on the priority enum values being sequential having starting at 0, | 19 // We rely on the priority enum values being sequential having starting at 0, |
| 20 // and increasing for lower priorities. | 20 // and increasing for lower priorities. |
| 21 COMPILE_ASSERT(HIGHEST == 0u && | 21 COMPILE_ASSERT(HIGHEST == 0u && |
| 22 LOWEST > HIGHEST && | 22 LOWEST > HIGHEST && |
| 23 IDLE > LOWEST && | 23 IDLE > LOWEST && |
| 24 NUM_PRIORITIES > IDLE, | 24 NUM_PRIORITIES > IDLE, |
| 25 priority_indexes_incompatible); | 25 priority_indexes_incompatible); |
| 26 | 26 |
| 27 class PrioritizedDispatcherTest : public testing::Test { | 27 class PrioritizedDispatcherTest : public testing::Test { |
| 28 public: | 28 public: |
| 29 typedef PrioritizedDispatcher::Priority Priority; | 29 typedef PrioritizedDispatcher::Priority Priority; |
| 30 // A job that appends |data| to |log_| when started and '.' when finished. | 30 // A job that appends |data| to |log_| when started and '.' when finished. |
|
stevez
2012/04/11 16:13:58
So that means it's ambiguous in the log which job
szym
2012/05/14 22:56:02
The intention here is the the order of jobs finish
| |
| 31 // This is intended to confirm the execution order of a sequence of jobs added | 31 // This is intended to confirm the execution order of a sequence of jobs added |
| 32 // to the dispatcher. | 32 // to the dispatcher. |
| 33 class TestJob : public PrioritizedDispatcher::Job { | 33 class TestJob : public PrioritizedDispatcher::Job { |
| 34 public: | 34 public: |
| 35 TestJob(PrioritizedDispatcherTest* test, char data, Priority priority) | 35 TestJob(PrioritizedDispatcherTest* test, char data, Priority priority) |
|
stevez
2012/04/11 16:13:58
Fix parameter order.
szym
2012/05/14 22:56:02
As before, I prefer to leave this one as is. This
| |
| 36 : test_(test), data_(data), priority_(priority), running_(false) {} | 36 : test_(test), data_(data), priority_(priority), running_(false) {} |
| 37 | 37 |
| 38 // MSVS does not accept EXPECT_EQ(this, ...) so wrap it up. | 38 // MSVS does not accept EXPECT_EQ(this, ...) so wrap it up. |
|
stevez
2012/04/11 16:13:58
This doesn't seem so useful to me. If any expecta
szym
2012/05/14 22:56:02
Done.
| |
| 39 PrioritizedDispatcher::Job* this_job() { | 39 PrioritizedDispatcher::Job* this_job() { |
| 40 return this; | 40 return this; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void Add() { | 43 void Add() { |
| 44 EXPECT_TRUE(handle_.is_null()); | 44 EXPECT_TRUE(handle_.is_null()); |
|
stevez
2012/04/11 16:13:58
Many of these expectations do not depend on the co
szym
2012/05/14 22:56:02
I generally agree. However, a PrioritizedDispatche
stevez
2012/05/17 17:57:07
I was suggesting CHECK for a reason, not DCHECK.
szym
2012/05/17 19:52:01
An ASSERT_ within TestJob::Add or TestJob::ChangeP
| |
| 45 EXPECT_FALSE(running_); | 45 EXPECT_FALSE(running_); |
| 46 size_t num_queued = dispatch().num_queued_jobs(); | 46 size_t num_queued = dispatch().num_queued_jobs(); |
| 47 size_t num_running = dispatch().num_running_jobs(); | 47 size_t num_running = dispatch().num_running_jobs(); |
| 48 | 48 |
| 49 handle_ = dispatch().Add(this, priority_); | 49 handle_ = dispatch().Add(this, priority_); |
| 50 | 50 |
| 51 if (handle_.is_null()) { | 51 if (handle_.is_null()) { |
| 52 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); | 52 EXPECT_EQ(num_queued, dispatch().num_queued_jobs()); |
| 53 EXPECT_TRUE(running_); | 53 EXPECT_TRUE(running_); |
| 54 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); | 54 EXPECT_EQ(num_running + 1, dispatch().num_running_jobs()); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 virtual void Start() OVERRIDE { | 104 virtual void Start() OVERRIDE { |
| 105 EXPECT_FALSE(running_); | 105 EXPECT_FALSE(running_); |
| 106 handle_ = PrioritizedDispatcher::Handle(); | 106 handle_ = PrioritizedDispatcher::Handle(); |
| 107 running_ = true; | 107 running_ = true; |
| 108 test_->log_.append(1u, data_); | 108 test_->log_.append(1u, data_); |
| 109 } | 109 } |
| 110 | 110 |
| 111 private: | 111 private: |
| 112 PrioritizedDispatcher& dispatch() { return *(test_->dispatch_); } | 112 PrioritizedDispatcher& dispatch() { return *(test_->dispatch_); } |
| 113 | 113 |
| 114 PrioritizedDispatcherTest* test_; | 114 PrioritizedDispatcherTest* test_; |
|
stevez
2012/04/11 16:13:58
This seems a bit sketchy to me (it sets up circula
szym
2012/05/14 22:56:02
Done.
| |
| 115 | 115 |
| 116 char data_; | 116 char data_; |
| 117 Priority priority_; | 117 Priority priority_; |
| 118 | 118 |
| 119 PrioritizedDispatcher::Handle handle_; | 119 PrioritizedDispatcher::Handle handle_; |
| 120 bool running_; | 120 bool running_; |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 protected: | 123 protected: |
| 124 void Prepare(const PrioritizedDispatcher::Limits& limits) { | 124 void Prepare(const PrioritizedDispatcher::Limits& limits) { |
| 125 dispatch_.reset(new PrioritizedDispatcher(limits)); | 125 dispatch_.reset(new PrioritizedDispatcher(limits)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 TestJob* AddJob(char data, Priority priority) { | 128 TestJob* AddJob(char data, Priority priority) { |
| 129 TestJob* job = new TestJob(this, data, priority); | 129 TestJob* job = new TestJob(this, data, priority); |
| 130 jobs_.push_back(job); | 130 jobs_.push_back(job); |
| 131 job->Add(); | 131 job->Add(); |
| 132 return job; | 132 return job; |
| 133 } | 133 } |
| 134 | 134 |
| 135 void Expect(std::string log) { | 135 void Expect(std::string log) { |
| 136 EXPECT_EQ(0u, dispatch_->num_queued_jobs()); | 136 EXPECT_EQ(0u, dispatch_->num_queued_jobs()); |
| 137 EXPECT_EQ(0u, dispatch_->num_running_jobs()); | 137 EXPECT_EQ(0u, dispatch_->num_running_jobs()); |
| 138 EXPECT_EQ(log, log_); | 138 EXPECT_EQ(log, log_); |
| 139 log_.clear(); | 139 log_.clear(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 std::string log_; | 142 std::string log_; |
| 143 scoped_ptr<PrioritizedDispatcher> dispatch_; | 143 scoped_ptr<PrioritizedDispatcher> dispatch_; |
|
stevez
2012/04/11 16:13:58
dispatcher_
szym
2012/05/14 22:56:02
Done.
| |
| 144 ScopedVector<TestJob> jobs_; | 144 ScopedVector<TestJob> jobs_; |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { | 147 TEST_F(PrioritizedDispatcherTest, AddAFIFO) { |
| 148 // Allow only one running job. | 148 // Allow only one running job. |
| 149 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); | 149 PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1); |
| 150 Prepare(limits); | 150 Prepare(limits); |
| 151 | 151 |
| 152 TestJob* job_a = AddJob('a', IDLE); | 152 TestJob* job_a = AddJob('a', IDLE); |
| 153 TestJob* job_b = AddJob('b', IDLE); | 153 TestJob* job_b = AddJob('b', IDLE); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 | 265 |
| 266 EXPECT_EQ(job_b, dispatch_->EvictOldestLowest()); | 266 EXPECT_EQ(job_b, dispatch_->EvictOldestLowest()); |
| 267 EXPECT_EQ(job_d, dispatch_->EvictOldestLowest()); | 267 EXPECT_EQ(job_d, dispatch_->EvictOldestLowest()); |
| 268 | 268 |
| 269 job_a->Finish(); | 269 job_a->Finish(); |
| 270 job_c->Finish(); | 270 job_c->Finish(); |
| 271 job_e->Finish(); | 271 job_e->Finish(); |
| 272 | 272 |
| 273 Expect("a.c.e."); | 273 Expect("a.c.e."); |
| 274 } | 274 } |
| 275 | 275 |
|
stevez
2012/04/11 16:13:58
What about cancelling a null, or not-in-queue hand
szym
2012/05/14 22:56:02
Not legal. Will crash on DCHECK.
stevez
2012/05/17 17:57:07
Great, why not add a death test for that?
szym
2012/05/17 19:52:01
I can't find an example of a death test in Chromiu
szym
2012/05/17 20:28:58
Forgive my ignorance. I now added a death test.
| |
| 276 } // namespace | 276 } // namespace |
| 277 | 277 |
| 278 } // namespace net | 278 } // namespace net |
| 279 | 279 |
| 280 | |
| OLD | NEW |