Skip to content

Commit

Permalink
more test work
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 10, 2024
1 parent 9e0393f commit 7be6e23
Show file tree
Hide file tree
Showing 41 changed files with 274 additions and 87 deletions.
5 changes: 4 additions & 1 deletion examples/simple_torrent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ enum Setup {
}

pub fn tracing_stdout_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);
let builder = tracing_subscriber::fmt()
.with_max_level(filter)
.with_ansi(true)
.with_writer(std::io::stdout);

builder.pretty().with_file(true).init();

Expand Down
4 changes: 2 additions & 2 deletions packages/dht/examples/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl HandshakerTrait for SimpleHandshaker {
fn metadata(&mut self, (): Self::MetadataEnvelope) {}
}

fn tracing_stdout_init(filter: LevelFilter) {
fn tracing_stderr_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);

builder.pretty().with_file(true).init();
Expand All @@ -62,7 +62,7 @@ fn tracing_stdout_init(filter: LevelFilter) {
#[tokio::main]
async fn main() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

let hash = InfoHash::from_bytes(b"My Unique Info Hash");
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/examples/add_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::level_filters::LevelFilter;

static INIT: Once = Once::new();

fn tracing_stdout_init(filter: LevelFilter) {
fn tracing_stderr_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);

builder.pretty().with_file(true).init();
Expand All @@ -21,7 +21,7 @@ fn tracing_stdout_init(filter: LevelFilter) {
#[tokio::main]
async fn main() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

tracing::info!("Utility For Allocating Disk Space For A Torrent File");
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/src/disk/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ where
}

fn start_send(self: std::pin::Pin<&mut Self>, item: IDiskMessage) -> Result<(), Self::Error> {
tracing::info!("Starting Send For DiskManagerSink With IDiskMessage");
tracing::trace!("Starting Send For DiskManagerSink With IDiskMessage");
self.task_set
.lock()
.unwrap()
Expand Down Expand Up @@ -269,7 +269,7 @@ impl Stream for DiskManagerStream {
type Item = Result<ODiskMessage, ()>;

fn poll_next(mut self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
tracing::info!("Polling DiskManagerStream For ODiskMessage");
tracing::trace!("Polling DiskManagerStream For ODiskMessage");

match self.recv.poll_next_unpin(cx) {
Poll::Ready(Some(msg)) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/tests/add_torrent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use common::{
random_buffer, runtime_loop_with_timeout, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT,
random_buffer, runtime_loop_with_timeout, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT,
INIT,
};
use disk::{DiskManagerBuilder, FileSystem as _, IDiskMessage, ODiskMessage};
Expand All @@ -13,7 +13,7 @@ mod common;
#[tokio::test]
async fn positive_add_torrent() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

// Create some "files" as random bytes
Expand Down
7 changes: 5 additions & 2 deletions packages/disk/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ pub const DEFAULT_TIMEOUT: Duration = Duration::from_millis(500);
pub static INIT: Once = Once::new();

#[allow(dead_code)]
pub fn tracing_stdout_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);
pub fn tracing_stderr_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt()
.with_max_level(filter)
.with_ansi(true)
.with_writer(std::io::stderr);

builder.pretty().with_file(true).init();

Expand Down
12 changes: 6 additions & 6 deletions packages/disk/tests/complete_torrent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use common::{
random_buffer, runtime_loop_with_timeout, send_block, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor,
random_buffer, runtime_loop_with_timeout, send_block, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor,
DEFAULT_TIMEOUT, INIT,
};
use disk::{DiskManagerBuilder, IDiskMessage, ODiskMessage};
Expand All @@ -17,7 +17,7 @@ mod common;
#[tokio::test]
async fn positive_complete_torrent() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::DEBUG);
tracing_stderr_init(LevelFilter::INFO);
});

let mut tasks = JoinSet::new();
Expand Down Expand Up @@ -60,7 +60,7 @@ async fn positive_complete_torrent() {
b
};

tracing::warn!("send two blocks that are known to be good, then one bad block");
tracing::debug!("send two blocks that are known to be good, then one bad block");

let send_one_bad_and_two_good = {
let mut send = send.clone();
Expand Down Expand Up @@ -90,7 +90,7 @@ async fn positive_complete_torrent() {

tasks.spawn(send_one_bad_and_two_good);

tracing::warn!("verify that piece 0 is bad, but piece 1 and 2 are good");
tracing::debug!("verify that piece 0 is bad, but piece 1 and 2 are good");

let (recv, piece_zero_good, piece_one_good, piece_two_good) = runtime_loop_with_timeout(
DEFAULT_TIMEOUT,
Expand Down Expand Up @@ -142,7 +142,7 @@ async fn positive_complete_torrent() {
assert!(tasks.is_empty(), "all the tasks should have finished now");
}

tracing::warn!("resend piece 0 with good blocks");
tracing::debug!("resend piece 0 with good blocks");

let resend_with_good_blocks = {
let mut send = send.clone();
Expand All @@ -158,7 +158,7 @@ async fn positive_complete_torrent() {

tasks.spawn(resend_with_good_blocks);

tracing::warn!("verify that piece 0 is now good");
tracing::debug!("verify that piece 0 is now good");

let piece_zero_good = runtime_loop_with_timeout(
DEFAULT_TIMEOUT,
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/tests/disk_manager_send_backpressure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common::{random_buffer, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT, INIT};
use common::{random_buffer, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT, INIT};
use disk::{DiskManagerBuilder, IDiskMessage};
use futures::{FutureExt, SinkExt as _, StreamExt as _};
use metainfo::{Metainfo, MetainfoBuilder, PieceLength};
Expand All @@ -9,7 +9,7 @@ mod common;
#[tokio::test]
async fn positive_disk_manager_send_backpressure() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

// Create some "files" as random bytes
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/tests/load_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytes::BytesMut;
use common::{random_buffer, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor, INIT};
use common::{random_buffer, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor, INIT};
use disk::{Block, BlockMetadata, BlockMut, DiskManagerBuilder, IDiskMessage, ODiskMessage};
use futures::{SinkExt as _, StreamExt as _};
use metainfo::{Metainfo, MetainfoBuilder, PieceLength};
Expand All @@ -11,7 +11,7 @@ mod common;
#[tokio::test]
async fn positive_load_block() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

// Create some "files" as random bytes
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/tests/process_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytes::BytesMut;
use common::{
random_buffer, runtime_loop_with_timeout, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT,
random_buffer, runtime_loop_with_timeout, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT,
INIT,
};
use disk::{Block, BlockMetadata, DiskManagerBuilder, FileSystem, IDiskMessage, ODiskMessage};
Expand All @@ -13,7 +13,7 @@ mod common;
#[tokio::test]
async fn positive_process_block() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

// Create some "files" as random bytes
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/tests/remove_torrent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bytes::BytesMut;
use common::{
random_buffer, runtime_loop_with_timeout, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT,
random_buffer, runtime_loop_with_timeout, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor, DEFAULT_TIMEOUT,
INIT,
};
use disk::{Block, BlockMetadata, DiskManagerBuilder, IDiskMessage, ODiskMessage};
Expand All @@ -13,7 +13,7 @@ mod common;
#[tokio::test]
async fn positive_remove_torrent() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

// Create some "files" as random bytes
Expand Down
4 changes: 2 additions & 2 deletions packages/disk/tests/resume_torrent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use common::{
random_buffer, runtime_loop_with_timeout, send_block, tracing_stdout_init, InMemoryFileSystem, MultiFileDirectAccessor,
random_buffer, runtime_loop_with_timeout, send_block, tracing_stderr_init, InMemoryFileSystem, MultiFileDirectAccessor,
DEFAULT_TIMEOUT, INIT,
};
use disk::{DiskManagerBuilder, IDiskMessage, ODiskMessage};
Expand All @@ -13,7 +13,7 @@ mod common;
#[tokio::test]
async fn positive_complete_torrent() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::INFO);
tracing_stderr_init(LevelFilter::INFO);
});

// Create some "files" as random bytes
Expand Down
19 changes: 11 additions & 8 deletions packages/handshake/src/bittorrent/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,11 @@ mod tests {

pub static INIT: Once = Once::new();

pub fn tracing_stdout_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);
pub fn tracing_stderr_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt()
.with_max_level(filter)
.with_ansi(true)
.with_writer(std::io::stderr);

builder.pretty().with_file(true).init();

Expand All @@ -333,7 +336,7 @@ mod tests {
#[tokio::test]
async fn write_and_read_into_async_buffer() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let mut v = vec![0; 100];
Expand All @@ -360,7 +363,7 @@ mod tests {
#[tokio::test]
async fn positive_write_handshake_message() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let message = HandshakeMessage::from_parts(Protocol::BitTorrent, any_extensions(), any_info_hash(), any_peer_id());
Expand All @@ -380,7 +383,7 @@ mod tests {
#[tokio::test]
async fn positive_write_multiple_handshake_messages() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let message_one = HandshakeMessage::from_parts(Protocol::BitTorrent, any_extensions(), any_info_hash(), any_peer_id());
Expand Down Expand Up @@ -408,7 +411,7 @@ mod tests {
#[tokio::test]
async fn positive_read_handshake_message() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let exp_message = HandshakeMessage::from_parts(Protocol::BitTorrent, any_extensions(), any_info_hash(), any_peer_id());
Expand Down Expand Up @@ -436,7 +439,7 @@ mod tests {
#[tokio::test]
async fn positive_read_byte_after_handshake() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let exp_message = HandshakeMessage::from_parts(Protocol::BitTorrent, any_extensions(), any_info_hash(), any_peer_id());
Expand Down Expand Up @@ -465,7 +468,7 @@ mod tests {
#[tokio::test]
async fn positive_read_bytes_after_handshake() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let exp_message = HandshakeMessage::from_parts(Protocol::BitTorrent, any_extensions(), any_info_hash(), any_peer_id());
Expand Down
7 changes: 5 additions & 2 deletions packages/handshake/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ pub enum TimeoutResult {
}

#[allow(dead_code)]
pub fn tracing_stdout_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt().with_max_level(filter).with_ansi(true);
pub fn tracing_stderr_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt()
.with_max_level(filter)
.with_ansi(true)
.with_writer(std::io::stderr);

builder.pretty().with_file(true).init();

Expand Down
4 changes: 2 additions & 2 deletions packages/handshake/tests/test_byte_after_handshake.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::{Read, Write};
use std::net::TcpStream;

use common::{tracing_stdout_init, INIT};
use common::{tracing_stderr_init, INIT};
use futures::stream::StreamExt;
use handshake::transports::TcpTransport;
use handshake::{DiscoveryInfo, HandshakerBuilder};
Expand All @@ -14,7 +14,7 @@ mod common;
#[tokio::test]
async fn positive_recover_bytes() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let mut handshaker_one_addr = "127.0.0.1:0".parse().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions packages/handshake/tests/test_bytes_after_handshake.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::{Read, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};

use common::{tracing_stdout_init, INIT};
use common::{tracing_stderr_init, INIT};
use futures::stream::StreamExt;
use handshake::transports::TcpTransport;
use handshake::{DiscoveryInfo, HandshakerBuilder};
Expand All @@ -14,7 +14,7 @@ mod common;
#[tokio::test]
async fn positive_recover_bytes() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let mut handshaker_one_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0);
Expand Down
4 changes: 2 additions & 2 deletions packages/handshake/tests/test_connect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use common::{tracing_stdout_init, INIT};
use common::{tracing_stderr_init, INIT};
use futures::future::try_join;
use futures::sink::SinkExt;
use futures::stream::StreamExt;
Expand All @@ -13,7 +13,7 @@ mod common;
#[tokio::test]
async fn positive_connect() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let handshaker_one_addr = "127.0.0.1:0".parse().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions packages/handshake/tests/test_filter_allow_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::any::Any;
use std::net::SocketAddr;
use std::time::Duration;

use common::{tracing_stdout_init, INIT};
use common::{tracing_stderr_init, INIT};
use futures::sink::SinkExt;
use futures::stream::{self, StreamExt};
use futures::FutureExt as _;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl HandshakeFilter for FilterAllowAll {
#[tokio::test]
async fn test_filter_all() {
INIT.call_once(|| {
tracing_stdout_init(LevelFilter::TRACE);
tracing_stderr_init(LevelFilter::INFO);
});

let handshaker_one_addr = "127.0.0.1:0".parse().unwrap();
Expand Down Expand Up @@ -102,7 +102,7 @@ async fn test_filter_all() {
let res = tokio::time::timeout(Duration::from_millis(50), get_handshake).await;

if let Ok(item) = res {
tracing::info!("handshake was produced: {item:?}");
tracing::debug!("handshake was produced: {item:?}");
} else {
panic!("expected item, but got a timeout!");
}
Expand Down
Loading

0 comments on commit 7be6e23

Please sign in to comment.