Skip to content

Commit 31c8e1d

Browse files
committed
storage: clean up test code
1 parent d28fbec commit 31c8e1d

File tree

5 files changed

+45
-43
lines changed

5 files changed

+45
-43
lines changed

src/storage/bitcask.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,15 @@ impl Log {
360360
}
361361
}
362362

363+
/// Most storage tests are Goldenscripts under src/storage/testscripts.
363364
#[cfg(test)]
364365
mod tests {
365366
use super::super::engine::test::Runner;
366367
use super::*;
367368
use crate::encoding::format::{self, Formatter as _};
368-
use std::error::Error as StdError;
369+
369370
use std::fmt::Write as _;
370-
use std::result::Result as StdResult;
371+
use std::{error::Error as StdError, result::Result as StdResult};
371372
use test_case::test_case;
372373
use test_each_file::test_each_path;
373374

src/storage/engine.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ impl Status {
8888
pub mod test {
8989
use super::*;
9090
use crate::encoding::format::{self, Formatter as _};
91+
9192
use crossbeam::channel::Sender;
9293
use itertools::Itertools as _;
9394
use regex::Regex;
94-
use std::error::Error as StdError;
9595
use std::fmt::Write as _;
96-
use std::result::Result as StdResult;
96+
use std::{error::Error as StdError, result::Result as StdResult};
9797

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

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

src/storage/memory.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ impl<'a> DoubleEndedIterator for ScanIterator<'a> {
8383
}
8484
}
8585

86+
/// Most storage tests are Goldenscripts under src/storage/testscripts.
8687
#[cfg(test)]
8788
mod tests {
8889
use super::super::engine::test::Runner;
8990
use super::*;
91+
9092
use test_each_file::test_each_path;
9193

9294
// Run common goldenscript tests in src/storage/testscripts/engine.

src/storage/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! Key/value storage engines, including an MVCC transaction layer. For
2+
//! details, see the `engine`, `bitcask`, and `mvcc` module documentation.
3+
14
mod bitcask;
25
pub mod engine;
36
mod memory;

src/storage/mvcc.rs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ impl<'a, I: engine::ScanIterator> Iterator for VersionIterator<'a, I> {
742742
}
743743
}
744744

745+
/// Most storage tests are Goldenscripts under src/storage/testscripts.
745746
#[cfg(test)]
746747
pub mod tests {
747748
use super::*;
@@ -826,6 +827,8 @@ pub mod tests {
826827
impl goldenscript::Runner for MVCCRunner {
827828
fn run(&mut self, command: &goldenscript::Command) -> Result<String, Box<dyn Error>> {
828829
let mut output = String::new();
830+
let mut tags = command.tags.clone();
831+
829832
match command.name.as_str() {
830833
// txn: begin [readonly] [as_of=VERSION]
831834
"begin" => {
@@ -961,7 +964,7 @@ pub mod tests {
961964
parse_key_range(args.next_pos().map(|a| a.value.as_str()).unwrap_or(".."))?;
962965
args.reject_rest()?;
963966

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

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

10221024
// status
1023-
"status" => {
1024-
let status = self.mvcc.status()?;
1025-
writeln!(output, "{status:#?}")?;
1026-
}
1025+
"status" => writeln!(output, "{:#?}", self.mvcc.status()?)?,
10271026

10281027
name => return Err(format!("invalid command {name}").into()),
10291028
}
1030-
Ok(output)
1031-
}
10321029

1033-
/// If requested via [ops] tag, output engine operations for the command.
1034-
fn end_command(
1035-
&mut self,
1036-
command: &goldenscript::Command,
1037-
) -> Result<String, Box<dyn Error>> {
1038-
// Parse tags.
1039-
let mut show_ops = false;
1040-
for tag in &command.tags {
1041-
match tag.as_str() {
1042-
"ops" => show_ops = true,
1043-
tag => return Err(format!("invalid tag {tag}").into()),
1030+
// If requested, output engine operations.
1031+
if tags.remove("ops") {
1032+
while let Ok(op) = self.op_rx.try_recv() {
1033+
match op {
1034+
Operation::Delete { key } => {
1035+
let fmtkey = format::MVCC::<format::Raw>::key(&key);
1036+
let rawkey = format::Raw::key(&key);
1037+
writeln!(output, "engine delete {fmtkey} [{rawkey}]")?
1038+
}
1039+
Operation::Flush => writeln!(output, "engine flush")?,
1040+
Operation::Set { key, value } => {
1041+
let fmtkv = format::MVCC::<format::Raw>::key_value(&key, &value);
1042+
let rawkv = format::Raw::key_value(&key, &value);
1043+
writeln!(output, "engine set {fmtkv} [{rawkv}]")?
1044+
}
1045+
}
10441046
}
10451047
}
10461048

1047-
// Process engine operations, either output or drain.
1048-
let mut output = String::new();
1049-
while let Ok(op) = self.op_rx.try_recv() {
1050-
match op {
1051-
_ if !show_ops => {}
1052-
Operation::Delete { key } => {
1053-
let fmtkey = format::MVCC::<format::Raw>::key(&key);
1054-
let rawkey = format::Raw::key(&key);
1055-
writeln!(output, "engine delete {fmtkey} [{rawkey}]")?
1056-
}
1057-
Operation::Flush => writeln!(output, "engine flush")?,
1058-
Operation::Set { key, value } => {
1059-
let fmtkv = format::MVCC::<format::Raw>::key_value(&key, &value);
1060-
let rawkv = format::Raw::key_value(&key, &value);
1061-
writeln!(output, "engine set {fmtkv} [{rawkv}]")?
1062-
}
1063-
}
1049+
if let Some(tag) = tags.iter().next() {
1050+
return Err(format!("unknown tag {tag}").into());
10641051
}
1052+
10651053
Ok(output)
10661054
}
1055+
1056+
// Drain unhandled engine operations.
1057+
fn end_command(&mut self, _: &goldenscript::Command) -> Result<String, Box<dyn Error>> {
1058+
while self.op_rx.try_recv().is_ok() {}
1059+
Ok(String::new())
1060+
}
10671061
}
10681062
}

0 commit comments

Comments
 (0)