1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <memory>
#include "gtest/gtest.h"
#include "mozilla/SharedThreadPool.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/TaskQueue.h"
#include "mozilla/Unused.h"
#include "nsITargetShutdownTask.h"
#include "VideoUtils.h"
namespace TestTaskQueue {
using namespace mozilla;
TEST(TaskQueue, EventOrder)
{
RefPtr<TaskQueue> tq1 =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::SUPERVISOR),
"TestTaskQueue tq1", true);
RefPtr<TaskQueue> tq2 =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::SUPERVISOR),
"TestTaskQueue tq2", true);
RefPtr<TaskQueue> tq3 =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::SUPERVISOR),
"TestTaskQueue tq3", true);
bool errored = false;
int counter = 0;
int sync = 0;
Monitor monitor MOZ_UNANNOTATED("TaskQueue::EventOrder::monitor");
// We expect task1 happens before task3.
for (int i = 0; i < 10000; ++i) {
Unused << tq1->Dispatch(
NS_NewRunnableFunction(
"TestTaskQueue::TaskQueue_EventOrder_Test::TestBody",
[&]() {
Unused << tq2->Dispatch(NS_NewRunnableFunction(
"TestTaskQueue::TaskQueue_EventOrder_Test::TestBody",
[]() { // task0
}));
Unused << tq3->Dispatch(NS_NewRunnableFunction(
"TestTaskQueue::TaskQueue_EventOrder_Test::TestBody",
[&]() { // task1
EXPECT_EQ(1, ++counter);
errored = counter != 1;
MonitorAutoLock mon(monitor);
++sync;
mon.Notify();
}));
Unused << tq2->Dispatch(NS_NewRunnableFunction(
"TestTaskQueue::TaskQueue_EventOrder_Test::TestBody",
[&]() { // task2
Unused << tq3->Dispatch(NS_NewRunnableFunction(
"TestTaskQueue::TaskQueue_EventOrder_Test::TestBody",
[&]() { // task3
EXPECT_EQ(0, --counter);
errored = counter != 0;
MonitorAutoLock mon(monitor);
++sync;
mon.Notify();
}));
}));
}),
AbstractThread::TailDispatch);
// Ensure task1 and task3 are done before next loop.
MonitorAutoLock mon(monitor);
while (sync != 2) {
mon.Wait();
}
sync = 0;
if (errored) {
break;
}
}
tq1->BeginShutdown();
tq1->AwaitShutdownAndIdle();
tq2->BeginShutdown();
tq2->AwaitShutdownAndIdle();
tq3->BeginShutdown();
tq3->AwaitShutdownAndIdle();
}
TEST(TaskQueue, GetCurrentSerialEventTarget)
{
RefPtr<TaskQueue> tq1 =
TaskQueue::Create(GetMediaThreadPool(MediaThreadType::SUPERVISOR),
"TestTaskQueue GetCurrentSerialEventTarget", false);
Unused << tq1->Dispatch(NS_NewRunnableFunction(
"TestTaskQueue::TestCurrentSerialEventTarget::TestBody", [tq1]() {
nsCOMPtr<nsISerialEventTarget> thread = GetCurrentSerialEventTarget();
EXPECT_EQ(thread, tq1);
}));
tq1->BeginShutdown();
tq1->AwaitShutdownAndIdle();
}
namespace {
class TestShutdownTask final : public nsITargetShutdownTask {
public:
NS_DECL_THREADSAFE_ISUPPORTS
explicit TestShutdownTask(std::function<void()> aCallback)
: mCallback(std::move(aCallback)) {}
void TargetShutdown() override {
if (mCallback) {
mCallback();
}
}
private:
~TestShutdownTask() = default;
std::function<void()> mCallback;
};
NS_IMPL_ISUPPORTS(TestShutdownTask, nsITargetShutdownTask)
} // namespace
TEST(TaskQueue, ShutdownTask)
{
auto shutdownTaskRun = std::make_shared<bool>();
auto runnableFromShutdownRun = std::make_shared<bool>();
RefPtr<TaskQueue> tq = TaskQueue::Create(
GetMediaThreadPool(MediaThreadType::SUPERVISOR), "Testing TaskQueue");
nsCOMPtr<nsITargetShutdownTask> shutdownTask = new TestShutdownTask([=] {
EXPECT_TRUE(tq->IsOnCurrentThread());
ASSERT_FALSE(*shutdownTaskRun);
*shutdownTaskRun = true;
nsCOMPtr<nsITargetShutdownTask> dummyTask = new TestShutdownTask([] {});
nsresult rv = tq->RegisterShutdownTask(dummyTask);
EXPECT_TRUE(rv == NS_ERROR_UNEXPECTED);
MOZ_ALWAYS_SUCCEEDS(
tq->Dispatch(NS_NewRunnableFunction("afterShutdownTask", [=] {
EXPECT_TRUE(tq->IsOnCurrentThread());
nsCOMPtr<nsITargetShutdownTask> dummyTask =
new TestShutdownTask([] {});
nsresult rv = tq->RegisterShutdownTask(dummyTask);
EXPECT_TRUE(rv == NS_ERROR_UNEXPECTED);
ASSERT_FALSE(*runnableFromShutdownRun);
*runnableFromShutdownRun = true;
})));
});
MOZ_ALWAYS_SUCCEEDS(tq->RegisterShutdownTask(shutdownTask));
ASSERT_FALSE(*shutdownTaskRun);
ASSERT_FALSE(*runnableFromShutdownRun);
RefPtr<mozilla::SyncRunnable> syncWithThread =
new mozilla::SyncRunnable(NS_NewRunnableFunction("dummy", [] {}));
MOZ_ALWAYS_SUCCEEDS(syncWithThread->DispatchToThread(tq));
ASSERT_FALSE(*shutdownTaskRun);
ASSERT_FALSE(*runnableFromShutdownRun);
tq->BeginShutdown();
tq->AwaitShutdownAndIdle();
ASSERT_TRUE(*shutdownTaskRun);
ASSERT_TRUE(*runnableFromShutdownRun);
}
TEST(TaskQueue, UnregisteredShutdownTask)
{
RefPtr<TaskQueue> tq = TaskQueue::Create(
GetMediaThreadPool(MediaThreadType::SUPERVISOR), "Testing TaskQueue");
nsCOMPtr<nsITargetShutdownTask> shutdownTask =
new TestShutdownTask([=] { MOZ_CRASH("should not be run"); });
MOZ_ALWAYS_SUCCEEDS(tq->RegisterShutdownTask(shutdownTask));
RefPtr<mozilla::SyncRunnable> syncWithThread =
new mozilla::SyncRunnable(NS_NewRunnableFunction("dummy", [] {}));
MOZ_ALWAYS_SUCCEEDS(syncWithThread->DispatchToThread(tq));
MOZ_ALWAYS_SUCCEEDS(tq->UnregisterShutdownTask(shutdownTask));
tq->BeginShutdown();
tq->AwaitShutdownAndIdle();
}
TEST(AbstractThread, GetCurrentSerialEventTarget)
{
RefPtr<AbstractThread> mainThread = AbstractThread::GetCurrent();
EXPECT_EQ(mainThread, AbstractThread::MainThread());
Unused << mainThread->Dispatch(NS_NewRunnableFunction(
"TestAbstractThread::TestCurrentSerialEventTarget::TestBody",
[mainThread]() {
nsCOMPtr<nsISerialEventTarget> thread = GetCurrentSerialEventTarget();
EXPECT_EQ(thread, mainThread);
}));
// Spin the event loop.
NS_ProcessPendingEvents(nullptr);
}
} // namespace TestTaskQueue
|