| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "net/dns/serial_worker.h" | 5 #include "net/dns/serial_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 class SerialWorkerTest : public testing::Test { | 17 class SerialWorkerTest : public testing::Test { |
| 18 public: | 18 public: |
| 19 // The class under test | 19 // The class under test |
| 20 class TestSerialWorker : public SerialWorker { | 20 class TestSerialWorker : public SerialWorker { |
| 21 public: | 21 public: |
| 22 explicit TestSerialWorker(SerialWorkerTest* t) | 22 explicit TestSerialWorker(SerialWorkerTest* t) : test_(t) {} |
| 23 : test_(t) {} | |
| 24 virtual void DoWork() OVERRIDE { | 23 virtual void DoWork() OVERRIDE { |
| 25 ASSERT_TRUE(test_); | 24 ASSERT_TRUE(test_); |
| 26 test_->OnWork(); | 25 test_->OnWork(); |
| 27 } | 26 } |
| 28 virtual void OnWorkFinished() OVERRIDE { | 27 virtual void OnWorkFinished() OVERRIDE { |
| 29 ASSERT_TRUE(test_); | 28 ASSERT_TRUE(test_); |
| 30 test_->OnWorkFinished(); | 29 test_->OnWorkFinished(); |
| 31 } | 30 } |
| 31 |
| 32 private: | 32 private: |
| 33 virtual ~TestSerialWorker() {} | 33 virtual ~TestSerialWorker() {} |
| 34 SerialWorkerTest* test_; | 34 SerialWorkerTest* test_; |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 // Mocks | 37 // Mocks |
| 38 | 38 |
| 39 void OnWork() { | 39 void OnWork() { |
| 40 { // Check that OnWork is executed serially. | 40 { // Check that OnWork is executed serially. |
| 41 base::AutoLock lock(work_lock_); | 41 base::AutoLock lock(work_lock_); |
| 42 EXPECT_FALSE(work_running_) << "DoRead is not called serially!"; | 42 EXPECT_FALSE(work_running_) << "DoRead is not called serially!"; |
| 43 work_running_ = true; | 43 work_running_ = true; |
| 44 } | 44 } |
| 45 BreakNow("OnWork"); | 45 BreakNow("OnWork"); |
| 46 work_allowed_.Wait(); | 46 work_allowed_.Wait(); |
| 47 // Calling from WorkerPool, but protected by work_allowed_/work_called_. | 47 // Calling from WorkerPool, but protected by work_allowed_/work_called_. |
| 48 output_value_ = input_value_; | 48 output_value_ = input_value_; |
| 49 | 49 |
| 50 { // This lock might be destroyed after work_called_ is signalled. | 50 { // This lock might be destroyed after work_called_ is signalled. |
| 51 base::AutoLock lock(work_lock_); | 51 base::AutoLock lock(work_lock_); |
| 52 work_running_ = false; | 52 work_running_ = false; |
| 53 } | 53 } |
| 54 work_called_.Signal(); | 54 work_called_.Signal(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void OnWorkFinished() { | 57 void OnWorkFinished() { |
| 58 EXPECT_TRUE(message_loop_ == base::MessageLoop::current()); | 58 EXPECT_TRUE(message_loop_ == base::MessageLoop::current()); |
| 59 EXPECT_EQ(output_value_, input_value_); | 59 EXPECT_EQ(output_value_, input_value_); |
| 60 BreakNow("OnWorkFinished"); | 60 BreakNow("OnWorkFinished"); |
| 61 } | 61 } |
| 62 | 62 |
| 63 protected: | 63 protected: |
| 64 void BreakCallback(std::string breakpoint) { | 64 void BreakCallback(std::string breakpoint) { |
| 65 breakpoint_ = breakpoint; | 65 breakpoint_ = breakpoint; |
| 66 base::MessageLoop::current()->QuitNow(); | 66 base::MessageLoop::current()->QuitNow(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void BreakNow(std::string b) { | 69 void BreakNow(std::string b) { |
| 70 message_loop_->PostTask(FROM_HERE, | 70 message_loop_->PostTask( |
| 71 base::Bind(&SerialWorkerTest::BreakCallback, | 71 FROM_HERE, |
| 72 base::Unretained(this), b)); | 72 base::Bind( |
| 73 &SerialWorkerTest::BreakCallback, base::Unretained(this), b)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void RunUntilBreak(std::string b) { | 76 void RunUntilBreak(std::string b) { |
| 76 message_loop_->Run(); | 77 message_loop_->Run(); |
| 77 ASSERT_EQ(breakpoint_, b); | 78 ASSERT_EQ(breakpoint_, b); |
| 78 } | 79 } |
| 79 | 80 |
| 80 SerialWorkerTest() | 81 SerialWorkerTest() |
| 81 : input_value_(0), | 82 : input_value_(0), |
| 82 output_value_(-1), | 83 output_value_(-1), |
| 83 work_allowed_(false, false), | 84 work_allowed_(false, false), |
| 84 work_called_(false, false), | 85 work_called_(false, false), |
| 85 work_running_(false) { | 86 work_running_(false) {} |
| 86 } | |
| 87 | 87 |
| 88 // Helpers for tests. | 88 // Helpers for tests. |
| 89 | 89 |
| 90 // Lets OnWork run and waits for it to complete. Can only return if OnWork is | 90 // Lets OnWork run and waits for it to complete. Can only return if OnWork is |
| 91 // executed on a concurrent thread. | 91 // executed on a concurrent thread. |
| 92 void WaitForWork() { | 92 void WaitForWork() { |
| 93 RunUntilBreak("OnWork"); | 93 RunUntilBreak("OnWork"); |
| 94 work_allowed_.Signal(); | 94 work_allowed_.Signal(); |
| 95 work_called_.Wait(); | 95 work_called_.Wait(); |
| 96 } | 96 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 WaitForWork(); | 153 WaitForWork(); |
| 154 RunUntilBreak("OnWorkFinished"); | 154 RunUntilBreak("OnWorkFinished"); |
| 155 | 155 |
| 156 // No more tasks should remain. | 156 // No more tasks should remain. |
| 157 EXPECT_TRUE(message_loop_->IsIdleForTesting()); | 157 EXPECT_TRUE(message_loop_->IsIdleForTesting()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 } // namespace | 160 } // namespace |
| 161 | 161 |
| 162 } // namespace net | 162 } // namespace net |
| 163 | |
| OLD | NEW |