forked from kiwibrowser/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoped_task_environment_unittest.cc
324 lines (273 loc) · 12.3 KB
/
scoped_task_environment_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
// Copyright 2017 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/test/scoped_task_environment.h"
#include <memory>
#include "base/atomicops.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/synchronization/atomic_flag.h"
#include "base/synchronization/waitable_event.h"
#include "base/task_scheduler/post_task.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/sequence_local_storage_slot.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/tick_clock.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
#include <unistd.h>
#include "base/files/file_descriptor_watcher_posix.h"
#endif // defined(OS_POSIX)
namespace base {
namespace test {
namespace {
class ScopedTaskEnvironmentTest
: public testing::TestWithParam<ScopedTaskEnvironment::MainThreadType> {};
void VerifyRunUntilIdleDidNotReturnAndSetFlag(
AtomicFlag* run_until_idle_returned,
AtomicFlag* task_ran) {
EXPECT_FALSE(run_until_idle_returned->IsSet());
task_ran->Set();
}
void RunUntilIdleTest(
ScopedTaskEnvironment::MainThreadType main_thread_type,
ScopedTaskEnvironment::ExecutionMode execution_control_mode) {
AtomicFlag run_until_idle_returned;
ScopedTaskEnvironment scoped_task_environment(main_thread_type,
execution_control_mode);
AtomicFlag first_main_thread_task_ran;
ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag,
Unretained(&run_until_idle_returned),
Unretained(&first_main_thread_task_ran)));
AtomicFlag first_task_scheduler_task_ran;
PostTask(FROM_HERE, BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag,
Unretained(&run_until_idle_returned),
Unretained(&first_task_scheduler_task_ran)));
AtomicFlag second_task_scheduler_task_ran;
AtomicFlag second_main_thread_task_ran;
PostTaskAndReply(FROM_HERE,
BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag,
Unretained(&run_until_idle_returned),
Unretained(&second_task_scheduler_task_ran)),
BindOnce(&VerifyRunUntilIdleDidNotReturnAndSetFlag,
Unretained(&run_until_idle_returned),
Unretained(&second_main_thread_task_ran)));
scoped_task_environment.RunUntilIdle();
run_until_idle_returned.Set();
EXPECT_TRUE(first_main_thread_task_ran.IsSet());
EXPECT_TRUE(first_task_scheduler_task_ran.IsSet());
EXPECT_TRUE(second_task_scheduler_task_ran.IsSet());
EXPECT_TRUE(second_main_thread_task_ran.IsSet());
}
} // namespace
TEST_P(ScopedTaskEnvironmentTest, QueuedRunUntilIdle) {
RunUntilIdleTest(GetParam(), ScopedTaskEnvironment::ExecutionMode::QUEUED);
}
TEST_P(ScopedTaskEnvironmentTest, AsyncRunUntilIdle) {
RunUntilIdleTest(GetParam(), ScopedTaskEnvironment::ExecutionMode::ASYNC);
}
// Verify that tasks posted to an ExecutionMode::QUEUED ScopedTaskEnvironment do
// not run outside of RunUntilIdle().
TEST_P(ScopedTaskEnvironmentTest, QueuedTasksDoNotRunOutsideOfRunUntilIdle) {
ScopedTaskEnvironment scoped_task_environment(
GetParam(), ScopedTaskEnvironment::ExecutionMode::QUEUED);
AtomicFlag run_until_idle_called;
PostTask(FROM_HERE, BindOnce(
[](AtomicFlag* run_until_idle_called) {
EXPECT_TRUE(run_until_idle_called->IsSet());
},
Unretained(&run_until_idle_called)));
PlatformThread::Sleep(TestTimeouts::tiny_timeout());
run_until_idle_called.Set();
scoped_task_environment.RunUntilIdle();
AtomicFlag other_run_until_idle_called;
PostTask(FROM_HERE, BindOnce(
[](AtomicFlag* other_run_until_idle_called) {
EXPECT_TRUE(other_run_until_idle_called->IsSet());
},
Unretained(&other_run_until_idle_called)));
PlatformThread::Sleep(TestTimeouts::tiny_timeout());
other_run_until_idle_called.Set();
scoped_task_environment.RunUntilIdle();
}
// Verify that a task posted to an ExecutionMode::ASYNC ScopedTaskEnvironment
// can run without a call to RunUntilIdle().
TEST_P(ScopedTaskEnvironmentTest, AsyncTasksRunAsTheyArePosted) {
ScopedTaskEnvironment scoped_task_environment(
GetParam(), ScopedTaskEnvironment::ExecutionMode::ASYNC);
WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
PostTask(FROM_HERE,
BindOnce([](WaitableEvent* task_ran) { task_ran->Signal(); },
Unretained(&task_ran)));
task_ran.Wait();
}
// Verify that a task posted to an ExecutionMode::ASYNC ScopedTaskEnvironment
// after a call to RunUntilIdle() can run without another call to
// RunUntilIdle().
TEST_P(ScopedTaskEnvironmentTest,
AsyncTasksRunAsTheyArePostedAfterRunUntilIdle) {
ScopedTaskEnvironment scoped_task_environment(
GetParam(), ScopedTaskEnvironment::ExecutionMode::ASYNC);
scoped_task_environment.RunUntilIdle();
WaitableEvent task_ran(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
PostTask(FROM_HERE,
BindOnce([](WaitableEvent* task_ran) { task_ran->Signal(); },
Unretained(&task_ran)));
task_ran.Wait();
}
TEST_P(ScopedTaskEnvironmentTest, DelayedTasks) {
// Use a QUEUED execution-mode environment, so that no tasks are actually
// executed until RunUntilIdle()/FastForwardBy() are invoked.
ScopedTaskEnvironment scoped_task_environment(
GetParam(), ScopedTaskEnvironment::ExecutionMode::QUEUED);
subtle::Atomic32 counter = 0;
constexpr base::TimeDelta kShortTaskDelay = TimeDelta::FromDays(1);
// Should run only in MOCK_TIME environment when time is fast-forwarded.
ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
Bind(
[](subtle::Atomic32* counter) {
subtle::NoBarrier_AtomicIncrement(counter, 4);
},
Unretained(&counter)),
kShortTaskDelay);
// TODO(gab): This currently doesn't run because the TaskScheduler's clock
// isn't mocked but it should be.
PostDelayedTask(FROM_HERE,
Bind(
[](subtle::Atomic32* counter) {
subtle::NoBarrier_AtomicIncrement(counter, 128);
},
Unretained(&counter)),
kShortTaskDelay);
constexpr base::TimeDelta kLongTaskDelay = TimeDelta::FromDays(7);
// Same as first task, longer delays to exercise
// FastForwardUntilNoTasksRemain().
ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
Bind(
[](subtle::Atomic32* counter) {
subtle::NoBarrier_AtomicIncrement(counter, 8);
},
Unretained(&counter)),
TimeDelta::FromDays(5));
ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
Bind(
[](subtle::Atomic32* counter) {
subtle::NoBarrier_AtomicIncrement(counter, 16);
},
Unretained(&counter)),
kLongTaskDelay);
ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, Bind(
[](subtle::Atomic32* counter) {
subtle::NoBarrier_AtomicIncrement(counter, 1);
},
Unretained(&counter)));
PostTask(FROM_HERE, Bind(
[](subtle::Atomic32* counter) {
subtle::NoBarrier_AtomicIncrement(counter, 2);
},
Unretained(&counter)));
// This expectation will fail flakily if the preceding PostTask() is executed
// asynchronously, indicating a problem with the QUEUED execution mode.
int expected_value = 0;
EXPECT_EQ(expected_value, counter);
// RunUntilIdle() should process non-delayed tasks only in all queues.
scoped_task_environment.RunUntilIdle();
expected_value += 1;
expected_value += 2;
EXPECT_EQ(expected_value, counter);
if (GetParam() == ScopedTaskEnvironment::MainThreadType::MOCK_TIME) {
// Delay inferior to the delay of the first posted task.
constexpr base::TimeDelta kInferiorTaskDelay = TimeDelta::FromSeconds(1);
static_assert(kInferiorTaskDelay < kShortTaskDelay,
"|kInferiorTaskDelay| should be "
"set to a value inferior to the first posted task's delay.");
scoped_task_environment.FastForwardBy(kInferiorTaskDelay);
EXPECT_EQ(expected_value, counter);
scoped_task_environment.FastForwardBy(kShortTaskDelay - kInferiorTaskDelay);
expected_value += 4;
EXPECT_EQ(expected_value, counter);
scoped_task_environment.FastForwardUntilNoTasksRemain();
expected_value += 8;
expected_value += 16;
EXPECT_EQ(expected_value, counter);
}
}
// Regression test for https://crbug.com/824770.
TEST_P(ScopedTaskEnvironmentTest, SupportsSequenceLocalStorageOnMainThread) {
ScopedTaskEnvironment scoped_task_environment(
GetParam(), ScopedTaskEnvironment::ExecutionMode::ASYNC);
SequenceLocalStorageSlot<int> sls_slot;
sls_slot.Set(5);
EXPECT_EQ(5, sls_slot.Get());
}
#if defined(OS_POSIX)
TEST_F(ScopedTaskEnvironmentTest, SupportsFileDescriptorWatcherOnIOMainThread) {
ScopedTaskEnvironment scoped_task_environment(
ScopedTaskEnvironment::MainThreadType::IO,
ScopedTaskEnvironment::ExecutionMode::ASYNC);
int pipe_fds_[2];
ASSERT_EQ(0, pipe(pipe_fds_));
RunLoop run_loop;
// The write end of a newly created pipe is immediately writable.
auto controller = FileDescriptorWatcher::WatchWritable(
pipe_fds_[1], run_loop.QuitClosure());
// This will hang if the notification doesn't occur as expected.
run_loop.Run();
}
#endif // defined(OS_POSIX)
// Verify that the TickClock returned by
// |ScopedTaskEnvironment::GetMockTickClock| gets updated when the
// FastForward(By|UntilNoTasksRemain) functions are called.
TEST_F(ScopedTaskEnvironmentTest, FastForwardAdvanceTickClock) {
// Use a QUEUED execution-mode environment, so that no tasks are actually
// executed until RunUntilIdle()/FastForwardBy() are invoked.
ScopedTaskEnvironment scoped_task_environment(
ScopedTaskEnvironment::MainThreadType::MOCK_TIME,
ScopedTaskEnvironment::ExecutionMode::QUEUED);
constexpr base::TimeDelta kShortTaskDelay = TimeDelta::FromDays(1);
ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, base::DoNothing(),
kShortTaskDelay);
constexpr base::TimeDelta kLongTaskDelay = TimeDelta::FromDays(7);
ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, base::DoNothing(),
kLongTaskDelay);
const base::TickClock* tick_clock =
scoped_task_environment.GetMockTickClock();
base::TimeTicks tick_clock_ref = tick_clock->NowTicks();
// Make sure that |FastForwardBy| advances the clock.
scoped_task_environment.FastForwardBy(kShortTaskDelay);
EXPECT_EQ(kShortTaskDelay, tick_clock->NowTicks() - tick_clock_ref);
// Make sure that |FastForwardUntilNoTasksRemain| advances the clock.
scoped_task_environment.FastForwardUntilNoTasksRemain();
EXPECT_EQ(kLongTaskDelay, tick_clock->NowTicks() - tick_clock_ref);
// Fast-forwarding to a time at which there's no tasks should also advance the
// clock.
scoped_task_environment.FastForwardBy(kLongTaskDelay);
EXPECT_EQ(kLongTaskDelay * 2, tick_clock->NowTicks() - tick_clock_ref);
}
INSTANTIATE_TEST_CASE_P(
MainThreadDefault,
ScopedTaskEnvironmentTest,
::testing::Values(ScopedTaskEnvironment::MainThreadType::DEFAULT));
INSTANTIATE_TEST_CASE_P(
MainThreadMockTime,
ScopedTaskEnvironmentTest,
::testing::Values(ScopedTaskEnvironment::MainThreadType::MOCK_TIME));
INSTANTIATE_TEST_CASE_P(
MainThreadUI,
ScopedTaskEnvironmentTest,
::testing::Values(ScopedTaskEnvironment::MainThreadType::UI));
INSTANTIATE_TEST_CASE_P(
MainThreadIO,
ScopedTaskEnvironmentTest,
::testing::Values(ScopedTaskEnvironment::MainThreadType::IO));
} // namespace test
} // namespace base