Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Oct 31, 2024
1 parent f47cea6 commit 289f3ad
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 43 deletions.
42 changes: 26 additions & 16 deletions src/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,14 +548,14 @@ pub trait Wal<S> {
fn minimum_version(&self) -> u64;

#[inline]
fn update_versions(&mut self, version: u64) {
fn update_versions(&self, version: u64) {
self.update_maximum_version(version);
self.update_minimum_version(version);
}

fn update_maximum_version(&mut self, version: u64);
fn update_maximum_version(&self, version: u64);

fn update_minimum_version(&mut self, version: u64);
fn update_minimum_version(&self, version: u64);

#[inline]
fn insert_pointer<'a>(
Expand Down Expand Up @@ -785,12 +785,19 @@ pub trait Wal<S> {
};

res.and_then(|(offset, kp, vp)| {
self.insert_pointer(version, kp, vp).map_err(|e| {
unsafe {
self.allocator().rewind(ArenaPosition::Start(offset as u32));
};
Among::Right(e)
})
self
.insert_pointer(version, kp, vp)
.map(|_| {
if let Some(version) = version {
self.update_versions(version)
}
})
.map_err(|e| {
unsafe {
self.allocator().rewind(ArenaPosition::Start(offset as u32));

Check warning on line 797 in src/sealed.rs

View check run for this annotation

Codecov / codecov/patch

src/sealed.rs#L796-L797

Added lines #L796 - L797 were not covered by tests
};
Among::Right(e)

Check warning on line 799 in src/sealed.rs

View check run for this annotation

Codecov / codecov/patch

src/sealed.rs#L799

Added line #L799 was not covered by tests
})
})
}

Expand Down Expand Up @@ -820,6 +827,8 @@ pub trait Wal<S> {
let opts = self.options();
let maximum_key_size = opts.maximum_key_size().to_u32();
let minimum_value_size = opts.maximum_value_size();
let mut minimum_version = self.minimum_version();
let mut maximum_version = self.maximum_version();
let start_offset = unsafe {
let (mut cursor, allocator, mut buf) = batch
.iter_mut()
Expand Down Expand Up @@ -864,9 +873,6 @@ pub trait Wal<S> {
})
.map_err(Among::Right)?;

let mut minimum = self.minimum_version();
let mut maximum = self.maximum_version();

for ent in batch.iter_mut() {
let meta = ent.encoded_meta();
let version_size = if ent.internal_version().is_some() {
Expand Down Expand Up @@ -896,12 +902,12 @@ pub trait Wal<S> {
let (key_ptr, val_ptr) = if let Some(version) = ent.internal_version() {
buf.put_u64_le_unchecked(version);

if maximum < version {
maximum = version;
if maximum_version < version {
maximum_version = version;
}

if minimum > version {
minimum = version;
if minimum_version > version {
minimum_version = version;

Check warning on line 910 in src/sealed.rs

View check run for this annotation

Codecov / codecov/patch

src/sealed.rs#L910

Added line #L910 was not covered by tests
}

(
Expand Down Expand Up @@ -977,6 +983,10 @@ pub trait Wal<S> {
let (kp, vp) = e.take_pointer().unwrap();
(e.internal_version(), kp, vp)
}))
.map(|_| {
self.update_maximum_version(maximum_version);
self.update_minimum_version(minimum_version);
})
.map_err(|e| {

Check warning on line 990 in src/sealed.rs

View check run for this annotation

Codecov / codecov/patch

src/sealed.rs#L990

Added line #L990 was not covered by tests
// Safety: the writer is single threaded, the memory chunk in buf cannot be accessed by other threads,
// so it's safe to rewind the arena.
Expand Down
21 changes: 11 additions & 10 deletions src/swmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ mod reader;
mod wal;
mod writer;

#[cfg(all(
test,
any(
all_orderwal_tests,
test_swmr_constructor,
test_swmr_insert,
test_swmr_get,
test_swmr_iters,
)
))]
// #[cfg(all(
// test,
// any(
// all_orderwal_tests,
// test_swmr_constructor,
// test_swmr_insert,
// test_swmr_get,
// test_swmr_iters,
// )
// ))]
#[cfg(test)]
mod tests;

/// The ordered write-ahead log without multiple version support.
Expand Down
38 changes: 38 additions & 0 deletions src/swmr/tests/constructor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use base::{Reader, Writer};
use skl::KeySize;

use crate::memtable::{
alternative::{Table, TableOptions},
Expand Down Expand Up @@ -70,3 +71,40 @@ expand_unit_tests!(
}),
}
);

#[test]
#[cfg_attr(miri, ignore)]
fn reopen_wrong_kind() {
use crate::Builder;

let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_reopen_wrong_kind");
let wal = unsafe {
Builder::new()
.with_capacity(MB)
.with_maximum_key_size(KeySize::with(10))
.with_maximum_value_size(10)
.with_create_new(true)
.with_read(true)
.with_write(true)
.map_mut::<OrderWal<Person, String>, _>(path.as_path())
.unwrap()
};

assert!(!wal.read_only());
assert_eq!(wal.capacity(), MB);
assert!(wal.remaining() < MB);
assert_eq!(wal.maximum_key_size(), 10);
assert_eq!(wal.maximum_value_size(), 10);
assert_eq!(wal.path().unwrap().as_path(), path.as_path());
assert_eq!(wal.options().maximum_key_size(), 10);

let err = unsafe {
Builder::new()
.with_capacity(MB)
.with_read(true)
.map_mut::<crate::multiple_version::OrderWal<Person, String>, _>(path.as_path())
.unwrap_err()
};
assert!(matches!(err, crate::error::Error::KindMismatch { .. }));
}
13 changes: 12 additions & 1 deletion src/swmr/tests/multiple_version_constructor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use multiple_version::{OrderWal, Reader, Writer};
use skl::KeySize;

use crate::memtable::{
alternative::{MultipleVersionTable, TableOptions},
Expand Down Expand Up @@ -78,16 +79,26 @@ fn reopen_wrong_kind() {

let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("test_reopen_wrong_kind");
let _ = unsafe {
let wal = unsafe {
Builder::new()
.with_capacity(MB)
.with_maximum_key_size(KeySize::with(10))
.with_maximum_value_size(10)
.with_create_new(true)
.with_read(true)
.with_write(true)
.map_mut::<OrderWal<Person, String>, _>(path.as_path())
.unwrap()
};

assert!(!wal.read_only());
assert_eq!(wal.capacity(), MB);
assert!(wal.remaining() < MB);
assert_eq!(wal.maximum_key_size(), 10);
assert_eq!(wal.maximum_value_size(), 10);
assert_eq!(wal.path().unwrap().as_path(), path.as_path());
assert_eq!(wal.options().maximum_key_size(), 10);

let err = unsafe {
Builder::new()
.with_capacity(MB)
Expand Down
Loading

0 comments on commit 289f3ad

Please sign in to comment.