OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/deferred_sequenced_task_runner.h" | 5 #include "base/deferred_sequenced_task_runner.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 class DeferredSequencedTaskRunnerTest : public testing::Test, | 20 class DeferredSequencedTaskRunnerTest : public testing::Test, |
21 public base::NonThreadSafe { | 21 public base::NonThreadSafe { |
22 public: | 22 public: |
| 23 // TODO(mgiuca): Avoid using UnsafeRefCounted. http://crbug.com/469952. |
23 class ExecuteTaskOnDestructor : | 24 class ExecuteTaskOnDestructor : |
24 public base::RefCounted<ExecuteTaskOnDestructor> { | 25 public base::UnsafeRefCounted<ExecuteTaskOnDestructor> { |
25 public: | 26 public: |
26 ExecuteTaskOnDestructor( | 27 ExecuteTaskOnDestructor( |
27 DeferredSequencedTaskRunnerTest* executor, | 28 DeferredSequencedTaskRunnerTest* executor, |
28 int task_id) | 29 int task_id) |
29 : executor_(executor), | 30 : executor_(executor), |
30 task_id_(task_id) { | 31 task_id_(task_id) { |
31 } | 32 } |
32 private: | 33 private: |
33 friend class base::RefCounted<ExecuteTaskOnDestructor>; | 34 friend class base::UnsafeRefCounted<ExecuteTaskOnDestructor>; |
34 virtual ~ExecuteTaskOnDestructor() { | 35 virtual ~ExecuteTaskOnDestructor() { |
35 executor_->ExecuteTask(task_id_); | 36 executor_->ExecuteTask(task_id_); |
36 } | 37 } |
37 DeferredSequencedTaskRunnerTest* executor_; | 38 DeferredSequencedTaskRunnerTest* executor_; |
38 int task_id_; | 39 int task_id_; |
39 }; | 40 }; |
40 | 41 |
41 void ExecuteTask(int task_id) { | 42 void ExecuteTask(int task_id) { |
42 base::AutoLock lock(lock_); | 43 base::AutoLock lock(lock_); |
43 executed_task_ids_.push_back(task_id); | 44 executed_task_ids_.push_back(task_id); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 StartRunner(); | 176 StartRunner(); |
176 } | 177 } |
177 | 178 |
178 // All |short_lived_object| with id |2 * i| are destroyed before the task | 179 // All |short_lived_object| with id |2 * i| are destroyed before the task |
179 // |2 * i + 1| is executed. | 180 // |2 * i + 1| is executed. |
180 EXPECT_THAT(executed_task_ids_, | 181 EXPECT_THAT(executed_task_ids_, |
181 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); | 182 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)); |
182 } | 183 } |
183 | 184 |
184 } // namespace | 185 } // namespace |
OLD | NEW |