Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Aug 10, 2023
1 parent e715c0b commit c7170a5
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 144 deletions.
31 changes: 31 additions & 0 deletions iroh-sync/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,37 @@ pub struct GetFilter {
pub key: KeyFilter,
}

impl GetFilter {
/// Create a new get filter. Defaults to latest entries for all keys and authors.
pub fn new() -> Self {
Self {
latest: true,
author: None,
key: KeyFilter::All,
}
}
/// Filter by exact key match.
pub fn with_key(mut self, key: Vec<u8>) -> Self {
self.key = KeyFilter::Key(key);
self
}
/// Filter by prefix key match.
pub fn with_prefix(mut self, prefix: Vec<u8>) -> Self {
self.key = KeyFilter::Prefix(prefix);
self
}
/// Filter by author.
pub fn with_author(mut self, author: AuthorId) -> Self {
self.author = Some(author);
self
}
/// Include not only latest entries but also all historical entries.
pub fn with_history(mut self) -> Self {
self.latest = false;
self
}
}

/// Filter the keys in a namespace
#[derive(Debug, Serialize, Deserialize)]
pub enum KeyFilter {
Expand Down
7 changes: 3 additions & 4 deletions iroh/examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use indicatif::HumanBytes;
use iroh::{database::flat::writable::WritableFileDatabase, node::Node};
use iroh::node::Node;
use iroh_bytes::util::runtime;
use iroh_sync::{
store::{GetFilter, KeyFilter},
Expand All @@ -9,11 +9,10 @@ use tokio_stream::StreamExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let dir = tempfile::tempdir()?;
let rt = runtime::Handle::from_currrent(1)?;
let db = WritableFileDatabase::new(dir.path().into()).await?;
let db = iroh::baomap::mem::Store::new(rt.clone());
let store = iroh_sync::store::memory::Store::default();
let node = Node::builder(db.db().clone(), store, dir.path().into())
let node = Node::builder(db.clone(), store)
.runtime(&rt)
.spawn()
.await?;
Expand Down
5 changes: 4 additions & 1 deletion iroh/examples/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ async fn main() -> anyhow::Result<()> {
// create a new iroh runtime with 1 worker thread, reusing the existing tokio runtime
let rt = runtime::Handle::from_currrent(1)?;

// create an in-memory doc store for iroh sync (not used here)
let doc_store = iroh_sync::store::memory::Store::default();

// create a new node
// we must configure the iroh collection parser so the node understands iroh collections
let node = iroh::node::Node::builder(db)
let node = iroh::node::Node::builder(db, doc_store)
.collection_parser(IrohCollectionParser)
.runtime(&rt)
.spawn()
Expand Down
7 changes: 6 additions & 1 deletion iroh/examples/hello-world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ async fn main() -> anyhow::Result<()> {
setup_logging();
// create a new, empty in memory database
let mut db = iroh::baomap::readonly_mem::Store::default();
// create an in-memory doc store (not used in the example)
let doc_store = iroh_sync::store::memory::Store::default();
// create a new iroh runtime with 1 worker thread, reusing the existing tokio runtime
let rt = runtime::Handle::from_currrent(1)?;
// add some data and remember the hash
let hash = db.insert(b"Hello, world!");
// create a new node
let node = iroh::node::Node::builder(db).runtime(&rt).spawn().await?;
let node = iroh::node::Node::builder(db, doc_store)
.runtime(&rt)
.spawn()
.await?;
// create a ticket
let ticket = node.ticket(hash).await?.with_recursive(false);
// print some info about the node
Expand Down
3 changes: 2 additions & 1 deletion iroh/examples/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ async fn run(db: impl Store) -> anyhow::Result<()> {

// create a new node
// we must configure the iroh collection parser so the node understands iroh collections
let node = iroh::node::Node::builder(db)
let doc_store = iroh_sync::store::memory::Store::default();
let node = iroh::node::Node::builder(db, doc_store)
.keypair(keypair)
.collection_parser(IrohCollectionParser)
.runtime(&rt)
Expand Down
2 changes: 1 addition & 1 deletion iroh/src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ impl Doc {
)
}
}
println!("");
println!("");
}
}
Doc::List { old, prefix } => {
Expand Down
11 changes: 7 additions & 4 deletions iroh/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ const ENDPOINT_WAIT: Duration = Duration::from_secs(5);
pub fn init_metrics_collection() {
use iroh_metrics::core::Metric;

iroh_metrics::core::Core::init(|reg, metrics| {
iroh_metrics::core::Core::try_init(|reg, metrics| {
metrics.insert(crate::metrics::Metrics::new(reg));
metrics.insert(iroh_sync::metrics::Metrics::new(reg));
metrics.insert(iroh_net::metrics::MagicsockMetrics::new(reg));
metrics.insert(iroh_net::metrics::NetcheckMetrics::new(reg));
metrics.insert(iroh_net::metrics::PortmapMetrics::new(reg));
metrics.insert(iroh_net::metrics::DerpMetrics::new(reg));
});
})
.ok();
}

/// Builder for the [`Node`].
Expand Down Expand Up @@ -1352,8 +1353,9 @@ mod tests {
async fn test_ticket_multiple_addrs() {
let rt = test_runtime();
let (db, hashes) = crate::baomap::readonly_mem::Store::new([("test", b"hello")]);
let doc_store = iroh_sync::store::memory::Store::default();
let hash = hashes["test"].into();
let node = Node::builder(db)
let node = Node::builder(db, doc_store)
.bind_addr((Ipv4Addr::UNSPECIFIED, 0).into())
.runtime(&rt)
.spawn()
Expand All @@ -1370,7 +1372,8 @@ mod tests {
async fn test_node_add_collection_event() -> Result<()> {
let rt = runtime::Handle::from_currrent(1)?;
let db = crate::baomap::mem::Store::new(rt);
let node = Node::builder(db)
let doc_store = iroh_sync::store::memory::Store::default();
let node = Node::builder(db, doc_store)
.bind_addr((Ipv4Addr::UNSPECIFIED, 0).into())
.runtime(&test_runtime())
.spawn()
Expand Down
3 changes: 2 additions & 1 deletion iroh/tests/provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ async fn test_custom_collection_parser() {
let collection_bytes = postcard::to_allocvec(&collection).unwrap();
let collection_hash = db.insert(collection_bytes.clone());
let addr = "127.0.0.1:0".parse().unwrap();
let node = Node::builder(db)
let doc_store = iroh_sync::store::memory::Store::default();
let node = Node::builder(db, doc_store)
.collection_parser(CollectionsAreJustLinks)
.bind_addr(addr)
.runtime(&rt)
Expand Down
Loading

0 comments on commit c7170a5

Please sign in to comment.