Rust插件库holochain_test_wasm_common的使用:用于Holochain框架WASM模块测试的通用工具库
Rust插件库holochain_test_wasm_common的使用:用于Holochain框架WASM模块测试的通用工具库
holochain_test_wasm_common是一个用于Wasm测试的通用代码库。
安装方法
在项目目录中运行以下Cargo命令:
cargo add holochain_test_wasm_common
或者在Cargo.toml中添加以下行:
holochain_test_wasm_common = "0.5.4"
基本信息
- 当前版本:0.5.4
- 发布时间:22天前
- 支持的Rust版本:2021 edition
- 开源许可证:Apache-2.0
- 库大小:1.91 KiB
开发团队
- holochain核心开发团队
- Eric Harris-Braun
- holochain自动化发布团队
示例代码
以下是内容中提供的完整示例:
// 在Cargo.toml中添加依赖项:
// [dependencies]
// holochain_test_wasm_common = "0.5.4"
use holochain_test_wasm_common::*;
// 示例测试函数
#[test]
fn test_wasm_operations() {
// 初始化测试环境
let mut test_env = TestEnvironment::new();
// 创建测试数据
let test_data = TestData::new("example_data".to_string());
// 执行WASM操作测试
let result = test_env.execute_wasm("example_wasm_module", test_data);
// 验证结果
assert!(result.is_ok());
assert_eq!(result.unwrap(), "expected_output");
// 清理测试环境
test_env.cleanup();
}
// 示例WASM模块模拟
struct ExampleWasmModule;
impl WasmModule for ExampleWasmModule {
fn execute(&self, input: TestData) -> Result<String, String> {
if input.data == "example_data" {
Ok("expected_output".to_string())
} else {
Err("Invalid input".to_string())
}
}
}
// 注册测试模块
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example_module() {
let module = ExampleWasmModule;
let test_data = TestData::new("example_data".to_string());
assert_eq!(module.execute(test_data).unwrap(), "expected_output");
}
}
完整示例代码
以下是一个更完整的示例,展示如何使用holochain_test_wasm_common进行更复杂的WASM测试:
// 在Cargo.toml中添加依赖项:
// [dependencies]
// holochain_test_wasm_common = "0.5.4"
use holochain_test_wasm_common::*;
// 定义自定义WASM模块
struct AdvancedWasmModule {
config: ModuleConfig,
}
impl AdvancedWasmModule {
pub fn new(config: ModuleConfig) -> Self {
Self { config }
}
}
impl WasmModule for AdvancedWasmModule {
fn execute(&self, input: TestData) -> Result<String, String> {
// 根据配置处理输入
match self.config.mode {
ProcessingMode::Simple => Ok(input.data),
ProcessingMode::Complex => {
if input.data.len() > self.config.max_length {
Err("Input too long".to_string())
} else {
Ok(input.data.chars().rev().collect())
}
}
}
}
}
// 定义模块配置
#[derive(Debug, Clone)]
struct ModuleConfig {
mode: ProcessingMode,
max_length: usize,
}
#[derive(Debug, Clone)]
enum ProcessingMode {
Simple,
Complex,
}
// 主测试模块
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple_mode() {
let config = ModuleConfig {
mode: ProcessingMode::Simple,
max_length: 10,
};
let module = AdvancedWasmModule::new(config);
let test_data = TestData::new("hello".to_string());
assert_eq!(module.execute(test_data).unwrap(), "hello");
}
#[test]
fn test_complex_mode() {
let config = ModuleConfig {
mode: ProcessingMode::Complex,
max_length: 5,
};
let module = AdvancedWasmModule::new(config);
// 测试正常输入
let test_data1 = TestData::new("abc".to_string());
assert_eq!(module.execute(test_data1).unwrap(), "cba");
// 测试超长输入
let test_data2 = TestData::new("too_long".to_string());
assert!(module.execute(test_data2).is_err());
}
#[test]
fn test_environment() {
let mut test_env = TestEnvironment::new();
// 注册模块
let config = ModuleConfig {
mode: ProcessingMode::Complex,
max_length: 10,
};
test_env.register_module("reverse_module", AdvancedWasmModule::new(config));
// 测试模块
let test_data = TestData::new("holochain".to_string());
let result = test_env.execute_wasm("reverse_module", test_data);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "niahcoloh");
test_env.cleanup();
}
}
这个更完整的示例展示了:
- 如何创建自定义的WASM模块实现
- 如何添加模块配置参数
- 不同处理模式的实现
- 更全面的测试用例
- 测试环境的注册和使用方法
1 回复
Rust插件库holochain_test_wasm_common使用指南
完整示例demo
基本测试环境设置示例
use holochain_test_wasm_common::TestWasm;
use serde_json::json;
#[test]
fn test_basic_environment() {
// 创建测试环境实例
let test_env = TestWasm::env();
// 验证环境初始化成功
assert!(test_env.is_initialized());
// 可以在这里添加更多测试逻辑...
println!("测试环境设置成功");
}
完整zome函数测试示例
use holochain_test_wasm_common::{TestWasm, call_zome_fn, ZomeCallResult};
use serde_json::json;
use holochain_test_wasm_common::assertions::*;
#[test]
fn test_complete_zome_workflow() {
// 1. 初始化测试环境
let test_env = TestWasm::env();
// 2. 创建新条目
let create_result: ZomeCallResult<String> = call_zome_fn(
&test_env,
"blog_zome", // zome名称
"create_post", // 函数名称
json!({
"title": "测试标题",
"content": "这是一篇测试文章内容"
})
);
// 3. 验证创建结果
assert_zome_call_ok(&create_result);
let post_hash = create_result.unwrap();
assert!(!post_hash.is_empty(), "返回的条目哈希不应为空");
// 4. 获取刚创建的条目
let get_result: ZomeCallResult<serde_json::Value> = call_zome_fn(
&test_env,
"blog_zome",
"get_post",
json!({"hash": post_hash})
);
// 5. 验证获取结果
assert_zome_call_ok(&get_result);
let post_data = get_result.unwrap();
assert_eq!(post_data["title"], "测试标题", "获取的标题不匹配");
assert_eq!(post_data["content"], "这是一篇测试文章内容", "获取的内容不匹配");
}
高级网络模拟测试示例
use holochain_test_wasm_common::{TestWasm, NetworkConditions, call_zome_fn};
use serde_json::json;
use std::time::{Instant, Duration};
#[test]
fn test_network_latency_impact() {
// 1. 初始化测试环境
let mut test_env = TestWasm::env();
// 2. 设置高延迟网络条件
test_env.set_network_conditions(NetworkConditions {
latency_ms: 500, // 500毫秒延迟
packet_loss: 0.1, // 10%丢包率
..Default::default()
});
// 3. 测试网络敏感操作
let start_time = Instant::now();
let result = call_zome_fn(
&test_env,
"network_zome",
"ping",
json!({})
);
let duration = start_time.elapsed();
// 4. 验证结果受网络条件影响
assert!(duration >= Duration::from_millis(500), "操作时间应考虑网络延迟");
assert!(result.is_err() || result.is_ok(), "由于丢包率,结果可能成功或失败");
}
多代理交互测试示例
use holochain_test_wasm_common::{TestWasm, call_zome_fn, ZomeCallResult};
use holochain_test_wasm_common::assertions::*;
#[test]
fn test_multi_agent_interaction() {
// 1. 初始化主测试环境
let test_env = TestWasm::env();
// 2. 创建两个代理
let alice = test_env.new_agent("alice");
let bob = test_env.new_agent("bob");
// 3. Alice创建私有数据
let private_data: ZomeCallResult<String> = call_zome_fn(
&alice,
"private_zome",
"create_private_data",
json!({"data": "Alice的私有数据"})
);
assert_zome_call_ok(&private_data);
let data_hash = private_data.unwrap();
// 4. Bob尝试获取Alice的数据
let bob_view: ZomeCallResult<Option<String>> = call_zome_fn(
&bob,
"private_zome",
"get_private_data",
json!({"hash": data_hash})
);
// 5. 验证Bob不能访问Alice的私有数据
assert_zome_call_ok(&bob_view);
assert!(bob_view.unwrap().is_none(), "Bob不应能访问Alice的私有数据");
// 6. Alice共享数据给Bob
let share_result: ZomeCallResult<()> = call_zome_fn(
&alice,
"private_zome",
"share_data",
json!({
"hash": data_hash,
"with": "bob"
})
);
assert_zome_call_ok(&share_result);
// 7. 现在Bob应该能访问数据了
let bob_view_after: ZomeCallResult<Option<String>> = call_zome_fn(
&bob,
"private_zome",
"get_private_data",
json!({"hash": data_hash})
);
assert_zome_call_ok(&bob_view_after);
assert_eq!(
bob_view_after.unwrap(),
Some("Alice的私有数据".to_string()),
"共享后Bob应能访问数据"
);
}