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
3 // found in the LICENSE file.
5 #include "base/deferred_sequenced_task_runner.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/threading/thread.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class DeferredSequencedTaskRunnerTest
: public testing::Test
,
21 public base::NonThreadSafe
{
23 class ExecuteTaskOnDestructor
:
24 public base::RefCounted
<ExecuteTaskOnDestructor
> {
26 ExecuteTaskOnDestructor(
27 DeferredSequencedTaskRunnerTest
* executor
,
29 : executor_(executor
),
33 friend class base::RefCounted
<ExecuteTaskOnDestructor
>;
34 virtual ~ExecuteTaskOnDestructor() {
35 executor_
->ExecuteTask(task_id_
);
37 DeferredSequencedTaskRunnerTest
* executor_
;
41 void ExecuteTask(int task_id
) {
42 base::AutoLock
lock(lock_
);
43 executed_task_ids_
.push_back(task_id
);
46 void PostExecuteTask(int task_id
) {
47 runner_
->PostTask(FROM_HERE
,
48 base::Bind(&DeferredSequencedTaskRunnerTest::ExecuteTask
,
49 base::Unretained(this),
57 void DoNothing(ExecuteTaskOnDestructor
* object
) {
61 DeferredSequencedTaskRunnerTest() :
64 new base::DeferredSequencedTaskRunner(loop_
.message_loop_proxy())) {
67 base::MessageLoop loop_
;
68 scoped_refptr
<base::DeferredSequencedTaskRunner
> runner_
;
69 mutable base::Lock lock_
;
70 std::vector
<int> executed_task_ids_
;
73 TEST_F(DeferredSequencedTaskRunnerTest
, Stopped
) {
76 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre());
79 TEST_F(DeferredSequencedTaskRunnerTest
, Start
) {
83 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1));
86 TEST_F(DeferredSequencedTaskRunnerTest
, StartWithMultipleElements
) {
88 for (int i
= 1; i
< 5; ++i
)
92 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1, 2, 3, 4));
95 TEST_F(DeferredSequencedTaskRunnerTest
, DeferredStart
) {
98 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre());
101 loop_
.RunUntilIdle();
102 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1));
105 loop_
.RunUntilIdle();
106 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1, 2));
109 TEST_F(DeferredSequencedTaskRunnerTest
, DeferredStartWithMultipleElements
) {
110 for (int i
= 1; i
< 5; ++i
)
112 loop_
.RunUntilIdle();
113 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre());
116 for (int i
= 5; i
< 9; ++i
)
118 loop_
.RunUntilIdle();
119 EXPECT_THAT(executed_task_ids_
, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
122 TEST_F(DeferredSequencedTaskRunnerTest
, DeferredStartWithMultipleThreads
) {
124 base::Thread
thread1("DeferredSequencedTaskRunnerTestThread1");
125 base::Thread
thread2("DeferredSequencedTaskRunnerTestThread2");
128 for (int i
= 0; i
< 5; ++i
) {
129 thread1
.message_loop()->PostTask(
131 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask
,
132 base::Unretained(this),
134 thread2
.message_loop()->PostTask(
136 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask
,
137 base::Unretained(this),
140 thread1
.message_loop()->PostTask(
142 base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner
,
143 base::Unretained(this)));
148 loop_
.RunUntilIdle();
149 EXPECT_THAT(executed_task_ids_
,
150 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
153 TEST_F(DeferredSequencedTaskRunnerTest
, ObjectDestructionOrder
) {
155 base::Thread
thread("DeferredSequencedTaskRunnerTestThread");
158 new base::DeferredSequencedTaskRunner(thread
.message_loop_proxy());
159 for (int i
= 0; i
< 5; ++i
) {
161 // Use a block to ensure that no reference to |short_lived_object|
162 // is kept on the main thread after it is posted to |runner_|.
163 scoped_refptr
<ExecuteTaskOnDestructor
> short_lived_object
=
164 new ExecuteTaskOnDestructor(this, 2 * i
);
167 base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing
,
168 base::Unretained(this),
169 short_lived_object
));
171 // |short_lived_object| with id |2 * i| should be destroyed before the
172 // task |2 * i + 1| is executed.
173 PostExecuteTask(2 * i
+ 1);
178 // All |short_lived_object| with id |2 * i| are destroyed before the task
179 // |2 * i + 1| is executed.
180 EXPECT_THAT(executed_task_ids_
,
181 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));