OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "sync/internal_api/public/base/cancelation_signal.h" | 5 #include "components/sync/base/cancelation_signal.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/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "sync/internal_api/public/base/cancelation_observer.h" | 14 #include "components/sync/base/cancelation_observer.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace syncer { | 17 namespace syncer { |
18 | 18 |
19 class BlockingTask : public CancelationObserver { | 19 class BlockingTask : public CancelationObserver { |
20 public: | 20 public: |
21 explicit BlockingTask(CancelationSignal* cancel_signal); | 21 explicit BlockingTask(CancelationSignal* cancel_signal); |
22 ~BlockingTask() override; | 22 ~BlockingTask() override; |
23 | 23 |
24 // Starts the |exec_thread_| and uses it to execute DoRun(). | 24 // Starts the |exec_thread_| and uses it to execute DoRun(). |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 void BlockingTask::RunAsync(base::WaitableEvent* task_start_signal, | 64 void BlockingTask::RunAsync(base::WaitableEvent* task_start_signal, |
65 base::WaitableEvent* task_done_signal) { | 65 base::WaitableEvent* task_done_signal) { |
66 exec_thread_.Start(); | 66 exec_thread_.Start(); |
67 exec_thread_.task_runner()->PostTask( | 67 exec_thread_.task_runner()->PostTask( |
68 FROM_HERE, base::Bind(&BlockingTask::Run, base::Unretained(this), | 68 FROM_HERE, base::Bind(&BlockingTask::Run, base::Unretained(this), |
69 base::Unretained(task_start_signal), | 69 base::Unretained(task_start_signal), |
70 base::Unretained(task_done_signal))); | 70 base::Unretained(task_done_signal))); |
71 } | 71 } |
72 | 72 |
73 void BlockingTask::Run( | 73 void BlockingTask::Run(base::WaitableEvent* task_start_signal, |
74 base::WaitableEvent* task_start_signal, | 74 base::WaitableEvent* task_done_signal) { |
75 base::WaitableEvent* task_done_signal) { | |
76 if (cancel_signal_->TryRegisterHandler(this)) { | 75 if (cancel_signal_->TryRegisterHandler(this)) { |
77 DCHECK(!event_.IsSignaled()); | 76 DCHECK(!event_.IsSignaled()); |
78 was_started_ = true; | 77 was_started_ = true; |
79 task_start_signal->Signal(); | 78 task_start_signal->Signal(); |
80 event_.Wait(); | 79 event_.Wait(); |
81 } | 80 } |
82 task_done_signal->Signal(); | 81 task_done_signal->Signal(); |
83 } | 82 } |
84 | 83 |
85 void BlockingTask::OnSignalReceived() { | 84 void BlockingTask::OnSignalReceived() { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 // progress. | 178 // progress. |
180 TEST_F(CancelationSignalTest, Cancel) { | 179 TEST_F(CancelationSignalTest, Cancel) { |
181 StartBlockingTaskAndWaitForItToStart(); | 180 StartBlockingTaskAndWaitForItToStart(); |
182 | 181 |
183 // Wait for the task to finish and let verify it has been started. | 182 // Wait for the task to finish and let verify it has been started. |
184 CancelBlocking(); | 183 CancelBlocking(); |
185 EXPECT_FALSE(VerifyTaskNotStarted()); | 184 EXPECT_FALSE(VerifyTaskNotStarted()); |
186 } | 185 } |
187 | 186 |
188 } // namespace syncer | 187 } // namespace syncer |
OLD | NEW |