forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbind_post_task_unittest.cc
158 lines (126 loc) · 4.74 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
// 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/bind_post_task.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/sequence_checker_impl.h"
#include "base/sequenced_task_runner.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;
}
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, 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);
}
} // namespace base