[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: |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 36 | ~TestSerialWorker() override = default; |
[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; |
Wez | 6122361 | 2018-01-04 17:36:17 | [diff] [blame^] | 73 | run_loop_->Quit(); |
[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) { |
Wez | 6122361 | 2018-01-04 17:36:17 | [diff] [blame^] | 83 | base::RunLoop run_loop; |
| 84 | ASSERT_FALSE(run_loop_); |
| 85 | run_loop_ = &run_loop; |
| 86 | run_loop_->Run(); |
| 87 | run_loop_ = nullptr; |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 88 | ASSERT_EQ(breakpoint_, b); |
| 89 | } |
| 90 | |
| 91 | SerialWorkerTest() |
| 92 | : input_value_(0), |
| 93 | output_value_(-1), |
gab | 07efd10 | 2016-06-02 13:33:17 | [diff] [blame] | 94 | work_allowed_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 95 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 96 | work_called_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 97 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 98 | work_running_(false) {} |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 99 | |
| 100 | // Helpers for tests. |
| 101 | |
| 102 | // Lets OnWork run and waits for it to complete. Can only return if OnWork is |
| 103 | // executed on a concurrent thread. |
| 104 | void WaitForWork() { |
| 105 | RunUntilBreak("OnWork"); |
| 106 | work_allowed_.Signal(); |
| 107 | work_called_.Wait(); |
| 108 | } |
| 109 | |
| 110 | // test::Test methods |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 111 | void SetUp() override { |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 112 | message_loop_ = base::MessageLoop::current(); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 113 | worker_ = new TestSerialWorker(this); |
| 114 | } |
| 115 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 116 | void TearDown() override { |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 117 | // Cancel the worker to catch if it makes a late DoWork call. |
| 118 | worker_->Cancel(); |
| 119 | // Check if OnWork is stalled. |
| 120 | EXPECT_FALSE(work_running_) << "OnWork should be done by TearDown"; |
| 121 | // Release it for cleanliness. |
| 122 | if (work_running_) { |
| 123 | WaitForWork(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Input value read on WorkerPool. |
| 128 | int input_value_; |
| 129 | // Output value written on WorkerPool. |
| 130 | int output_value_; |
| 131 | |
| 132 | // read is called on WorkerPool so we need to synchronize with it. |
| 133 | base::WaitableEvent work_allowed_; |
| 134 | base::WaitableEvent work_called_; |
| 135 | |
| 136 | // Protected by read_lock_. Used to verify that read calls are serialized. |
| 137 | bool work_running_; |
| 138 | base::Lock work_lock_; |
| 139 | |
| 140 | // Loop for this thread. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 141 | base::MessageLoop* message_loop_; |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 142 | |
| 143 | // WatcherDelegate under test. |
| 144 | scoped_refptr<TestSerialWorker> worker_; |
| 145 | |
| 146 | std::string breakpoint_; |
Wez | 6122361 | 2018-01-04 17:36:17 | [diff] [blame^] | 147 | base::RunLoop* run_loop_ = nullptr; |
| 148 | |
| 149 | DISALLOW_COPY_AND_ASSIGN(SerialWorkerTest); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | TEST_F(SerialWorkerTest, ExecuteAndSerializeReads) { |
| 153 | for (int i = 0; i < 3; ++i) { |
| 154 | ++input_value_; |
| 155 | worker_->WorkNow(); |
| 156 | WaitForWork(); |
| 157 | RunUntilBreak("OnWorkFinished"); |
| 158 | |
[email protected] | 54aa4f1 | 2013-07-22 22:24:13 | [diff] [blame] | 159 | EXPECT_TRUE(message_loop_->IsIdleForTesting()); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Schedule two calls. OnWork checks if it is called serially. |
| 163 | ++input_value_; |
| 164 | worker_->WorkNow(); |
| 165 | // read is blocked, so this will have to induce re-work |
| 166 | worker_->WorkNow(); |
| 167 | WaitForWork(); |
| 168 | WaitForWork(); |
| 169 | RunUntilBreak("OnWorkFinished"); |
| 170 | |
| 171 | // No more tasks should remain. |
[email protected] | 54aa4f1 | 2013-07-22 22:24:13 | [diff] [blame] | 172 | EXPECT_TRUE(message_loop_->IsIdleForTesting()); |
[email protected] | 76d7f72 | 2011-10-10 17:22:41 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // namespace |
| 176 | |
| 177 | } // namespace net |