Skip to content

Commit

Permalink
storage: clean up test code
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jul 22, 2024
1 parent d28fbec commit 31c8e1d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/storage/bitcask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,15 @@ impl Log {
}
}

/// Most storage tests are Goldenscripts under src/storage/testscripts.
#[cfg(test)]
mod tests {
use super::super::engine::test::Runner;
use super::*;
use crate::encoding::format::{self, Formatter as _};
use std::error::Error as StdError;

use std::fmt::Write as _;
use std::result::Result as StdResult;
use std::{error::Error as StdError, result::Result as StdResult};
use test_case::test_case;
use test_each_file::test_each_path;

Expand Down
10 changes: 6 additions & 4 deletions src/storage/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ impl Status {
pub mod test {
use super::*;
use crate::encoding::format::{self, Formatter as _};

use crossbeam::channel::Sender;
use itertools::Itertools as _;
use regex::Regex;
use std::error::Error as StdError;
use std::fmt::Write as _;
use std::result::Result as StdResult;
use std::{error::Error as StdError, result::Result as StdResult};

/// Goldenscript runner for engines. All engines use a common set of
/// goldenscripts in src/storage/testscripts/engine, as well as their own
Expand Down Expand Up @@ -142,7 +142,8 @@ pub mod test {
self.engine.scan(range).try_collect()?
};
for (key, value) in items {
writeln!(output, "{}", format::Raw::key_value(&key, &value))?;
let fmtkv = format::Raw::key_value(&key, &value);
writeln!(output, "{fmtkv}")?;
}
}

Expand All @@ -153,7 +154,8 @@ pub mod test {
args.reject_rest()?;
let mut scan = self.engine.scan_prefix(&prefix);
while let Some((key, value)) = scan.next().transpose()? {
writeln!(output, "{}", format::Raw::key_value(&key, &value))?;
let fmtkv = format::Raw::key_value(&key, &value);
writeln!(output, "{fmtkv}")?;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/storage/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ impl<'a> DoubleEndedIterator for ScanIterator<'a> {
}
}

/// Most storage tests are Goldenscripts under src/storage/testscripts.
#[cfg(test)]
mod tests {
use super::super::engine::test::Runner;
use super::*;

use test_each_file::test_each_path;

// Run common goldenscript tests in src/storage/testscripts/engine.
Expand Down
3 changes: 3 additions & 0 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! Key/value storage engines, including an MVCC transaction layer. For
//! details, see the `engine`, `bitcask`, and `mvcc` module documentation.

mod bitcask;
pub mod engine;
mod memory;
Expand Down
68 changes: 31 additions & 37 deletions src/storage/mvcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ impl<'a, I: engine::ScanIterator> Iterator for VersionIterator<'a, I> {
}
}

/// Most storage tests are Goldenscripts under src/storage/testscripts.
#[cfg(test)]
pub mod tests {
use super::*;
Expand Down Expand Up @@ -826,6 +827,8 @@ pub mod tests {
impl goldenscript::Runner for MVCCRunner {
fn run(&mut self, command: &goldenscript::Command) -> Result<String, Box<dyn Error>> {
let mut output = String::new();
let mut tags = command.tags.clone();

match command.name.as_str() {
// txn: begin [readonly] [as_of=VERSION]
"begin" => {
Expand Down Expand Up @@ -961,7 +964,7 @@ pub mod tests {
parse_key_range(args.next_pos().map(|a| a.value.as_str()).unwrap_or(".."))?;
args.reject_rest()?;

let kvs: Vec<_> = txn.scan(range).collect::<crate::error::Result<_>>()?;
let kvs: Vec<_> = txn.scan(range).try_collect()?;
for (key, value) in kvs {
writeln!(output, "{}", format::Raw::key_value(&key, &value))?;
}
Expand All @@ -974,8 +977,7 @@ pub mod tests {
let prefix = decode_binary(&args.next_pos().ok_or("prefix not given")?.value);
args.reject_rest()?;

let kvs: Vec<_> =
txn.scan_prefix(&prefix).collect::<crate::error::Result<_>>()?;
let kvs: Vec<_> = txn.scan_prefix(&prefix).try_collect()?;
for (key, value) in kvs {
writeln!(output, "{}", format::Raw::key_value(&key, &value))?;
}
Expand Down Expand Up @@ -1020,49 +1022,41 @@ pub mod tests {
}

// status
"status" => {
let status = self.mvcc.status()?;
writeln!(output, "{status:#?}")?;
}
"status" => writeln!(output, "{:#?}", self.mvcc.status()?)?,

name => return Err(format!("invalid command {name}").into()),
}
Ok(output)
}

/// If requested via [ops] tag, output engine operations for the command.
fn end_command(
&mut self,
command: &goldenscript::Command,
) -> Result<String, Box<dyn Error>> {
// Parse tags.
let mut show_ops = false;
for tag in &command.tags {
match tag.as_str() {
"ops" => show_ops = true,
tag => return Err(format!("invalid tag {tag}").into()),
// If requested, output engine operations.
if tags.remove("ops") {
while let Ok(op) = self.op_rx.try_recv() {
match op {
Operation::Delete { key } => {
let fmtkey = format::MVCC::<format::Raw>::key(&key);
let rawkey = format::Raw::key(&key);
writeln!(output, "engine delete {fmtkey} [{rawkey}]")?
}
Operation::Flush => writeln!(output, "engine flush")?,
Operation::Set { key, value } => {
let fmtkv = format::MVCC::<format::Raw>::key_value(&key, &value);
let rawkv = format::Raw::key_value(&key, &value);
writeln!(output, "engine set {fmtkv} [{rawkv}]")?
}
}
}
}

// Process engine operations, either output or drain.
let mut output = String::new();
while let Ok(op) = self.op_rx.try_recv() {
match op {
_ if !show_ops => {}
Operation::Delete { key } => {
let fmtkey = format::MVCC::<format::Raw>::key(&key);
let rawkey = format::Raw::key(&key);
writeln!(output, "engine delete {fmtkey} [{rawkey}]")?
}
Operation::Flush => writeln!(output, "engine flush")?,
Operation::Set { key, value } => {
let fmtkv = format::MVCC::<format::Raw>::key_value(&key, &value);
let rawkv = format::Raw::key_value(&key, &value);
writeln!(output, "engine set {fmtkv} [{rawkv}]")?
}
}
if let Some(tag) = tags.iter().next() {
return Err(format!("unknown tag {tag}").into());
}

Ok(output)
}

// Drain unhandled engine operations.
fn end_command(&mut self, _: &goldenscript::Command) -> Result<String, Box<dyn Error>> {
while self.op_rx.try_recv().is_ok() {}
Ok(String::new())
}
}
}

0 comments on commit 31c8e1d

Please sign in to comment.