diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-03-27 16:01:52 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-27 22:01:52 +0000 |
commit | 86c3c4f34397a29c2bf1847bddfea562a2369a4f (patch) | |
tree | bfbabf6f6d55dc14db47c4e06d4aae50277ab5ca /test_util/src | |
parent | 8c051dbd1a075ad3c228f78b29b13f0e455972a7 (diff) |
feat(core): initialize SQLite off-main-thread (#18401)
This gets SQLite off the flamegraph and reduces initialization time by
somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity
to move all the cache management code to a single place and reduce
duplication. While the PR has a net gain of lines, much of that is just
being a bit more deliberate with how we're recovering from errors.
The existing caches had various policies for dealing with cache
corruption, so I've unified them and tried to isolate the decisions we
make for recovery in a single place (see `open_connection` in
`CacheDB`). The policy I chose was:
1. Retry twice to open on-disk caches
2. If that fails, try to delete the file and recreate it on-disk
3. If we fail to delete the file or re-create a new cache, use a
fallback strategy that can be chosen per-cache: InMemory (temporary
cache for the process run), BlackHole (ignore writes, return empty
reads), or Error (fail on every operation).
The caches all use the same general code now, and share the cache
failure recovery policy.
In addition, it cleans up a TODO in the `NodeAnalysisCache`.
Diffstat (limited to 'test_util/src')
-rw-r--r-- | test_util/src/lib.rs | 111 |
1 files changed, 103 insertions, 8 deletions
diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index 33b5ae8bc..d4effd88b 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -1897,22 +1897,117 @@ pub fn new_deno_dir() -> TempDir { TempDir::new() } +/// Because we need to keep the [`TempDir`] alive for the entire run of this command, +/// we have to effectively reproduce the entire builder-pattern object for [`Command`]. pub struct DenoCmd { - // keep the deno dir directory alive for the duration of the command _deno_dir: TempDir, cmd: Command, } -impl Deref for DenoCmd { - type Target = Command; - fn deref(&self) -> &Command { - &self.cmd +impl DenoCmd { + pub fn args<I, S>(&mut self, args: I) -> &mut Self + where + I: IntoIterator<Item = S>, + S: AsRef<std::ffi::OsStr>, + { + self.cmd.args(args); + self + } + + pub fn arg<S>(&mut self, arg: S) -> &mut Self + where + S: AsRef<std::ffi::OsStr>, + { + self.cmd.arg(arg); + self + } + + pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self + where + I: IntoIterator<Item = (K, V)>, + K: AsRef<std::ffi::OsStr>, + V: AsRef<std::ffi::OsStr>, + { + self.cmd.envs(vars); + self + } + + pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self + where + K: AsRef<std::ffi::OsStr>, + V: AsRef<std::ffi::OsStr>, + { + self.cmd.env(key, val); + self + } + + pub fn env_remove<K>(&mut self, key: K) -> &mut Self + where + K: AsRef<std::ffi::OsStr>, + { + self.cmd.env_remove(key); + self + } + + pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self { + self.cmd.stdin(cfg); + self + } + + pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self { + self.cmd.stdout(cfg); + self + } + + pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self { + self.cmd.stderr(cfg); + self + } + + pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self { + self.cmd.current_dir(dir); + self + } + + pub fn output(&mut self) -> Result<std::process::Output, std::io::Error> { + self.cmd.output() + } + + pub fn status(&mut self) -> Result<std::process::ExitStatus, std::io::Error> { + self.cmd.status() + } + + pub fn spawn(&mut self) -> Result<DenoChild, std::io::Error> { + Ok(DenoChild { + _deno_dir: self._deno_dir.clone(), + child: self.cmd.spawn()?, + }) + } +} + +/// We need to keep the [`TempDir`] around until the child has finished executing, so +/// this acts as a RAII guard. +pub struct DenoChild { + _deno_dir: TempDir, + child: Child, +} + +impl Deref for DenoChild { + type Target = Child; + fn deref(&self) -> &Child { + &self.child + } +} + +impl DerefMut for DenoChild { + fn deref_mut(&mut self) -> &mut Child { + &mut self.child } } -impl DerefMut for DenoCmd { - fn deref_mut(&mut self) -> &mut Command { - &mut self.cmd +impl DenoChild { + pub fn wait_with_output(self) -> Result<Output, std::io::Error> { + self.child.wait_with_output() } } |