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 349
|
// Copyright 2021 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/webrtc_overrides/low_precision_timer.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc_overrides/metronome_source.h"
#include "third_party/webrtc_overrides/timer_based_tick_provider.h"
namespace blink {
namespace {
base::TimeDelta TickPeriod() {
return TimerBasedTickProvider::kDefaultPeriod;
}
class LowPrecisionTimerTest : public ::testing::Test {
public:
LowPrecisionTimerTest()
: task_environment_(
base::test::TaskEnvironment::ThreadingMode::MULTIPLE_THREADS,
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
// Ensure mock time is aligned with metronome tick.
base::TimeTicks now = base::TimeTicks::Now();
task_environment_.FastForwardBy(
TimerBasedTickProvider::TimeSnappedToNextTick(
now, TimerBasedTickProvider::kDefaultPeriod) -
now);
}
protected:
base::test::TaskEnvironment task_environment_;
};
class CallbackListener {
public:
CallbackListener()
: task_runner_(base::ThreadPool::CreateSequencedTaskRunner({})) {}
void Callback() {
EXPECT_TRUE(task_runner_->RunsTasksInCurrentSequence());
++callback_count_;
}
void set_task_runner(scoped_refptr<base::SequencedTaskRunner> task_runner) {
task_runner_ = task_runner;
}
scoped_refptr<base::SequencedTaskRunner> task_runner() const {
return task_runner_;
}
size_t callback_count() const { return callback_count_; }
private:
scoped_refptr<base::SequencedTaskRunner> task_runner_;
size_t callback_count_ = 0u;
};
class RecursiveStartOneShotter {
public:
RecursiveStartOneShotter(size_t repeat_count, base::TimeDelta delay)
: timer_(base::ThreadPool::CreateSequencedTaskRunner({}),
base::BindRepeating(&RecursiveStartOneShotter::Callback,
base::Unretained(this))),
repeat_count_(repeat_count),
delay_(delay) {
timer_.StartOneShot(delay_);
}
~RecursiveStartOneShotter() { timer_.Shutdown(); }
size_t callback_count() const { return callback_count_; }
void Callback() {
++callback_count_;
DCHECK(repeat_count_);
--repeat_count_;
if (repeat_count_) {
timer_.StartOneShot(delay_);
}
}
private:
LowPrecisionTimer timer_;
size_t repeat_count_;
base::TimeDelta delay_;
size_t callback_count_ = 0u;
};
class RecursiveStopper {
public:
explicit RecursiveStopper(base::TimeDelta delay)
: timer_(base::ThreadPool::CreateSequencedTaskRunner({}),
base::BindRepeating(&RecursiveStopper::Callback,
base::Unretained(this))) {
timer_.StartRepeating(delay);
}
~RecursiveStopper() { timer_.Shutdown(); }
size_t callback_count() const { return callback_count_; }
void Callback() {
++callback_count_;
timer_.Stop();
}
private:
LowPrecisionTimer timer_;
size_t callback_count_ = 0u;
};
class IsActiveChecker {
public:
IsActiveChecker()
: timer_(base::ThreadPool::CreateSequencedTaskRunner({}),
base::BindRepeating(&IsActiveChecker::Callback,
base::Unretained(this))) {}
~IsActiveChecker() { timer_.Shutdown(); }
LowPrecisionTimer& timer() { return timer_; }
bool was_active_in_last_callback() const {
return was_active_in_last_callback_;
}
void Callback() { was_active_in_last_callback_ = timer_.IsActive(); }
private:
LowPrecisionTimer timer_;
bool was_active_in_last_callback_;
};
} // namespace
TEST_F(LowPrecisionTimerTest, StartOneShot) {
CallbackListener listener;
LowPrecisionTimer timer(listener.task_runner(),
base::BindRepeating(&CallbackListener::Callback,
base::Unretained(&listener)));
// Schedule to fire on the first tick.
timer.StartOneShot(TickPeriod());
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 1u);
// The task does not repeat automatically.
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 1u);
// Schedule to fire a millisecond before the next tick. Advancing to that
// time does not result in a callback.
timer.StartOneShot(TickPeriod() - base::Milliseconds(1));
task_environment_.FastForwardBy(TickPeriod() - base::Milliseconds(1));
EXPECT_EQ(listener.callback_count(), 1u);
// But it fires on the next tick.
task_environment_.FastForwardBy(base::Milliseconds(1));
EXPECT_EQ(listener.callback_count(), 2u);
// Fire a little after the next tick. Two ticks has to pass before anything
// happens.
timer.StartOneShot(TickPeriod() + base::Milliseconds(1));
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 2u);
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 3u);
// Schedule to fire but shutdown the timer before it has time to fire.
timer.StartOneShot(TickPeriod());
timer.Shutdown();
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 3u);
}
TEST_F(LowPrecisionTimerTest, RecursiveStartOneShot) {
base::TimeDelta delay = base::Milliseconds(1);
RecursiveStartOneShotter recursive_shotter(/*repeat_count=*/2, delay);
// A full tick is needed before the callback fires.
task_environment_.FastForwardBy(delay);
EXPECT_EQ(recursive_shotter.callback_count(), 0u);
task_environment_.FastForwardBy(TickPeriod() - delay);
EXPECT_EQ(recursive_shotter.callback_count(), 1u);
// The same is true the second time it fires. This is not a high precision
// timer and no attempt is taken to fire the callback multiple times per tick
// to "catch up" with what the callback count would have been if the timer had
// higher precision.
task_environment_.FastForwardBy(delay);
EXPECT_EQ(recursive_shotter.callback_count(), 1u);
task_environment_.FastForwardBy(TickPeriod() - delay);
EXPECT_EQ(recursive_shotter.callback_count(), 2u);
// It is not repeated a third time.
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(recursive_shotter.callback_count(), 2u);
}
TEST_F(LowPrecisionTimerTest, MoveToNewTaskRunner) {
CallbackListener listener;
LowPrecisionTimer timer(listener.task_runner(),
base::BindRepeating(&CallbackListener::Callback,
base::Unretained(&listener)));
// Schedule on the next tick, and advance time close to that.
timer.StartOneShot(TickPeriod());
task_environment_.FastForwardBy(TickPeriod() - base::Milliseconds(3));
EXPECT_EQ(listener.callback_count(), 0u);
// Move to a new task runner. The CallbackListener will EXPECT_TRUE that the
// correct task runner is used.
listener.set_task_runner(base::ThreadPool::CreateSequencedTaskRunner({}));
timer.MoveToNewTaskRunner(listener.task_runner());
// Advance to scheduled time (the next tick).
task_environment_.FastForwardBy(base::Milliseconds(3));
EXPECT_EQ(listener.callback_count(), 1u);
// Cleanup.
timer.Shutdown();
}
TEST_F(LowPrecisionTimerTest, StartRepeating) {
CallbackListener listener;
LowPrecisionTimer timer(listener.task_runner(),
base::BindRepeating(&CallbackListener::Callback,
base::Unretained(&listener)));
// The timer can only fire on ticks, so 10 milliseconds is not enough here.
timer.StartRepeating(base::Milliseconds(10));
task_environment_.FastForwardBy(base::Milliseconds(10));
EXPECT_EQ(listener.callback_count(), 0u);
// But it does repeat on every tick.
task_environment_.FastForwardBy(TickPeriod() - base::Milliseconds(10));
EXPECT_EQ(listener.callback_count(), 1u);
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 2u);
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 3u);
timer.Shutdown();
// The timer stops on shutdown.
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 3u);
}
TEST_F(LowPrecisionTimerTest, StopRepeatingTimer) {
CallbackListener listener;
LowPrecisionTimer timer(listener.task_runner(),
base::BindRepeating(&CallbackListener::Callback,
base::Unretained(&listener)));
// Repeat every tick.
timer.StartRepeating(TickPeriod());
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 1u);
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 2u);
// Stop the timer and ensure it stops repeating.
timer.Stop();
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 2u);
// The timer is reusable - can start and stop again.
timer.StartRepeating(TickPeriod());
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 3u);
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 4u);
timer.Stop();
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(listener.callback_count(), 4u);
// Cleanup.
timer.Shutdown();
}
// Ensures stopping inside the timer callback does not deadlock.
TEST_F(LowPrecisionTimerTest, StopTimerFromInsideCallback) {
// Stops its own timer from inside the callback after a tick.
RecursiveStopper recursive_stopper(TickPeriod());
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(recursive_stopper.callback_count(), 1u);
// Ensure we are stopped, the callback count does not increase.
task_environment_.FastForwardBy(TickPeriod());
EXPECT_EQ(recursive_stopper.callback_count(), 1u);
}
// Ensures in-parallel stopping while the task may be running does not
// deadlock in race condition. Coverage for https://crbug.com/1281399.
TEST(LowPrecisionTimerRealThreadsTest, StopTimerWithRaceCondition) {
base::test::TaskEnvironment task_environment(
base::test::TaskEnvironment::ThreadingMode::MULTIPLE_THREADS,
base::test::TaskEnvironment::TimeSource::SYSTEM_TIME);
CallbackListener listener;
LowPrecisionTimer timer(listener.task_runner(),
base::BindRepeating(&CallbackListener::Callback,
base::Unretained(&listener)));
scoped_refptr<base::SequencedTaskRunner> dedicated_task_runner =
base::ThreadPool::CreateSingleThreadTaskRunner(
{}, base::SingleThreadTaskRunnerThreadMode::DEDICATED);
// Create a race condition between running the timer's task and stopping the
// timer.
timer.StartOneShot(base::Milliseconds(0));
base::WaitableEvent event;
dedicated_task_runner->PostTask(
FROM_HERE, base::BindOnce(
[](LowPrecisionTimer* timer, base::WaitableEvent* event) {
timer->Stop();
event->Signal();
},
base::Unretained(&timer), base::Unretained(&event)));
event.Wait();
timer.Shutdown();
}
TEST_F(LowPrecisionTimerTest, IsActive) {
IsActiveChecker is_active_checker;
// StartOneShot() makes the timer temporarily active.
EXPECT_FALSE(is_active_checker.timer().IsActive());
is_active_checker.timer().StartOneShot(TickPeriod());
EXPECT_TRUE(is_active_checker.timer().IsActive());
task_environment_.FastForwardBy(TickPeriod());
EXPECT_FALSE(is_active_checker.timer().IsActive());
// The timer is said to be inactive inside the one-shot callback.
EXPECT_FALSE(is_active_checker.was_active_in_last_callback());
// StartRepeating() makes the timer active until stopped.
EXPECT_FALSE(is_active_checker.timer().IsActive());
is_active_checker.timer().StartRepeating(TickPeriod());
EXPECT_TRUE(is_active_checker.timer().IsActive());
task_environment_.FastForwardBy(TickPeriod());
EXPECT_TRUE(is_active_checker.timer().IsActive());
// The timer is said to be active inside the repeating callback.
EXPECT_TRUE(is_active_checker.was_active_in_last_callback());
}
} // namespace blink
|