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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
base / threading / thread_checker_unittest.cc [blame]
// Copyright 2012 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/threading/thread_checker.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_token.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/simple_thread.h"
#include "base/threading/thread_local.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base::internal {
namespace {
// A thread that runs a callback.
class RunCallbackThread : public SimpleThread {
public:
explicit RunCallbackThread(OnceClosure callback)
: SimpleThread("RunCallbackThread"), callback_(std::move(callback)) {}
RunCallbackThread(const RunCallbackThread&) = delete;
RunCallbackThread& operator=(const RunCallbackThread&) = delete;
private:
// SimpleThread:
void Run() override { std::move(callback_).Run(); }
OnceClosure callback_;
};
// Runs a callback on a new thread synchronously.
void RunCallbackOnNewThreadSynchronously(OnceClosure callback) {
RunCallbackThread run_callback_thread(std::move(callback));
run_callback_thread.Start();
run_callback_thread.Join();
}
void ExpectCalledOnValidThread(ThreadCheckerImpl* thread_checker) {
ASSERT_TRUE(thread_checker);
// This should bind |thread_checker| to the current thread if it wasn't
// already bound to a thread.
EXPECT_TRUE(thread_checker->CalledOnValidThread());
// Since |thread_checker| is now bound to the current thread, another call to
// CalledOnValidThread() should return true.
EXPECT_TRUE(thread_checker->CalledOnValidThread());
}
void ExpectNotCalledOnValidThread(ThreadCheckerImpl* thread_checker) {
ASSERT_TRUE(thread_checker);
EXPECT_FALSE(thread_checker->CalledOnValidThread());
}
void ExpectNotCalledOnValidThreadWithSequenceTokenThreadBound(
ThreadCheckerImpl* thread_checker,
SequenceToken sequence_token) {
TaskScope task_scope(sequence_token, /* is_thread_bound=*/true);
ExpectNotCalledOnValidThread(thread_checker);
}
} // namespace
TEST(ThreadCheckerTest, AllowedSameThreadNoSequenceToken) {
ThreadCheckerImpl thread_checker;
EXPECT_TRUE(thread_checker.CalledOnValidThread());
}
TEST(ThreadCheckerTest, AllowedSameThreadAndSequenceDifferentThreadBoundTasks) {
std::unique_ptr<ThreadCheckerImpl> thread_checker;
const SequenceToken sequence_token = SequenceToken::Create();
{
TaskScope task_scope(sequence_token,
/* is_thread_bound=*/true);
thread_checker = std::make_unique<ThreadCheckerImpl>();
}
{
TaskScope task_scope(sequence_token,
/* is_thread_bound=*/true);
EXPECT_TRUE(thread_checker->CalledOnValidThread());
}
}
TEST(ThreadCheckerTest, AllowedSameThreadSequenceAndTaskNotThreadBound) {
TaskScope task_scope(SequenceToken::Create(),
/* is_thread_bound=*/false);
ThreadCheckerImpl thread_checker;
EXPECT_TRUE(thread_checker.CalledOnValidThread());
}
TEST(ThreadCheckerTest,
DisallowedSameThreadAndSequenceDifferentTasksNotThreadBound) {
std::unique_ptr<ThreadCheckerImpl> thread_checker;
{
TaskScope task_scope(SequenceToken::Create(),
/* is_thread_bound=*/false);
thread_checker = std::make_unique<ThreadCheckerImpl>();
}
{
TaskScope task_scope(SequenceToken::Create(),
/* is_thread_bound=*/false);
EXPECT_FALSE(thread_checker->CalledOnValidThread());
}
}
TEST(ThreadCheckerTest, DisallowedDifferentThreadsNoSequenceToken) {
ThreadCheckerImpl thread_checker;
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectNotCalledOnValidThread, Unretained(&thread_checker)));
}
TEST(ThreadCheckerTest, DisallowedDifferentThreadsSameSequence) {
SingleThreadTaskRunner::CurrentDefaultHandle
single_thread_task_runner_current_default_handle(
MakeRefCounted<TestSimpleTaskRunner>());
const SequenceToken sequence_token(SequenceToken::Create());
TaskScope task_scope(sequence_token,
/* is_thread_bound=*/false);
ThreadCheckerImpl thread_checker;
EXPECT_TRUE(thread_checker.CalledOnValidThread());
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectNotCalledOnValidThreadWithSequenceTokenThreadBound,
Unretained(&thread_checker), sequence_token));
}
TEST(ThreadCheckerTest, DisallowedSameThreadDifferentSequence) {
std::unique_ptr<ThreadCheckerImpl> thread_checker;
SingleThreadTaskRunner::CurrentDefaultHandle
single_thread_task_runner_current_default_handle(
MakeRefCounted<TestSimpleTaskRunner>());
{
TaskScope task_scope(SequenceToken::Create(),
/* is_thread_bound=*/false);
thread_checker = std::make_unique<ThreadCheckerImpl>();
}
{
// Different SequenceToken.
TaskScope task_scope(SequenceToken::Create(),
/* is_thread_bound=*/false);
EXPECT_FALSE(thread_checker->CalledOnValidThread());
}
// No SequenceToken.
EXPECT_FALSE(thread_checker->CalledOnValidThread());
}
TEST(ThreadCheckerTest, DetachFromThread) {
ThreadCheckerImpl thread_checker;
thread_checker.DetachFromThread();
// Verify that CalledOnValidThread() returns true when called on a different
// thread after a call to DetachFromThread().
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&thread_checker)));
EXPECT_FALSE(thread_checker.CalledOnValidThread());
}
TEST(ThreadCheckerTest, DetachFromThreadWithSequenceToken) {
SingleThreadTaskRunner::CurrentDefaultHandle
single_thread_task_runner_current_default_handle(
MakeRefCounted<TestSimpleTaskRunner>());
TaskScope task_scope(SequenceToken::Create(),
/* is_thread_bound=*/false);
ThreadCheckerImpl thread_checker;
thread_checker.DetachFromThread();
// Verify that CalledOnValidThread() returns true when called on a different
// thread after a call to DetachFromThread().
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&thread_checker)));
EXPECT_FALSE(thread_checker.CalledOnValidThread());
}
// Owns a ThreadCheckerImpl and asserts that CalledOnValidThread() is valid
// in ~ThreadCheckerOwner.
class ThreadCheckerOwner {
public:
explicit ThreadCheckerOwner(bool detach_from_thread) {
if (detach_from_thread)
checker_.DetachFromThread();
}
ThreadCheckerOwner(const ThreadCheckerOwner&) = delete;
ThreadCheckerOwner& operator=(const ThreadCheckerOwner&) = delete;
~ThreadCheckerOwner() { EXPECT_TRUE(checker_.CalledOnValidThread()); }
private:
ThreadCheckerImpl checker_;
};
// Verifies ThreadCheckerImpl::CalledOnValidThread() returns true if called
// during thread destruction.
TEST(ThreadCheckerTest, CalledOnValidThreadFromThreadDestruction) {
ThreadLocalOwnedPointer<ThreadCheckerOwner> thread_local_owner;
RunCallbackOnNewThreadSynchronously(BindLambdaForTesting([&] {
thread_local_owner.Set(std::make_unique<ThreadCheckerOwner>(false));
}));
}
// Variant of CalledOnValidThreadFromThreadDestruction that calls
// ThreadCheckerImpl::DetachFromThread().
TEST(ThreadCheckerTest, CalledOnValidThreadFromThreadDestructionDetached) {
ThreadLocalOwnedPointer<ThreadCheckerOwner> thread_local_owner;
RunCallbackOnNewThreadSynchronously(BindLambdaForTesting([&] {
thread_local_owner.Set(std::make_unique<ThreadCheckerOwner>(true));
}));
}
TEST(ThreadCheckerTest, Move) {
ThreadCheckerImpl initial;
EXPECT_TRUE(initial.CalledOnValidThread());
ThreadCheckerImpl move_constructed(std::move(initial));
EXPECT_TRUE(move_constructed.CalledOnValidThread());
ThreadCheckerImpl move_assigned;
move_assigned = std::move(move_constructed);
EXPECT_TRUE(move_assigned.CalledOnValidThread());
// The two ThreadCheckerImpls moved from should be able to rebind to another
// thread.
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&initial)));
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&move_constructed)));
// But the latest one shouldn't be able to run on another thread.
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectNotCalledOnValidThread, Unretained(&move_assigned)));
EXPECT_TRUE(move_assigned.CalledOnValidThread());
}
TEST(ThreadCheckerTest, MoveAssignIntoDetached) {
ThreadCheckerImpl initial;
ThreadCheckerImpl move_assigned;
move_assigned.DetachFromThread();
move_assigned = std::move(initial);
// |initial| is detached after move.
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&initial)));
// |move_assigned| should be associated with the main thread.
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectNotCalledOnValidThread, Unretained(&move_assigned)));
EXPECT_TRUE(move_assigned.CalledOnValidThread());
}
TEST(ThreadCheckerTest, MoveFromDetachedRebinds) {
ThreadCheckerImpl initial;
initial.DetachFromThread();
ThreadCheckerImpl moved_into(std::move(initial));
// |initial| is still detached after move.
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&initial)));
// |moved_into| is bound to the current thread as part of the move.
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectNotCalledOnValidThread, Unretained(&moved_into)));
EXPECT_TRUE(moved_into.CalledOnValidThread());
}
TEST(ThreadCheckerTest, MoveOffThreadBanned) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
ThreadCheckerImpl other_thread;
other_thread.DetachFromThread();
RunCallbackOnNewThreadSynchronously(
BindOnce(&ExpectCalledOnValidThread, Unretained(&other_thread)));
EXPECT_DCHECK_DEATH(ThreadCheckerImpl main_thread(std::move(other_thread)));
}
namespace {
// This fixture is a helper for unit testing the thread checker macros as it is
// not possible to inline ExpectDeathOnOtherThread() and
// ExpectNoDeathOnOtherThreadAfterDetach() as lambdas since binding
// |Unretained(&my_sequence_checker)| wouldn't compile on non-dcheck builds
// where it won't be defined.
class ThreadCheckerMacroTest : public testing::Test {
public:
ThreadCheckerMacroTest() = default;
ThreadCheckerMacroTest(const ThreadCheckerMacroTest&) = delete;
ThreadCheckerMacroTest& operator=(const ThreadCheckerMacroTest&) = delete;
void ExpectDeathOnOtherThread() {
#if DCHECK_IS_ON()
EXPECT_DCHECK_DEATH({ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); });
#else
// Happily no-ops on non-dcheck builds.
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
#endif
}
void ExpectNoDeathOnOtherThreadAfterDetach() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
protected:
THREAD_CHECKER(thread_checker_);
};
} // namespace
TEST_F(ThreadCheckerMacroTest, Macros) {
GTEST_FLAG_SET(death_test_style, "threadsafe");
THREAD_CHECKER(my_thread_checker);
RunCallbackOnNewThreadSynchronously(BindOnce(
&ThreadCheckerMacroTest::ExpectDeathOnOtherThread, Unretained(this)));
DETACH_FROM_THREAD(thread_checker_);
RunCallbackOnNewThreadSynchronously(
BindOnce(&ThreadCheckerMacroTest::ExpectNoDeathOnOtherThreadAfterDetach,
Unretained(this)));
}
} // namespace base::internal