mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-31 15:20:15 +00:00
Assert log output for determine_btc_to_swap
This commit is contained in:
parent
a347dd8b97
commit
56ea23c2a3
@ -483,18 +483,17 @@ where
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Mutex;
|
||||
|
||||
use ::bitcoin::Amount;
|
||||
use tracing::subscriber;
|
||||
|
||||
use crate::determine_btc_to_swap;
|
||||
|
||||
use super::*;
|
||||
use crate::determine_btc_to_swap;
|
||||
use ::bitcoin::Amount;
|
||||
use std::io;
|
||||
use std::sync::Mutex;
|
||||
use tracing::subscriber;
|
||||
use tracing_subscriber::fmt::MakeWriter;
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_balance_and_transfers_less_than_max_swaps_max_giveable() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.0009).unwrap(),
|
||||
@ -517,11 +516,19 @@ mod tests {
|
||||
let expected_amount = Amount::from_btc(0.0009).unwrap();
|
||||
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
||||
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
||||
INFO swap: Received Bitcoin new_balance=0.00100000 BTC max_giveable=0.00090000 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_balance_and_transfers_more_then_swaps_max_quantity_from_quote() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.1).unwrap(),
|
||||
@ -544,12 +551,19 @@ mod tests {
|
||||
let expected_amount = Amount::from_btc(0.01).unwrap();
|
||||
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
||||
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
||||
INFO swap: Received Bitcoin new_balance=0.10010000 BTC max_giveable=0.10000000 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_initial_balance_below_max_quantity_swaps_max_givable() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::from_btc(0.0049).unwrap(),
|
||||
Amount::from_btc(99.9).unwrap(),
|
||||
@ -572,12 +586,17 @@ mod tests {
|
||||
let expected_amount = Amount::from_btc(0.0049).unwrap();
|
||||
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
||||
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_initial_balance_above_max_quantity_swaps_max_quantity() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::from_btc(0.1).unwrap(),
|
||||
Amount::from_btc(99.9).unwrap(),
|
||||
@ -600,12 +619,17 @@ mod tests {
|
||||
let expected_amount = Amount::from_btc(0.01).unwrap();
|
||||
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
||||
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.00000000 BTC maximum_amount=0.01000000 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_initial_balance_then_min_wait_for_sufficient_deposit() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.01).unwrap(),
|
||||
@ -628,12 +652,19 @@ mod tests {
|
||||
let expected_amount = Amount::from_btc(0.01).unwrap();
|
||||
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
||||
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_balance_less_then_min_wait_for_sufficient_deposit() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::from_btc(0.0001).unwrap(),
|
||||
Amount::from_btc(0.01).unwrap(),
|
||||
@ -656,12 +687,19 @@ mod tests {
|
||||
let expected_amount = Amount::from_btc(0.01).unwrap();
|
||||
let expected_fees = Amount::from_btc(0.0001).unwrap();
|
||||
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees))
|
||||
assert_eq!((amount, fees), (expected_amount, expected_fees));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00010000 BTC minimum_amount=0.01000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_initial_balance_and_transfers_less_than_min_keep_waiting() {
|
||||
let _guard = subscriber::set_default(tracing_subscriber::fmt().with_test_writer().finish());
|
||||
let writer = capture_logs();
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.01).unwrap(),
|
||||
@ -687,7 +725,73 @@ mod tests {
|
||||
.await
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(error, tokio::time::error::Elapsed { .. }))
|
||||
assert!(matches!(error, tokio::time::error::Elapsed { .. }));
|
||||
assert_eq!(
|
||||
writer.captured(),
|
||||
r" INFO swap: Received quote: 1 XMR ~ price=0.00100000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.00000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
INFO swap: Received Bitcoin new_balance=0.01010000 BTC max_giveable=0.01000000 BTC
|
||||
INFO swap: Deposited amount is less than `min_quantity`
|
||||
INFO swap: Waiting for Bitcoin deposit deposit_address=1PdfytjS7C8wwd9Lq5o4x9aXA2YRqaCpH6 max_giveable=0.01000000 BTC minimum_amount=0.10000000 BTC maximum_amount=184467440737.09551615 BTC
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
/// Setup tracing with a capturing writer, allowing assertions on the log
|
||||
/// messages.
|
||||
///
|
||||
/// Time and ANSI are disabled to make the output more predictable and
|
||||
/// readable.
|
||||
fn capture_logs() -> MakeCapturingWriter {
|
||||
let make_writer = MakeCapturingWriter::default();
|
||||
|
||||
let guard = subscriber::set_default(
|
||||
tracing_subscriber::fmt()
|
||||
.with_ansi(false)
|
||||
.without_time()
|
||||
.with_writer(make_writer.clone())
|
||||
.finish(),
|
||||
);
|
||||
// don't clean up guard we stay initialized
|
||||
std::mem::forget(guard);
|
||||
|
||||
make_writer
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
struct MakeCapturingWriter {
|
||||
writer: CapturingWriter,
|
||||
}
|
||||
|
||||
impl MakeCapturingWriter {
|
||||
fn captured(&self) -> String {
|
||||
let captured = &self.writer.captured;
|
||||
let cursor = captured.lock().unwrap();
|
||||
String::from_utf8(cursor.clone().into_inner()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl MakeWriter for MakeCapturingWriter {
|
||||
type Writer = CapturingWriter;
|
||||
|
||||
fn make_writer(&self) -> Self::Writer {
|
||||
self.writer.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
struct CapturingWriter {
|
||||
captured: Arc<Mutex<io::Cursor<Vec<u8>>>>,
|
||||
}
|
||||
|
||||
impl io::Write for CapturingWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
self.captured.lock().unwrap().write(buf)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
struct MaxGiveable {
|
||||
|
Loading…
Reference in New Issue
Block a user