Skip to content

Commit

Permalink
add mock for p2p and contract_client
Browse files Browse the repository at this point in the history
  • Loading branch information
oac1771 committed Dec 17, 2024
1 parent 904313d commit fc696c6
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 139 deletions.
1 change: 1 addition & 0 deletions crates/clis/worker/src/controller/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ where
<C as Config>::AccountId: Display,
S: Signer<C> + Wallet,
CC: ContractClient<C = C>,
CC::Err: Display,
NC: NetworkClient,
JR: WasmJobRunnerService,
WorkerControllerError: From<<NC as NetworkClient>::Err>
Expand Down
106 changes: 101 additions & 5 deletions crates/utils/src/services/contract_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ use subxt::{
pub trait ContractClient {
type C: Config;
type E: Environment;
type Err: From<codec::Error>;
type Err: From<codec::Error> + From<subxt::Error>;
type ContractEmitted: ContractEmittedT;

fn contract_event_sub(
&self,
contract_address: <Self::C as Config>::AccountId,
) -> impl Future<
Output = Result<impl Stream<Item = Result<Self::ContractEmitted, subxt::Error>>, Self::Err>,
Output = Result<impl Stream<Item = Result<Self::ContractEmitted, Self::Err>>, Self::Err>,
> + Send;

fn write<Ev: Decode, Args: Encode + Sync + Send>(
fn write<Ev: Decode + 'static, Args: Encode + Sync + Send>(
&self,
address: <Self::C as Config>::AccountId,
message: &str,
Expand Down Expand Up @@ -86,7 +86,7 @@ where
async fn contract_event_sub(
&self,
contract_address: <Self::C as Config>::AccountId,
) -> Result<impl Stream<Item = Result<Self::ContractEmitted, subxt::Error>>, Self::Err> {
) -> Result<impl Stream<Item = Result<Self::ContractEmitted, Self::Err>>, Self::Err> {
let client = self.online_client().await?;
let addr: AccountId32 = contract_address.into();

Expand All @@ -105,7 +105,8 @@ where
match ext.events().await {
Ok(ev) => Ok(Some(iter(
ev.find::<ContractEmitted>()
.collect::<Vec<Result<ContractEmitted, subxt::Error>>>(),
.map(|r| r.map_err(|e| <Self as ContractClient>::Err::from(e)))
.collect::<Vec<Result<ContractEmitted, Self::Err>>>(),
))),
Err(err) => Err(err),
}
Expand Down Expand Up @@ -562,3 +563,98 @@ impl ContractEmittedT for ContractEmitted {
self.data
}
}

#[cfg(test)]
mod test {
use super::*;

use ink::env::DefaultEnvironment;
use std::{any::Any, collections::HashMap, marker::Send, sync::Mutex};
use subxt::SubstrateConfig;

use crate::services::test::Expectation;

struct MockContractClient {
expectations: Mutex<HashMap<String, Box<dyn Any>>>,
}

impl MockContractClient {
fn _expect_contract_event_sub(
&mut self,
) -> &mut Expectation<Result<Vec<Result<ContractEmitted, subxt::Error>>, ContractClientError>>
{
self._expectation::<Result<Vec<Result<ContractEmitted, subxt::Error>>, ContractClientError>>("contract_event_sub")
}

fn _expect_write<Ev: Decode + 'static>(
&mut self,
) -> &mut Expectation<Result<Ev, ContractClientError>> {
self._expectation::<Result<Ev, ContractClientError>>("write")
}

fn _expectation<T: 'static>(&mut self, entry: &str) -> &mut Expectation<T> {
self.expectations
.get_mut()
.unwrap()
.entry(entry.to_string())
.or_insert_with(|| {
let expectation: Expectation<T> = Expectation::new();
Box::new(Some(expectation))
})
.downcast_mut::<Expectation<T>>()
.unwrap()
}

fn into_expectation<T: 'static>(&self, entry: &str) -> Box<Expectation<T>> {
self.expectations
.lock()
.unwrap()
.remove(entry)
.unwrap()
.downcast::<Expectation<T>>()
.unwrap()
}
}

impl ContractClient for MockContractClient {
type C = SubstrateConfig;
type E = DefaultEnvironment;
type Err = ContractClientError;
type ContractEmitted = ContractEmitted;

fn contract_event_sub(
&self,
_contract_address: <Self::C as Config>::AccountId,
) -> impl Future<
Output = Result<
impl Stream<Item = Result<Self::ContractEmitted, Self::Err>>,
Self::Err,
>,
> + Send {
let expectation = self
.into_expectation::<Result<Vec<Result<ContractEmitted, ContractClientError>>, ContractClientError>>(
"publish_message",
);
let res = expectation.func().unwrap()().map(|i| iter(i.into_iter()));

async { res }
}

fn write<Ev: Decode + 'static, Args: Encode + Sync + Send>(
&self,
_address: <Self::C as Config>::AccountId,
_message: &str,
_args: &Args,
_value: <Self::E as Environment>::Balance,
) -> impl Future<Output = Result<Ev, Self::Err>> + Send {
let expectation = self.into_expectation::<Result<Ev, ContractClientError>>("write");
let func = expectation.func().unwrap();
async move { func() }
}

fn decode_event<Ev: Decode>(&self, mut ev_data: &[u8]) -> Result<Ev, Self::Err> {
let result = <Ev as Decode>::decode(&mut ev_data)?;
Ok(result)
}
}
}
4 changes: 4 additions & 0 deletions crates/utils/src/services/job/test_job_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ fn raw_params_parses_some_option_to_vec_strings() {

#[test]
fn successfully_parse_params_into_encoded_values() {
// let mut foo = job_handler::MockJobHandlerService::new();

// let bar = foo.expect_unpack_results().returning(|x| 10);

let val = 10;
let parameters = Some(String::from(val.clone().to_string()));
let code = wat2wasm(ADD_ONE).unwrap();
Expand Down
22 changes: 22 additions & 0 deletions crates/utils/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
pub mod contract_client;
pub mod job;
pub mod p2p;

#[cfg(test)]
pub mod test {
type Func<T> = Box<dyn Fn() -> T + Send + Sync + 'static>;
pub struct Expectation<T> {
func: Option<Func<T>>,
}

impl<T> Expectation<T> {
pub fn new() -> Self {
Self { func: None }
}

pub fn func(self) -> Option<Func<T>> {
self.func
}

pub fn _returns(&mut self, func: Func<T>) {
self.func = Some(func);
}
}
}
Loading

0 comments on commit fc696c6

Please sign in to comment.