Add test to ensure task deleted on TQ

Bug: webrtc:14449
Change-Id: I85757af9c1ad6ad630d9ffe7b2528ca7c7161883
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/308900
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40301}
This commit is contained in:
Per K 2023-06-16 15:42:00 +02:00 committed by WebRTC LUCI CQ
parent 213090bf4b
commit 18aba66271

View file

@ -177,14 +177,35 @@ TEST_P(TaskQueueTest, PostedUnexecutedClosureDestroyedOnTaskQueue) {
CreateTaskQueue(factory, "PostedUnexecutedClosureDestroyedOnTaskQueue"); CreateTaskQueue(factory, "PostedUnexecutedClosureDestroyedOnTaskQueue");
TaskQueueBase* queue_ptr = queue.get(); TaskQueueBase* queue_ptr = queue.get();
queue->PostTask([] { SleepFor(TimeDelta::Millis(100)); }); queue->PostTask([] { SleepFor(TimeDelta::Millis(100)); });
// Give the task queue a chance to start executing the first lambda. // Give the task queue a chance to start executing the first lambda.
SleepFor(TimeDelta::Millis(10)); SleepFor(TimeDelta::Millis(10));
// Then ensure the next lambda (which is likely not executing yet) is rtc::Event finished;
// destroyed in the task queue context when the queue is deleted. // Then ensure the next lambda (which is likely not executing yet) is
auto cleanup = absl::Cleanup( // destroyed in the task queue context when the queue is deleted.
[queue_ptr] { EXPECT_EQ(queue_ptr, TaskQueueBase::Current()); }); auto cleanup = absl::Cleanup([queue_ptr, &finished] {
EXPECT_EQ(queue_ptr, TaskQueueBase::Current());
finished.Set();
});
queue->PostTask([cleanup = std::move(cleanup)] {}); queue->PostTask([cleanup = std::move(cleanup)] {});
queue = nullptr; queue = nullptr;
finished.Wait(TimeDelta::Seconds(1));
}
TEST_P(TaskQueueTest, PostedClosureDestroyedOnTaskQueue) {
std::unique_ptr<webrtc::TaskQueueFactory> factory = GetParam()(nullptr);
auto queue = CreateTaskQueue(factory, "PostedClosureDestroyedOnTaskQueue");
TaskQueueBase* queue_ptr = queue.get();
rtc::Event finished;
auto cleanup = absl::Cleanup([queue_ptr, &finished] {
EXPECT_EQ(queue_ptr, TaskQueueBase::Current());
finished.Set();
});
// The cleanup task may or may not have had time to execute when the task
// queue is destroyed. Regardless, the task should be destroyed on the
// queue.
queue->PostTask([cleanup = std::move(cleanup)] {});
queue = nullptr;
finished.Wait(TimeDelta::Seconds(1));
} }
TEST_P(TaskQueueTest, PostedExecutedClosureDestroyedOnTaskQueue) { TEST_P(TaskQueueTest, PostedExecutedClosureDestroyedOnTaskQueue) {
@ -198,7 +219,7 @@ TEST_P(TaskQueueTest, PostedExecutedClosureDestroyedOnTaskQueue) {
EXPECT_EQ(queue_ptr, TaskQueueBase::Current()); EXPECT_EQ(queue_ptr, TaskQueueBase::Current());
finished.Set(); finished.Set();
})] {}); })] {});
finished.Wait(rtc::Event::kForever); finished.Wait(TimeDelta::Seconds(1));
} }
TEST_P(TaskQueueTest, PostAndReuse) { TEST_P(TaskQueueTest, PostAndReuse) {