Skip to content

Commit

Permalink
add trycp common call_zome function
Browse files Browse the repository at this point in the history
  • Loading branch information
neonphog committed Aug 21, 2024
1 parent 6692b27 commit a5589b8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 55 deletions.
28 changes: 28 additions & 0 deletions bindings/trycp_runner/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,31 @@ pub fn disconnect_trycp_client<SV: UserValuesConstraint>(

Ok(())
}

/// Calls a zome function on the cell specified in `ctx.get().cell_id()`.
///
/// You must have a valid trycp_client in your context.
pub fn call_zome<I, O, SV>(
ctx: &mut AgentContext<TryCPRunnerContext, TryCPAgentContext<SV>>,
zome_name: &str,
fn_name: &str,
payload: I,
timeout: Option<Duration>,
) -> anyhow::Result<O>
where
O: std::fmt::Debug + serde::de::DeserializeOwned,
I: serde::Serialize + std::fmt::Debug,
SV: UserValuesConstraint,
{
let client = ctx.get().trycp_client();
let app_port = ctx.get().app_port();
let cell_id = ctx.get().cell_id();
ctx.runner_context().executor().execute_in_place(async {
client
.call_zome(app_port, cell_id, zome_name, fn_name, payload, timeout)
.await
.map_err(|e| anyhow::anyhow!("call failure: {:?}", e))?
.decode()
.map_err(|e| anyhow::anyhow!("Decoding failure: {:?}", e))
})
}
4 changes: 2 additions & 2 deletions bindings/trycp_runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod runner_context;
pub mod prelude {
pub use crate::cli::WindTunnelTryCPScenarioCli;
pub use crate::common::{
connect_trycp_client, disconnect_trycp_client, dump_logs, install_app, reset_trycp_remote,
shutdown_remote, try_wait_for_min_peers,
call_zome, connect_trycp_client, disconnect_trycp_client, dump_logs, install_app,
reset_trycp_remote, shutdown_remote, try_wait_for_min_peers,
};
pub use crate::context::{DefaultScenarioValues, TryCPAgentContext};
pub use crate::definition::TryCPScenarioDefinitionBuilder;
Expand Down
84 changes: 31 additions & 53 deletions scenarios/trycp_write_validated/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,62 +45,40 @@ fn agent_setup(
fn agent_behaviour(
ctx: &mut AgentContext<TryCPRunnerContext, TryCPAgentContext<ScenarioValues>>,
) -> HookResult {
let client = ctx.get().trycp_client();

let app_port = ctx.get().app_port();
let cell_id = ctx.get().cell_id();
let reporter = ctx.runner_context().reporter();

ctx.runner_context()
.executor()
.execute_in_place(async move {
let start = Instant::now();

let action_hash: ActionHash = client
.call_zome(
app_port,
cell_id.clone(),
"validated",
"create_sample_entry",
"this is a test entry value",
Some(Duration::from_secs(80)),
)
.await
.map_err(|e| anyhow::anyhow!("call failure: {:?}", e))?
.decode()
.map_err(|e| anyhow::anyhow!("Decoding failure: {:?}", e))?;

reporter.add_custom(
ReportMetric::new("create_sample_entry_time")
.with_field("value", start.elapsed().as_secs_f64()),
);

let start = Instant::now();

let _: ActionHash = client
.call_zome(
app_port,
cell_id,
"validated",
"update_sample_entry",
UpdateSampleEntryInput {
original: action_hash,
new_value: "the old string was a bit boring".to_string(),
},
Some(Duration::from_secs(80)),
)
.await
.map_err(|e| anyhow::anyhow!("call failure: {:?}", e))?
.decode()
.map_err(|e| anyhow::anyhow!("Decoding failure: {:?}", e))?;

reporter.add_custom(
ReportMetric::new("update_sample_entry_time")
.with_field("value", start.elapsed().as_secs_f64()),
);
let start = Instant::now();

Ok(())
})?;
let action_hash: ActionHash = call_zome(
ctx,
"validated",
"create_sample_entry",
"this is a test entry value",
Some(Duration::from_secs(80)),
)?;

reporter.add_custom(
ReportMetric::new("create_sample_entry_time")
.with_field("value", start.elapsed().as_secs_f64()),
);

let start = Instant::now();

let _: ActionHash = call_zome(
ctx,
"validated",
"update_sample_entry",
UpdateSampleEntryInput {
original: action_hash,
new_value: "the old string was a bit boring".to_string(),
},
Some(Duration::from_secs(80)),
)?;

reporter.add_custom(
ReportMetric::new("update_sample_entry_time")
.with_field("value", start.elapsed().as_secs_f64()),
);

Ok(())
}
Expand Down

0 comments on commit a5589b8

Please sign in to comment.