From 2c5e0c0323eb46d4d8b857727196d433dba03a64 Mon Sep 17 00:00:00 2001 From: rishflab Date: Wed, 6 Oct 2021 18:58:30 +1100 Subject: [PATCH] Use sqlite in tests --- swap/tests/harness/mod.rs | 51 ++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index 1c04f86f..3cf54d09 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use std::time::Duration; use swap::asb::FixedRate; use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund}; -use swap::database::SledDatabase; +use swap::database::SqliteDatabase; use swap::env::{Config, GetConfig}; use swap::fs::ensure_directory_exists; use swap::network::swarm; @@ -25,7 +25,7 @@ use swap::protocol::bob::BobState; use swap::protocol::{alice, bob}; use swap::seed::Seed; use swap::{asb, bitcoin, cli, env, monero}; -use tempfile::tempdir; +use tempfile::{tempdir, NamedTempFile}; use testcontainers::clients::Cli; use testcontainers::{Container, Docker, RunArgs}; use tokio::sync::mpsc; @@ -83,7 +83,7 @@ where .parse() .expect("failed to parse Alice's address"); - let alice_db_path = tempdir().unwrap().into_path(); + let alice_db_path = NamedTempFile::new().unwrap().path().to_path_buf(); let (alice_handle, alice_swap_handle) = start_alice( &alice_seed, alice_db_path.clone(), @@ -111,7 +111,7 @@ where let bob_params = BobParams { seed: Seed::random().unwrap(), - db_path: tempdir().unwrap().path().to_path_buf(), + db_path: NamedTempFile::new().unwrap().path().to_path_buf(), bitcoin_wallet: bob_bitcoin_wallet.clone(), monero_wallet: bob_monero_wallet.clone(), alice_address: alice_listen_address.clone(), @@ -157,16 +157,13 @@ async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) { .await .unwrap(); - ( - monero, - Containers { - bitcoind_url, - bitcoind, - monerod_container, - monero_wallet_rpc_containers, - electrs, - }, - ) + (monero, Containers { + bitcoind_url, + bitcoind, + monerod_container, + monero_wallet_rpc_containers, + electrs, + }) } async fn init_bitcoind_container( @@ -226,7 +223,13 @@ async fn start_alice( bitcoin_wallet: Arc, monero_wallet: Arc, ) -> (AliceApplicationHandle, Receiver) { - let db = Arc::new(SledDatabase::open(db_path.as_path()).await.unwrap()); + if let Some(parent_dir) = db_path.parent() { + ensure_directory_exists(parent_dir).unwrap(); + } + if !&db_path.exists() { + tokio::fs::File::create(&db_path).await.unwrap(); + } + let db = Arc::new(SqliteDatabase::open(db_path.as_path()).await.unwrap()); let min_buy = bitcoin::Amount::from_sat(u64::MIN); let max_buy = bitcoin::Amount::from_sat(u64::MAX); @@ -406,7 +409,14 @@ struct BobParams { impl BobParams { pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, cli::EventLoop)> { let (event_loop, handle) = self.new_eventloop(swap_id).await?; - let db = Arc::new(SledDatabase::open(&self.db_path).await?); + + if let Some(parent_dir) = self.db_path.parent() { + ensure_directory_exists(parent_dir)?; + } + if !self.db_path.exists() { + tokio::fs::File::create(&self.db_path).await?; + } + let db = Arc::new(SqliteDatabase::open(&self.db_path).await?); let swap = bob::Swap::from_db( db, @@ -429,7 +439,14 @@ impl BobParams { let swap_id = Uuid::new_v4(); let (event_loop, handle) = self.new_eventloop(swap_id).await?; - let db = Arc::new(SledDatabase::open(&self.db_path).await?); + + if let Some(parent_dir) = self.db_path.parent() { + ensure_directory_exists(parent_dir)?; + } + if !self.db_path.exists() { + tokio::fs::File::create(&self.db_path).await?; + } + let db = Arc::new(SqliteDatabase::open(&self.db_path).await?); let swap = bob::Swap::new( db,