Skip to content

Commit

Permalink
tests: add test for downloading existing collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Aug 2, 2024
1 parent 90258c5 commit ccf15d4
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions iroh/src/client/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,8 @@ mod tests {
use super::*;

use anyhow::Context as _;
use iroh_blobs::hashseq::HashSeq;
use iroh_net::NodeId;
use rand::RngCore;
use tokio::io::AsyncWriteExt;

Expand Down Expand Up @@ -1355,4 +1357,81 @@ mod tests {

Ok(())
}

/// Download a existing collection. Check that things succeed and no download is performed.
#[tokio::test]
async fn test_blob_get_existing_collection() -> Result<()> {
let _guard = iroh_test::logging::setup();

let node = crate::node::Node::memory().spawn().await?;
// We use a nonexisting node id because we just want to check that this succeeds without
// hitting the network.
let node_id = NodeId::from_bytes(&[0u8; 32])?;
let client = node.client();

let mut collection = Collection::default();
let mut tags = Vec::new();
let mut size = 0;
for value in ["f", "fo", "foo"] {
let import_outcome = client.blobs().add_bytes(value).await.context("add bytes")?;
collection.push(value.to_string(), import_outcome.hash);
tags.push(import_outcome.tag);
size += import_outcome.size;
}

let (hash, _tag) = client
.blobs()
.create_collection(collection, SetTagOption::Auto, tags)
.await?;

// load the hashseq and collection header manually to calculate our expected size
let hashseq_bytes = client.blobs().read_to_bytes(hash).await?;
size += hashseq_bytes.len() as u64;
let hashseq = HashSeq::try_from(hashseq_bytes)?;
let collection_header_bytes = client
.blobs()
.read_to_bytes(hashseq.into_iter().next().expect("header to exist"))
.await?;
size += collection_header_bytes.len() as u64;

// Direct
let res = client
.blobs()
.download_with_opts(
hash,
DownloadOptions {
format: BlobFormat::HashSeq,
nodes: vec![node_id.into()],
tag: SetTagOption::Auto,
mode: DownloadMode::Direct,
},
)
.await?
.await
.context("direct (download)")?;

assert_eq!(res.local_size, size);
assert_eq!(res.downloaded_size, 0);

// Queued
let res = client
.blobs()
.download_with_opts(
hash,
DownloadOptions {
format: BlobFormat::HashSeq,
nodes: vec![node_id.into()],
tag: SetTagOption::Auto,
mode: DownloadMode::Queued,
},
)
.await?
.await
.context("queued")?;

assert_eq!(res.local_size, size);
assert_eq!(res.downloaded_size, 0);

Ok(())
}
}

0 comments on commit ccf15d4

Please sign in to comment.