Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ic-canister-runtime/src/stub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<VecDeque<Vec<u8>>>,
call_results: Arc<Mutex<VecDeque<Vec<u8>>>>,
}

impl StubRuntime {
Expand Down
22 changes: 22 additions & 0 deletions ic-canister-runtime/src/stub/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64, IcError> = original_runtime
.query_call(DEFAULT_PRINCIPAL, DEFAULT_METHOD, DEFAULT_ARGS)
.await;
assert_eq!(result1, Ok(1));
let result2: Result<u64, IcError> = cloned_runtime
.query_call(DEFAULT_PRINCIPAL, DEFAULT_METHOD, DEFAULT_ARGS)
.await;
assert_eq!(result2, Ok(2));
let result3: Result<u64, IcError> = 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),
Expand Down