File: bind_post_task_test.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (278 lines) | stat: -rw-r--r-- 8,146 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/wtf/bind_post_task.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace WTF {

namespace {

void Increment(int* value) {
  ++(*value);
}

void Add(int* sum, int a, int b) {
  *sum = a + b;
}

void SetIntFromUniquePtr(int* variable, std::unique_ptr<int> value) {
  *variable = *value;
}

int Multiply(int value) {
  return value * 5;
}

// Helper class from base/task/bind_post_task_unittest.cc, adapted for WTF.
class WTFSequenceRestrictionChecker {
 public:
  WTFSequenceRestrictionChecker(bool* set_on_destroy,
                                base::OnceClosure quit_closure)
      : set_on_destroy_(set_on_destroy),
        quit_closure_(std::move(quit_closure)) {}

  ~WTFSequenceRestrictionChecker() {
    EXPECT_TRUE(checker_.CalledOnValidSequence());
    *set_on_destroy_ = true;
    if (quit_closure_) {
      std::move(quit_closure_).Run();
    }
  }

  void Run() { EXPECT_TRUE(checker_.CalledOnValidSequence()); }

 private:
  base::SequenceCheckerImpl checker_;
  bool* set_on_destroy_;
  base::OnceClosure quit_closure_;
};

}  // namespace

class BindPostTaskTest : public testing::Test {
 public:
  ~BindPostTaskTest() override {
    // Ensures boundg tasks destruct cleanly before teardown.
    CycleTasks();
  }

 protected:
  void CycleTasks() {
    task_runner_->PostTask(FROM_HERE, task_environment_.QuitClosure());
    task_environment_.RunUntilQuit();
  }

  base::test::TaskEnvironment task_environment_;
  scoped_refptr<base::SequencedTaskRunner> task_runner_ =
      task_environment_.GetMainThreadTaskRunner();
};

TEST_F(BindPostTaskTest, OnceFunctionNoArgs) {
  int count = 0;

  auto bound_task = BindPostTask(
      task_runner_,
      CrossThreadBindOnce(&Increment, CrossThreadUnretained(&count)));
  std::move(bound_task).Run();

  EXPECT_EQ(0, count);
  CycleTasks();
  EXPECT_EQ(1, count);
}

TEST_F(BindPostTaskTest, OnceFunctionWithArgs) {
  int sum = 0;

  auto bound_task = BindPostTask(
      task_runner_,
      CrossThreadBindOnce(&Add, CrossThreadUnretained(&sum), 5, 10));
  std::move(bound_task).Run();

  EXPECT_EQ(0, sum);
  CycleTasks();
  EXPECT_EQ(15, sum);
}

TEST_F(BindPostTaskTest, OnceWithBoundMoveOnlyArg) {
  int val = 0;

  auto inner_cb =
      CrossThreadBindOnce(&SetIntFromUniquePtr, CrossThreadUnretained(&val),
                          std::make_unique<int>(10));
  auto post_cb = BindPostTask(task_runner_, std::move(inner_cb));
  std::move(post_cb).Run();

  EXPECT_EQ(val, 0);
  CycleTasks();
  EXPECT_EQ(val, 10);
}

TEST_F(BindPostTaskTest, OnceWithUnboundMoveOnlyArg) {
  int val = 0;

  auto inner_cb =
      CrossThreadBindOnce(&SetIntFromUniquePtr, CrossThreadUnretained(&val));
  auto post_cb = BindPostTask(task_runner_, std::move(inner_cb));
  std::move(post_cb).Run(std::make_unique<int>(10));

  EXPECT_EQ(val, 0);
  CycleTasks();
  EXPECT_EQ(val, 10);
}

TEST_F(BindPostTaskTest, OnceWithIgnoreResult) {
  auto inner_cb = CrossThreadBindOnce(base::IgnoreResult(&Multiply));
  auto post_cb = BindPostTask(task_runner_, std::move(inner_cb));

  std::move(post_cb).Run(1);
  CycleTasks();
}

TEST_F(BindPostTaskTest, OnceRunDestroyedOnBound) {
  bool destroyed_on_main = false;
  auto checker = std::make_unique<WTFSequenceRestrictionChecker>(
      &destroyed_on_main, task_environment_.QuitClosure());

  auto cb_owning_checker = CrossThreadBindOnce(
      &WTFSequenceRestrictionChecker::Run, std::move(checker));
  auto post_cb = BindPostTask(task_runner_, std::move(cb_owning_checker));

  base::Thread other_thread("other_thread_once_run");
  ASSERT_TRUE(other_thread.Start());
  other_thread.task_runner()->PostTask(
      FROM_HERE, ConvertToBaseOnceCallback(std::move(post_cb)));
  other_thread.Stop();  // Flushes tasks and waits for completion.

  EXPECT_FALSE(destroyed_on_main);
  task_environment_.RunUntilQuit();
  EXPECT_TRUE(destroyed_on_main);
}

TEST_F(BindPostTaskTest, OnceNotRunDestroyedOnBound) {
  bool destroyed_on_main = false;
  auto checker = std::make_unique<WTFSequenceRestrictionChecker>(
      &destroyed_on_main, task_environment_.QuitClosure());

  auto cb_owning_checker = CrossThreadBindOnce(
      &WTFSequenceRestrictionChecker::Run, std::move(checker));
  auto post_cb = BindPostTask(task_runner_, std::move(cb_owning_checker));

  base::Thread other_thread("other_thread_once_not_run");
  ASSERT_TRUE(other_thread.Start());
  // Destroy post_cb on another thread. This should post back to main thread
  // for destruction of cb_owning_checker.
  other_thread.task_runner()->PostTask(
      FROM_HERE, base::DoNothingWithBoundArgs(
                     ConvertToBaseOnceCallback(std::move(post_cb))));
  other_thread.Stop();

  EXPECT_FALSE(destroyed_on_main);
  task_environment_.RunUntilQuit();
  EXPECT_TRUE(destroyed_on_main);
}

TEST_F(BindPostTaskTest, RepeatingFunctionNoArgs) {
  int count = 0;

  auto bound_task = BindPostTask(
      task_runner_,
      CrossThreadBindRepeating(&Increment, CrossThreadUnretained(&count)));

  bound_task.Run();
  EXPECT_EQ(0, count);
  CycleTasks();
  EXPECT_EQ(1, count);

  bound_task.Run();
  EXPECT_EQ(1, count);
  CycleTasks();
  EXPECT_EQ(2, count);
}

TEST_F(BindPostTaskTest, RepeatingFunctionWithArgs) {
  int sum = 0;

  auto bound_task = BindPostTask(
      task_runner_,
      CrossThreadBindRepeating(&Add, CrossThreadUnretained(&sum), 3, 4));

  bound_task.Run();
  EXPECT_EQ(0, sum);
  CycleTasks();
  EXPECT_EQ(7, sum);

  // Re-running a repeating callback will re-evaluate arguments if not owned.
  // In this case, the values are hardcoded, so the result is the same.
  sum = 0;
  bound_task.Run();
  EXPECT_EQ(0, sum);
  CycleTasks();
  EXPECT_EQ(7, sum);
}

TEST_F(BindPostTaskTest, RepeatingWithIgnoreResult) {
  auto inner_cb = CrossThreadBindRepeating(base::IgnoreResult(&Multiply));
  auto post_cb = BindPostTask(task_runner_, std::move(inner_cb));

  post_cb.Run(1);
  CycleTasks();

  post_cb.Run(2);
  CycleTasks();
}

TEST_F(BindPostTaskTest, RepeatingRunDestroyedOnBound) {
  bool destroyed_on_main = false;
  auto checker_ptr = std::make_unique<WTFSequenceRestrictionChecker>(
      &destroyed_on_main, task_environment_.QuitClosure());
  auto cb_owning_checker = CrossThreadBindRepeating(
      &WTFSequenceRestrictionChecker::Run, std::move(checker_ptr));
  auto post_cb = BindPostTask(task_runner_, std::move(cb_owning_checker));

  base::Thread other_thread("other_thread_repeating_run");
  ASSERT_TRUE(other_thread.Start());
  other_thread.task_runner()->PostTask(
      FROM_HERE, base::BindOnce(base::BindLambdaForTesting([&]() {
        post_cb.Run();
        post_cb.Run();
        post_cb.Reset();
      })));
  other_thread.Stop();

  EXPECT_FALSE(destroyed_on_main);
  task_environment_.RunUntilQuit();
  EXPECT_TRUE(destroyed_on_main);
}

TEST_F(BindPostTaskTest, RepeatingNotRunDestroyedOnBound) {
  bool destroyed_on_main = false;
  auto checker_ptr = std::make_unique<WTFSequenceRestrictionChecker>(
      &destroyed_on_main, task_environment_.QuitClosure());
  auto cb_owning_checker = CrossThreadBindRepeating(
      &WTFSequenceRestrictionChecker::Run, std::move(checker_ptr));
  auto post_cb = BindPostTask(task_runner_, std::move(cb_owning_checker));

  base::Thread other_thread("other_thread_repeating_run");
  ASSERT_TRUE(other_thread.Start());
  // Destroy post_cb on another thread. This should post back to main thread
  // for destruction of cb_owning_checker.
  other_thread.task_runner()->PostTask(
      FROM_HERE, base::DoNothingWithBoundArgs(
                     ConvertToBaseRepeatingCallback(std::move(post_cb))));
  other_thread.Stop();  // Flushes tasks and waits for completion.

  EXPECT_FALSE(destroyed_on_main);
  task_environment_.RunUntilQuit();
  EXPECT_TRUE(destroyed_on_main);
}

}  // namespace WTF