Skip to content

Commit

Permalink
chore: improve doctests & axum test a bit (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Feb 26, 2024
1 parent 206f6d8 commit 74eb5dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ libipld = "0.16"
libipld-core = "0.16"
serde_ipld_dagcbor = "0.4"
wnfs-common = { version = "0.2.0" }
wnfs-unixfs-file = { version = "0.2.0" }

# See https://doc.rust-lang.org/cargo/reference/profiles.html for more info.
[profile.release.package.car-mirror-wasm]
Expand Down
7 changes: 4 additions & 3 deletions car-mirror-reqwest/integration/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ async fn main() -> Result<()> {
.route("/dag/push/:cid", post(car_mirror_push))
.with_state(ServerState::new());

let listener = tokio::net::TcpListener::bind("0.0.0.0:3344").await?;
let listener = tokio::net::TcpListener::bind("0.0.0.0:0").await?;
let port = listener.local_addr()?.port();
tokio::spawn(axum::serve(listener, app).into_future());

// You can issue requests from your client like so:
Expand All @@ -40,13 +41,13 @@ async fn main() -> Result<()> {

let client = Client::new();
client
.post(format!("http://localhost:3344/dag/push/{root}"))
.post(format!("http://localhost:{port}/dag/push/{root}"))
.run_car_mirror_push(root, &store, &NoCache) // rounds of push protocol
.await?;

let store = MemoryBlockStore::new(); // clear out data
client
.get(format!("http://localhost:3344/dag/pull/{root}"))
.get(format!("http://localhost:{port}/dag/pull/{root}"))
.run_car_mirror_pull(root, config, &store, &NoCache) // rounds of pull protocol
.await?;

Expand Down
49 changes: 13 additions & 36 deletions car-mirror/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,19 @@ pub mod messages;
/// let client_cache = InMemoryCache::new(100_000);
/// let server_cache = InMemoryCache::new(100_000);
///
/// let file_bytes = async_std::fs::read("../Cargo.lock").await?;
/// // At time of writing, Cargo.lock is 86KB, so this ends u ~8MB
/// let file_bytes = async_std::fs::read("../Cargo.lock").await?.repeat(100);
///
/// // Load some data onto the client
/// let root = FileBuilder::new()
/// .content_bytes(file_bytes.clone())
/// .fixed_chunker(1024) // Generate lots of small blocks
/// .degree(4)
/// .build()?
/// .store(&client_store)
/// .await?;
///
/// // The server may already have a subset of the data
/// FileBuilder::new()
/// .content_bytes(file_bytes[0..10_000].to_vec())
/// .fixed_chunker(1024) // Generate lots of small blocks
/// .degree(4)
/// .content_bytes(file_bytes[0..1_000_000].to_vec())
/// .build()?
/// .store(&server_store)
/// .await?;
Expand Down Expand Up @@ -107,20 +104,16 @@ pub mod messages;
/// # let client_cache = InMemoryCache::new(100_000);
/// # let server_cache = InMemoryCache::new(100_000);
/// #
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?;
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?.repeat(100);
/// #
/// # let root = FileBuilder::new()
/// # .content_bytes(file_bytes.clone())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .build()?
/// # .store(&client_store)
/// # .await?;
/// #
/// # FileBuilder::new()
/// # .content_bytes(file_bytes[0..10_000].to_vec())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .content_bytes(file_bytes[0..1_000_000].to_vec())
/// # .build()?
/// # .store(&server_store)
/// # .await?;
Expand Down Expand Up @@ -182,20 +175,16 @@ pub mod messages;
/// # let client_cache = InMemoryCache::new(100_000);
/// # let server_cache = InMemoryCache::new(100_000);
/// #
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?;
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?.repeat(100);
/// #
/// # let root = FileBuilder::new()
/// # .content_bytes(file_bytes.clone())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .build()?
/// # .store(&client_store)
/// # .await?;
/// #
/// # FileBuilder::new()
/// # .content_bytes(file_bytes[0..10_000].to_vec())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .content_bytes(file_bytes[0..1_000_000].to_vec())
/// # .build()?
/// # .store(&server_store)
/// # .await?;
Expand Down Expand Up @@ -260,22 +249,18 @@ pub mod pull;
/// let client_cache = InMemoryCache::new(100_000);
/// let server_cache = InMemoryCache::new(100_000);
///
/// let file_bytes = async_std::fs::read("../Cargo.lock").await?;
/// let file_bytes = async_std::fs::read("../Cargo.lock").await?.repeat(100);
///
/// // Load some data onto the client
/// let root = FileBuilder::new()
/// .content_bytes(file_bytes.clone())
/// .fixed_chunker(1024) // Generate lots of small blocks
/// .degree(4)
/// .build()?
/// .store(&client_store)
/// .await?;
///
/// // The server may already have a subset of the data
/// FileBuilder::new()
/// .content_bytes(file_bytes[0..10_000].to_vec())
/// .fixed_chunker(1024) // Generate lots of small blocks
/// .degree(4)
/// .content_bytes(file_bytes[0..1_000_000].to_vec())
/// .build()?
/// .store(&server_store)
/// .await?;
Expand Down Expand Up @@ -303,20 +288,16 @@ pub mod pull;
/// # let client_cache = InMemoryCache::new(100_000);
/// # let server_cache = InMemoryCache::new(100_000);
/// #
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?;
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?.repeat(100);
/// #
/// # let root = FileBuilder::new()
/// # .content_bytes(file_bytes.clone())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .build()?
/// # .store(&client_store)
/// # .await?;
/// #
/// # FileBuilder::new()
/// # .content_bytes(file_bytes[0..10_000].to_vec())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .content_bytes(file_bytes[0..1_000_000].to_vec())
/// # .build()?
/// # .store(&server_store)
/// # .await?;
Expand Down Expand Up @@ -378,20 +359,16 @@ pub mod pull;
/// # let client_cache = InMemoryCache::new(100_000);
/// # let server_cache = InMemoryCache::new(100_000);
/// #
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?;
/// # let file_bytes = async_std::fs::read("../Cargo.lock").await?.repeat(100);
/// #
/// # let root = FileBuilder::new()
/// # .content_bytes(file_bytes.clone())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .build()?
/// # .store(&client_store)
/// # .await?;
/// #
/// # FileBuilder::new()
/// # .content_bytes(file_bytes[0..10_000].to_vec())
/// # .fixed_chunker(1024) // Generate lots of small blocks
/// # .degree(4)
/// # .content_bytes(file_bytes[0..1_000_000].to_vec())
/// # .build()?
/// # .store(&server_store)
/// # .await?;
Expand Down

0 comments on commit 74eb5dc

Please sign in to comment.