From 583dd7c8f7977a7fd5090afa41bf6ed407317941 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 31 Aug 2024 20:54:14 +0200 Subject: [PATCH] Fix bad self-loop check --- central_backend/src/devices/device.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/central_backend/src/devices/device.rs b/central_backend/src/devices/device.rs index e75eb19..cbae2ea 100644 --- a/central_backend/src/devices/device.rs +++ b/central_backend/src/devices/device.rs @@ -165,7 +165,8 @@ impl DeviceRelay { } } - let relays_map = list.iter().map(|r| (r.id, r)).collect::>(); + let mut relays_map = list.iter().map(|r| (r.id, r)).collect::>(); + 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()); } }