Skip to content

Commit 2107613

Browse files
committed
feat: allow rpc mock to insert multiple same requests by migrating from Hashmap to Vec
1 parent 98eb135 commit 2107613

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

rpc-client/src/mock_sender.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,40 @@ use {
4141

4242
pub const PUBKEY: &str = "7RoSF9fUmdphVCpabEoefH81WwrW7orsWonXWqTXkKV8";
4343

44-
pub type Mocks = HashMap<RpcRequest, Value>;
44+
#[derive(Default)]
45+
pub struct Mocks(Vec<(RpcRequest, Value)>);
46+
47+
impl Mocks {
48+
pub fn new() -> Self {
49+
Self(Vec::new())
50+
}
51+
52+
pub fn from<const N: usize>(mocks: [(RpcRequest, Value); N]) -> Self {
53+
Self(mocks.to_vec())
54+
}
55+
56+
pub fn insert(&mut self, request: RpcRequest, response: Value) {
57+
self.0.push((request, response));
58+
}
59+
60+
pub fn remove(&mut self, request: &RpcRequest) -> Option<Value> {
61+
self.0
62+
.iter()
63+
.position(|(r, _)| r == request)
64+
.map(|index| self.0.remove(index).1)
65+
}
66+
}
67+
68+
impl FromIterator<(RpcRequest, Value)> for Mocks {
69+
fn from_iter<T: IntoIterator<Item = (RpcRequest, Value)>>(iter: T) -> Self {
70+
let mut mocks = Mocks::new();
71+
for (request, response) in iter {
72+
mocks.insert(request, response);
73+
}
74+
mocks
75+
}
76+
}
77+
4578
pub struct MockSender {
4679
mocks: RwLock<Mocks>,
4780
url: String,

rpc-client/src/nonblocking/rpc_client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4707,7 +4707,7 @@ pub(crate) fn parse_keyed_accounts(
47074707

47084708
#[doc(hidden)]
47094709
pub fn create_rpc_client_mocks() -> crate::mock_sender::Mocks {
4710-
let mut mocks = std::collections::HashMap::new();
4710+
let mut mocks = crate::mock_sender::Mocks::default();
47114711

47124712
let get_account_request = RpcRequest::GetAccountInfo;
47134713
let get_account_response = serde_json::to_value(Response {

rpc-client/src/rpc_client.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3677,7 +3677,7 @@ impl RpcClient {
36773677
/// Mocks for documentation examples
36783678
#[doc(hidden)]
36793679
pub fn create_rpc_client_mocks() -> crate::mock_sender::Mocks {
3680-
let mut mocks = std::collections::HashMap::new();
3680+
let mut mocks = crate::mock_sender::Mocks::default();
36813681

36823682
let get_account_request = RpcRequest::GetAccountInfo;
36833683
let get_account_response = serde_json::to_value(Response {
@@ -3980,7 +3980,7 @@ mod tests {
39803980

39813981
// Test: with context
39823982
{
3983-
let mocks: Mocks = [(
3983+
let mocks: Mocks = Mocks::from([(
39843984
RpcRequest::GetProgramAccounts,
39853985
serde_json::to_value(OptionalContext::Context(Response {
39863986
context: RpcResponseContext {
@@ -3990,9 +3990,7 @@ mod tests {
39903990
value: vec![keyed_account],
39913991
}))
39923992
.unwrap(),
3993-
)]
3994-
.into_iter()
3995-
.collect();
3993+
)]);
39963994
let rpc_client = RpcClient::new_mock_with_mocks("mock_client".to_string(), mocks);
39973995
let result = rpc_client
39983996
.get_program_accounts_with_config(

0 commit comments

Comments
 (0)