diff --git a/Cargo.toml b/Cargo.toml index 2cf1320..e594fa3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,20 +40,23 @@ thiserror = "1.0.50" tokio = { version = "1.34.0", features = ["macros", "rt", "sync"] } tokio-stream = "0.1.14" tracing = "0.1.40" -uniffi = { version = "0.26.1", features = ["cli"], optional = true } +tracing-subscriber = {version = "0.3.18", features = ["env-filter"] } +uniffi = { version = "0.28.1", features = ["cli", "tokio"], optional = true } [dependencies.hypercore] -version = "0.13.0" +default-features = false +features = ["sparse", "tokio"] +#version = "0.13.0" +path = "../core" [build-dependencies] prost-build = "0.12.1" -uniffi = { version = "0.26.1", features = ["build"], optional = true} +uniffi = { version = "0.28.1", features = ["build"], optional = true} [dev-dependencies] tracing-subscriber = "0.3.18" tokio = { version = "1.34.0", features = ["rt-multi-thread"] } async-recursion = "1.0.5" -random-access-memory = "3.0.0" once_cell = "1.19.0" tempfile = "3.10.0" serde_json = "1.0.114" diff --git a/src/ffi.rs b/src/ffi.rs index fdda5d1..8919db7 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -109,11 +109,25 @@ async fn hyperbee_from_storage_dir(path_to_storage_dir: &str) -> Result, } +/// initialize logging +#[uniffi::export] +pub fn init_env_logs() { + tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) // Reads `RUST_LOG` environment variable + .init(); +} + #[uniffi::export] impl Prefixed { /// Get the value associated with the provided key plus this [`Prefixed`]'s instance's `prefix` diff --git a/src/hb.rs b/src/hb.rs index 09db227..09033e8 100644 --- a/src/hb.rs +++ b/src/hb.rs @@ -133,11 +133,13 @@ impl Hyperbee { pub async fn from_storage_dir>( path_to_storage_dir: T, ) -> Result { + dbg!(tokio::runtime::Handle::try_current()); Self::from_tree(Tree::from_storage_dir(path_to_storage_dir).await?) } /// Create a [`Hyperbee`] in RAM pub async fn from_ram() -> Result { + dbg!(tokio::runtime::Handle::try_current()); Self::from_tree(Tree::from_ram().await?) } diff --git a/src/tree.rs b/src/tree.rs index 696cd28..51e8c08 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -148,18 +148,29 @@ impl Tree { pub async fn from_storage_dir>( path_to_storage_dir: T, ) -> Result { + dbg!(tokio::runtime::Handle::try_current()); + dbg!(); + dbg!(); + dbg!(); let p: PathBuf = path_to_storage_dir.as_ref().to_owned(); + dbg!(); let storage = Storage::new_disk(&p, false).await?; + dbg!(); + dbg!(tokio::runtime::Handle::current()); let hc = Arc::new(Mutex::new(HypercoreBuilder::new(storage).build().await?)); + dbg!(); let blocks = BlocksBuilder::default().core(hc).build()?; + dbg!(); Self::from_blocks(blocks) } pub async fn from_ram() -> Result { + dbg!(tokio::runtime::Handle::try_current()); let hc = Arc::new(Mutex::new( HypercoreBuilder::new(Storage::new_memory().await?) .build() .await?, )); + dbg!(tokio::runtime::Handle::try_current()); let blocks = BlocksBuilder::default().core(hc).build()?; Self::from_blocks(blocks) } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 63d9839..cce6bf2 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -88,6 +88,7 @@ pub fn run_code( ); let script_path = working_dir.path().join(script_file_name); let script_file = File::create(&script_path)?; + println!("{code}"); write!(&script_file, "{}", &code)?; let working_dir_path = working_dir.path().display().to_string(); @@ -107,7 +108,10 @@ pub fn run_code( } let script_path_str = script_path.display().to_string(); let cmd = build_command(&working_dir_path, &script_path_str); - check_cmd_output(Command::new("sh").arg("-c").arg(cmd).output()?) + let o = Command::new("sh").arg("-c").arg(cmd).output()?; + println!("OUT\n{}", String::from_utf8_lossy(&o.stdout)); + println!("ERR\n{}", String::from_utf8_lossy(&o.stderr)); + check_cmd_output(o) } pub fn run_make_from_with(dir: &str, arg: &str) -> Result { diff --git a/tests/common/python/foo.py b/tests/common/python/foo.py new file mode 100644 index 0000000..eef4b9f --- /dev/null +++ b/tests/common/python/foo.py @@ -0,0 +1,29 @@ +import asyncio +from target.hyperbee import * +from target.hyperbee import uniffi_set_event_loop + + +async def main(): + import json + init_env_logs() + try_current_rt() + print(asyncio.get_running_loop()) + uniffi_set_event_loop(asyncio.get_running_loop()) + #hb = await hyperbee_from_storage_dir('./hbdir') + hb = await hyperbee_from_ram(); + try_current_rt() + res = await hb.get(b'hello') + assert(res is None) + + try_current_rt() + hb = await hyperbee_from_storage_dir('./hbdir') + hb = await hyperbee_from_ram(); + res = await hb.get(b'hello') + + res = await hb.put(b'skipval', None) + res = await hb.get(b'skipval') + assert(res.value is None) + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/tests/python.rs b/tests/python.rs index ef159a3..44c8da4 100644 --- a/tests/python.rs +++ b/tests/python.rs @@ -79,6 +79,8 @@ async fn optionals() -> Result<()> { " async def main(): import json + init_env_logs() + try_current_rt() hb = await hyperbee_from_storage_dir('{}') res = await hb.get(b'hello') assert(res.value is None)