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