forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbind_post_task_unittest.cc
325 lines (258 loc) · 9.79 KB
/
bind_post_task_unittest.cc
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/bind_post_task_forward.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/sequence_checker_impl.h"
#include "base/task/sequenced_task_runner_forward.h"
#include "base/test/task_environment.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
void SetBool(bool* variable, bool value) {
*variable = value;
}
void SetInt(int* variable, int value) {
*variable = value;
}
void SetIntFromUniquePtr(int* variable, std::unique_ptr<int> value) {
*variable = *value;
}
int Multiply(int value) {
return value * 5;
}
void ClearReference(OnceClosure callback) {}
class SequenceRestrictionChecker {
public:
explicit SequenceRestrictionChecker(bool& set_on_destroy)
: set_on_destroy_(set_on_destroy) {}
~SequenceRestrictionChecker() {
EXPECT_TRUE(checker_.CalledOnValidSequence());
set_on_destroy_ = true;
}
void Run() { EXPECT_TRUE(checker_.CalledOnValidSequence()); }
private:
SequenceCheckerImpl checker_;
bool& set_on_destroy_;
};
} // namespace
class BindPostTaskTest : public testing::Test {
protected:
test::SingleThreadTaskEnvironment task_environment_;
scoped_refptr<SequencedTaskRunner> task_runner_ =
SequencedTaskRunnerHandle::Get();
};
TEST_F(BindPostTaskTest, OnceClosure) {
bool val = false;
OnceClosure cb = BindOnce(&SetBool, &val, true);
OnceClosure post_cb = BindPostTask(task_runner_, std::move(cb));
std::move(post_cb).Run();
EXPECT_FALSE(val);
RunLoop().RunUntilIdle();
EXPECT_TRUE(val);
}
TEST_F(BindPostTaskTest, OnceCallback) {
OnceCallback<void(bool*, bool)> cb = BindOnce(&SetBool);
OnceCallback<void(bool*, bool)> post_cb =
BindPostTask(task_runner_, std::move(cb));
bool val = false;
std::move(post_cb).Run(&val, true);
EXPECT_FALSE(val);
RunLoop().RunUntilIdle();
EXPECT_TRUE(val);
}
TEST_F(BindPostTaskTest, OnceWithBoundMoveOnlyArg) {
int val = 0;
OnceClosure cb =
BindOnce(&SetIntFromUniquePtr, &val, std::make_unique<int>(10));
OnceClosure post_cb = BindPostTask(task_runner_, std::move(cb));
std::move(post_cb).Run();
EXPECT_EQ(val, 0);
RunLoop().RunUntilIdle();
EXPECT_EQ(val, 10);
}
TEST_F(BindPostTaskTest, OnceWithUnboundMoveOnlyArg) {
int val = 0;
OnceCallback<void(std::unique_ptr<int>)> cb =
BindOnce(&SetIntFromUniquePtr, &val);
OnceCallback<void(std::unique_ptr<int>)> post_cb =
BindPostTask(task_runner_, std::move(cb));
std::move(post_cb).Run(std::make_unique<int>(10));
EXPECT_EQ(val, 0);
RunLoop().RunUntilIdle();
EXPECT_EQ(val, 10);
}
TEST_F(BindPostTaskTest, OnceWithIgnoreResult) {
OnceCallback<void(int)> post_cb =
BindPostTask(task_runner_, BindOnce(IgnoreResult(&Multiply)));
std::move(post_cb).Run(1);
RunLoop().RunUntilIdle();
}
TEST_F(BindPostTaskTest, OnceThen) {
int value = 0;
// Multiply() returns an int and SetInt() takes an int as a parameter.
OnceClosure then_cb =
BindOnce(&Multiply, 5)
.Then(BindPostTask(task_runner_, BindOnce(&SetInt, &value)));
std::move(then_cb).Run();
EXPECT_EQ(value, 0);
RunLoop().RunUntilIdle();
EXPECT_EQ(value, 25);
}
// Ensure that the input callback is run/destroyed on the correct thread even if
// the callback returned from BindPostTask() is run on a different thread.
TEST_F(BindPostTaskTest, OnceRunDestroyedOnBound) {
Thread target_thread("testing");
ASSERT_TRUE(target_thread.Start());
// SequenceRestrictionChecker checks it's creation, Run() and deletion all
// happen on the main thread.
bool destroyed = false;
auto checker = std::make_unique<SequenceRestrictionChecker>(destroyed);
// `checker` is owned by `cb` which is wrapped in `post_cb`. `post_cb` is run
// on a different thread which triggers a PostTask() back to the test main
// thread to invoke `cb` which runs SequenceRestrictionChecker::Run(). After
// `cb` has been invoked `checker` is destroyed along with the BindState.
OnceClosure cb =
BindOnce(&SequenceRestrictionChecker::Run, std::move(checker));
OnceClosure post_cb = BindPostTask(task_runner_, std::move(cb));
target_thread.task_runner()->PostTask(FROM_HERE, std::move(post_cb));
target_thread.FlushForTesting();
EXPECT_FALSE(destroyed);
RunLoop().RunUntilIdle();
EXPECT_TRUE(destroyed);
}
// Ensure that the input callback is destroyed on the correct thread even if the
// callback returned from BindPostTask() is destroyed without being run on a
// different thread.
TEST_F(BindPostTaskTest, OnceNotRunDestroyedOnBound) {
Thread target_thread("testing");
ASSERT_TRUE(target_thread.Start());
// SequenceRestrictionChecker checks it's creation and deletion all happen on
// the test main thread.
bool destroyed = false;
auto checker = std::make_unique<SequenceRestrictionChecker>(destroyed);
// `checker` is owned by `cb` which is wrapped in `post_cb`. `post_cb` is
// deleted on a different thread which triggers a PostTask() back to the test
// main thread to destroy `cb` and `checker`.
OnceClosure cb =
BindOnce(&SequenceRestrictionChecker::Run, std::move(checker));
OnceClosure post_cb = BindPostTask(task_runner_, std::move(cb));
target_thread.task_runner()->PostTask(
FROM_HERE, BindOnce(&ClearReference, std::move(post_cb)));
target_thread.FlushForTesting();
EXPECT_FALSE(destroyed);
RunLoop().RunUntilIdle();
EXPECT_TRUE(destroyed);
}
TEST_F(BindPostTaskTest, RepeatingClosure) {
bool val = false;
RepeatingClosure cb = BindRepeating(&SetBool, &val, true);
RepeatingClosure post_cb = BindPostTask(task_runner_, std::move(cb));
post_cb.Run();
EXPECT_FALSE(val);
RunLoop().RunUntilIdle();
EXPECT_TRUE(val);
val = false;
post_cb.Run();
EXPECT_FALSE(val);
RunLoop().RunUntilIdle();
EXPECT_TRUE(val);
}
TEST_F(BindPostTaskTest, RepeatingCallback) {
RepeatingCallback<void(bool*, bool)> cb = BindRepeating(&SetBool);
RepeatingCallback<void(bool*, bool)> post_cb =
BindPostTask(task_runner_, std::move(cb));
bool val = false;
post_cb.Run(&val, true);
EXPECT_FALSE(val);
RunLoop().RunUntilIdle();
EXPECT_TRUE(val);
post_cb.Run(&val, false);
EXPECT_TRUE(val);
RunLoop().RunUntilIdle();
EXPECT_FALSE(val);
}
TEST_F(BindPostTaskTest, RepeatingWithUnboundMoveOnlyArg) {
int val = 0;
RepeatingCallback<void(std::unique_ptr<int>)> cb =
BindRepeating(&SetIntFromUniquePtr, &val);
RepeatingCallback<void(std::unique_ptr<int>)> post_cb =
BindPostTask(task_runner_, std::move(cb));
post_cb.Run(std::make_unique<int>(10));
EXPECT_EQ(val, 0);
RunLoop().RunUntilIdle();
EXPECT_EQ(val, 10);
post_cb.Run(std::make_unique<int>(20));
EXPECT_EQ(val, 10);
RunLoop().RunUntilIdle();
EXPECT_EQ(val, 20);
}
TEST_F(BindPostTaskTest, RepeatingWithIgnoreResult) {
RepeatingCallback<void(int)> post_cb =
BindPostTask(task_runner_, BindRepeating(IgnoreResult(&Multiply)));
std::move(post_cb).Run(1);
RunLoop().RunUntilIdle();
}
TEST_F(BindPostTaskTest, RepeatingThen) {
int value = 0;
// Multiply() returns an int and SetInt() takes an int as a parameter.
RepeatingCallback<void(int)> then_cb = BindRepeating(&Multiply).Then(
BindPostTask(task_runner_, BindRepeating(&SetInt, &value)));
then_cb.Run(5);
EXPECT_EQ(value, 0);
RunLoop().RunUntilIdle();
EXPECT_EQ(value, 25);
then_cb.Run(10);
EXPECT_EQ(value, 25);
RunLoop().RunUntilIdle();
EXPECT_EQ(value, 50);
}
// Ensure that the input callback is run/destroyed on the correct thread even if
// the callback returned from BindPostTask() is run on a different thread.
TEST_F(BindPostTaskTest, RepeatingRunDestroyedOnBound) {
Thread target_thread("testing");
ASSERT_TRUE(target_thread.Start());
// SequenceRestrictionChecker checks it's creation, Run() and deletion all
// happen on the main thread.
bool destroyed = false;
auto checker = std::make_unique<SequenceRestrictionChecker>(destroyed);
// `checker` is owned by `cb` which is wrapped in `post_cb`. `post_cb` is run
// on a different thread which triggers a PostTask() back to the test main
// thread to invoke `cb` which runs SequenceRestrictionChecker::Run(). After
// `cb` has been invoked `checker` is destroyed along with the BindState.
RepeatingClosure cb =
BindRepeating(&SequenceRestrictionChecker::Run, std::move(checker));
RepeatingClosure post_cb = BindPostTask(task_runner_, std::move(cb));
target_thread.task_runner()->PostTask(FROM_HERE, std::move(post_cb));
target_thread.FlushForTesting();
EXPECT_FALSE(destroyed);
RunLoop().RunUntilIdle();
EXPECT_TRUE(destroyed);
}
// Ensure that the input callback is destroyed on the correct thread even if the
// callback returned from BindPostTask() is destroyed without being run on a
// different thread.
TEST_F(BindPostTaskTest, RepeatingNotRunDestroyedOnBound) {
Thread target_thread("testing");
ASSERT_TRUE(target_thread.Start());
// SequenceRestrictionChecker checks it's creation and deletion all happen on
// the test main thread.
bool destroyed = false;
auto checker = std::make_unique<SequenceRestrictionChecker>(destroyed);
// `checker` is owned by `cb` which is wrapped in `post_cb`. `post_cb` is
// deleted on a different thread which triggers a PostTask() back to the test
// main thread to destroy `cb` and `checker`.
RepeatingClosure cb =
BindRepeating(&SequenceRestrictionChecker::Run, std::move(checker));
RepeatingClosure post_cb = BindPostTask(task_runner_, std::move(cb));
target_thread.task_runner()->PostTask(
FROM_HERE, BindRepeating(&ClearReference, std::move(post_cb)));
target_thread.FlushForTesting();
EXPECT_FALSE(destroyed);
RunLoop().RunUntilIdle();
EXPECT_TRUE(destroyed);
}
} // namespace base