Skip to content

Commit 7a8a609

Browse files
committed
test: Wait until we receive atleast 2 pieces to verify bitfield serialization and deserialization works
1 parent a9163a7 commit 7a8a609

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

crates/libtortillas/src/torrent/actor.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -837,15 +837,19 @@ mod tests {
837837
.with_env_filter("libtortillas=trace,off")
838838
.pretty()
839839
.init();
840+
let metainfo = TorrentFile::parse(include_bytes!(
841+
"../../tests/torrents/big-buck-bunny.torrent"
842+
))
843+
.unwrap();
844+
let info_dict = match &metainfo {
845+
MetaInfo::Torrent(file) => file.info.clone(),
846+
_ => unreachable!(),
847+
};
840848

841-
// Test with a magnet URI, since magnet URIs don't come with an info dict
842-
let path = std::env::current_dir()
843-
.unwrap()
844-
.join("tests/magneturis/big-buck-bunny.txt");
845-
let contents = tokio::fs::read_to_string(path).await.unwrap();
849+
let info_hash = info_dict.hash().unwrap();
846850

847-
let metainfo = MagnetUri::parse(contents).unwrap();
848-
let info_hash = metainfo.info_hash().unwrap();
851+
let piece_path = std::env::temp_dir().join("tortillas");
852+
let file_path = piece_path.join("files");
849853

850854
let peer_id = PeerId::default();
851855

@@ -854,8 +858,6 @@ mod tests {
854858
UtpSocket::new_udp(SocketAddr::from_str("0.0.0.0:0").expect("Failed to parse"))
855859
.await
856860
.unwrap();
857-
let piece_path = std::env::temp_dir().join("tortillas");
858-
let file_path = piece_path.join("files");
859861

860862
let actor = TorrentActor::spawn(TorrentActorArgs {
861863
peer_id,
@@ -873,9 +875,17 @@ mod tests {
873875

874876
assert!(torrent.poll_ready().await.is_ok());
875877

876-
torrent.start().await.unwrap();
877-
878-
sleep(Duration::from_millis(1000)).await; // Sleep for a second
878+
loop {
879+
let bitfield = match actor.ask(TorrentRequest::Bitfield).await.unwrap() {
880+
TorrentResponse::Bitfield(bitfield) => bitfield,
881+
_ => unreachable!(),
882+
};
883+
// Wait until we have at least 2 pieces
884+
if bitfield.count_ones() > 2 {
885+
break;
886+
}
887+
sleep(Duration::from_millis(100)).await;
888+
}
879889

880890
let export = torrent.export().await;
881891

@@ -887,13 +897,24 @@ mod tests {
887897

888898
// Test serialization
889899
use serde_json::{from_str, to_string};
890-
let export = to_string(&export).unwrap();
891-
trace!("Export: {}", export);
892-
let export: TorrentExport = from_str(&export).unwrap();
893-
assert_eq!(export.info_hash, info_hash);
900+
901+
let export_str = to_string(&export).unwrap();
902+
903+
let from_export: TorrentExport = from_str(&export_str).unwrap();
904+
assert_eq!(export.info_hash, from_export.info_hash);
905+
assert_eq!(export.state, from_export.state);
906+
assert_eq!(export.auto_start, from_export.auto_start);
907+
assert_eq!(export.sufficient_peers, from_export.sufficient_peers);
908+
assert_eq!(export.output_path, from_export.output_path);
909+
assert_eq!(export.bitfield, from_export.bitfield);
910+
894911
assert!(
895912
export.info_dict.is_some(),
896913
"Torrent shouldn't have started without info dict"
897914
);
915+
916+
trace!("Export: {export_str}");
917+
918+
actor.stop_gracefully().await.unwrap();
898919
}
899920
}

0 commit comments

Comments
 (0)