Skip to content

Commit

Permalink
test: implement test utils in wal
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <69835502+bsbds@users.noreply.github.com>
  • Loading branch information
bsbds committed Mar 11, 2024
1 parent 1919293 commit 36302ef
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/curp/src/server/storage/wal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ mod remover;
/// WAL segment
mod segment;

/// WAL test utils
#[cfg(test)]
mod test_util;

/// WAL storage tests
#[cfg(test)]
mod tests;
Expand Down
88 changes: 88 additions & 0 deletions crates/curp/src/server/storage/wal/test_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use bytes::BytesMut;
use curp_external_api::{cmd::ProposeId, LogIndex};
use curp_test_utils::test_cmd::TestCommand;
use parking_lot::Mutex;
use tokio_util::codec::Encoder;

use crate::log_entry::{EntryData, LogEntry};

use super::codec::{DataFrame, WAL};

pub(super) struct EntryGenerator {
inner: Mutex<Inner>,
}

struct Inner {
next_index: u64,
segment_size: u64,
logs_sent: Vec<LogEntry<TestCommand>>,
}

impl EntryGenerator {
pub(super) fn new(segment_size: u64) -> Self {
Self {
inner: Mutex::new(Inner {
next_index: 1,
segment_size,
logs_sent: Vec::new(),
}),
}
}

pub(super) fn skip(&self, num_index: usize) {
let mut this = self.inner.lock();
this.next_index += num_index as u64;
}

pub(super) fn next(&self) -> LogEntry<TestCommand> {
let mut this = self.inner.lock();
let entry =
LogEntry::<TestCommand>::new(this.next_index, 1, EntryData::Empty(ProposeId(1, 2)));
this.logs_sent.push(entry.clone());
this.next_index += 1;
entry
}

pub(super) fn take(&self, num: usize) -> Vec<LogEntry<TestCommand>> {
(0..num).map(|_| self.next()).collect()
}

pub(super) fn reset_next_index_to(&self, index: LogIndex) {
let mut this = self.inner.lock();
this.next_index = index;
this.logs_sent.truncate(index as usize - 1);
}

pub(super) fn current_index(&self) -> LogIndex {
let this = self.inner.lock();
this.next_index - 1
}

pub(super) fn all_logs(&self) -> Vec<LogEntry<TestCommand>> {
self.inner.lock().logs_sent.clone()
}

pub(super) fn num_entries_per_page(&self) -> usize {
let page_size = 4096;
self.cal_num(page_size)
}

pub(super) fn num_entries_per_segment(&self) -> usize {
let this = self.inner.lock();
self.cal_num(this.segment_size as usize)
}

fn cal_num(&self, size: usize) -> usize {
let header_size = 32;
let entry_size = self.entry_size();
(size - header_size + entry_size - 1) / entry_size
}

fn entry_size(&self) -> usize {
let sample_entry = LogEntry::<TestCommand>::new(1, 1, EntryData::Empty(ProposeId(1, 2)));
let mut wal_codec = WAL::<TestCommand>::new();
let mut buf = BytesMut::new();
wal_codec.encode(vec![DataFrame::Entry(sample_entry)], &mut buf);
buf.len()
}
}

0 comments on commit 36302ef

Please sign in to comment.