trying to make tests work with new metadata

This commit is contained in:
Igor Katson 2024-08-14 10:25:05 +01:00
parent 55649e181a
commit c196c11860
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
6 changed files with 45 additions and 29 deletions

View file

@ -44,6 +44,10 @@ install: build-release
$(MAKE) sign-release
install target/release/rqbit "$(HOME)/bin/"
@PHONY: test
test:
ulimit -n unlimited && cargo test
@PHONY: release-macos-universal
release-macos-universal:
cargo build --target aarch64-apple-darwin --profile release-github

View file

@ -1,3 +1,4 @@
use bytes::Bytes;
use librqbit::{
storage::{StorageFactory, StorageFactoryExt, TorrentStorage},
SessionOptions,
@ -79,9 +80,9 @@ async fn main() -> anyhow::Result<()> {
.await?;
let handle = s
.add_torrent(
librqbit::AddTorrent::TorrentFileBytes(
include_bytes!("../resources/ubuntu-21.04-live-server-amd64.iso.torrent").into(),
),
librqbit::AddTorrent::TorrentFileBytes(Bytes::from_static(include_bytes!(
"../resources/ubuntu-21.04-live-server-amd64.iso.torrent"
))),
Some(librqbit::AddTorrentOptions {
storage_factory: Some(CustomStorageFactory::default().boxed()),
paused: false,

View file

@ -6,6 +6,7 @@ use std::path::Path;
use anyhow::Context;
use bencode::bencode_serialize_to_writer;
use buffers::ByteBufOwned;
use bytes::Bytes;
use librqbit_core::torrent_metainfo::{TorrentMetaV1File, TorrentMetaV1Info, TorrentMetaV1Owned};
use librqbit_core::Id20;
use sha1w::{ISha1, Sha1};
@ -185,10 +186,10 @@ impl CreateTorrentResult {
self.meta.info_hash
}
pub fn as_bytes(&self) -> anyhow::Result<Vec<u8>> {
pub fn as_bytes(&self) -> anyhow::Result<Bytes> {
let mut b = Vec::new();
bencode_serialize_to_writer(&self.meta, &mut b).context("error serializing torrent")?;
Ok(b)
Ok(b.into())
}
}

View file

@ -421,7 +421,7 @@ pub fn read_local_file_including_stdin(filename: &str) -> anyhow::Result<Vec<u8>
pub enum AddTorrent<'a> {
Url(Cow<'a, str>),
TorrentFileBytes(Cow<'a, [u8]>),
TorrentFileBytes(Bytes),
TorrentInfo(Box<TorrentMetaV1Owned>, Bytes),
}
@ -439,7 +439,7 @@ impl<'a> AddTorrent<'a> {
Self::Url(url.into())
}
pub fn from_bytes(bytes: impl Into<Cow<'a, [u8]>>) -> Self {
pub fn from_bytes(bytes: impl Into<Bytes>) -> Self {
Self::TorrentFileBytes(bytes.into())
}
@ -448,13 +448,13 @@ impl<'a> AddTorrent<'a> {
pub fn from_local_filename(filename: &str) -> anyhow::Result<Self> {
let file = read_local_file_including_stdin(filename)
.with_context(|| format!("error reading local file {filename:?}"))?;
Ok(Self::TorrentFileBytes(Cow::Owned(file)))
Ok(Self::TorrentFileBytes(file.into()))
}
pub fn into_bytes(self) -> Vec<u8> {
pub fn into_bytes(self) -> Bytes {
match self {
Self::Url(s) => s.into_owned().into_bytes(),
Self::TorrentFileBytes(b) => b.into_owned(),
Self::Url(s) => s.into_owned().into_bytes().into(),
Self::TorrentFileBytes(b) => b,
Self::TorrentInfo(..) => unimplemented!(),
}
}
@ -1012,16 +1012,22 @@ impl Session {
announce_port,
opts.force_tracker_interval,
)?;
let initial_peers_stream = opts
.initial_peers
.clone()
.and_then(|v| if v.is_empty() { None } else { Some(v) })
.map(futures::stream::iter);
let peer_rx = merge_two_optional_streams(peer_rx, initial_peers_stream);
let peer_rx = match peer_rx {
Some(peer_rx) => peer_rx,
None => bail!("can't find peers: DHT disabled and no trackers in magnet"),
None => bail!("can't find peers: DHT is disabled, no trackers in magnet, and no initial peers provided"),
};
debug!(?info_hash, "querying DHT");
match read_metainfo_from_peer_receiver(
self.peer_id,
info_hash,
opts.initial_peers.clone().unwrap_or_default(),
Default::default(),
peer_rx,
Some(self.merge_peer_opts(opts.peer_opts)),
self.connector.clone(),
@ -1066,18 +1072,12 @@ impl Session {
url
)
}
AddTorrent::TorrentFileBytes(bytes) => {
let bytes = match bytes {
Cow::Borrowed(b) => ::bytes::Bytes::copy_from_slice(b),
Cow::Owned(v) => ::bytes::Bytes::from(v),
};
(
torrent_from_bytes(&bytes)
.map(|t| t.clone_to_owned(Some(&bytes)))
.context("error decoding torrent")?,
ByteBufOwned(bytes),
)
}
AddTorrent::TorrentFileBytes(bytes) => (
torrent_from_bytes(&bytes)
.map(|t| t.clone_to_owned(Some(&bytes)))
.context("error decoding torrent")?,
ByteBufOwned(bytes),
),
AddTorrent::TorrentInfo(t, bytes) => (*t, bytes.into()),
};

View file

@ -1,11 +1,11 @@
use std::{
borrow::Cow,
net::{Ipv4Addr, SocketAddr},
time::Duration,
};
use anyhow::bail;
use futures::{stream::FuturesUnordered, StreamExt};
use librqbit_core::magnet::Magnet;
use rand::Rng;
use tokio::{
spawn,
@ -84,7 +84,7 @@ async fn test_e2e_download() {
let handle = session
.add_torrent(
crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes)),
crate::AddTorrent::TorrentFileBytes(torrent_file_bytes),
Some(AddTorrentOptions {
overwrite: true,
output_folder: Some(tempdir.to_str().unwrap().to_owned()),
@ -139,6 +139,8 @@ async fn test_e2e_download() {
.and_then(|v| v.parse().ok())
.unwrap_or(1usize);
let magnet = Magnet::from_id20(torrent_file.info_hash(), Vec::new()).to_string();
// 3. Start a client with the initial peers, and download the file.
for _ in 0..client_iters {
let outdir = tempfile::TempDir::with_prefix("rqbit_e2e_client").unwrap();
@ -163,7 +165,7 @@ async fn test_e2e_download() {
let (id, handle) = {
let r = session
.add_torrent(
crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes.clone())),
crate::AddTorrent::Url((&magnet).into()),
Some(AddTorrentOptions {
initial_peers: Some(peers.clone()),
// only_files: Some(vec![0]),
@ -235,7 +237,7 @@ async fn test_e2e_download() {
// 4. After downloading, recheck its integrity.
let handle = session
.add_torrent(
crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes.clone())),
crate::AddTorrent::TorrentFileBytes(torrent_file_bytes.clone()),
Some(AddTorrentOptions {
paused: true,
overwrite: true,

View file

@ -20,6 +20,14 @@ impl Magnet {
self.id32
}
pub fn from_id20(id20: Id20, trackers: Vec<String>) -> Self {
Self {
id20: Some(id20),
id32: None,
trackers,
}
}
/// Parse a magnet link.
pub fn parse(url: &str) -> anyhow::Result<Magnet> {
let url = url::Url::parse(url).context("magnet link must be a valid URL")?;