Skip to main content

coven_foundation/files/
file_budget_tally.rs

1//! What a test reads of a file budget's use. A build without test support
2//! keeps nothing.
3
4#[cfg(any(test, feature = "test-utils"))]
5use super::BudgetShared;
6#[cfg(any(test, feature = "test-utils"))]
7use std::sync::Arc;
8
9#[cfg(any(test, feature = "test-utils"))]
10#[derive(Default)]
11struct Counts {
12    leased: usize,
13    most_leased: usize,
14    holding: usize,
15    most_holding: usize,
16    waits: usize,
17}
18
19/// The use of one budget, counted when a test can read it. Each change
20/// wakes the waits a test started on it.
21pub(super) struct Tally {
22    #[cfg(any(test, feature = "test-utils"))]
23    counts: tokio::sync::watch::Sender<Counts>,
24}
25
26impl Tally {
27    pub(super) fn new() -> Self {
28        Self {
29            #[cfg(any(test, feature = "test-utils"))]
30            counts: tokio::sync::watch::channel(Counts::default()).0,
31        }
32    }
33
34    /// A lease found the budget full and waited.
35    pub(super) fn waited(&self) {
36        #[cfg(any(test, feature = "test-utils"))]
37        self.counts.send_modify(|counts| counts.waits += 1);
38    }
39
40    /// A lease of `count` places was granted.
41    pub(super) fn leased(&self, count: usize) {
42        #[cfg(any(test, feature = "test-utils"))]
43        self.counts.send_modify(|counts| {
44            counts.leased += count;
45            counts.most_leased = counts.most_leased.max(counts.leased);
46        });
47        #[cfg(not(any(test, feature = "test-utils")))]
48        let _ = count;
49    }
50
51    /// A lease of `count` places went back to the budget.
52    pub(super) fn returned(&self, count: usize) {
53        #[cfg(any(test, feature = "test-utils"))]
54        self.counts.send_modify(|counts| counts.leased -= count);
55        #[cfg(not(any(test, feature = "test-utils")))]
56        let _ = count;
57    }
58
59    /// A place was taken from a lease.
60    pub(super) fn holding(&self) {
61        #[cfg(any(test, feature = "test-utils"))]
62        self.counts.send_modify(|counts| {
63            counts.holding += 1;
64            counts.most_holding = counts.most_holding.max(counts.holding);
65        });
66    }
67
68    /// A place went back to its lease.
69    pub(super) fn released(&self) {
70        #[cfg(any(test, feature = "test-utils"))]
71        self.counts.send_modify(|counts| counts.holding -= 1);
72    }
73}
74
75/// What a test reads of a budget: the places its leases hold, the places
76/// holding an open file or stream, and the waits for the budget. Cloning
77/// shares the reading.
78#[cfg(any(test, feature = "test-utils"))]
79#[derive(Clone)]
80pub struct FileBudgetWatch {
81    shared: Arc<BudgetShared>,
82}
83
84#[cfg(any(test, feature = "test-utils"))]
85impl FileBudgetWatch {
86    pub(super) fn of(shared: Arc<BudgetShared>) -> Self {
87        Self { shared }
88    }
89    /// The places leased now.
90    pub fn leased(&self) -> usize {
91        self.shared.tally.counts.borrow().leased
92    }
93    /// The most places leased at once.
94    pub fn most_leased(&self) -> usize {
95        self.shared.tally.counts.borrow().most_leased
96    }
97    /// The places holding an open file or stream now: taken from a lease and
98    /// not yet given back.
99    pub fn holding(&self) -> usize {
100        self.shared.tally.counts.borrow().holding
101    }
102    /// The most places holding an open file or stream at once.
103    pub fn most_holding(&self) -> usize {
104        self.shared.tally.counts.borrow().most_holding
105    }
106    /// Wait until leases have found the budget full and waited `count` more
107    /// times than before this call.
108    pub fn lease_waits_from_now(
109        &self,
110        count: usize,
111    ) -> impl std::future::Future<Output = ()> + Send + 'static {
112        let mut counts = self.shared.tally.counts.subscribe();
113        let now = counts.borrow_and_update().waits;
114        async move {
115            crate::clock::within(counts.wait_for(|counts| counts.waits >= now + count))
116                .await
117                .expect("the lease wait")
118                .expect("the budget is kept");
119        }
120    }
121    /// Wait until the budget is empty: every lease has given its places
122    /// back, so no place holds an open file or stream either. A budget empty
123    /// at the call is empty at once.
124    pub fn empty(&self) -> impl std::future::Future<Output = ()> + Send + 'static {
125        let mut counts = self.shared.tally.counts.subscribe();
126        async move {
127            crate::clock::within(counts.wait_for(|counts| counts.leased == 0))
128                .await
129                .expect("the budget empties")
130                .expect("the budget is kept");
131        }
132    }
133}