mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-12-23 22:49:41 -05:00
Make capture_logs
available for the whole crate
This commit is contained in:
parent
cacfc50fb2
commit
2eb7fab0c3
@ -484,14 +484,13 @@ mod tests {
|
||||
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;
|
||||
use swap::tracing_ext::capture_logs;
|
||||
use tracing::level_filters::LevelFilter;
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_balance_and_transfers_less_than_max_swaps_max_giveable() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.0009).unwrap(),
|
||||
@ -526,7 +525,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_balance_and_transfers_more_then_swaps_max_quantity_from_quote() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.1).unwrap(),
|
||||
@ -561,7 +560,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_initial_balance_below_max_quantity_swaps_max_givable() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::from_btc(0.0049).unwrap(),
|
||||
Amount::from_btc(99.9).unwrap(),
|
||||
@ -594,7 +593,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_initial_balance_above_max_quantity_swaps_max_quantity() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::from_btc(0.1).unwrap(),
|
||||
Amount::from_btc(99.9).unwrap(),
|
||||
@ -627,7 +626,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_initial_balance_then_min_wait_for_sufficient_deposit() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.01).unwrap(),
|
||||
@ -662,7 +661,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_balance_less_then_min_wait_for_sufficient_deposit() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::from_btc(0.0001).unwrap(),
|
||||
Amount::from_btc(0.01).unwrap(),
|
||||
@ -697,7 +696,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_no_initial_balance_and_transfers_less_than_min_keep_waiting() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::from_btc(0.01).unwrap(),
|
||||
@ -737,7 +736,7 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn given_longer_delay_until_deposit_should_not_spam_user() {
|
||||
let writer = capture_logs();
|
||||
let writer = capture_logs(LevelFilter::INFO);
|
||||
let givable = Arc::new(Mutex::new(MaxGiveable::new(vec![
|
||||
Amount::ZERO,
|
||||
Amount::ZERO,
|
||||
@ -779,63 +778,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
amounts: Vec<Amount>,
|
||||
call_counter: usize,
|
||||
|
@ -29,5 +29,6 @@ pub mod network;
|
||||
pub mod protocol;
|
||||
pub mod seed;
|
||||
pub mod tor;
|
||||
pub mod tracing_ext;
|
||||
|
||||
mod monero_ext;
|
||||
|
65
swap/src/tracing_ext.rs
Normal file
65
swap/src/tracing_ext.rs
Normal file
@ -0,0 +1,65 @@
|
||||
#![allow(clippy::unwrap_used)] // This is only meant to be used in tests.
|
||||
|
||||
use std::io;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tracing::subscriber;
|
||||
use tracing_subscriber::filter::LevelFilter;
|
||||
use tracing_subscriber::fmt::MakeWriter;
|
||||
|
||||
/// 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.
|
||||
pub fn capture_logs(min_level: LevelFilter) -> MakeCapturingWriter {
|
||||
let make_writer = MakeCapturingWriter::default();
|
||||
|
||||
let guard = subscriber::set_default(
|
||||
tracing_subscriber::fmt()
|
||||
.with_ansi(false)
|
||||
.without_time()
|
||||
.with_writer(make_writer.clone())
|
||||
.with_env_filter(format!("{}", min_level))
|
||||
.finish(),
|
||||
);
|
||||
// don't clean up guard we stay initialized
|
||||
std::mem::forget(guard);
|
||||
|
||||
make_writer
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct MakeCapturingWriter {
|
||||
writer: CapturingWriter,
|
||||
}
|
||||
|
||||
impl MakeCapturingWriter {
|
||||
pub 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)]
|
||||
pub 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(())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user