Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Artem Titov | c374d11 | 2022-06-16 19:27:45 | [diff] [blame] | 11 | #include "api/task_queue/pending_task_safety_flag.h" |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
Dor Hen | 5ac2c74 | 2024-08-05 08:21:02 | [diff] [blame] | 14 | #include <utility> |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 15 | |
Dor Hen | 5ac2c74 | 2024-08-05 08:21:02 | [diff] [blame] | 16 | #include "api/scoped_refptr.h" |
| 17 | #include "api/task_queue/task_queue_base.h" |
| 18 | #include "rtc_base/checks.h" |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 19 | #include "rtc_base/event.h" |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 20 | #include "rtc_base/task_queue_for_test.h" |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 21 | #include "test/gtest.h" |
| 22 | |
| 23 | namespace webrtc { |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 24 | |
| 25 | TEST(PendingTaskSafetyFlagTest, Basic) { |
Tommi | a98cea8 | 2020-05-13 13:06:19 | [diff] [blame] | 26 | rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag; |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 27 | { |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 28 | // Scope for the `owner` instance. |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 29 | class Owner { |
| 30 | public: |
| 31 | Owner() = default; |
| 32 | ~Owner() { flag_->SetNotAlive(); } |
| 33 | |
Tommi | a98cea8 | 2020-05-13 13:06:19 | [diff] [blame] | 34 | rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ = |
| 35 | PendingTaskSafetyFlag::Create(); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 36 | } owner; |
| 37 | EXPECT_TRUE(owner.flag_->alive()); |
| 38 | safety_flag = owner.flag_; |
| 39 | EXPECT_TRUE(safety_flag->alive()); |
| 40 | } |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 41 | // `owner` now out of scope. |
Tommi | a98cea8 | 2020-05-13 13:06:19 | [diff] [blame] | 42 | EXPECT_FALSE(safety_flag->alive()); |
| 43 | } |
| 44 | |
| 45 | TEST(PendingTaskSafetyFlagTest, BasicScoped) { |
| 46 | rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag; |
| 47 | { |
| 48 | struct Owner { |
| 49 | ScopedTaskSafety safety; |
| 50 | } owner; |
| 51 | safety_flag = owner.safety.flag(); |
| 52 | EXPECT_TRUE(safety_flag->alive()); |
| 53 | } |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 54 | // `owner` now out of scope. |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 55 | EXPECT_FALSE(safety_flag->alive()); |
| 56 | } |
| 57 | |
| 58 | TEST(PendingTaskSafetyFlagTest, PendingTaskSuccess) { |
| 59 | TaskQueueForTest tq1("OwnerHere"); |
| 60 | TaskQueueForTest tq2("OwnerNotHere"); |
| 61 | |
| 62 | class Owner { |
| 63 | public: |
| 64 | Owner() : tq_main_(TaskQueueBase::Current()) { RTC_DCHECK(tq_main_); } |
| 65 | ~Owner() { |
| 66 | RTC_DCHECK(tq_main_->IsCurrent()); |
| 67 | flag_->SetNotAlive(); |
| 68 | } |
| 69 | |
| 70 | void DoStuff() { |
| 71 | RTC_DCHECK(!tq_main_->IsCurrent()); |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 72 | rtc::scoped_refptr<PendingTaskSafetyFlag> safe = flag_; |
| 73 | tq_main_->PostTask([safe = std::move(safe), this]() { |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 74 | if (!safe->alive()) |
| 75 | return; |
| 76 | stuff_done_ = true; |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 77 | }); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool stuff_done() const { return stuff_done_; } |
| 81 | |
| 82 | private: |
| 83 | TaskQueueBase* const tq_main_; |
| 84 | bool stuff_done_ = false; |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 85 | rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ = |
| 86 | PendingTaskSafetyFlag::Create(); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | std::unique_ptr<Owner> owner; |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 90 | tq1.SendTask([&owner]() { |
| 91 | owner = std::make_unique<Owner>(); |
| 92 | EXPECT_FALSE(owner->stuff_done()); |
| 93 | }); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 94 | ASSERT_TRUE(owner); |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 95 | tq2.SendTask([&owner]() { owner->DoStuff(); }); |
| 96 | tq1.SendTask([&owner]() { |
| 97 | EXPECT_TRUE(owner->stuff_done()); |
| 98 | owner.reset(); |
| 99 | }); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 100 | ASSERT_FALSE(owner); |
| 101 | } |
| 102 | |
| 103 | TEST(PendingTaskSafetyFlagTest, PendingTaskDropped) { |
| 104 | TaskQueueForTest tq1("OwnerHere"); |
| 105 | TaskQueueForTest tq2("OwnerNotHere"); |
| 106 | |
| 107 | class Owner { |
| 108 | public: |
| 109 | explicit Owner(bool* stuff_done) |
| 110 | : tq_main_(TaskQueueBase::Current()), stuff_done_(stuff_done) { |
| 111 | RTC_DCHECK(tq_main_); |
| 112 | *stuff_done_ = false; |
| 113 | } |
Artem Titov | c374d11 | 2022-06-16 19:27:45 | [diff] [blame] | 114 | ~Owner() { RTC_DCHECK(tq_main_->IsCurrent()); } |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 115 | |
| 116 | void DoStuff() { |
| 117 | RTC_DCHECK(!tq_main_->IsCurrent()); |
Tommi | a98cea8 | 2020-05-13 13:06:19 | [diff] [blame] | 118 | tq_main_->PostTask( |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 119 | SafeTask(safety_.flag(), [this]() { *stuff_done_ = true; })); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private: |
| 123 | TaskQueueBase* const tq_main_; |
| 124 | bool* const stuff_done_; |
Tommi | a98cea8 | 2020-05-13 13:06:19 | [diff] [blame] | 125 | ScopedTaskSafety safety_; |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | std::unique_ptr<Owner> owner; |
| 129 | bool stuff_done = false; |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 130 | tq1.SendTask([&owner, &stuff_done]() { |
| 131 | owner = std::make_unique<Owner>(&stuff_done); |
| 132 | }); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 133 | ASSERT_TRUE(owner); |
| 134 | // Queue up a task on tq1 that will execute before the 'DoStuff' task |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 135 | // can, and delete the `owner` before the 'stuff' task can execute. |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 136 | rtc::Event blocker; |
| 137 | tq1.PostTask([&blocker, &owner]() { |
| 138 | blocker.Wait(rtc::Event::kForever); |
| 139 | owner.reset(); |
| 140 | }); |
| 141 | |
| 142 | // Queue up a DoStuff... |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 143 | tq2.SendTask([&owner]() { owner->DoStuff(); }); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 144 | |
| 145 | ASSERT_TRUE(owner); |
| 146 | blocker.Set(); |
| 147 | |
| 148 | // Run an empty task on tq1 to flush all the queued tasks. |
Tomas Gunnarsson | 25e7352 | 2021-04-19 08:14:10 | [diff] [blame] | 149 | tq1.WaitForPreviouslyPostedTasks(); |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 150 | ASSERT_FALSE(owner); |
| 151 | EXPECT_FALSE(stuff_done); |
| 152 | } |
Tomas Gunnarsson | 25e7352 | 2021-04-19 08:14:10 | [diff] [blame] | 153 | |
| 154 | TEST(PendingTaskSafetyFlagTest, PendingTaskNotAliveInitialized) { |
| 155 | TaskQueueForTest tq("PendingTaskNotAliveInitialized"); |
| 156 | |
| 157 | // Create a new flag that initially not `alive`. |
| 158 | auto flag = PendingTaskSafetyFlag::CreateDetachedInactive(); |
Danil Chapovalov | e519f38 | 2022-08-11 10:26:09 | [diff] [blame] | 159 | tq.SendTask([&flag]() { EXPECT_FALSE(flag->alive()); }); |
Tomas Gunnarsson | 25e7352 | 2021-04-19 08:14:10 | [diff] [blame] | 160 | |
| 161 | bool task_1_ran = false; |
| 162 | bool task_2_ran = false; |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 163 | tq.PostTask(SafeTask(flag, [&task_1_ran]() { task_1_ran = true; })); |
Tomas Gunnarsson | 25e7352 | 2021-04-19 08:14:10 | [diff] [blame] | 164 | tq.PostTask([&flag]() { flag->SetAlive(); }); |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 165 | tq.PostTask(SafeTask(flag, [&task_2_ran]() { task_2_ran = true; })); |
Tomas Gunnarsson | 25e7352 | 2021-04-19 08:14:10 | [diff] [blame] | 166 | |
| 167 | tq.WaitForPreviouslyPostedTasks(); |
| 168 | EXPECT_FALSE(task_1_ran); |
| 169 | EXPECT_TRUE(task_2_ran); |
| 170 | } |
| 171 | |
Tommi | 8da5953 | 2023-10-24 21:43:56 | [diff] [blame] | 172 | TEST(PendingTaskSafetyFlagTest, PendingTaskInitializedForTaskQueue) { |
| 173 | TaskQueueForTest tq("PendingTaskAliveInitializedForTaskQueue"); |
| 174 | |
| 175 | // Create a new flag that initially `alive`, attached to a specific TQ. |
| 176 | auto flag = PendingTaskSafetyFlag::CreateAttachedToTaskQueue(true, tq.Get()); |
| 177 | tq.SendTask([&flag]() { EXPECT_TRUE(flag->alive()); }); |
| 178 | // Repeat the same steps but initialize as inactive. |
| 179 | flag = PendingTaskSafetyFlag::CreateAttachedToTaskQueue(false, tq.Get()); |
| 180 | tq.SendTask([&flag]() { EXPECT_FALSE(flag->alive()); }); |
| 181 | } |
| 182 | |
Danil Chapovalov | a7e15a2 | 2022-07-05 14:03:03 | [diff] [blame] | 183 | TEST(PendingTaskSafetyFlagTest, SafeTask) { |
| 184 | rtc::scoped_refptr<PendingTaskSafetyFlag> flag = |
| 185 | PendingTaskSafetyFlag::Create(); |
| 186 | |
| 187 | int count = 0; |
| 188 | // Create two identical tasks that increment the `count`. |
| 189 | auto task1 = SafeTask(flag, [&count] { ++count; }); |
| 190 | auto task2 = SafeTask(flag, [&count] { ++count; }); |
| 191 | |
| 192 | EXPECT_EQ(count, 0); |
| 193 | std::move(task1)(); |
| 194 | EXPECT_EQ(count, 1); |
| 195 | flag->SetNotAlive(); |
| 196 | // Now task2 should actually not run. |
| 197 | std::move(task2)(); |
| 198 | EXPECT_EQ(count, 1); |
| 199 | } |
| 200 | |
Tommi | 3c9bcc1 | 2020-04-15 14:45:47 | [diff] [blame] | 201 | } // namespace webrtc |