trying to make tests work with new metadata
This commit is contained in:
parent
55649e181a
commit
c196c11860
6 changed files with 45 additions and 29 deletions
4
Makefile
4
Makefile
|
|
@ -44,6 +44,10 @@ install: build-release
|
||||||
$(MAKE) sign-release
|
$(MAKE) sign-release
|
||||||
install target/release/rqbit "$(HOME)/bin/"
|
install target/release/rqbit "$(HOME)/bin/"
|
||||||
|
|
||||||
|
@PHONY: test
|
||||||
|
test:
|
||||||
|
ulimit -n unlimited && cargo test
|
||||||
|
|
||||||
@PHONY: release-macos-universal
|
@PHONY: release-macos-universal
|
||||||
release-macos-universal:
|
release-macos-universal:
|
||||||
cargo build --target aarch64-apple-darwin --profile release-github
|
cargo build --target aarch64-apple-darwin --profile release-github
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
use bytes::Bytes;
|
||||||
use librqbit::{
|
use librqbit::{
|
||||||
storage::{StorageFactory, StorageFactoryExt, TorrentStorage},
|
storage::{StorageFactory, StorageFactoryExt, TorrentStorage},
|
||||||
SessionOptions,
|
SessionOptions,
|
||||||
|
|
@ -79,9 +80,9 @@ async fn main() -> anyhow::Result<()> {
|
||||||
.await?;
|
.await?;
|
||||||
let handle = s
|
let handle = s
|
||||||
.add_torrent(
|
.add_torrent(
|
||||||
librqbit::AddTorrent::TorrentFileBytes(
|
librqbit::AddTorrent::TorrentFileBytes(Bytes::from_static(include_bytes!(
|
||||||
include_bytes!("../resources/ubuntu-21.04-live-server-amd64.iso.torrent").into(),
|
"../resources/ubuntu-21.04-live-server-amd64.iso.torrent"
|
||||||
),
|
))),
|
||||||
Some(librqbit::AddTorrentOptions {
|
Some(librqbit::AddTorrentOptions {
|
||||||
storage_factory: Some(CustomStorageFactory::default().boxed()),
|
storage_factory: Some(CustomStorageFactory::default().boxed()),
|
||||||
paused: false,
|
paused: false,
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use std::path::Path;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use bencode::bencode_serialize_to_writer;
|
use bencode::bencode_serialize_to_writer;
|
||||||
use buffers::ByteBufOwned;
|
use buffers::ByteBufOwned;
|
||||||
|
use bytes::Bytes;
|
||||||
use librqbit_core::torrent_metainfo::{TorrentMetaV1File, TorrentMetaV1Info, TorrentMetaV1Owned};
|
use librqbit_core::torrent_metainfo::{TorrentMetaV1File, TorrentMetaV1Info, TorrentMetaV1Owned};
|
||||||
use librqbit_core::Id20;
|
use librqbit_core::Id20;
|
||||||
use sha1w::{ISha1, Sha1};
|
use sha1w::{ISha1, Sha1};
|
||||||
|
|
@ -185,10 +186,10 @@ impl CreateTorrentResult {
|
||||||
self.meta.info_hash
|
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();
|
let mut b = Vec::new();
|
||||||
bencode_serialize_to_writer(&self.meta, &mut b).context("error serializing torrent")?;
|
bencode_serialize_to_writer(&self.meta, &mut b).context("error serializing torrent")?;
|
||||||
Ok(b)
|
Ok(b.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -421,7 +421,7 @@ pub fn read_local_file_including_stdin(filename: &str) -> anyhow::Result<Vec<u8>
|
||||||
|
|
||||||
pub enum AddTorrent<'a> {
|
pub enum AddTorrent<'a> {
|
||||||
Url(Cow<'a, str>),
|
Url(Cow<'a, str>),
|
||||||
TorrentFileBytes(Cow<'a, [u8]>),
|
TorrentFileBytes(Bytes),
|
||||||
TorrentInfo(Box<TorrentMetaV1Owned>, Bytes),
|
TorrentInfo(Box<TorrentMetaV1Owned>, Bytes),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -439,7 +439,7 @@ impl<'a> AddTorrent<'a> {
|
||||||
Self::Url(url.into())
|
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())
|
Self::TorrentFileBytes(bytes.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -448,13 +448,13 @@ impl<'a> AddTorrent<'a> {
|
||||||
pub fn from_local_filename(filename: &str) -> anyhow::Result<Self> {
|
pub fn from_local_filename(filename: &str) -> anyhow::Result<Self> {
|
||||||
let file = read_local_file_including_stdin(filename)
|
let file = read_local_file_including_stdin(filename)
|
||||||
.with_context(|| format!("error reading local file {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 {
|
match self {
|
||||||
Self::Url(s) => s.into_owned().into_bytes(),
|
Self::Url(s) => s.into_owned().into_bytes().into(),
|
||||||
Self::TorrentFileBytes(b) => b.into_owned(),
|
Self::TorrentFileBytes(b) => b,
|
||||||
Self::TorrentInfo(..) => unimplemented!(),
|
Self::TorrentInfo(..) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1012,16 +1012,22 @@ impl Session {
|
||||||
announce_port,
|
announce_port,
|
||||||
opts.force_tracker_interval,
|
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 {
|
let peer_rx = match peer_rx {
|
||||||
Some(peer_rx) => 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");
|
debug!(?info_hash, "querying DHT");
|
||||||
match read_metainfo_from_peer_receiver(
|
match read_metainfo_from_peer_receiver(
|
||||||
self.peer_id,
|
self.peer_id,
|
||||||
info_hash,
|
info_hash,
|
||||||
opts.initial_peers.clone().unwrap_or_default(),
|
Default::default(),
|
||||||
peer_rx,
|
peer_rx,
|
||||||
Some(self.merge_peer_opts(opts.peer_opts)),
|
Some(self.merge_peer_opts(opts.peer_opts)),
|
||||||
self.connector.clone(),
|
self.connector.clone(),
|
||||||
|
|
@ -1066,18 +1072,12 @@ impl Session {
|
||||||
url
|
url
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
AddTorrent::TorrentFileBytes(bytes) => {
|
AddTorrent::TorrentFileBytes(bytes) => (
|
||||||
let bytes = match bytes {
|
torrent_from_bytes(&bytes)
|
||||||
Cow::Borrowed(b) => ::bytes::Bytes::copy_from_slice(b),
|
.map(|t| t.clone_to_owned(Some(&bytes)))
|
||||||
Cow::Owned(v) => ::bytes::Bytes::from(v),
|
.context("error decoding torrent")?,
|
||||||
};
|
ByteBufOwned(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()),
|
AddTorrent::TorrentInfo(t, bytes) => (*t, bytes.into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
|
||||||
net::{Ipv4Addr, SocketAddr},
|
net::{Ipv4Addr, SocketAddr},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::bail;
|
use anyhow::bail;
|
||||||
use futures::{stream::FuturesUnordered, StreamExt};
|
use futures::{stream::FuturesUnordered, StreamExt};
|
||||||
|
use librqbit_core::magnet::Magnet;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
spawn,
|
spawn,
|
||||||
|
|
@ -84,7 +84,7 @@ async fn test_e2e_download() {
|
||||||
|
|
||||||
let handle = session
|
let handle = session
|
||||||
.add_torrent(
|
.add_torrent(
|
||||||
crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes)),
|
crate::AddTorrent::TorrentFileBytes(torrent_file_bytes),
|
||||||
Some(AddTorrentOptions {
|
Some(AddTorrentOptions {
|
||||||
overwrite: true,
|
overwrite: true,
|
||||||
output_folder: Some(tempdir.to_str().unwrap().to_owned()),
|
output_folder: Some(tempdir.to_str().unwrap().to_owned()),
|
||||||
|
|
@ -139,6 +139,8 @@ async fn test_e2e_download() {
|
||||||
.and_then(|v| v.parse().ok())
|
.and_then(|v| v.parse().ok())
|
||||||
.unwrap_or(1usize);
|
.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.
|
// 3. Start a client with the initial peers, and download the file.
|
||||||
for _ in 0..client_iters {
|
for _ in 0..client_iters {
|
||||||
let outdir = tempfile::TempDir::with_prefix("rqbit_e2e_client").unwrap();
|
let outdir = tempfile::TempDir::with_prefix("rqbit_e2e_client").unwrap();
|
||||||
|
|
@ -163,7 +165,7 @@ async fn test_e2e_download() {
|
||||||
let (id, handle) = {
|
let (id, handle) = {
|
||||||
let r = session
|
let r = session
|
||||||
.add_torrent(
|
.add_torrent(
|
||||||
crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes.clone())),
|
crate::AddTorrent::Url((&magnet).into()),
|
||||||
Some(AddTorrentOptions {
|
Some(AddTorrentOptions {
|
||||||
initial_peers: Some(peers.clone()),
|
initial_peers: Some(peers.clone()),
|
||||||
// only_files: Some(vec![0]),
|
// only_files: Some(vec![0]),
|
||||||
|
|
@ -235,7 +237,7 @@ async fn test_e2e_download() {
|
||||||
// 4. After downloading, recheck its integrity.
|
// 4. After downloading, recheck its integrity.
|
||||||
let handle = session
|
let handle = session
|
||||||
.add_torrent(
|
.add_torrent(
|
||||||
crate::AddTorrent::TorrentFileBytes(Cow::Owned(torrent_file_bytes.clone())),
|
crate::AddTorrent::TorrentFileBytes(torrent_file_bytes.clone()),
|
||||||
Some(AddTorrentOptions {
|
Some(AddTorrentOptions {
|
||||||
paused: true,
|
paused: true,
|
||||||
overwrite: true,
|
overwrite: true,
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,14 @@ impl Magnet {
|
||||||
self.id32
|
self.id32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn from_id20(id20: Id20, trackers: Vec<String>) -> Self {
|
||||||
|
Self {
|
||||||
|
id20: Some(id20),
|
||||||
|
id32: None,
|
||||||
|
trackers,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse a magnet link.
|
/// Parse a magnet link.
|
||||||
pub fn parse(url: &str) -> anyhow::Result<Magnet> {
|
pub fn parse(url: &str) -> anyhow::Result<Magnet> {
|
||||||
let url = url::Url::parse(url).context("magnet link must be a valid URL")?;
|
let url = url::Url::parse(url).context("magnet link must be a valid URL")?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue