Skip to content

Commit

Permalink
use tempdir for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Horusiath committed Jul 9, 2024
1 parent 6a6e425 commit c120af8
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 105 deletions.
4 changes: 1 addition & 3 deletions yrs-kvstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ where
let start = Key::from_const([V1, KEYSPACE_OID]);
let end = Key::from_const([V1, KEYSPACE_DOC]);
let cursor = self.iter_range(&start, &end)?;
Ok(DocsNameIter { cursor, start, end })
Ok(DocsNameIter { cursor })
}

/// Returns an iterator over all metadata entries stored for a given document.
Expand Down Expand Up @@ -488,8 +488,6 @@ where
E: KVEntry,
{
cursor: I,
start: Key<2>,
end: Key<2>,
}

impl<I, E> Iterator for DocsNameIter<I, E>
Expand Down
4 changes: 2 additions & 2 deletions yrs-lmdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ readme = "./README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
yrs-kvstore = {version = "0.3", path = "../yrs-kvstore"}
yrs-kvstore = { version = "0.3", path = "../yrs-kvstore" }
lmdb-rs = { version = "0.7" }

[dev-dependencies]
yrs = "0.19"
criterion = "0.5"
rand = "0.7"
tempdir = "0.3"

[[bench]]
name = "benches"
Expand Down
11 changes: 6 additions & 5 deletions yrs-lmdb/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::sync::Arc;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use lib0::decoding::{Cursor, Read};
use lmdb_rs::core::DbCreate;
use lmdb_rs::Environment;
use rand::thread_rng;
use std::sync::Arc;
use yrs::encoding::read::{Cursor, Read};
use yrs::{uuid_v4, Doc, Text, Transact};

use yrs_kvstore::DocOps;
use yrs_lmdb::LmdbStore;

Expand All @@ -27,7 +28,7 @@ fn insert_doc(c: &mut Criterion) {
&(doc, env, handle),
|b, (doc, env, handle)| {
b.iter(|| {
let name = uuid_v4(&mut thread_rng()).to_string();
let name = uuid_v4().to_string();
let txn = env.new_transaction().unwrap();
let db = LmdbStore::from(txn.bind(&handle));
db.insert_doc(&name, &doc.transact()).unwrap();
Expand All @@ -54,7 +55,7 @@ fn updates(c: &mut Criterion) {
b.iter(|| {
let env = env.clone();
let handle = handle.clone();
let name = uuid_v4(&mut thread_rng()).to_string();
let name = uuid_v4().to_string();
let _sub = doc.observe_update_v1(move |_, e| {
let db_txn = env.new_transaction().unwrap();
let db = LmdbStore::from(db_txn.bind(&handle));
Expand Down
65 changes: 21 additions & 44 deletions yrs-lmdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,35 +259,12 @@ mod test {
use crate::{DocOps, LmdbStore};
use lmdb_rs::core::DbCreate;
use lmdb_rs::Environment;
use std::path::Path;
use std::sync::Arc;
use tempdir::TempDir;
use yrs::{Doc, GetString, ReadTxn, Text, Transact};

struct Cleaner(&'static str);

impl Cleaner {
fn new(dir: &'static str) -> Self {
Self::cleanup(dir);
Cleaner(dir)
}

fn dir(&self) -> &str {
self.0
}

fn cleanup(dir: &str) {
if let Err(_) = std::fs::remove_dir_all(dir) {
// if dir doesn't exists, ignore
}
}
}

impl Drop for Cleaner {
fn drop(&mut self) {
Self::cleanup(self.dir());
}
}

fn init_env(dir: &str) -> Environment {
fn init_env<P: AsRef<Path>>(dir: P) -> Environment {
let env = Environment::new()
.autocreate_dir(true)
.max_dbs(4)
Expand All @@ -298,8 +275,8 @@ mod test {

#[test]
fn create_get_remove() {
let cleaner = Cleaner::new("lmdb-create_get_remove");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-create_get_remove").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();

// insert document
Expand Down Expand Up @@ -352,8 +329,8 @@ mod test {
}
#[test]
fn multi_insert() {
let cleaner = Cleaner::new("lmdb-multi_insert");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-multi_insert").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();

// insert document twice
Expand Down Expand Up @@ -392,8 +369,8 @@ mod test {
#[test]
fn incremental_updates() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("lmdb-incremental_updates");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-incremental_updates").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();
let env = Arc::new(env);
let h = Arc::new(h);
Expand Down Expand Up @@ -446,8 +423,8 @@ mod test {
#[test]
fn state_vector_updates_only() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("lmdb-state_vector_updates_only");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-state_vector_updates_only").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();
let env = Arc::new(env);
let h = Arc::new(h);
Expand Down Expand Up @@ -483,8 +460,8 @@ mod test {
#[test]
fn state_diff_from_updates() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("lmdb-state_diff_from_updates");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-state_diff_from_updates").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();
let env = Arc::new(env);
let h = Arc::new(h);
Expand Down Expand Up @@ -520,8 +497,8 @@ mod test {
#[test]
fn state_diff_from_doc() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("lmdb-state_diff_from_doc");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-state_diff_from_doc").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();

let (sv, expected) = {
Expand Down Expand Up @@ -551,8 +528,8 @@ mod test {
#[test]
fn doc_meta() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("lmdb-doc_meta");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-doc_meta").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();

let db_txn = env.new_transaction().unwrap();
Expand Down Expand Up @@ -582,8 +559,8 @@ mod test {

#[test]
fn doc_meta_iter() {
let cleaner = Cleaner::new("lmdb-doc_meta_iter");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-doc_meta_iter").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();
let db_txn = env.new_transaction().unwrap();
let db = LmdbStore::from(db_txn.bind(&h));
Expand All @@ -601,8 +578,8 @@ mod test {

#[test]
fn doc_iter() {
let cleaner = Cleaner::new("lmdb-doc_iter");
let env = init_env(cleaner.dir());
let dir = TempDir::new("lmdb-doc_iter").unwrap();
let env = init_env(&dir);
let h = env.create_db("yrs", DbCreate).unwrap();
let env = Arc::new(env);
let h = Arc::new(h);
Expand Down
4 changes: 2 additions & 2 deletions yrs-rocksdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ readme = "./README.md"

[dependencies]
yrs-kvstore = { version = "0.3", path = "../yrs-kvstore" }
rocksdb = { version = "0.21" }
rocksdb = { version = "0.22" }

[dev-dependencies]
yrs = "0.19"
criterion = "0.5"
rand = "0.7"
tempdir = "0.3"

[[bench]]
name = "benches"
Expand Down
11 changes: 6 additions & 5 deletions yrs-rocksdb/benches/benches.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::sync::Arc;

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use lib0::decoding::{Cursor, Read};
use rand::thread_rng;
use rocksdb::TransactionDB;
use std::sync::Arc;
use yrs::encoding::read::{Cursor, Read};
use yrs::{uuid_v4, Doc, Text, Transact};

use yrs_kvstore::DocOps;
use yrs_rocksdb::RocksDBStore;

Expand All @@ -25,7 +26,7 @@ fn insert_doc(c: &mut Criterion) {
&(doc, db),
|b, (doc, db)| {
b.iter(|| {
let name = uuid_v4(&mut thread_rng()).to_string();
let name = uuid_v4().to_string();
let db_txn = RocksDBStore::from(db.transaction());
db_txn.insert_doc(&name, &doc.transact()).unwrap();
db_txn.commit().unwrap();
Expand All @@ -49,7 +50,7 @@ fn updates(c: &mut Criterion) {
|b, (doc, text, ops, db)| {
b.iter(|| {
let db = db.clone();
let name = uuid_v4(&mut thread_rng()).to_string();
let name = uuid_v4().to_string();
let _sub = doc.observe_update_v1(move |_, e| {
let db_txn = RocksDBStore::from(db.transaction());
db_txn.push_update(&name, &e.update).unwrap();
Expand Down
71 changes: 27 additions & 44 deletions yrs-rocksdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,44 +217,21 @@ impl KVEntry for RocksDBEntry {
mod test {
use crate::RocksDBStore;
use rocksdb::TransactionDB;
use std::path::Path;
use std::sync::Arc;
use tempdir::TempDir;
use yrs::{Doc, GetString, ReadTxn, Text, Transact};
use yrs_kvstore::DocOps;

struct Cleaner(&'static str);

impl Cleaner {
fn new(dir: &'static str) -> Self {
Self::cleanup(dir);
Cleaner(dir)
}

fn dir(&self) -> &str {
self.0
}

fn cleanup(dir: &str) {
if let Err(_) = std::fs::remove_dir_all(dir) {
// if dir doesn't exists, ignore
}
}
}

impl Drop for Cleaner {
fn drop(&mut self) {
Self::cleanup(self.dir());
}
}

fn init_env(dir: &str) -> TransactionDB {
fn init_env<P: AsRef<Path>>(dir: P) -> TransactionDB {
let db = TransactionDB::open_default(dir).unwrap();
db
}

#[test]
fn create_get_remove() {
let cleaner = Cleaner::new("rocksdb-create_get_remove");
let db = init_env(cleaner.dir());
let tmp = TempDir::new("rocksdb-create_get_remove").unwrap();
let db = init_env(&tmp);

// insert document
{
Expand Down Expand Up @@ -303,8 +280,8 @@ mod test {
}
#[test]
fn multi_insert() {
let cleaner = Cleaner::new("rocksdb-multi_insert");
let db = init_env(cleaner.dir());
let tmp = TempDir::new("rocksdb-multi_insert").unwrap();
let db = init_env(&tmp);

// insert document twice
{
Expand Down Expand Up @@ -339,8 +316,9 @@ mod test {
#[test]
fn incremental_updates() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("rocksdb-incremental_updates");
let db = Arc::new(init_env(cleaner.dir()));
let tmp = TempDir::new("rocksdb-incremental_updates").unwrap();
let db = init_env(&tmp);
let db = Arc::new(db);

// store document updates
{
Expand Down Expand Up @@ -386,8 +364,9 @@ mod test {
#[test]
fn state_vector_updates_only() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("rocksdb-state_vector_updates_only");
let db = Arc::new(init_env(cleaner.dir()));
let tmp = TempDir::new("rocksdb-state_vector_updates_only").unwrap();
let db = init_env(&tmp);
let db = Arc::new(db);

// store document updates
{
Expand Down Expand Up @@ -417,8 +396,9 @@ mod test {
#[test]
fn state_diff_from_updates() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("rocksdb-state_diff_from_updates");
let db = Arc::new(init_env(cleaner.dir()));
let tmp = TempDir::new("rocksdb-state_diff_from_updates").unwrap();
let db = init_env(&tmp);
let db = Arc::new(db);

let (sv, expected) = {
let doc = Doc::new();
Expand Down Expand Up @@ -448,8 +428,9 @@ mod test {
#[test]
fn state_diff_from_doc() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("rocksdb-state_diff_from_doc");
let db = init_env(cleaner.dir());
let tmp = TempDir::new("rocksdb-state_diff_from_doc").unwrap();
let db = init_env(&tmp);
let db = Arc::new(db);

let (sv, expected) = {
let doc = Doc::new();
Expand All @@ -476,8 +457,9 @@ mod test {
#[test]
fn doc_meta() {
const DOC_NAME: &str = "doc";
let cleaner = Cleaner::new("lmdb-doc_meta");
let db = init_env(cleaner.dir());
let tmp = TempDir::new("rocksdb-doc_meta").unwrap();
let db = init_env(&tmp);
let db = Arc::new(db);

let db_txn = RocksDBStore::from(db.transaction());
let value = db_txn.get_meta(DOC_NAME, "key").unwrap();
Expand Down Expand Up @@ -505,8 +487,8 @@ mod test {

#[test]
fn doc_meta_iter() {
let cleaner = Cleaner::new("rocksdb-doc_meta_iter");
let db = init_env(cleaner.dir());
let tmp = TempDir::new("rocksdb-doc_meta_iter").unwrap();
let db = init_env(&tmp);
let db_txn = RocksDBStore::from(db.transaction());

db_txn.insert_meta("A", "key1", [1].as_ref()).unwrap();
Expand All @@ -522,8 +504,9 @@ mod test {

#[test]
fn doc_iter() {
let cleaner = Cleaner::new("rocksdb-doc_iter");
let db = Arc::new(init_env(cleaner.dir()));
let tmp = TempDir::new("rocksdb-doc_iter").unwrap();
let db = init_env(&tmp);
let db = Arc::new(db);

// insert metadata
{
Expand Down

0 comments on commit c120af8

Please sign in to comment.