File: test_timeout_application_not_responding_detector.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (469 lines) | stat: -rw-r--r-- 14,928 bytes parent folder | download | duplicates (2)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "src/server/scene/timeout_application_not_responding_detector.h"

#include "mir/test/doubles/mock_scene_session.h"
#include "mir/test/doubles/fake_alarm_factory.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace mt = mir::time;
namespace ms = mir::scene;
namespace mtd = mir::test::doubles;

namespace
{
class MockObserver : public ms::ApplicationNotRespondingDetector::Observer
{
public:
    MOCK_METHOD1(session_unresponsive, void(ms::Session const*));
    MOCK_METHOD1(session_now_responsive, void(ms::Session const*));
};
}

TEST(TimeoutApplicationNotRespondingDetector, pings_registered_sessions_on_schedule)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;
    
    mtd::FakeAlarmFactory fake_alarms;
    
    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};
    
    bool first_session_pinged{false}, second_session_pinged{false};
    
    NiceMock<mtd::MockSceneSession> session_one, session_two;
    
    detector.register_session(&session_one, [&first_session_pinged]() { first_session_pinged = true; });
    detector.register_session(&session_two, [&second_session_pinged]() { second_session_pinged = true; });
    
    fake_alarms.advance_by(999ms);

    EXPECT_FALSE(first_session_pinged);
    EXPECT_FALSE(second_session_pinged);

    fake_alarms.advance_by(2ms);

    EXPECT_TRUE(first_session_pinged);
    EXPECT_TRUE(second_session_pinged);
}

TEST(TimeoutApplicationNotRespondingDetector, pings_repeatedly)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    int first_session_pinged{0}, second_session_pinged{0};

    NiceMock<mtd::MockSceneSession> session_one, session_two;

    detector.register_session(&session_one, [&first_session_pinged]() { first_session_pinged++; });
    detector.register_session(&session_two, [&second_session_pinged]() { second_session_pinged++; });

    int const expected_count{900};

    for (int i = 0; i < expected_count ; ++i)
    {
        fake_alarms.advance_by(1001ms);
        detector.pong_received(&session_one);
        detector.pong_received(&session_two);
    }

    EXPECT_THAT(first_session_pinged, Eq(expected_count));
    EXPECT_THAT(second_session_pinged, Eq(expected_count));
}

TEST(TimeoutApplicationNotRespondingDetector, triggers_anr_signal_when_session_fails_to_pong)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    bool session_not_responding{false};
    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&session_not_responding](auto /*session*/)
    {
        session_not_responding = true;
    }));
    detector.register_observer(observer);

    NiceMock<mtd::MockSceneSession> session_one;

    detector.register_session(&session_one, [](){});

    fake_alarms.advance_by(1001ms);
    // Should now have pung, but not marked as unresponsive
    EXPECT_FALSE(session_not_responding);

    // Now a full ping cycle has elapsed, should have marked as unresponsive
    fake_alarms.advance_by(1001ms);
    EXPECT_TRUE(session_not_responding);
}

TEST(TimeoutApplicationNotRespondingDetector, does_not_trigger_anr_when_session_pongs)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    bool session_not_responding{false};
    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&session_not_responding](auto /*session*/)
    {
        session_not_responding = true;
    }));
    detector.register_observer(observer);

    NiceMock<mtd::MockSceneSession> session_one;

    detector.register_session(&session_one, [](){});

    fake_alarms.advance_by(1001ms);
    // Should now have pung, but not marked as unresponsive
    EXPECT_FALSE(session_not_responding);

    detector.pong_received(&session_one);

    // Now a full ping cycle has elapsed, but the session has pung and so isn't unresponsive
    fake_alarms.advance_by(1001ms);
    EXPECT_FALSE(session_not_responding);
}

TEST(TimeoutApplicationNotRespondingDetector, triggers_now_responsive_signal_when_ponged)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    bool session_not_responding{false};
    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&session_not_responding](auto /*session*/)
    {
        session_not_responding = true;
    }));
    ON_CALL(*observer, session_now_responsive(_))
        .WillByDefault(Invoke([&session_not_responding](auto /*session*/)
    {
        session_not_responding = false;
    }));
    detector.register_observer(observer);

    NiceMock<mtd::MockSceneSession> session_one;

    detector.register_session(&session_one, [](){});

    fake_alarms.advance_by(1001ms);
    // Should now have pung, but not marked as unresponsive
    EXPECT_FALSE(session_not_responding);

    // Now a full ping cycle has elapsed, should have marked as unresponsive
    fake_alarms.advance_by(1001ms);
    EXPECT_TRUE(session_not_responding);

    detector.pong_received(&session_one);

    EXPECT_FALSE(session_not_responding);
}

TEST(TimeoutApplicationNotRespondingDetector, fiddling_with_sessions_from_callbacks_doesnt_deadlock)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    NiceMock<mtd::MockSceneSession> session_one, session_two, session_three;

    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&detector, &session_two](auto /*session*/)
    {
        detector.register_session(&session_two, [](){});
    }));
    ON_CALL(*observer, session_now_responsive(_))
        .WillByDefault(Invoke([&detector, &session_three](auto /*session*/)
    {
        detector.unregister_session(&session_three);
    }));
    detector.register_observer(observer);

    detector.register_session(&session_one, [](){});
    detector.register_session(&session_three, [](){});

    fake_alarms.advance_by(1001ms);
    // Should now have pung

    fake_alarms.advance_by(1001ms);
    // Should now have marked session three unresponsive, registering session two

    detector.pong_received(&session_three);
    // And now session three sholud be morked as responsive, unregistering itself
}

TEST(TimeoutApplicationNotRespondingDetector, does_not_generate_signals_after_unregister)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    bool session_not_responding{false};
    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&session_not_responding](auto /*session*/)
    {
        session_not_responding = true;
    }));
    detector.register_observer(observer);

    NiceMock<mtd::MockSceneSession> session_one;

    detector.register_session(&session_one, [](){});

    fake_alarms.advance_by(1001ms);
    // Should now have pung, but not marked as unresponsive
    EXPECT_FALSE(session_not_responding);

    // Now a full ping cycle has elapsed, should have marked as unresponsive
    fake_alarms.advance_by(1001ms);
    EXPECT_TRUE(session_not_responding);

    // Mark session as responding
    detector.pong_received(&session_one);
    session_not_responding = false;

    detector.unregister_observer(observer);

    fake_alarms.advance_by(1001ms);
    fake_alarms.advance_by(1001ms);

    // Notification should not have been generated
    EXPECT_FALSE(session_not_responding);
}

TEST(TimeoutApplicationNotRespondingDetector, does_not_schedule_alarm_when_no_sessions)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    // Go through several ping cycles.
    fake_alarms.advance_smoothly_by(5000ms);

    EXPECT_THAT(fake_alarms.wakeup_count(), Eq(0));

    NiceMock<mtd::MockSceneSession> session;

    detector.register_session(&session, [](){});

    fake_alarms.advance_smoothly_by(5000ms);
    EXPECT_THAT(fake_alarms.wakeup_count(), Gt(0));

    int const previous_wakeup_count{fake_alarms.wakeup_count()};

    detector.unregister_session(&session);

    fake_alarms.advance_smoothly_by(5000ms);

    // We allow one extra wakeup to notice there are no active sessions.
    EXPECT_THAT(fake_alarms.wakeup_count(), Le(previous_wakeup_count + 1));
}

TEST(TimeoutApplicationNotRespondingDetector, does_not_schedule_alarm_when_all_sessions_are_unresponsive)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    NiceMock<mtd::MockSceneSession> session;
    bool session_unresponsive{false};

    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&session_unresponsive](auto /*session*/)
    {
        session_unresponsive = true;
    }));
    detector.register_observer(observer);

    detector.register_session(&session, [](){});

    fake_alarms.advance_smoothly_by(5000ms);

    ASSERT_TRUE(session_unresponsive);
    EXPECT_THAT(fake_alarms.wakeup_count(), Gt(0));

    int const previous_wakeup_count{fake_alarms.wakeup_count()};

    // All the sessions are now unresponsive, so we shouldn't be triggering wakeups
    fake_alarms.advance_smoothly_by(5000ms);
    EXPECT_THAT(fake_alarms.wakeup_count(), Eq(previous_wakeup_count));
}

TEST(TimeoutApplicationNotRespondingDetector, session_switches_between_responsive_and_unresponsive)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    NiceMock<mtd::MockSceneSession> session;
    bool session_unresponsive{false};

    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&session_unresponsive](auto /*session*/)
    {
        session_unresponsive = true;
    }));
    ON_CALL(*observer, session_now_responsive(_))
        .WillByDefault(Invoke([&session_unresponsive](auto /*session*/)
    {
        session_unresponsive = false;
    }));
    detector.register_observer(observer);

    detector.register_session(&session, [](){});

    fake_alarms.advance_smoothly_by(5000ms);

    EXPECT_TRUE(session_unresponsive);

    detector.pong_received(&session);
    EXPECT_FALSE(session_unresponsive);

    fake_alarms.advance_smoothly_by(5000ms);
    EXPECT_TRUE(session_unresponsive);

    detector.pong_received(&session);
    EXPECT_FALSE(session_unresponsive);
}

TEST(TimeoutApplicationNotRespondingDetector, sends_unresponsive_notification_only_once)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, 1s};

    NiceMock<mtd::MockSceneSession> session_one;
    NiceMock<mtd::MockSceneSession> session_two;
    auto delayed_dispatch_pong = fake_alarms.create_alarm(
        [&detector, &session_two]()
        {
            detector.pong_received(&session_two);
        });
    int unresponsive_notifications{0};

    auto observer = std::make_shared<NiceMock<MockObserver>>();
    ON_CALL(*observer, session_unresponsive(_))
        .WillByDefault(Invoke([&unresponsive_notifications](auto /*session*/)
    {
        ++unresponsive_notifications;
    }));
    detector.register_observer(observer);

    detector.register_session(&session_one, [](){});
    // We need a responsive session to ensure the ANRDetector keeps rescheduling wakeups.
    //
    // We need the delayed_dispatch_pong so that we can do the pong outside of the
    // ping callback; otherwise this would deadlock.
    detector.register_session(&session_two,
        [&delayed_dispatch_pong]()
        {
            delayed_dispatch_pong->reschedule_in(1ms);
        });

    fake_alarms.advance_smoothly_by(5000ms);

    EXPECT_THAT(unresponsive_notifications, Eq(1));
}

TEST(TimeoutApplicationNotRespondingDetector, sends_pings_on_schedule_as_sessions_are_connecting)
{
    using namespace testing;
    using namespace std::literals::chrono_literals;

    mtd::FakeAlarmFactory fake_alarms;

    auto const cycle_time = 1s;
    ms::TimeoutApplicationNotRespondingDetector detector{fake_alarms, cycle_time};

    NiceMock<mtd::MockSceneSession> session;
    std::atomic<int> ping_count{0};
    auto delayed_dispatch_pong = fake_alarms.create_alarm(
        [&detector, &session, &ping_count]()
        {
            detector.pong_received(&session);
            ++ping_count;
        });

    detector.register_session(&session,
        [&delayed_dispatch_pong]()
        {
            delayed_dispatch_pong->reschedule_in(1ms);
        });


    auto duration = 0ms;
    auto const step = 500ms;

    // <= ensures we go past the threshold for the cycle
    while (duration <= 5s)
    {
        NiceMock<mtd::MockSceneSession> dummy_session;
        detector.register_session(&dummy_session, [](){});

        fake_alarms.advance_smoothly_by(step);
        duration += step;

        detector.unregister_session(&dummy_session);
    }

    EXPECT_THAT(ping_count, Ge(duration / cycle_time));
}