From 56ea23c2a369b54b2419054d0551a04c324d8b49 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 8 Jul 2021 11:50:37 +1000 Subject: [PATCH] Assert log output for `determine_btc_to_swap` --- swap/src/bin/swap.rs | 144 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 124 insertions(+), 20 deletions(-) diff --git a/swap/src/bin/swap.rs b/swap/src/bin/swap.rs index f14c5ef9..1ac76ea7 100644 --- a/swap/src/bin/swap.rs +++ b/swap/src/bin/swap.rs @@ -483,18 +483,17 @@ where #[cfg(test)] mod tests { - use std::sync::Mutex; - + use super::*; + use crate::determine_btc_to_swap; use ::bitcoin::Amount; + use std::io; + use std::sync::Mutex; use tracing::subscriber; - - use crate::determine_btc_to_swap; - - use super::*; + 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>>>, + } + + impl io::Write for CapturingWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.captured.lock().unwrap().write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } } struct MaxGiveable {