| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use session::App; |
| 4 | |
| 5 | const MAX_COMMAND_WAITS: usize = 5; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 8 | pub(crate) enum ParkDecision { |
| 9 | Park, |
| 10 | Wait, |
| 11 | } |
| 12 | |
| 13 | /// A navigation of the app tab destroys the page context of every running protocol call |
| 14 | /// and the Trouter sockets of a live call, so parking waits until the tab is idle. |
| 15 | #[derive(Debug, Default)] |
| 16 | pub(crate) struct ParkGate { |
| 17 | holds: HashMap<App, usize>, |
| 18 | in_flight: HashMap<App, usize>, |
| 19 | command_waits: HashMap<App, usize>, |
| 20 | } |
| 21 | |
| 22 | impl ParkGate { |
| 23 | pub(crate) fn hold(&mut self, app: App, held: bool) { |
| 24 | adjust(&mut self.holds, app, held); |
| 25 | } |
| 26 | |
| 27 | pub(crate) fn command_started(&mut self, app: App) { |
| 28 | adjust(&mut self.in_flight, app, true); |
| 29 | } |
| 30 | |
| 31 | pub(crate) fn command_finished(&mut self, app: App) { |
| 32 | adjust(&mut self.in_flight, app, false); |
| 33 | } |
| 34 | |
| 35 | pub(crate) fn forget_commands(&mut self) { |
| 36 | self.in_flight.clear(); |
| 37 | } |
| 38 | |
| 39 | pub(crate) fn in_flight(&self, app: App) -> usize { |
| 40 | self.in_flight.get(&app).copied().unwrap_or(0) |
| 41 | } |
| 42 | |
| 43 | /// A protocol call whose page promise never settles stays in flight; after a few waits it no longer blocks the park. |
| 44 | pub(crate) fn decide(&mut self, app: App) -> ParkDecision { |
| 45 | if self.holds.get(&app).copied().unwrap_or(0) > 0 { |
| 46 | return ParkDecision::Wait; |
| 47 | } |
| 48 | let waits = self.command_waits.entry(app).or_default(); |
| 49 | if self.in_flight.get(&app).copied().unwrap_or(0) > 0 && *waits < MAX_COMMAND_WAITS { |
| 50 | *waits += 1; |
| 51 | return ParkDecision::Wait; |
| 52 | } |
| 53 | *waits = 0; |
| 54 | ParkDecision::Park |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn adjust(counts: &mut HashMap<App, usize>, app: App, up: bool) { |
| 59 | let count = counts.entry(app).or_default(); |
| 60 | *count = if up { *count + 1 } else { count.saturating_sub(1) }; |
| 61 | } |
| 62 | |
| 63 | #[cfg(test)] |
| 64 | mod tests { |
| 65 | use super::*; |
| 66 | |
| 67 | #[test] |
| 68 | fn an_idle_tab_parks() { |
| 69 | assert_eq!(ParkGate::default().decide(App::Teams), ParkDecision::Park); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn a_running_command_delays_the_park_until_it_answers() { |
| 74 | let mut gate = ParkGate::default(); |
| 75 | gate.command_started(App::Teams); |
| 76 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 77 | assert_eq!(gate.decide(App::Outlook), ParkDecision::Park); |
| 78 | gate.command_finished(App::Teams); |
| 79 | assert_eq!(gate.decide(App::Teams), ParkDecision::Park); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn a_command_that_never_answers_stops_blocking_after_a_few_waits() { |
| 84 | let mut gate = ParkGate::default(); |
| 85 | gate.command_started(App::Teams); |
| 86 | for _ in 0..MAX_COMMAND_WAITS { |
| 87 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 88 | } |
| 89 | assert_eq!(gate.decide(App::Teams), ParkDecision::Park); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn a_held_tab_waits_for_every_hold_to_end() { |
| 94 | let mut gate = ParkGate::default(); |
| 95 | gate.hold(App::Teams, true); |
| 96 | gate.hold(App::Teams, true); |
| 97 | gate.hold(App::Teams, false); |
| 98 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 99 | gate.hold(App::Teams, false); |
| 100 | assert_eq!(gate.decide(App::Teams), ParkDecision::Park); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn extra_releases_do_not_underflow() { |
| 105 | let mut gate = ParkGate::default(); |
| 106 | gate.hold(App::Teams, false); |
| 107 | gate.command_finished(App::Teams); |
| 108 | gate.hold(App::Teams, true); |
| 109 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn a_restart_forgets_unanswered_commands_but_keeps_holds() { |
| 114 | let mut gate = ParkGate::default(); |
| 115 | gate.command_started(App::Outlook); |
| 116 | gate.hold(App::Teams, true); |
| 117 | gate.forget_commands(); |
| 118 | assert_eq!(gate.decide(App::Outlook), ParkDecision::Park); |
| 119 | assert_eq!(gate.decide(App::Teams), ParkDecision::Wait); |
| 120 | } |
| 121 | } |