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
11 #include "api/task_queue/pending_task_safety_flag.h"
12
13 #include <memory>
14
15 #include "rtc_base/event.h"
16 #include "rtc_base/logging.h"
17 #include "rtc_base/task_queue_for_test.h"
18 #include "test/gmock.h"
19 #include "test/gtest.h"
20
21 namespace webrtc {
22
TEST(PendingTaskSafetyFlagTest,Basic)23 TEST(PendingTaskSafetyFlagTest, Basic) {
24 rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag;
25 {
26 // Scope for the `owner` instance.
27 class Owner {
28 public:
29 Owner() = default;
30 ~Owner() { flag_->SetNotAlive(); }
31
32 rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
33 PendingTaskSafetyFlag::Create();
34 } owner;
35 EXPECT_TRUE(owner.flag_->alive());
36 safety_flag = owner.flag_;
37 EXPECT_TRUE(safety_flag->alive());
38 }
39 // `owner` now out of scope.
40 EXPECT_FALSE(safety_flag->alive());
41 }
42
TEST(PendingTaskSafetyFlagTest,BasicScoped)43 TEST(PendingTaskSafetyFlagTest, BasicScoped) {
44 rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag;
45 {
46 struct Owner {
47 ScopedTaskSafety safety;
48 } owner;
49 safety_flag = owner.safety.flag();
50 EXPECT_TRUE(safety_flag->alive());
51 }
52 // `owner` now out of scope.
53 EXPECT_FALSE(safety_flag->alive());
54 }
55
TEST(PendingTaskSafetyFlagTest,PendingTaskSuccess)56 TEST(PendingTaskSafetyFlagTest, PendingTaskSuccess) {
57 TaskQueueForTest tq1("OwnerHere");
58 TaskQueueForTest tq2("OwnerNotHere");
59
60 class Owner {
61 public:
62 Owner() : tq_main_(TaskQueueBase::Current()) { RTC_DCHECK(tq_main_); }
63 ~Owner() {
64 RTC_DCHECK(tq_main_->IsCurrent());
65 flag_->SetNotAlive();
66 }
67
68 void DoStuff() {
69 RTC_DCHECK(!tq_main_->IsCurrent());
70 rtc::scoped_refptr<PendingTaskSafetyFlag> safe = flag_;
71 tq_main_->PostTask([safe = std::move(safe), this]() {
72 if (!safe->alive())
73 return;
74 stuff_done_ = true;
75 });
76 }
77
78 bool stuff_done() const { return stuff_done_; }
79
80 private:
81 TaskQueueBase* const tq_main_;
82 bool stuff_done_ = false;
83 rtc::scoped_refptr<PendingTaskSafetyFlag> flag_ =
84 PendingTaskSafetyFlag::Create();
85 };
86
87 std::unique_ptr<Owner> owner;
88 tq1.SendTask([&owner]() {
89 owner = std::make_unique<Owner>();
90 EXPECT_FALSE(owner->stuff_done());
91 });
92 ASSERT_TRUE(owner);
93 tq2.SendTask([&owner]() { owner->DoStuff(); });
94 tq1.SendTask([&owner]() {
95 EXPECT_TRUE(owner->stuff_done());
96 owner.reset();
97 });
98 ASSERT_FALSE(owner);
99 }
100
TEST(PendingTaskSafetyFlagTest,PendingTaskDropped)101 TEST(PendingTaskSafetyFlagTest, PendingTaskDropped) {
102 TaskQueueForTest tq1("OwnerHere");
103 TaskQueueForTest tq2("OwnerNotHere");
104
105 class Owner {
106 public:
107 explicit Owner(bool* stuff_done)
108 : tq_main_(TaskQueueBase::Current()), stuff_done_(stuff_done) {
109 RTC_DCHECK(tq_main_);
110 *stuff_done_ = false;
111 }
112 ~Owner() { RTC_DCHECK(tq_main_->IsCurrent()); }
113
114 void DoStuff() {
115 RTC_DCHECK(!tq_main_->IsCurrent());
116 tq_main_->PostTask(
117 SafeTask(safety_.flag(), [this]() { *stuff_done_ = true; }));
118 }
119
120 private:
121 TaskQueueBase* const tq_main_;
122 bool* const stuff_done_;
123 ScopedTaskSafety safety_;
124 };
125
126 std::unique_ptr<Owner> owner;
127 bool stuff_done = false;
128 tq1.SendTask([&owner, &stuff_done]() {
129 owner = std::make_unique<Owner>(&stuff_done);
130 });
131 ASSERT_TRUE(owner);
132 // Queue up a task on tq1 that will execute before the 'DoStuff' task
133 // can, and delete the `owner` before the 'stuff' task can execute.
134 rtc::Event blocker;
135 tq1.PostTask([&blocker, &owner]() {
136 blocker.Wait(rtc::Event::kForever);
137 owner.reset();
138 });
139
140 // Queue up a DoStuff...
141 tq2.SendTask([&owner]() { owner->DoStuff(); });
142
143 ASSERT_TRUE(owner);
144 blocker.Set();
145
146 // Run an empty task on tq1 to flush all the queued tasks.
147 tq1.WaitForPreviouslyPostedTasks();
148 ASSERT_FALSE(owner);
149 EXPECT_FALSE(stuff_done);
150 }
151
TEST(PendingTaskSafetyFlagTest,PendingTaskNotAliveInitialized)152 TEST(PendingTaskSafetyFlagTest, PendingTaskNotAliveInitialized) {
153 TaskQueueForTest tq("PendingTaskNotAliveInitialized");
154
155 // Create a new flag that initially not `alive`.
156 auto flag = PendingTaskSafetyFlag::CreateDetachedInactive();
157 tq.SendTask([&flag]() { EXPECT_FALSE(flag->alive()); });
158
159 bool task_1_ran = false;
160 bool task_2_ran = false;
161 tq.PostTask(SafeTask(flag, [&task_1_ran]() { task_1_ran = true; }));
162 tq.PostTask([&flag]() { flag->SetAlive(); });
163 tq.PostTask(SafeTask(flag, [&task_2_ran]() { task_2_ran = true; }));
164
165 tq.WaitForPreviouslyPostedTasks();
166 EXPECT_FALSE(task_1_ran);
167 EXPECT_TRUE(task_2_ran);
168 }
169
TEST(PendingTaskSafetyFlagTest,SafeTask)170 TEST(PendingTaskSafetyFlagTest, SafeTask) {
171 rtc::scoped_refptr<PendingTaskSafetyFlag> flag =
172 PendingTaskSafetyFlag::Create();
173
174 int count = 0;
175 // Create two identical tasks that increment the `count`.
176 auto task1 = SafeTask(flag, [&count] { ++count; });
177 auto task2 = SafeTask(flag, [&count] { ++count; });
178
179 EXPECT_EQ(count, 0);
180 std::move(task1)();
181 EXPECT_EQ(count, 1);
182 flag->SetNotAlive();
183 // Now task2 should actually not run.
184 std::move(task2)();
185 EXPECT_EQ(count, 1);
186 }
187
188 } // namespace webrtc
189