Skip to content

Commit

Permalink
Move Rust integration tests to tests folder..
Browse files Browse the repository at this point in the history
.. as all of our tests in `functional_tests.rs` are essentially
integration tests.
  • Loading branch information
tnull committed Dec 19, 2023
1 parent 6abe232 commit e9caf8f
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 1,041 deletions.
3 changes: 1 addition & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::test::utils::TestLogger;
use lightning::util::test_utils::TestStore;
use lightning::util::test_utils::{TestLogger, TestStore};

#[test]
fn event_queue_persistence() {
Expand Down
2 changes: 1 addition & 1 deletion src/io/sqlite_store/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(super) fn migrate_schema(
mod tests {
use crate::io::sqlite_store::SqliteStore;
use crate::io::test_utils::do_read_write_remove_list_persist;
use crate::test::utils::random_storage_path;
use crate::test_utils::random_storage_path;

use lightning::util::persist::KVStore;

Expand Down
2 changes: 1 addition & 1 deletion src/io/sqlite_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl KVStore for SqliteStore {
mod tests {
use super::*;
use crate::io::test_utils::{do_read_write_remove_list_persist, do_test_store};
use crate::test::utils::random_storage_path;
use crate::test_utils::random_storage_path;

impl Drop for SqliteStore {
fn drop(&mut self) {
Expand Down
152 changes: 1 addition & 151 deletions src/io/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use crate::io::sqlite_store::SqliteStore;
use lightning_persister::fs_store::FilesystemStore;

use lightning::ln::functional_test_utils::{
connect_block, create_announced_chan_between_nodes, create_chanmon_cfgs, create_dummy_block,
create_network, create_node_cfgs, create_node_chanmgrs, send_payment,
Expand All @@ -9,12 +6,10 @@ use lightning::util::persist::{read_channel_monitors, KVStore, KVSTORE_NAMESPACE

use lightning::chain::channelmonitor::CLOSED_CHANNEL_UPDATE_ID;
use lightning::events::ClosureReason;
use lightning::util::test_utils::{self, TestStore};
use lightning::util::test_utils;
use lightning::{check_added_monitors, check_closed_broadcast, check_closed_event};

use std::panic::RefUnwindSafe;
use std::path::PathBuf;
use std::sync::RwLock;

pub(crate) fn do_read_write_remove_list_persist<K: KVStore + RefUnwindSafe>(kv_store: &K) {
let data = [42u8; 32];
Expand Down Expand Up @@ -173,148 +168,3 @@ pub(crate) fn do_test_store<K: KVStore>(store_0: &K, store_1: &K) {
// Make sure everything is persisted as expected after close.
check_persisted_data!(CLOSED_CHANNEL_UPDATE_ID);
}

// A `KVStore` impl for testing purposes that wraps all our `KVStore`s and asserts their synchronicity.
pub(crate) struct TestSyncStore {
serializer: RwLock<()>,
test_store: TestStore,
fs_store: FilesystemStore,
sqlite_store: SqliteStore,
}

impl TestSyncStore {
pub(crate) fn new(dest_dir: PathBuf) -> Self {
let serializer = RwLock::new(());
let mut fs_dir = dest_dir.clone();
fs_dir.push("fs_store");
let fs_store = FilesystemStore::new(fs_dir);
let mut sql_dir = dest_dir.clone();
sql_dir.push("sqlite_store");
let sqlite_store = SqliteStore::new(
sql_dir,
Some("test_sync_db".to_string()),
Some("test_sync_table".to_string()),
)
.unwrap();
let test_store = TestStore::new(false);
Self { serializer, fs_store, sqlite_store, test_store }
}

fn do_list(
&self, primary_namespace: &str, secondary_namespace: &str,
) -> std::io::Result<Vec<String>> {
let fs_res = self.fs_store.list(primary_namespace, secondary_namespace);
let sqlite_res = self.sqlite_store.list(primary_namespace, secondary_namespace);
let test_res = self.test_store.list(primary_namespace, secondary_namespace);

match fs_res {
Ok(mut list) => {
list.sort();

let mut sqlite_list = sqlite_res.unwrap();
sqlite_list.sort();
assert_eq!(list, sqlite_list);

let mut test_list = test_res.unwrap();
test_list.sort();
assert_eq!(list, test_list);

Ok(list)
}
Err(e) => {
assert!(sqlite_res.is_err());
assert!(test_res.is_err());
Err(e)
}
}
}
}

impl KVStore for TestSyncStore {
fn read(
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
) -> std::io::Result<Vec<u8>> {
let _guard = self.serializer.read().unwrap();

let fs_res = self.fs_store.read(primary_namespace, secondary_namespace, key);
let sqlite_res = self.sqlite_store.read(primary_namespace, secondary_namespace, key);
let test_res = self.test_store.read(primary_namespace, secondary_namespace, key);

match fs_res {
Ok(read) => {
assert_eq!(read, sqlite_res.unwrap());
assert_eq!(read, test_res.unwrap());
Ok(read)
}
Err(e) => {
assert!(sqlite_res.is_err());
assert_eq!(e.kind(), unsafe { sqlite_res.unwrap_err_unchecked().kind() });
assert!(test_res.is_err());
assert_eq!(e.kind(), unsafe { test_res.unwrap_err_unchecked().kind() });
Err(e)
}
}
}

fn write(
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8],
) -> std::io::Result<()> {
let _guard = self.serializer.write().unwrap();
let fs_res = self.fs_store.write(primary_namespace, secondary_namespace, key, buf);
let sqlite_res = self.sqlite_store.write(primary_namespace, secondary_namespace, key, buf);
let test_res = self.test_store.write(primary_namespace, secondary_namespace, key, buf);

assert!(self
.do_list(primary_namespace, secondary_namespace)
.unwrap()
.contains(&key.to_string()));

match fs_res {
Ok(()) => {
assert!(sqlite_res.is_ok());
assert!(test_res.is_ok());
Ok(())
}
Err(e) => {
assert!(sqlite_res.is_err());
assert!(test_res.is_err());
Err(e)
}
}
}

fn remove(
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
) -> std::io::Result<()> {
let _guard = self.serializer.write().unwrap();
let fs_res = self.fs_store.remove(primary_namespace, secondary_namespace, key, lazy);
let sqlite_res =
self.sqlite_store.remove(primary_namespace, secondary_namespace, key, lazy);
let test_res = self.test_store.remove(primary_namespace, secondary_namespace, key, lazy);

assert!(!self
.do_list(primary_namespace, secondary_namespace)
.unwrap()
.contains(&key.to_string()));

match fs_res {
Ok(()) => {
assert!(sqlite_res.is_ok());
assert!(test_res.is_ok());
Ok(())
}
Err(e) => {
assert!(sqlite_res.is_err());
assert!(test_res.is_err());
Err(e)
}
}
}

fn list(
&self, primary_namespace: &str, secondary_namespace: &str,
) -> std::io::Result<Vec<String>> {
let _guard = self.serializer.read().unwrap();
self.do_list(primary_namespace, secondary_namespace)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ mod payment_store;
mod peer_store;
mod sweep;
#[cfg(test)]
mod test;
mod test_utils;
mod tx_broadcaster;
mod types;
#[cfg(feature = "uniffi")]
Expand Down
3 changes: 1 addition & 2 deletions src/payment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::test::utils::TestLogger;
use lightning::util::test_utils::TestStore;
use lightning::util::test_utils::{TestLogger, TestStore};
use std::sync::Arc;

#[test]
Expand Down
4 changes: 1 addition & 3 deletions src/peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ impl_writeable_tlv_based!(PeerInfo, {
#[cfg(test)]
mod tests {
use super::*;
use crate::test::utils::TestLogger;

use lightning::util::test_utils::TestStore;
use lightning::util::test_utils::{TestLogger, TestStore};

use std::str::FromStr;
use std::sync::Arc;
Expand Down
Loading

0 comments on commit e9caf8f

Please sign in to comment.