Skip to content

Commit

Permalink
improve test coverage of memtable.rs (#139)
Browse files Browse the repository at this point in the history
* improve test coverage of memtable.rs

Signed-off-by: tabokie <xy.tao@outlook.com>

* simplify code

Signed-off-by: tabokie <xy.tao@outlook.com>

* clippy

Signed-off-by: tabokie <xy.tao@outlook.com>

* fix grcov build

Signed-off-by: tabokie <xy.tao@outlook.com>

* more tests

Signed-off-by: tabokie <xy.tao@outlook.com>

* minor fix

Signed-off-by: tabokie <xy.tao@outlook.com>
  • Loading branch information
tabokie authored Nov 3, 2021
1 parent 605b5a2 commit 7849231
Show file tree
Hide file tree
Showing 5 changed files with 673 additions and 554 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
with:
sharedKey: ubuntu-latest
- name: Install grcov
run: if [[ ! -e ~/.cargo/bin/grcov ]]; then cargo install grcov; fi
run: if [[ ! -e ~/.cargo/bin/grcov ]]; then cargo install --locked grcov; fi
- name: Run tests
run: cargo test --features failpoints --all --verbose
env:
Expand Down
2 changes: 1 addition & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
let (mut memtables_rewrite, rewrite_stats) = rewrite.finish();
global_stats.merge(&rewrite_stats);

memtables.merge_lower_prio(&mut memtables_rewrite);
memtables.merge_rewrite_table(&mut memtables_rewrite);

let cfg = Arc::new(cfg);
let purge_manager = PurgeManager::new(
Expand Down
28 changes: 28 additions & 0 deletions src/log_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ fn from_nix_error(e: nix::Error, custom: &'static str) -> std::io::Error {

impl LogFd {
pub fn open<P: ?Sized + NixPath>(path: &P) -> IoResult<Self> {
fail_point!("log_fd::open::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
let flags = OFlag::O_RDWR;
// Permission 644
let mode = Mode::S_IRUSR | Mode::S_IWUSR | Mode::S_IRGRP | Mode::S_IROTH;
Expand All @@ -49,6 +52,9 @@ impl LogFd {
}

pub fn create<P: ?Sized + NixPath>(path: &P) -> IoResult<Self> {
fail_point!("log_fd::create::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
let flags = OFlag::O_RDWR | OFlag::O_CREAT;
// Permission 644
let mode = Mode::S_IRUSR | Mode::S_IWUSR | Mode::S_IRGRP | Mode::S_IROTH;
Expand All @@ -57,10 +63,16 @@ impl LogFd {
}

pub fn close(&self) -> IoResult<()> {
fail_point!("log_fd::close::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
close(self.0).map_err(|e| from_nix_error(e, "close"))
}

pub fn sync(&self) -> IoResult<()> {
fail_point!("log_fd::sync::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
#[cfg(target_os = "linux")]
{
nix::unistd::fdatasync(self.0).map_err(|e| from_nix_error(e, "fdatasync"))
Expand All @@ -72,6 +84,9 @@ impl LogFd {
}

pub fn read(&self, mut offset: usize, buf: &mut [u8]) -> IoResult<usize> {
fail_point!("log_fd::read::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
let mut readed = 0;
while readed < buf.len() {
let bytes = match pread(self.0, &mut buf[readed..], offset as i64) {
Expand All @@ -90,6 +105,10 @@ impl LogFd {
}

pub fn write(&self, mut offset: usize, content: &[u8]) -> IoResult<usize> {
fail_point!("log_fd::write::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
fail_point!("log_fd::write::zero", |_| { Ok(0) });
let mut written = 0;
while written < content.len() {
let bytes = match pwrite(self.0, &content[written..], offset as i64) {
Expand All @@ -107,17 +126,26 @@ impl LogFd {
}

pub fn file_size(&self) -> IoResult<usize> {
fail_point!("log_fd::file_size::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
lseek(self.0, 0, Whence::SeekEnd)
.map(|n| n as usize)
.map_err(|e| from_nix_error(e, "lseek"))
}

pub fn truncate(&self, offset: usize) -> IoResult<()> {
fail_point!("log_fd::truncate::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
ftruncate(self.0, offset as i64).map_err(|e| from_nix_error(e, "ftruncate"))
}

#[allow(unused_variables)]
pub fn allocate(&self, offset: usize, size: usize) -> IoResult<()> {
fail_point!("log_fd::allocate::err", |_| {
Err(from_nix_error(nix::Error::invalid_argument(), "fp"))
});
#[cfg(target_os = "linux")]
{
fcntl::fallocate(
Expand Down
Loading

0 comments on commit 7849231

Please sign in to comment.