[Android] Make adb_install_apk use the device blacklist.
[chromium-blink-merge.git] / base / deferred_sequenced_task_runner_unittest.cc
blob6e17ad93a8fc89e27cd45ba6065ced5d08a5c9de
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"
8 #include "base/bind.h"
9 #include "base/bind_helpers.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.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"
18 namespace {
20 class DeferredSequencedTaskRunnerTest : public testing::Test,
21 public base::NonThreadSafe {
22 public:
23 class ExecuteTaskOnDestructor :
24 public base::RefCounted<ExecuteTaskOnDestructor> {
25 public:
26 ExecuteTaskOnDestructor(
27 DeferredSequencedTaskRunnerTest* executor,
28 int task_id)
29 : executor_(executor),
30 task_id_(task_id) {
32 private:
33 friend class base::RefCounted<ExecuteTaskOnDestructor>;
34 virtual ~ExecuteTaskOnDestructor() {
35 executor_->ExecuteTask(task_id_);
37 DeferredSequencedTaskRunnerTest* executor_;
38 int task_id_;
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),
50 task_id));
53 void StartRunner() {
54 runner_->Start();
57 void DoNothing(ExecuteTaskOnDestructor* object) {
60 protected:
61 DeferredSequencedTaskRunnerTest()
62 : loop_(),
63 runner_(new base::DeferredSequencedTaskRunner(loop_.task_runner())) {}
65 base::MessageLoop loop_;
66 scoped_refptr<base::DeferredSequencedTaskRunner> runner_;
67 mutable base::Lock lock_;
68 std::vector<int> executed_task_ids_;
71 TEST_F(DeferredSequencedTaskRunnerTest, Stopped) {
72 PostExecuteTask(1);
73 loop_.RunUntilIdle();
74 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
77 TEST_F(DeferredSequencedTaskRunnerTest, Start) {
78 StartRunner();
79 PostExecuteTask(1);
80 loop_.RunUntilIdle();
81 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
84 TEST_F(DeferredSequencedTaskRunnerTest, StartWithMultipleElements) {
85 StartRunner();
86 for (int i = 1; i < 5; ++i)
87 PostExecuteTask(i);
89 loop_.RunUntilIdle();
90 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4));
93 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStart) {
94 PostExecuteTask(1);
95 loop_.RunUntilIdle();
96 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
98 StartRunner();
99 loop_.RunUntilIdle();
100 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1));
102 PostExecuteTask(2);
103 loop_.RunUntilIdle();
104 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2));
107 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleElements) {
108 for (int i = 1; i < 5; ++i)
109 PostExecuteTask(i);
110 loop_.RunUntilIdle();
111 EXPECT_THAT(executed_task_ids_, testing::ElementsAre());
113 StartRunner();
114 for (int i = 5; i < 9; ++i)
115 PostExecuteTask(i);
116 loop_.RunUntilIdle();
117 EXPECT_THAT(executed_task_ids_, testing::ElementsAre(1, 2, 3, 4, 5, 6, 7, 8));
120 TEST_F(DeferredSequencedTaskRunnerTest, DeferredStartWithMultipleThreads) {
122 base::Thread thread1("DeferredSequencedTaskRunnerTestThread1");
123 base::Thread thread2("DeferredSequencedTaskRunnerTestThread2");
124 thread1.Start();
125 thread2.Start();
126 for (int i = 0; i < 5; ++i) {
127 thread1.task_runner()->PostTask(
128 FROM_HERE,
129 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
130 base::Unretained(this), 2 * i));
131 thread2.task_runner()->PostTask(
132 FROM_HERE,
133 base::Bind(&DeferredSequencedTaskRunnerTest::PostExecuteTask,
134 base::Unretained(this), 2 * i + 1));
135 if (i == 2) {
136 thread1.task_runner()->PostTask(
137 FROM_HERE, base::Bind(&DeferredSequencedTaskRunnerTest::StartRunner,
138 base::Unretained(this)));
143 loop_.RunUntilIdle();
144 EXPECT_THAT(executed_task_ids_,
145 testing::WhenSorted(testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)));
148 TEST_F(DeferredSequencedTaskRunnerTest, ObjectDestructionOrder) {
150 base::Thread thread("DeferredSequencedTaskRunnerTestThread");
151 thread.Start();
152 runner_ = new base::DeferredSequencedTaskRunner(thread.task_runner());
153 for (int i = 0; i < 5; ++i) {
155 // Use a block to ensure that no reference to |short_lived_object|
156 // is kept on the main thread after it is posted to |runner_|.
157 scoped_refptr<ExecuteTaskOnDestructor> short_lived_object =
158 new ExecuteTaskOnDestructor(this, 2 * i);
159 runner_->PostTask(
160 FROM_HERE,
161 base::Bind(&DeferredSequencedTaskRunnerTest::DoNothing,
162 base::Unretained(this),
163 short_lived_object));
165 // |short_lived_object| with id |2 * i| should be destroyed before the
166 // task |2 * i + 1| is executed.
167 PostExecuteTask(2 * i + 1);
169 StartRunner();
172 // All |short_lived_object| with id |2 * i| are destroyed before the task
173 // |2 * i + 1| is executed.
174 EXPECT_THAT(executed_task_ids_,
175 testing::ElementsAre(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
178 } // namespace