Fix bad self-loop check

This commit is contained in:
Pierre HUBERT 2024-08-31 20:54:14 +02:00
parent 78663854cc
commit 583dd7c8f7

View File

@ -165,7 +165,8 @@ impl DeviceRelay {
}
}
let relays_map = list.iter().map(|r| (r.id, r)).collect::<HashMap<_, _>>();
let mut relays_map = list.iter().map(|r| (r.id, r)).collect::<HashMap<_, _>>();
relays_map.insert(self.id, self);
if self.depends_on.iter().any(|d| !relays_map.contains_key(d)) {
return Some("A specified dependent relay does not exists!");
@ -362,5 +363,21 @@ mod tests {
assert!(target_relay
.error(&[other_dep.clone(), second_dep.clone(), target_relay.clone()])
.is_none());
// self loop
let mut self_loop = DeviceRelay {
id: DeviceRelayID::default(),
name: "self_loop".to_string(),
..Default::default()
};
let self_loop_good = self_loop.clone();
self_loop.depends_on = vec![self_loop.id];
assert!(self_loop.error(&[]).is_some());
assert!(self_loop.error(&[self_loop.clone()]).is_some());
assert!(self_loop.error(&[self_loop_good.clone()]).is_some());
self_loop.depends_on = vec![];
assert!(self_loop_good.error(&[]).is_none());
assert!(self_loop_good.error(&[self_loop_good.clone()]).is_none());
assert!(self_loop_good.error(&[self_loop.clone()]).is_none());
}
}