/
hang_monitor_tests.rs
107 lines (86 loc) · 4.1 KB
/
hang_monitor_tests.rs
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
/* 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 https://mozilla.org/MPL/2.0/. */
use background_hang_monitor::HangMonitorRegister;
use ipc_channel::ipc;
use msg::constellation_msg::ScriptHangAnnotation;
use msg::constellation_msg::TEST_PIPELINE_ID;
use msg::constellation_msg::{HangAlert, HangAnnotation};
use msg::constellation_msg::{MonitoredComponentId, MonitoredComponentType};
use std::thread;
use std::time::Duration;
#[test]
fn test_hang_monitoring() {
let (background_hang_monitor_ipc_sender, background_hang_monitor_receiver) =
ipc::channel().expect("ipc channel failure");
let background_hang_monitor_register =
HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone());
let background_hang_monitor = background_hang_monitor_register.register_component(
MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script),
Duration::from_millis(10),
Duration::from_millis(1000),
);
// Start an activity.
let hang_annotation = HangAnnotation::Script(ScriptHangAnnotation::AttachLayout);
background_hang_monitor.notify_activity(hang_annotation);
// Sleep until the "transient" timeout has been reached.
thread::sleep(Duration::from_millis(10));
// Check for a transient hang alert.
match background_hang_monitor_receiver.recv().unwrap() {
HangAlert::Transient(component_id, _annotation) => {
let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script);
assert_eq!(expected, component_id);
},
HangAlert::Permanent(..) => unreachable!(),
}
// Sleep until the "permanent" timeout has been reached.
thread::sleep(Duration::from_millis(1000));
// Check for a permanent hang alert.
match background_hang_monitor_receiver.recv().unwrap() {
HangAlert::Permanent(component_id, _annotation, _profile) => {
let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script);
assert_eq!(expected, component_id);
},
HangAlert::Transient(..) => unreachable!(),
}
// Now the component is not hanging anymore.
background_hang_monitor.notify_activity(hang_annotation);
assert!(background_hang_monitor_receiver.try_recv().is_err());
// Sleep for a while.
thread::sleep(Duration::from_millis(10));
// Check for a transient hang alert.
match background_hang_monitor_receiver.recv().unwrap() {
HangAlert::Transient(component_id, _annotation) => {
let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script);
assert_eq!(expected, component_id);
},
HangAlert::Permanent(..) => unreachable!(),
}
// Now the component is waiting for a new task.
background_hang_monitor.notify_wait();
// Sleep for a while.
thread::sleep(Duration::from_millis(100));
// The component is still waiting, but not hanging.
assert!(background_hang_monitor_receiver.try_recv().is_err());
// New task handling starts.
background_hang_monitor.notify_activity(hang_annotation);
// Sleep for a while.
thread::sleep(Duration::from_millis(10));
// We're getting new hang alerts for the latest task.
match background_hang_monitor_receiver.recv().unwrap() {
HangAlert::Transient(component_id, _annotation) => {
let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script);
assert_eq!(expected, component_id);
},
HangAlert::Permanent(..) => unreachable!(),
}
// No new alert yet
assert!(background_hang_monitor_receiver.try_recv().is_err());
// Shut-down the hang monitor
drop(background_hang_monitor_register);
drop(background_hang_monitor);
// Sleep until the "max-timeout" has been reached.
thread::sleep(Duration::from_millis(1000));
// Still no new alerts because the hang monitor has shut-down already.
assert!(background_hang_monitor_receiver.try_recv().is_err());
}