[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 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 "net/dns/serial_worker.h" |
| 6 | |
[email protected] | 221c035c | 2011-11-30 06:37:40 | [diff] [blame] | 7 | #include "base/bind.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 8 | #include "base/location.h" |
[email protected] | 5ee2098 | 2013-07-17 21:51:18 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 10 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 11 | #include "base/single_thread_task_runner.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 12 | #include "base/synchronization/lock.h" |
| 13 | #include "base/synchronization/waitable_event.h" |
Francois Doray | becd1ef7 | 2017-09-25 20:58:45 | [diff] [blame^] | 14 | #include "base/threading/thread_restrictions.h" |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class SerialWorkerTest : public testing::Test { |
| 22 | public: |
| 23 | // The class under test |
| 24 | class TestSerialWorker : public SerialWorker { |
| 25 | public: |
Francois Doray | becd1ef7 | 2017-09-25 20:58:45 | [diff] [blame^] | 26 | explicit TestSerialWorker(SerialWorkerTest* t) : test_(t) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 27 | void DoWork() override { |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 28 | ASSERT_TRUE(test_); |
| 29 | test_->OnWork(); |
| 30 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 31 | void OnWorkFinished() override { |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 32 | ASSERT_TRUE(test_); |
| 33 | test_->OnWorkFinished(); |
| 34 | } |
| 35 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 36 | ~TestSerialWorker() override {} |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 37 | SerialWorkerTest* test_; |
| 38 | }; |
| 39 | |
| 40 | // Mocks |
| 41 | |
| 42 | void OnWork() { |
| 43 | { // Check that OnWork is executed serially. |
| 44 | base::AutoLock lock(work_lock_); |
| 45 | EXPECT_FALSE(work_running_) << "DoRead is not called serially!"; |
| 46 | work_running_ = true; |
| 47 | } |
| 48 | BreakNow("OnWork"); |
Francois Doray | becd1ef7 | 2017-09-25 20:58:45 | [diff] [blame^] | 49 | { |
| 50 | base::ScopedAllowBaseSyncPrimitivesForTesting |
| 51 | scoped_allow_base_sync_primitives; |
| 52 | work_allowed_.Wait(); |
| 53 | } |
| 54 | // Calling from TaskScheduler, but protected by work_allowed_/work_called_. |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 55 | output_value_ = input_value_; |
| 56 | |
| 57 | { // This lock might be destroyed after work_called_ is signalled. |
| 58 | base::AutoLock lock(work_lock_); |
| 59 | work_running_ = false; |
| 60 | } |
| 61 | work_called_.Signal(); |
| 62 | } |
| 63 | |
| 64 | void OnWorkFinished() { |
fdoray | 3920996 | 2016-06-29 14:44:29 | [diff] [blame] | 65 | EXPECT_TRUE(message_loop_->task_runner()->BelongsToCurrentThread()); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 66 | EXPECT_EQ(output_value_, input_value_); |
| 67 | BreakNow("OnWorkFinished"); |
| 68 | } |
| 69 | |
| 70 | protected: |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 71 | void BreakCallback(const std::string& breakpoint) { |
[email protected] | 221c035c | 2011-11-30 06:37:40 | [diff] [blame] | 72 | breakpoint_ = breakpoint; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 73 | base::RunLoop::QuitCurrentDeprecated(); |
[email protected] | 221c035c | 2011-11-30 06:37:40 | [diff] [blame] | 74 | } |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 75 | |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 76 | void BreakNow(const std::string& b) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 77 | message_loop_->task_runner()->PostTask( |
| 78 | FROM_HERE, base::Bind(&SerialWorkerTest::BreakCallback, |
| 79 | base::Unretained(this), b)); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 80 | } |
| 81 | |
ki.stfu | 144dce1 | 2015-09-22 01:07:57 | [diff] [blame] | 82 | void RunUntilBreak(const std::string& b) { |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 83 | base::RunLoop().Run(); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 84 | ASSERT_EQ(breakpoint_, b); |
| 85 | } |
| 86 | |
| 87 | SerialWorkerTest() |
| 88 | : input_value_(0), |
| 89 | output_value_(-1), |
gab | 07efd10 | 2016-06-02 13:33:17 | [diff] [blame] | 90 | work_allowed_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 91 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 92 | work_called_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 93 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 94 | work_running_(false) {} |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 95 | |
| 96 | // Helpers for tests. |
| 97 | |
| 98 | // Lets OnWork run and waits for it to complete. Can only return if OnWork is |
| 99 | // executed on a concurrent thread. |
| 100 | void WaitForWork() { |
| 101 | RunUntilBreak("OnWork"); |
| 102 | work_allowed_.Signal(); |
| 103 | work_called_.Wait(); |
| 104 | } |
| 105 | |
| 106 | // test::Test methods |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 107 | void SetUp() override { |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 108 | message_loop_ = base::MessageLoop::current(); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 109 | worker_ = new TestSerialWorker(this); |
| 110 | } |
| 111 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 112 | void TearDown() override { |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 113 | // Cancel the worker to catch if it makes a late DoWork call. |
| 114 | worker_->Cancel(); |
| 115 | // Check if OnWork is stalled. |
| 116 | EXPECT_FALSE(work_running_) << "OnWork should be done by TearDown"; |
| 117 | // Release it for cleanliness. |
| 118 | if (work_running_) { |
| 119 | WaitForWork(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Input value read on WorkerPool. |
| 124 | int input_value_; |
| 125 | // Output value written on WorkerPool. |
| 126 | int output_value_; |
| 127 | |
| 128 | // read is called on WorkerPool so we need to synchronize with it. |
| 129 | base::WaitableEvent work_allowed_; |
| 130 | base::WaitableEvent work_called_; |
| 131 | |
| 132 | // Protected by read_lock_. Used to verify that read calls are serialized. |
| 133 | bool work_running_; |
| 134 | base::Lock work_lock_; |
| 135 | |
| 136 | // Loop for this thread. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 137 | base::MessageLoop* message_loop_; |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 138 | |
| 139 | // WatcherDelegate under test. |
| 140 | scoped_refptr<TestSerialWorker> worker_; |
| 141 | |
| 142 | std::string breakpoint_; |
| 143 | }; |
| 144 | |
| 145 | TEST_F(SerialWorkerTest, ExecuteAndSerializeReads) { |
| 146 | for (int i = 0; i < 3; ++i) { |
| 147 | ++input_value_; |
| 148 | worker_->WorkNow(); |
| 149 | WaitForWork(); |
| 150 | RunUntilBreak("OnWorkFinished"); |
| 151 | |
[email protected] | 54aa4f1 | 2013-07-22 22:24:13 | [diff] [blame] | 152 | EXPECT_TRUE(message_loop_->IsIdleForTesting()); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Schedule two calls. OnWork checks if it is called serially. |
| 156 | ++input_value_; |
| 157 | worker_->WorkNow(); |
| 158 | // read is blocked, so this will have to induce re-work |
| 159 | worker_->WorkNow(); |
| 160 | WaitForWork(); |
| 161 | WaitForWork(); |
| 162 | RunUntilBreak("OnWorkFinished"); |
| 163 | |
| 164 | // No more tasks should remain. |
[email protected] | 54aa4f1 | 2013-07-22 22:24:13 | [diff] [blame] | 165 | EXPECT_TRUE(message_loop_->IsIdleForTesting()); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | } // namespace |
| 169 | |
| 170 | } // namespace net |