BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / callee_gate.rs

callee_gate.rs

callee_gate.rs 3.4 KiB Download Raw
1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2pub struct CalleeGate {
3 callees: usize,
4 failed: usize,
5 joined: bool,
6 media_connected: bool,
7}
8
9impl CalleeGate {
10 pub fn new(callees: usize) -> Self {
11 CalleeGate {
12 callees,
13 failed: 0,
14 joined: false,
15 media_connected: false,
16 }
17 }
18
19 pub fn is_waiting(&self) -> bool {
20 self.callees > 0 && !self.joined
21 }
22
23 pub fn callees_failed(&mut self, count: usize) -> bool {
24 if !self.is_waiting() {
25 return false;
26 }
27 self.failed += count;
28 self.failed >= self.callees
29 }
30
31 pub fn media_connected(&mut self) -> bool {
32 self.media_connected = true;
33 !self.is_waiting()
34 }
35
36 pub fn callee_joined(&mut self) -> bool {
37 let was_waiting = self.is_waiting();
38 self.joined = true;
39 was_waiting && self.media_connected
40 }
41}
42
43pub fn roster_has_callee<'a>(
44 participant_mris: impl IntoIterator<Item = &'a str>,
45 own_mri: &str,
46) -> bool {
47 participant_mris.into_iter().any(|mri| mri != own_mri)
48}
49
50/// The Echo bot hangs up after playing the recording back; Teams then ends the test call.
51#[derive(Debug, Default)]
52pub struct EchoWatch {
53 seen: bool,
54}
55
56impl EchoWatch {
57 pub fn bot_left<'a>(&mut self, participant_mris: impl IntoIterator<Item = &'a str>) -> bool {
58 if participant_mris.into_iter().any(|mri| mri == crate::signaling::ECHO_BOT_MRI) {
59 self.seen = true;
60 return false;
61 }
62 self.seen
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn a_roster_with_only_ourselves_has_no_callee() {
72 assert!(!roster_has_callee(["8:orgid:me"], "8:orgid:me"));
73 assert!(!roster_has_callee([], "8:orgid:me"));
74 }
75
76 #[test]
77 fn a_roster_with_someone_else_has_the_callee() {
78 assert!(roster_has_callee(
79 ["8:orgid:me", "8:orgid:them"],
80 "8:orgid:me"
81 ));
82 }
83
84 #[test]
85 fn media_waits_for_the_callee_when_required() {
86 let mut gate = CalleeGate::new(1);
87 assert!(!gate.media_connected());
88 assert!(gate.is_waiting());
89 assert!(gate.callee_joined());
90 assert!(!gate.is_waiting());
91 assert!(gate.media_connected());
92 }
93
94 #[test]
95 fn a_callee_before_media_does_not_fire_early() {
96 let mut gate = CalleeGate::new(1);
97 assert!(!gate.callee_joined());
98 assert!(gate.media_connected());
99 }
100
101 #[test]
102 fn the_call_fails_only_when_every_callee_failed() {
103 let mut gate = CalleeGate::new(3);
104 assert!(!gate.callees_failed(1));
105 assert!(gate.callees_failed(2));
106 let mut joined = CalleeGate::new(1);
107 joined.callee_joined();
108 assert!(!joined.callees_failed(1));
109 assert!(!CalleeGate::new(0).callees_failed(1));
110 }
111
112 #[test]
113 fn an_unrequired_gate_never_waits() {
114 let mut gate = CalleeGate::new(0);
115 assert!(!gate.is_waiting());
116 assert!(gate.media_connected());
117 assert!(!gate.callee_joined());
118 }
119
120 #[test]
121 fn the_echo_call_ends_only_after_the_bot_was_there_and_left() {
122 let me = "8:orgid:me";
123 let bot = crate::signaling::ECHO_BOT_MRI;
124 let mut watch = EchoWatch::default();
125 assert!(!watch.bot_left([me]));
126 assert!(!watch.bot_left([me, bot]));
127 assert!(!watch.bot_left([me, bot]));
128 assert!(watch.bot_left([me]));
129 }
130}