1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
base / task / lazy_thread_pool_task_runner_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/lazy_thread_pool_task_runner.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/sequence_checker_impl.h"
#include "base/task/scoped_set_task_priority_for_current_thread.h"
#include "base/test/task_environment.h"
#include "base/threading/thread_checker_impl.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/com_init_util.h"
#endif
namespace base {
namespace {
LazyThreadPoolSequencedTaskRunner g_sequenced_task_runner_user_visible =
LAZY_THREAD_POOL_SEQUENCED_TASK_RUNNER_INITIALIZER(
TaskTraits(TaskPriority::USER_VISIBLE));
LazyThreadPoolSequencedTaskRunner g_sequenced_task_runner_user_blocking =
LAZY_THREAD_POOL_SEQUENCED_TASK_RUNNER_INITIALIZER(
TaskTraits(TaskPriority::USER_BLOCKING));
LazyThreadPoolSingleThreadTaskRunner g_single_thread_task_runner_user_visible =
LAZY_THREAD_POOL_SINGLE_THREAD_TASK_RUNNER_INITIALIZER(
TaskTraits(TaskPriority::USER_VISIBLE),
SingleThreadTaskRunnerThreadMode::SHARED);
LazyThreadPoolSingleThreadTaskRunner g_single_thread_task_runner_user_blocking =
LAZY_THREAD_POOL_SINGLE_THREAD_TASK_RUNNER_INITIALIZER(
TaskTraits(TaskPriority::USER_BLOCKING),
SingleThreadTaskRunnerThreadMode::SHARED);
#if BUILDFLAG(IS_WIN)
LazyThreadPoolCOMSTATaskRunner g_com_sta_task_runner_user_visible =
LAZY_COM_STA_TASK_RUNNER_INITIALIZER(
TaskTraits(TaskPriority::USER_VISIBLE),
SingleThreadTaskRunnerThreadMode::SHARED);
LazyThreadPoolCOMSTATaskRunner g_com_sta_task_runner_user_blocking =
LAZY_COM_STA_TASK_RUNNER_INITIALIZER(
TaskTraits(TaskPriority::USER_BLOCKING),
SingleThreadTaskRunnerThreadMode::SHARED);
#endif // BUILDFLAG(IS_WIN)
void InitCheckers(SequenceCheckerImpl* sequence_checker,
ThreadCheckerImpl* thread_checker) {
sequence_checker->DetachFromSequence();
EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
thread_checker->DetachFromThread();
EXPECT_TRUE(thread_checker->CalledOnValidThread());
}
void ExpectSequencedEnvironment(SequenceCheckerImpl* sequence_checker,
ThreadCheckerImpl* thread_checker,
TaskPriority expected_priority) {
EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
EXPECT_FALSE(thread_checker->CalledOnValidThread());
EXPECT_EQ(expected_priority, internal::GetTaskPriorityForCurrentThread());
}
void ExpectSingleThreadEnvironment(SequenceCheckerImpl* sequence_checker,
ThreadCheckerImpl* thread_checker,
TaskPriority expected_priority
#if BUILDFLAG(IS_WIN)
,
bool expect_com_sta = false
#endif
) {
EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
EXPECT_TRUE(thread_checker->CalledOnValidThread());
EXPECT_EQ(expected_priority, internal::GetTaskPriorityForCurrentThread());
#if BUILDFLAG(IS_WIN)
if (expect_com_sta)
win::AssertComApartmentType(win::ComApartmentType::STA);
#endif
}
class LazyThreadPoolTaskRunnerEnvironmentTest : public testing::Test {
public:
LazyThreadPoolTaskRunnerEnvironmentTest(
const LazyThreadPoolTaskRunnerEnvironmentTest&) = delete;
LazyThreadPoolTaskRunnerEnvironmentTest& operator=(
const LazyThreadPoolTaskRunnerEnvironmentTest&) = delete;
protected:
LazyThreadPoolTaskRunnerEnvironmentTest() = default;
void TestTaskRunnerEnvironment(scoped_refptr<SequencedTaskRunner> task_runner,
bool expect_single_thread,
TaskPriority expected_priority
#if BUILDFLAG(IS_WIN)
,
bool expect_com_sta = false
#endif
) {
SequenceCheckerImpl sequence_checker;
ThreadCheckerImpl thread_checker;
task_runner->PostTask(FROM_HERE,
BindOnce(&InitCheckers, Unretained(&sequence_checker),
Unretained(&thread_checker)));
task_environment_.RunUntilIdle();
OnceClosure task =
expect_single_thread
? BindOnce(&ExpectSingleThreadEnvironment,
Unretained(&sequence_checker),
Unretained(&thread_checker), expected_priority
#if BUILDFLAG(IS_WIN)
,
expect_com_sta
#endif
)
: BindOnce(&ExpectSequencedEnvironment,
Unretained(&sequence_checker),
Unretained(&thread_checker), expected_priority);
task_runner->PostTask(FROM_HERE, std::move(task));
task_environment_.RunUntilIdle();
}
test::TaskEnvironment task_environment_;
};
} // namespace
TEST_F(LazyThreadPoolTaskRunnerEnvironmentTest,
LazyThreadPoolSequencedTaskRunnerUserVisible) {
TestTaskRunnerEnvironment(g_sequenced_task_runner_user_visible.Get(), false,
TaskPriority::USER_VISIBLE);
}
TEST_F(LazyThreadPoolTaskRunnerEnvironmentTest,
LazyThreadPoolSequencedTaskRunnerUserBlocking) {
TestTaskRunnerEnvironment(g_sequenced_task_runner_user_blocking.Get(), false,
TaskPriority::USER_BLOCKING);
}
TEST_F(LazyThreadPoolTaskRunnerEnvironmentTest,
LazyThreadPoolSingleThreadTaskRunnerUserVisible) {
TestTaskRunnerEnvironment(g_single_thread_task_runner_user_visible.Get(),
true, TaskPriority::USER_VISIBLE);
}
TEST_F(LazyThreadPoolTaskRunnerEnvironmentTest,
LazyThreadPoolSingleThreadTaskRunnerUserBlocking) {
TestTaskRunnerEnvironment(g_single_thread_task_runner_user_blocking.Get(),
true, TaskPriority::USER_BLOCKING);
}
#if BUILDFLAG(IS_WIN)
TEST_F(LazyThreadPoolTaskRunnerEnvironmentTest,
LazyThreadPoolCOMSTATaskRunnerUserVisible) {
TestTaskRunnerEnvironment(g_com_sta_task_runner_user_visible.Get(), true,
TaskPriority::USER_VISIBLE, true);
}
TEST_F(LazyThreadPoolTaskRunnerEnvironmentTest,
LazyThreadPoolCOMSTATaskRunnerUserBlocking) {
TestTaskRunnerEnvironment(g_com_sta_task_runner_user_blocking.Get(), true,
TaskPriority::USER_BLOCKING, true);
}
#endif // BUILDFLAG(IS_WIN)
TEST(LazyThreadPoolTaskRunnerTest, LazyThreadPoolSequencedTaskRunnerReset) {
for (int i = 0; i < 2; ++i) {
test::TaskEnvironment task_environment;
// If the TaskRunner isn't released when the test::TaskEnvironment
// goes out of scope, the second invocation of the line below will access a
// deleted ThreadPoolInstance and crash.
g_sequenced_task_runner_user_visible.Get()->PostTask(FROM_HERE,
DoNothing());
}
}
TEST(LazyThreadPoolTaskRunnerTest, LazyThreadPoolSingleThreadTaskRunnerReset) {
for (int i = 0; i < 2; ++i) {
test::TaskEnvironment task_environment;
// If the TaskRunner isn't released when the test::TaskEnvironment
// goes out of scope, the second invocation of the line below will access a
// deleted ThreadPoolInstance and crash.
g_single_thread_task_runner_user_visible.Get()->PostTask(FROM_HERE,
DoNothing());
}
}
#if BUILDFLAG(IS_WIN)
TEST(LazyThreadPoolTaskRunnerTest, LazyThreadPoolCOMSTATaskRunnerReset) {
for (int i = 0; i < 2; ++i) {
test::TaskEnvironment task_environment;
// If the TaskRunner isn't released when the test::TaskEnvironment
// goes out of scope, the second invocation of the line below will access a
// deleted ThreadPoolInstance and crash.
g_com_sta_task_runner_user_visible.Get()->PostTask(FROM_HERE, DoNothing());
}
}
#endif // BUILDFLAG(IS_WIN)
} // namespace base