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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
media / audio / audio_thread_hang_monitor_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/audio_thread_hang_monitor.h"
#include <optional>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::ElementsAre;
using testing::Test;
using HangAction = media::AudioThreadHangMonitor::HangAction;
namespace media {
namespace {
constexpr int kStarted =
static_cast<int>(AudioThreadHangMonitor::ThreadStatus::kStarted);
constexpr int kHung =
static_cast<int>(AudioThreadHangMonitor::ThreadStatus::kHung);
constexpr int kRecovered =
static_cast<int>(AudioThreadHangMonitor::ThreadStatus::kRecovered);
constexpr base::TimeDelta kShortHangDeadline = base::Seconds(5);
constexpr base::TimeDelta kLongHangDeadline = base::Minutes(30);
} // namespace
class AudioThreadHangMonitorTest : public Test {
public:
AudioThreadHangMonitorTest()
: task_env_(base::test::TaskEnvironment::TimeSource::MOCK_TIME),
histograms_(),
audio_thread_("Audio thread"),
hang_monitor_({nullptr, base::OnTaskRunnerDeleter(nullptr)}) {
CHECK(audio_thread_.Start());
// We must inject the main thread task runner as the hang monitor task
// runner since TaskEnvironment::FastForwardBy only works for the main
// thread.
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDoNothing, std::nullopt, task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
}
~AudioThreadHangMonitorTest() override {
hang_monitor_.reset();
task_env_.RunUntilIdle();
}
void SetHangActionCallbacksForTesting() {
hang_monitor_->SetHangActionCallbacksForTesting(
base::BindRepeating(&AudioThreadHangMonitorTest::HangActionDump,
base::Unretained(this)),
base::BindRepeating(&AudioThreadHangMonitorTest::HangActionTerminate,
base::Unretained(this)));
}
void RunUntilIdle() { task_env_.RunUntilIdle(); }
void FlushAudioThread() {
base::WaitableEvent ev;
audio_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&base::WaitableEvent::Signal, base::Unretained(&ev)));
ev.Wait();
}
void BlockAudioThreadUntilEvent() {
// We keep |event_| as a member of the test fixture to make sure that the
// audio thread terminates before |event_| is destructed.
event_.Reset();
audio_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce(&base::WaitableEvent::Wait, base::Unretained(&event_)));
}
MOCK_METHOD0(HangActionDump, void());
MOCK_METHOD0(HangActionTerminate, void());
base::WaitableEvent event_;
base::test::TaskEnvironment task_env_;
base::HistogramTester histograms_;
base::Thread audio_thread_;
AudioThreadHangMonitor::Ptr hang_monitor_;
};
TEST_F(AudioThreadHangMonitorTest, LogsThreadStarted) {
RunUntilIdle();
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 1)));
}
TEST_F(AudioThreadHangMonitorTest, DoesNotLogThreadHungWhenOk) {
RunUntilIdle();
for (int i = 0; i < 10; ++i) {
// Flush the audio thread, then advance the clock. The audio thread should
// register as "alive" every time.
FlushAudioThread();
task_env_.FastForwardBy(base::Minutes(1));
}
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 1)));
}
TEST_F(AudioThreadHangMonitorTest, LogsHungWhenAudioThreadIsBlocked) {
RunUntilIdle();
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(base::Minutes(10));
event_.Signal();
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 1), base::Bucket(kHung, 1)));
}
TEST_F(AudioThreadHangMonitorTest, DoesNotLogThreadHungWithShortDeadline) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDoNothing, kShortHangDeadline, task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
RunUntilIdle();
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(kShortHangDeadline / 2);
event_.Signal();
// Two started events, one for the originally created hang monitor and one for
// the new created here.
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2)));
}
TEST_F(AudioThreadHangMonitorTest, LogsThreadHungWithShortDeadline) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDoNothing, kShortHangDeadline, task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
RunUntilIdle();
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(kShortHangDeadline * 2);
event_.Signal();
// Two started events, one for the originally created hang monitor and one for
// the new created here.
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2), base::Bucket(kHung, 1)));
}
TEST_F(AudioThreadHangMonitorTest, DoesNotLogThreadHungWithLongDeadline) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDoNothing, kLongHangDeadline, task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
RunUntilIdle();
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(kLongHangDeadline / 2);
event_.Signal();
// Two started events, one for the originally created hang monitor and one for
// the new created here.
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2)));
}
TEST_F(AudioThreadHangMonitorTest, LogsThreadHungWithLongDeadline) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDoNothing, kLongHangDeadline, task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
RunUntilIdle();
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(kLongHangDeadline * 2);
event_.Signal();
// Two started events, one for the originally created hang monitor and one for
// the new created here.
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2), base::Bucket(kHung, 1)));
}
// Zero deadline means that the default deadline should be used.
TEST_F(AudioThreadHangMonitorTest, ZeroDeadlineMeansDefaultDeadline) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDoNothing, base::TimeDelta(), task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
RunUntilIdle();
for (int i = 0; i < 10; ++i) {
// Flush the audio thread, then advance the clock. The audio thread should
// register as "alive" every time.
FlushAudioThread();
task_env_.FastForwardBy(base::Minutes(1));
}
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2)));
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(base::Minutes(10));
event_.Signal();
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2), base::Bucket(kHung, 1)));
}
TEST_F(AudioThreadHangMonitorTest,
LogsRecoveredWhenAudioThreadIsBlockedThenRecovers) {
RunUntilIdle();
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(base::Minutes(10));
event_.Signal();
for (int i = 0; i < 10; ++i) {
// Flush the audio thread, then advance the clock. The audio thread should
// register as "alive" every time.
FlushAudioThread();
task_env_.FastForwardBy(base::Minutes(1));
}
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 1), base::Bucket(kHung, 1),
base::Bucket(kRecovered, 1)));
}
TEST_F(AudioThreadHangMonitorTest, NoHangActionWhenOk) {
SetHangActionCallbacksForTesting();
RunUntilIdle();
for (int i = 0; i < 10; ++i) {
// Flush the audio thread, then advance the clock. The audio thread should
// register as "alive" every time.
FlushAudioThread();
task_env_.FastForwardBy(base::Minutes(1));
}
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 1)));
}
TEST_F(AudioThreadHangMonitorTest, DumpsWhenAudioThreadIsBlocked) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDump, std::nullopt, task_env_.GetMockTickClock(),
audio_thread_.task_runner(), task_env_.GetMainThreadTaskRunner());
SetHangActionCallbacksForTesting();
RunUntilIdle();
EXPECT_CALL(*this, HangActionDump).Times(1);
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(base::Minutes(10));
event_.Signal();
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2), base::Bucket(kHung, 1)));
}
TEST_F(AudioThreadHangMonitorTest, TerminatesProcessWhenAudioThreadIsBlocked) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kTerminateCurrentProcess, std::nullopt,
task_env_.GetMockTickClock(), audio_thread_.task_runner(),
task_env_.GetMainThreadTaskRunner());
SetHangActionCallbacksForTesting();
RunUntilIdle();
EXPECT_CALL(*this, HangActionTerminate).Times(1);
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(base::Minutes(10));
event_.Signal();
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2), base::Bucket(kHung, 1)));
}
TEST_F(AudioThreadHangMonitorTest,
DumpsAndTerminatesProcessWhenAudioThreadIsBlocked) {
hang_monitor_ = AudioThreadHangMonitor::Create(
HangAction::kDumpAndTerminateCurrentProcess, std::nullopt,
task_env_.GetMockTickClock(), audio_thread_.task_runner(),
task_env_.GetMainThreadTaskRunner());
SetHangActionCallbacksForTesting();
RunUntilIdle();
EXPECT_CALL(*this, HangActionDump).Times(1);
EXPECT_CALL(*this, HangActionTerminate).Times(1);
BlockAudioThreadUntilEvent();
task_env_.FastForwardBy(base::Minutes(10));
event_.Signal();
EXPECT_THAT(histograms_.GetAllSamples("Media.AudioThreadStatus"),
ElementsAre(base::Bucket(kStarted, 2), base::Bucket(kHung, 1)));
}
} // namespace media