From 18aba662717443bf5bd9481372d377ec0ec323cd Mon Sep 17 00:00:00 2001 From: Per K Date: Fri, 16 Jun 2023 15:42:00 +0200 Subject: [PATCH] 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 Commit-Queue: Per Kjellander Cr-Commit-Position: refs/heads/main@{#40301} --- api/task_queue/task_queue_test.cc | 33 +++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/api/task_queue/task_queue_test.cc b/api/task_queue/task_queue_test.cc index 7849f4273d..b02333ec58 100644 --- a/api/task_queue/task_queue_test.cc +++ b/api/task_queue/task_queue_test.cc @@ -177,14 +177,35 @@ TEST_P(TaskQueueTest, PostedUnexecutedClosureDestroyedOnTaskQueue) { CreateTaskQueue(factory, "PostedUnexecutedClosureDestroyedOnTaskQueue"); TaskQueueBase* queue_ptr = queue.get(); 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)); - // Then ensure the next lambda (which is likely not executing yet) is - // destroyed in the task queue context when the queue is deleted. - auto cleanup = absl::Cleanup( - [queue_ptr] { EXPECT_EQ(queue_ptr, TaskQueueBase::Current()); }); + rtc::Event finished; + // Then ensure the next lambda (which is likely not executing yet) is + // destroyed in the task queue context when the queue is deleted. + auto cleanup = absl::Cleanup([queue_ptr, &finished] { + EXPECT_EQ(queue_ptr, TaskQueueBase::Current()); + finished.Set(); + }); queue->PostTask([cleanup = std::move(cleanup)] {}); queue = nullptr; + finished.Wait(TimeDelta::Seconds(1)); +} + +TEST_P(TaskQueueTest, PostedClosureDestroyedOnTaskQueue) { + std::unique_ptr 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) { @@ -198,7 +219,7 @@ TEST_P(TaskQueueTest, PostedExecutedClosureDestroyedOnTaskQueue) { EXPECT_EQ(queue_ptr, TaskQueueBase::Current()); finished.Set(); })] {}); - finished.Wait(rtc::Event::kForever); + finished.Wait(TimeDelta::Seconds(1)); } TEST_P(TaskQueueTest, PostAndReuse) {