Skip to content

Commit

Permalink
Implement support for Chrome task origin tracing. #3.5/4
Browse files Browse the repository at this point in the history
This CL migrates unit tests to the new TaskQueueBase interface.

Bug: chromium:1416199
Change-Id: Ic15c694b28eb67450ac99fdd56754de1246a4d95
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/295621
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39434}
  • Loading branch information
Markus Handell authored and WebRTC LUCI CQ committed Mar 1, 2023
1 parent f6afb3f commit a1ceae2
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 102 deletions.
17 changes: 12 additions & 5 deletions api/task_queue/test/mock_task_queue_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ namespace webrtc {

class MockTaskQueueBase : public TaskQueueBase {
public:
using TaskQueueBase::PostDelayedTaskTraits;
using TaskQueueBase::PostTaskTraits;

MOCK_METHOD(void, Delete, (), (override));
MOCK_METHOD(void, PostTask, (absl::AnyInvocable<void() &&>), (override));
MOCK_METHOD(void,
PostDelayedTask,
(absl::AnyInvocable<void() &&>, TimeDelta),
PostTaskImpl,
(absl::AnyInvocable<void() &&>,
const PostTaskTraits&,
const Location&),
(override));
MOCK_METHOD(void,
PostDelayedHighPrecisionTask,
(absl::AnyInvocable<void() &&>, TimeDelta),
PostDelayedTaskImpl,
(absl::AnyInvocable<void() &&>,
TimeDelta,
const PostDelayedTaskTraits&,
const Location&),
(override));
};

Expand Down
4 changes: 3 additions & 1 deletion audio/audio_state_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ struct FakeAsyncAudioProcessingHelper {
FakeTaskQueue() = default;

void Delete() override { delete this; }
void PostTask(absl::AnyInvocable<void() &&> task) override {
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& /*traits*/,
const Location& /*location*/) override {
std::move(task)();
}
};
Expand Down
8 changes: 5 additions & 3 deletions audio/voip/test/audio_channel_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/call/transport.h"
#include "api/task_queue/task_queue_base.h"
#include "api/task_queue/task_queue_factory.h"
#include "audio/voip/test/mock_task_queue.h"
#include "modules/audio_mixer/audio_mixer_impl.h"
Expand All @@ -31,6 +32,7 @@ using ::testing::Invoke;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::Unused;
using ::testing::WithArg;

constexpr uint64_t kStartTime = 123456789;
constexpr uint32_t kLocalSsrc = 0xdeadc0de;
Expand All @@ -49,9 +51,9 @@ class AudioChannelTest : public ::testing::Test {
decoder_factory_ = CreateBuiltinAudioDecoderFactory();

// By default, run the queued task immediately.
ON_CALL(task_queue_, PostTask)
.WillByDefault(
[](absl::AnyInvocable<void() &&> task) { std::move(task)(); });
ON_CALL(task_queue_, PostTaskImpl)
.WillByDefault(WithArg<0>(
[](absl::AnyInvocable<void() &&> task) { std::move(task)(); }));
}

void SetUp() override { audio_channel_ = CreateAudioChannel(kLocalSsrc); }
Expand Down
28 changes: 25 additions & 3 deletions net/dcsctp/timer/task_queue_timeout_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace dcsctp {
namespace {
using ::testing::_;
using ::testing::Field;
using ::testing::MockFunction;
using ::testing::NiceMock;

Expand Down Expand Up @@ -118,7 +119,14 @@ TEST_F(TaskQueueTimeoutTest, KilledBeforeExpired) {

TEST(TaskQueueTimeoutWithMockTaskQueueTest, CanSetTimeoutPrecisionToLow) {
NiceMock<webrtc::MockTaskQueueBase> mock_task_queue;
EXPECT_CALL(mock_task_queue, PostDelayedTask(_, _));
EXPECT_CALL(
mock_task_queue,
PostDelayedTaskImpl(
_, _,
Field(
&webrtc::MockTaskQueueBase::PostDelayedTaskTraits::high_precision,
false),
_));
TaskQueueTimeoutFactory factory(
mock_task_queue, []() { return TimeMs(1337); },
[](TimeoutID timeout_id) {});
Expand All @@ -129,7 +137,14 @@ TEST(TaskQueueTimeoutWithMockTaskQueueTest, CanSetTimeoutPrecisionToLow) {

TEST(TaskQueueTimeoutWithMockTaskQueueTest, CanSetTimeoutPrecisionToHigh) {
NiceMock<webrtc::MockTaskQueueBase> mock_task_queue;
EXPECT_CALL(mock_task_queue, PostDelayedHighPrecisionTask(_, _));
EXPECT_CALL(
mock_task_queue,
PostDelayedTaskImpl(
_, _,
Field(
&webrtc::MockTaskQueueBase::PostDelayedTaskTraits::high_precision,
true),
_));
TaskQueueTimeoutFactory factory(
mock_task_queue, []() { return TimeMs(1337); },
[](TimeoutID timeout_id) {});
Expand All @@ -140,7 +155,14 @@ TEST(TaskQueueTimeoutWithMockTaskQueueTest, CanSetTimeoutPrecisionToHigh) {

TEST(TaskQueueTimeoutWithMockTaskQueueTest, TimeoutPrecisionIsLowByDefault) {
NiceMock<webrtc::MockTaskQueueBase> mock_task_queue;
EXPECT_CALL(mock_task_queue, PostDelayedTask(_, _));
EXPECT_CALL(
mock_task_queue,
PostDelayedTaskImpl(
_, _,
Field(
&webrtc::MockTaskQueueBase::PostDelayedTaskTraits::high_precision,
false),
_));
TaskQueueTimeoutFactory factory(
mock_task_queue, []() { return TimeMs(1337); },
[](TimeoutID timeout_id) {});
Expand Down
1 change: 1 addition & 0 deletions rtc_base/task_utils/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ if (rtc_include_tests) {
"..:rtc_task_queue",
"..:task_queue_for_test",
"../../api/task_queue",
"../../api/task_queue/test:mock_task_queue_base",
"../../api/units:time_delta",
"../../api/units:timestamp",
"../../system_wrappers:system_wrappers",
Expand Down
48 changes: 20 additions & 28 deletions rtc_base/task_utils/repeating_task_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "absl/functional/any_invocable.h"
#include "api/task_queue/task_queue_base.h"
#include "api/task_queue/test/mock_task_queue_base.h"
#include "api/units/time_delta.h"
#include "api/units/timestamp.h"
#include "rtc_base/event.h"
Expand All @@ -32,6 +33,7 @@ using ::testing::Invoke;
using ::testing::MockFunction;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::WithArg;

constexpr TimeDelta kTimeout = TimeDelta::Millis(1000);

Expand All @@ -41,21 +43,10 @@ class MockClosure {
MOCK_METHOD(void, Delete, ());
};

class MockTaskQueue : public TaskQueueBase {
class MockTaskQueue : public MockTaskQueueBase {
public:
MockTaskQueue() : task_queue_setter_(this) {}

MOCK_METHOD(void, Delete, (), (override));
MOCK_METHOD(void, PostTask, (absl::AnyInvocable<void() &&>), (override));
MOCK_METHOD(void,
PostDelayedTask,
(absl::AnyInvocable<void() &&>, TimeDelta),
(override));
MOCK_METHOD(void,
PostDelayedHighPrecisionTask,
(absl::AnyInvocable<void() &&>, TimeDelta),
(override));

private:
CurrentTaskQueueSetter task_queue_setter_;
};
Expand All @@ -67,23 +58,22 @@ class FakeTaskQueue : public TaskQueueBase {

void Delete() override {}

void PostTask(absl::AnyInvocable<void() &&> task) override {
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& /*traits*/,
const Location& /*location*/) override {
last_task_ = std::move(task);
last_precision_ = absl::nullopt;
last_delay_ = TimeDelta::Zero();
}

void PostDelayedTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override {
void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& /*location*/) override {
last_task_ = std::move(task);
last_precision_ = TaskQueueBase::DelayPrecision::kLow;
last_delay_ = delay;
}

void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override {
last_task_ = std::move(task);
last_precision_ = TaskQueueBase::DelayPrecision::kHigh;
last_precision_ = traits.high_precision
? TaskQueueBase::DelayPrecision::kHigh
: TaskQueueBase::DelayPrecision::kLow;
last_delay_ = delay;
}

Expand Down Expand Up @@ -339,8 +329,10 @@ TEST(RepeatingTaskTest, ClockIntegration) {
SimulatedClock clock(Timestamp::Zero());

NiceMock<MockTaskQueue> task_queue;
ON_CALL(task_queue, PostDelayedTask)
.WillByDefault([&](absl::AnyInvocable<void() &&> task, TimeDelta delay) {
ON_CALL(task_queue, PostDelayedTaskImpl)
.WillByDefault([&](absl::AnyInvocable<void() &&> task, TimeDelta delay,
const MockTaskQueue::PostDelayedTaskTraits&,
const Location&) {
EXPECT_EQ(delay, expected_delay);
delayed_task = std::move(task);
});
Expand Down Expand Up @@ -368,10 +360,10 @@ TEST(RepeatingTaskTest, CanBeStoppedAfterTaskQueueDeletedTheRepeatingTask) {
absl::AnyInvocable<void() &&> repeating_task;

MockTaskQueue task_queue;
EXPECT_CALL(task_queue, PostDelayedTask)
.WillOnce([&](absl::AnyInvocable<void() &&> task, TimeDelta delay) {
EXPECT_CALL(task_queue, PostDelayedTaskImpl)
.WillOnce(WithArg<0>([&](absl::AnyInvocable<void() &&> task) {
repeating_task = std::move(task);
});
}));

RepeatingTaskHandle handle =
RepeatingTaskHandle::DelayedStart(&task_queue, TimeDelta::Millis(100),
Expand Down
12 changes: 7 additions & 5 deletions rtc_base/unique_id_generator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ class FakeTaskQueue : public webrtc::TaskQueueBase {
FakeTaskQueue() : task_queue_setter_(this) {}

void Delete() override {}
void PostTask(absl::AnyInvocable<void() &&> task) override {}
void PostDelayedTask(absl::AnyInvocable<void() &&> task,
webrtc::TimeDelta delay) override {}
void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
webrtc::TimeDelta delay) override {}
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const webrtc::Location& location) override {}
void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
webrtc::TimeDelta delay,
const PostDelayedTaskTraits& traits,
const webrtc::Location& location) override {}

private:
CurrentTaskQueueSetter task_queue_setter_;
Expand Down
23 changes: 12 additions & 11 deletions test/time_controller/external_time_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ class ExternalTimeController::TaskQueueWrapper : public TaskQueueBase {
std::unique_ptr<TaskQueueBase, TaskQueueDeleter> base)
: parent_(parent), base_(std::move(base)) {}

void PostTask(absl::AnyInvocable<void() &&> task) override {
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const Location& location) override {
parent_->UpdateTime();
base_->PostTask(TaskWrapper(std::move(task)));
parent_->ScheduleNext();
}

void PostDelayedTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override {
void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& location) override {
parent_->UpdateTime();
base_->PostDelayedTask(TaskWrapper(std::move(task)), delay);
parent_->ScheduleNext();
}

void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override {
parent_->UpdateTime();
base_->PostDelayedHighPrecisionTask(TaskWrapper(std::move(task)), delay);
if (traits.high_precision) {
base_->PostDelayedHighPrecisionTask(TaskWrapper(std::move(task)), delay);
} else {
base_->PostDelayedTask(TaskWrapper(std::move(task)), delay);
}
parent_->ScheduleNext();
}

Expand Down
15 changes: 7 additions & 8 deletions test/time_controller/simulated_task_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,19 @@ void SimulatedTaskQueue::RunReady(Timestamp at_time) {
}
}

void SimulatedTaskQueue::PostTask(absl::AnyInvocable<void() &&> task) {
void SimulatedTaskQueue::PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& /*traits*/,
const Location& /*location*/) {
MutexLock lock(&lock_);
ready_tasks_.push_back(std::move(task));
next_run_time_ = Timestamp::MinusInfinity();
}

void SimulatedTaskQueue::PostDelayedTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) {
PostDelayedHighPrecisionTask(std::move(task), delay);
}

void SimulatedTaskQueue::PostDelayedHighPrecisionTask(
void SimulatedTaskQueue::PostDelayedTaskImpl(
absl::AnyInvocable<void() &&> task,
TimeDelta delay) {
TimeDelta delay,
const PostDelayedTaskTraits& /*traits*/,
const Location& /*location*/) {
MutexLock lock(&lock_);
Timestamp target_time = handler_->CurrentTime() + delay;
delayed_tasks_[target_time].push_back(std::move(task));
Expand Down
12 changes: 7 additions & 5 deletions test/time_controller/simulated_task_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ class SimulatedTaskQueue : public TaskQueueBase,

// TaskQueueBase interface
void Delete() override;
void PostTask(absl::AnyInvocable<void() &&> task) override;
void PostDelayedTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override;
void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override;
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const Location& location) override;
void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& location) override;

private:
sim_time_impl::SimulatedTimeControllerImpl* const handler_;
Expand Down
23 changes: 9 additions & 14 deletions test/time_controller/simulated_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,19 @@ void SimulatedThread::BlockingCall(rtc::FunctionView<void()> functor) {
}
}

void SimulatedThread::PostTask(absl::AnyInvocable<void() &&> task) {
rtc::Thread::PostTask(std::move(task));
void SimulatedThread::PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const Location& location) {
rtc::Thread::PostTaskImpl(std::move(task), traits, location);
MutexLock lock(&lock_);
next_run_time_ = Timestamp::MinusInfinity();
}

void SimulatedThread::PostDelayedTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) {
rtc::Thread::PostDelayedTask(std::move(task), delay);
MutexLock lock(&lock_);
next_run_time_ =
std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis()) + delay);
}

void SimulatedThread::PostDelayedHighPrecisionTask(
absl::AnyInvocable<void() &&> task,
TimeDelta delay) {
rtc::Thread::PostDelayedHighPrecisionTask(std::move(task), delay);
void SimulatedThread::PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& location) {
rtc::Thread::PostDelayedTaskImpl(std::move(task), delay, traits, location);
MutexLock lock(&lock_);
next_run_time_ =
std::min(next_run_time_, Timestamp::Millis(rtc::TimeMillis()) + delay);
Expand Down
12 changes: 7 additions & 5 deletions test/time_controller/simulated_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ class SimulatedThread : public rtc::Thread,

// Thread interface
void BlockingCall(rtc::FunctionView<void()> functor) override;
void PostTask(absl::AnyInvocable<void() &&> task) override;
void PostDelayedTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override;
void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
TimeDelta delay) override;
void PostTaskImpl(absl::AnyInvocable<void() &&> task,
const PostTaskTraits& traits,
const Location& location) override;
void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task,
TimeDelta delay,
const PostDelayedTaskTraits& traits,
const Location& location) override;

void Stop() override;

Expand Down
Loading

0 comments on commit a1ceae2

Please sign in to comment.