Skip to main content

sqlive/
database_test_hooks.rs

1//! Hooks a test uses to fault or watch a database's upkeep threads and
2//! readers, and to reach each of its connections.
3
4use super::*;
5use tokio::sync::mpsc::UnboundedReceiver;
6
7impl<P: Policy> Database<P> {
8    fn checkpointer(&self) -> &WalCheckpointer<P> {
9        self.checkpoints.as_ref().expect("the database has a checkpointer")
10    }
11
12    fn statistics(&self) -> &UpkeepThread<PlannerStatistics<P>> {
13        self.statistics.as_ref().expect("the database keeps its planner's statistics")
14    }
15
16    /// Whether one of the database's own threads checkpoints its WAL.
17    pub fn checkpoints(&self) -> bool {
18        self.checkpoints.is_some()
19    }
20
21    /// Run `fault` as each checkpoint takes the writer.
22    pub fn fault_checkpoints(
23        &self,
24        fault: impl FnMut() -> Result<(), PolicyError<P>> + Send + 'static,
25    ) {
26        self.checkpointer().fault(fault);
27    }
28
29    /// A checkpoint copies the WAL in passes while writes go on. Run `step`
30    /// after each pass's copy returns, before the pass syncs.
31    pub fn after_checkpoint_copies(&self, step: impl FnMut() + Send + 'static) {
32        self.checkpointer().after_copy(step);
33    }
34
35    /// Return a receiver that gets what each later checkpoint does at its
36    /// last step. The checkpointer sends it while the checkpoint still holds
37    /// the writer.
38    pub fn report_checkpoints(&self) -> UnboundedReceiver<crate::WalCheckpoint> {
39        self.checkpointer().report_restarts()
40    }
41
42    /// Run one wake's checkpoint now, on the checkpointer's connection.
43    pub fn checkpoint_now(&self) -> Result<(), PolicyError<P>> {
44        self.checkpointer().run_now()
45    }
46
47    /// Return a receiver that gets a message after each later run of the
48    /// planner's statistics commits.
49    pub fn report_statistics_runs(&self) -> UnboundedReceiver<()> {
50        self.statistics().report_runs()
51    }
52
53    /// Return a receiver that gets a message when the next caller waits for
54    /// a reader.
55    pub fn observe_next_reader_wait(&self) -> tokio::sync::oneshot::Receiver<()> {
56        self.readers.observe_next_wait()
57    }
58
59    /// Run `visit` on each reader in turn, under its own lock.
60    pub fn each_reader(&self, visit: impl FnMut(&mut HostSql<P>)) {
61        self.readers.each(visit);
62    }
63
64    /// Run `visit` on every connection the database holds open, one at a
65    /// time, each under its own lock: the readers, the upkeep threads'
66    /// connections and the writer.
67    pub fn each_connection(&self, mut visit: impl FnMut(&HostSql<P>)) {
68        self.readers.each(|reader| visit(reader));
69        if let Some(checkpoints) = &self.checkpoints {
70            checkpoints.with_connection(&mut visit);
71        }
72        if let Some(statistics) = &self.statistics {
73            statistics.with_connection(&mut visit);
74        }
75        self.host(|writer| visit(writer));
76    }
77}