diff --git a/ic-canister-runtime/src/stub/mod.rs b/ic-canister-runtime/src/stub/mod.rs index 4500d3a..846c75f 100644 --- a/ic-canister-runtime/src/stub/mod.rs +++ b/ic-canister-runtime/src/stub/mod.rs @@ -5,6 +5,7 @@ use crate::{IcError, Runtime}; use async_trait::async_trait; use candid::{utils::ArgumentEncoder, CandidType, Decode, Encode, Principal}; use serde::de::DeserializeOwned; +use std::sync::Arc; use std::{collections::VecDeque, sync::Mutex}; /// An implementation of [`Runtime`] that returns pre-defined results from a queue. @@ -45,10 +46,10 @@ use std::{collections::VecDeque, sync::Mutex}; /// # Ok(()) /// # } /// ``` -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] pub struct StubRuntime { // Use a mutex so that this struct is Send and Sync - call_results: Mutex>>, + call_results: Arc>>>, } impl StubRuntime { diff --git a/ic-canister-runtime/src/stub/tests.rs b/ic-canister-runtime/src/stub/tests.rs index 22216b8..75ee108 100644 --- a/ic-canister-runtime/src/stub/tests.rs +++ b/ic-canister-runtime/src/stub/tests.rs @@ -67,6 +67,28 @@ async fn should_return_multiple_stub_responses() { assert_eq!(result3, Ok(expected3)); } +#[tokio::test] +async fn should_have_same_responses_in_clone() { + let original_runtime = StubRuntime::new() + .add_stub_response(1_u64) + .add_stub_response(2_u64) + .add_stub_response(3_u64); + let cloned_runtime = original_runtime.clone(); + + let result1: Result = original_runtime + .query_call(DEFAULT_PRINCIPAL, DEFAULT_METHOD, DEFAULT_ARGS) + .await; + assert_eq!(result1, Ok(1)); + let result2: Result = cloned_runtime + .query_call(DEFAULT_PRINCIPAL, DEFAULT_METHOD, DEFAULT_ARGS) + .await; + assert_eq!(result2, Ok(2)); + let result3: Result = original_runtime + .query_call(DEFAULT_PRINCIPAL, DEFAULT_METHOD, DEFAULT_ARGS) + .await; + assert_eq!(result3, Ok(3)); +} + #[derive(Clone, Debug, PartialEq, CandidType, Deserialize)] enum MultiResult { Consistent(String),