unregister components while exiting

This commit is contained in:
csmoe 2018-12-15 21:55:58 +08:00
parent c9229f3f99
commit 5600a1dca1
5 changed files with 44 additions and 0 deletions

View file

@ -72,6 +72,8 @@ impl BackgroundHangMonitorClone for HangMonitorRegister {
pub enum MonitoredComponentMsg {
/// Register component for monitoring,
Register(Box<Sampler>, Duration, Duration),
/// Unregister component for monitoring.
Unregister,
/// Notify start of new activity for a given component,
NotifyActivity(HangAnnotation),
/// Notify start of waiting for a new task to come-in for processing.
@ -119,6 +121,10 @@ impl BackgroundHangMonitor for BackgroundHangMonitorChan {
let msg = MonitoredComponentMsg::NotifyWait;
self.send(msg);
}
fn unregister(&self) {
let msg = MonitoredComponentMsg::Unregister;
self.send(msg);
}
}
struct MonitoredComponent {
@ -200,6 +206,12 @@ impl BackgroundHangMonitorWorker {
"This component was already registered for monitoring."
);
},
(component_id, MonitoredComponentMsg::Unregister) => {
let _ = self
.monitored_components
.remove_entry(&component_id)
.expect("Received Unregister for an unknown component");
},
(component_id, MonitoredComponentMsg::NotifyActivity(annotation)) => {
let component = self
.monitored_components

View file

@ -105,3 +105,30 @@ fn test_hang_monitoring() {
// Still no new alerts because the hang monitor has shut-down already.
assert!(background_hang_monitor_receiver.try_recv().is_err());
}
#[test]
fn test_hang_monitoring_unregister() {
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);
// Unregister the component.
background_hang_monitor.unregister();
// Sleep until the "transient" timeout has been reached.
thread::sleep(Duration::from_millis(10));
// No new alert yet
assert!(background_hang_monitor_receiver.try_recv().is_err());
}