diff --git a/components/background_hang_monitor/tests/hang_monitor_tests.rs b/components/background_hang_monitor/tests/hang_monitor_tests.rs index d7e6dcf268d..2ecb526e152 100644 --- a/components/background_hang_monitor/tests/hang_monitor_tests.rs +++ b/components/background_hang_monitor/tests/hang_monitor_tests.rs @@ -6,18 +6,63 @@ 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::{HangAlert, HangAnnotation, HangMonitorAlert}; use msg::constellation_msg::{MonitoredComponentId, MonitoredComponentType}; use std::thread; use std::time::Duration; +#[test] +#[cfg(target_os = "macos")] +fn test_sampler() { + use msg::constellation_msg::SamplerControlMsg; + use serde_json::Value; + + let (background_hang_monitor_ipc_sender, background_hang_monitor_receiver) = + ipc::channel().expect("ipc channel failure"); + let (sampler_sender, sampler_receiver) = ipc::channel().expect("ipc channel failure"); + + let background_hang_monitor_register = + HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone(), sampler_receiver); + let _background_hang_monitor = background_hang_monitor_register.register_component( + MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script), + Duration::from_millis(10), + Duration::from_millis(1000), + ); + + const RATE: u64 = 10; + sampler_sender + .send(SamplerControlMsg::Enable(Duration::from_millis(RATE))) + .unwrap(); + + thread::sleep(Duration::from_millis(30)); + + sampler_sender.send(SamplerControlMsg::Disable).unwrap(); + + loop { + match background_hang_monitor_receiver.recv().unwrap() { + HangMonitorAlert::Hang(_) => continue, + HangMonitorAlert::Profile(ref bytes) => { + let json: Value = serde_json::from_slice(bytes).unwrap(); + let rate = json["rate"].as_u64().unwrap(); + assert_eq!(rate, RATE); + let data = json["data"].as_array().unwrap(); + assert!(data.len() > 1); + assert_eq!(data[0]["name"].as_str().unwrap(), "test_sampler"); + assert!(data[0]["frames"].as_array().unwrap().len() > 0); + break; + }, + } + } +} + #[test] fn test_hang_monitoring() { let (background_hang_monitor_ipc_sender, background_hang_monitor_receiver) = ipc::channel().expect("ipc channel failure"); + let (_sampler_sender, sampler_receiver) = ipc::channel().expect("ipc channel failure"); let background_hang_monitor_register = - HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone()); + HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone(), sampler_receiver); let background_hang_monitor = background_hang_monitor_register.register_component( MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script), Duration::from_millis(10), @@ -33,11 +78,11 @@ fn test_hang_monitoring() { // Check for a transient hang alert. match background_hang_monitor_receiver.recv().unwrap() { - HangAlert::Transient(component_id, _annotation) => { + HangMonitorAlert::Hang(HangAlert::Transient(component_id, _annotation)) => { let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script); assert_eq!(expected, component_id); }, - HangAlert::Permanent(..) => unreachable!(), + _ => unreachable!(), } // Sleep until the "permanent" timeout has been reached. @@ -45,11 +90,11 @@ fn test_hang_monitoring() { // Check for a permanent hang alert. match background_hang_monitor_receiver.recv().unwrap() { - HangAlert::Permanent(component_id, _annotation, _profile) => { + HangMonitorAlert::Hang(HangAlert::Permanent(component_id, _annotation, _profile)) => { let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script); assert_eq!(expected, component_id); }, - HangAlert::Transient(..) => unreachable!(), + _ => unreachable!(), } // Now the component is not hanging anymore. @@ -61,11 +106,11 @@ fn test_hang_monitoring() { // Check for a transient hang alert. match background_hang_monitor_receiver.recv().unwrap() { - HangAlert::Transient(component_id, _annotation) => { + HangMonitorAlert::Hang(HangAlert::Transient(component_id, _annotation)) => { let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script); assert_eq!(expected, component_id); }, - HangAlert::Permanent(..) => unreachable!(), + _ => unreachable!(), } // Now the component is waiting for a new task. @@ -85,11 +130,11 @@ fn test_hang_monitoring() { // We're getting new hang alerts for the latest task. match background_hang_monitor_receiver.recv().unwrap() { - HangAlert::Transient(component_id, _annotation) => { + HangMonitorAlert::Hang(HangAlert::Transient(component_id, _annotation)) => { let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script); assert_eq!(expected, component_id); }, - HangAlert::Permanent(..) => unreachable!(), + _ => unreachable!(), } // No new alert yet @@ -110,9 +155,10 @@ fn test_hang_monitoring() { fn test_hang_monitoring_unregister() { let (background_hang_monitor_ipc_sender, background_hang_monitor_receiver) = ipc::channel().expect("ipc channel failure"); + let (_sampler_sender, sampler_receiver) = ipc::channel().expect("ipc channel failure"); let background_hang_monitor_register = - HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone()); + HangMonitorRegister::init(background_hang_monitor_ipc_sender.clone(), sampler_receiver); let background_hang_monitor = background_hang_monitor_register.register_component( MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script), Duration::from_millis(10),