Skip to content

Commit

Permalink
Fix last remaining test
Browse files Browse the repository at this point in the history
  • Loading branch information
rklaehn committed Nov 12, 2024
1 parent a2a8c64 commit 7fc22a0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 119 deletions.
99 changes: 30 additions & 69 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use anyhow::{Context, Result};
use futures_util::TryStreamExt;
use iroh_blobs::{
Expand All @@ -6,6 +8,7 @@ use iroh_blobs::{
};
use iroh_docs::store::Query;
use rand::RngCore;
use testresult::TestResult;
use tokio::io::AsyncWriteExt;
use util::Node;

Expand Down Expand Up @@ -153,128 +156,86 @@ async fn test_default_author_memory() -> Result<()> {
Ok(())
}

#[cfg(feature = "fs-store")]
#[tokio::test]
async fn test_default_author_persist() -> Result<()> {
use crate::util::path::IrohPaths;

async fn test_default_author_persist() -> TestResult<()> {
let _guard = iroh_test::logging::setup();

let iroh_root_dir = tempfile::TempDir::new().unwrap();
let iroh_root = iroh_root_dir.path();

// check that the default author exists and cannot be deleted.
let default_author = {
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await
.unwrap();
let author = iroh.authors().default().await.unwrap();
assert!(iroh.authors().export(author).await.unwrap().is_some());
assert!(iroh.authors().delete(author).await.is_err());
let iroh = Node::persistent(iroh_root).spawn().await.unwrap();
let author = iroh.docs().author_default().await.unwrap();
assert!(iroh.docs().author_export(author).await.unwrap().is_some());
assert!(iroh.docs().author_delete(author).await.is_err());
iroh.shutdown().await.unwrap();
author
};

// check that the default author is persisted across restarts.
{
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await
.unwrap();
let author = iroh.authors().default().await.unwrap();
let iroh = Node::persistent(iroh_root).spawn().await.unwrap();
let author = iroh.docs().author_default().await.unwrap();
assert_eq!(author, default_author);
assert!(iroh.authors().export(author).await.unwrap().is_some());
assert!(iroh.authors().delete(author).await.is_err());
assert!(iroh.docs().author_export(author).await.unwrap().is_some());
assert!(iroh.docs().author_delete(author).await.is_err());
iroh.shutdown().await.unwrap();
};

// check that a new default author is created if the default author file is deleted
// manually.
let default_author = {
tokio::fs::remove_file(IrohPaths::DefaultAuthor.with_root(iroh_root))
tokio::fs::remove_file(iroh_root.join("default-author"))
.await
.unwrap();
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await
.unwrap();
let author = iroh.authors().default().await.unwrap();
let iroh = Node::persistent(iroh_root).spawn().await.unwrap();
let author = iroh.docs().author_default().await.unwrap();
assert!(author != default_author);
assert!(iroh.authors().export(author).await.unwrap().is_some());
assert!(iroh.authors().delete(author).await.is_err());
assert!(iroh.docs().author_export(author).await.unwrap().is_some());
assert!(iroh.docs().author_delete(author).await.is_err());
iroh.shutdown().await.unwrap();
author
};

// check that the node fails to start if the default author is missing from the docs store.
{
let mut docs_store =
iroh_docs::store::fs::Store::persistent(IrohPaths::DocsDatabase.with_root(iroh_root))
.unwrap();
iroh_docs::store::fs::Store::persistent(iroh_root.join("docs.redb")).unwrap();
docs_store.delete_author(default_author).unwrap();
docs_store.flush().unwrap();
drop(docs_store);
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await;
let iroh = Node::persistent(iroh_root).spawn().await;
assert!(iroh.is_err());

// somehow the blob store is not shutdown correctly (yet?) on macos.
// so we give it some time until we find a proper fix.
#[cfg(target_os = "macos")]
tokio::time::sleep(Duration::from_secs(1)).await;

tokio::fs::remove_file(IrohPaths::DefaultAuthor.with_root(iroh_root))
tokio::fs::remove_file(iroh_root.join("default-author"))
.await
.unwrap();
drop(iroh);
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await;
assert!(iroh.is_ok());
let iroh = Node::persistent(iroh_root).spawn().await;
if let Err(cause) = iroh.as_ref() {
panic!("failed to start node: {:?}", cause);
}
iroh.unwrap().shutdown().await.unwrap();
}

// check that the default author can be set manually and is persisted.
let default_author = {
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await
.unwrap();
let author = iroh.authors().create().await.unwrap();
iroh.authors().set_default(author).await.unwrap();
assert_eq!(iroh.authors().default().await.unwrap(), author);
let iroh = Node::persistent(iroh_root).spawn().await.unwrap();
let author = iroh.docs().author_create().await.unwrap();
iroh.docs().author_set_default(author).await.unwrap();
assert_eq!(iroh.docs().author_default().await.unwrap(), author);
iroh.shutdown().await.unwrap();
author
};
{
let iroh = Node::persistent(iroh_root)
.await
.unwrap()
.enable_docs()
.spawn()
.await
.unwrap();
assert_eq!(iroh.authors().default().await.unwrap(), default_author);
let iroh = Node::persistent(iroh_root).spawn().await.unwrap();
assert_eq!(iroh.docs().author_default().await.unwrap(), default_author);
iroh.shutdown().await.unwrap();
}

Expand Down
59 changes: 39 additions & 20 deletions tests/gc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "rpc")]
use std::{
io::{Cursor, Write},
path::PathBuf,
time::Duration,
};

Expand All @@ -14,7 +15,7 @@ use iroh_blobs::{
BlobFormat, HashAndFormat, IROH_BLOCK_SIZE,
};
use rand::RngCore;
use util::{Builder, Node};
use util::Node;

mod util;

Expand All @@ -39,15 +40,34 @@ pub fn simulate_remote(data: &[u8]) -> (blake3::Hash, Cursor<Bytes>) {
}

/// Wrap a bao store in a node that has gc enabled.
async fn wrap_in_node<S>(
bao_store: S,
async fn mem_node(
gc_period: Duration,
) -> (Node<S>, async_channel::Receiver<()>)
where
S: iroh_blobs::store::Store,
{
) -> (
Node<iroh_blobs::store::mem::Store>,
async_channel::Receiver<()>,
) {
let (gc_send, gc_recv) = async_channel::unbounded();
let node = Builder::new(bao_store)
let node = Node::memory()
.gc_interval(Some(gc_period))
.register_gc_done_cb(Box::new(move || {
gc_send.send_blocking(()).ok();
}))
.spawn()
.await
.unwrap();
(node, gc_recv)
}

/// Wrap a bao store in a node that has gc enabled.
async fn persistent_node(
path: PathBuf,
gc_period: Duration,
) -> (
Node<iroh_blobs::store::fs::Store>,
async_channel::Receiver<()>,
) {
let (gc_send, gc_recv) = async_channel::unbounded();
let node = Node::persistent(path)
.gc_interval(Some(gc_period))
.register_gc_done_cb(Box::new(move || {
gc_send.send_blocking(()).ok();
Expand All @@ -63,9 +83,9 @@ async fn gc_test_node() -> (
iroh_blobs::store::mem::Store,
async_channel::Receiver<()>,
) {
let bao_store = iroh_blobs::store::mem::Store::new();
let (node, gc_recv) = wrap_in_node(bao_store.clone(), Duration::from_millis(500)).await;
(node, bao_store, gc_recv)
let (node, gc_recv) = mem_node(Duration::from_millis(500)).await;
let store = node.blob_store().clone();
(node, store, gc_recv)
}

async fn step(evs: &async_channel::Receiver<()>) {
Expand Down Expand Up @@ -230,8 +250,8 @@ mod file {
async fn redb_doc_import_stress() -> Result<()> {
let _ = tracing_subscriber::fmt::try_init();
let dir = testdir!();
let bao_store = iroh_blobs::store::fs::Store::load(dir.join("store")).await?;
let (node, _) = wrap_in_node(bao_store.clone(), Duration::from_secs(10)).await;
let (node, _) = persistent_node(dir.join("store"), Duration::from_secs(10)).await;
let bao_store = node.blob_store().clone();
let client = node.client();
let doc = client.docs().create().await?;
let author = client.docs().author_create().await?;
Expand Down Expand Up @@ -272,9 +292,8 @@ mod file {
let dir = testdir!();
let path = data_path(dir.clone());
let outboard_path = outboard_path(dir.clone());

let bao_store = iroh_blobs::store::fs::Store::load(dir.clone()).await?;
let (node, evs) = wrap_in_node(bao_store.clone(), Duration::from_millis(100)).await;
let (node, evs) = persistent_node(dir.clone(), Duration::from_millis(100)).await;
let bao_store = node.blob_store().clone();
let data1 = create_test_data(10000000);
let tt1 = bao_store
.import_bytes(data1.clone(), BlobFormat::Raw)
Expand Down Expand Up @@ -434,8 +453,8 @@ mod file {
let path = data_path(dir.clone());
let outboard_path = outboard_path(dir.clone());

let bao_store = iroh_blobs::store::fs::Store::load(dir.clone()).await?;
let (node, evs) = wrap_in_node(bao_store.clone(), Duration::from_millis(10)).await;
let (node, evs) = persistent_node(dir.clone(), Duration::from_millis(10)).await;
let bao_store = node.blob_store().clone();

let data1: Bytes = create_test_data(10000000);
let (_entry, tt1) = simulate_download_partial(&bao_store, data1.clone()).await?;
Expand Down Expand Up @@ -465,8 +484,8 @@ mod file {
let _ = tracing_subscriber::fmt::try_init();
let dir = testdir!();

let bao_store = iroh_blobs::store::fs::Store::load(dir.clone()).await?;
let (node, evs) = wrap_in_node(bao_store.clone(), Duration::from_secs(1)).await;
let (node, evs) = persistent_node(dir.clone(), Duration::from_secs(1)).await;
let bao_store = node.blob_store().clone();

let mut deleted = Vec::new();
let mut live = Vec::new();
Expand Down
2 changes: 0 additions & 2 deletions tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ async fn sync_restart_node() -> Result<()> {
let secret_key_1 = SecretKey::generate_with_rng(&mut rng);

let node1 = Node::persistent(&node1_dir)
.await?
.secret_key(secret_key_1.clone())
.insecure_skip_relay_cert_verify(true)
.relay_mode(RelayMode::Custom(relay_map.clone()))
Expand Down Expand Up @@ -662,7 +661,6 @@ async fn sync_restart_node() -> Result<()> {

info!(me = id1.fmt_short(), "node1 respawn");
let node1 = Node::persistent(&node1_dir)
.await?
.secret_key(secret_key_1.clone())
.insecure_skip_relay_cert_verify(true)
.relay_mode(RelayMode::Custom(relay_map.clone()))
Expand Down
Loading

0 comments on commit 7fc22a0

Please sign in to comment.