#![cfg(all(feature = "python", feature = "streaming"))]
use std::path::{Path, PathBuf};
use fidius_core::from_value;
use fidius_core::python_descriptor::PythonInterfaceDescriptor;
use fidius_host::PluginHost;
use futures::StreamExt;
use serde::Serialize;
#[derive(Serialize)]
struct Cfg {
base: u64,
}
fn ticker_descriptor() -> &'static PythonInterfaceDescriptor {
&test_plugin_smoke::__fidius_Ticker::Ticker_PYTHON_DESCRIPTOR
}
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}
fn copy_dir(src: &Path, dst: &Path) {
std::fs::create_dir_all(dst).unwrap();
for entry in std::fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let from = entry.path();
let to = dst.join(entry.file_name());
if from.is_dir() {
copy_dir(&from, &to);
} else {
std::fs::copy(&from, &to).unwrap();
}
}
}
fn stage(tmp: &tempfile::TempDir) -> PathBuf {
let plugins_root = tmp.path().to_path_buf();
let dest = plugins_root.join("py-configured-ticker");
copy_dir(
&repo_root().join("tests/test-plugin-py-configured-stream"),
&dest,
);
copy_dir(
&repo_root().join("python/fidius"),
&dest.join("vendor").join("fidius"),
);
let py = dest.join("configured_ticker.py");
let src = std::fs::read_to_string(&py).unwrap();
let injected = src.replace(
"__HASH_PLACEHOLDER__",
&format!("0x{:016X}", ticker_descriptor().interface_hash),
);
std::fs::write(&py, injected).unwrap();
plugins_root
}
#[tokio::test]
async fn configured_python_streaming_reads_bound_config() {
let tmp = tempfile::TempDir::new().unwrap();
let plugins = stage(&tmp);
let host = PluginHost::builder().search_path(&plugins).build().unwrap();
let handle = host
.load_python_configured(
"py-configured-ticker",
ticker_descriptor(),
&Cfg { base: 100 },
)
.expect("load_python_configured");
let idx = test_plugin_smoke::__fidius_Ticker::METHOD_TICK;
let mut stream = handle
.call_streaming::<_, u64>(idx, &(3u32,))
.await
.unwrap();
let mut got = Vec::new();
while let Some(item) = stream.next().await {
got.push(from_value::<u64>(item.unwrap()).unwrap());
}
assert_eq!(got, vec![100, 101, 102]);
}