Use sqlite in tests

This commit is contained in:
rishflab 2021-10-06 18:58:30 +11:00
parent cdfc8419ad
commit 2c5e0c0323

View File

@ -16,7 +16,7 @@ use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use swap::asb::FixedRate; use swap::asb::FixedRate;
use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund}; use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund};
use swap::database::SledDatabase; use swap::database::SqliteDatabase;
use swap::env::{Config, GetConfig}; use swap::env::{Config, GetConfig};
use swap::fs::ensure_directory_exists; use swap::fs::ensure_directory_exists;
use swap::network::swarm; use swap::network::swarm;
@ -25,7 +25,7 @@ use swap::protocol::bob::BobState;
use swap::protocol::{alice, bob}; use swap::protocol::{alice, bob};
use swap::seed::Seed; use swap::seed::Seed;
use swap::{asb, bitcoin, cli, env, monero}; use swap::{asb, bitcoin, cli, env, monero};
use tempfile::tempdir; use tempfile::{tempdir, NamedTempFile};
use testcontainers::clients::Cli; use testcontainers::clients::Cli;
use testcontainers::{Container, Docker, RunArgs}; use testcontainers::{Container, Docker, RunArgs};
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -83,7 +83,7 @@ where
.parse() .parse()
.expect("failed to parse Alice's address"); .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( let (alice_handle, alice_swap_handle) = start_alice(
&alice_seed, &alice_seed,
alice_db_path.clone(), alice_db_path.clone(),
@ -111,7 +111,7 @@ where
let bob_params = BobParams { let bob_params = BobParams {
seed: Seed::random().unwrap(), 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(), bitcoin_wallet: bob_bitcoin_wallet.clone(),
monero_wallet: bob_monero_wallet.clone(), monero_wallet: bob_monero_wallet.clone(),
alice_address: alice_listen_address.clone(), alice_address: alice_listen_address.clone(),
@ -157,16 +157,13 @@ async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
.await .await
.unwrap(); .unwrap();
( (monero, Containers {
monero, bitcoind_url,
Containers { bitcoind,
bitcoind_url, monerod_container,
bitcoind, monero_wallet_rpc_containers,
monerod_container, electrs,
monero_wallet_rpc_containers, })
electrs,
},
)
} }
async fn init_bitcoind_container( async fn init_bitcoind_container(
@ -226,7 +223,13 @@ async fn start_alice(
bitcoin_wallet: Arc<bitcoin::Wallet>, bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>, monero_wallet: Arc<monero::Wallet>,
) -> (AliceApplicationHandle, Receiver<alice::Swap>) { ) -> (AliceApplicationHandle, Receiver<alice::Swap>) {
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 min_buy = bitcoin::Amount::from_sat(u64::MIN);
let max_buy = bitcoin::Amount::from_sat(u64::MAX); let max_buy = bitcoin::Amount::from_sat(u64::MAX);
@ -406,7 +409,14 @@ struct BobParams {
impl BobParams { impl BobParams {
pub async fn new_swap_from_db(&self, swap_id: Uuid) -> Result<(bob::Swap, cli::EventLoop)> { 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 (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( let swap = bob::Swap::from_db(
db, db,
@ -429,7 +439,14 @@ impl BobParams {
let swap_id = Uuid::new_v4(); let swap_id = Uuid::new_v4();
let (event_loop, handle) = self.new_eventloop(swap_id).await?; 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( let swap = bob::Swap::new(
db, db,